LCOV - code coverage report
Current view: top level - mm/boehm-gc - mark_rts.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 112 179 62.6 %
Date: 2015-06-10 18:10:59 Functions: 16 23 69.6 %

          Line data    Source code
       1             : /*
       2             :  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
       3             :  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
       4             :  *
       5             :  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
       6             :  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
       7             :  *
       8             :  * Permission is hereby granted to use or copy this program
       9             :  * for any purpose,  provided the above notices are retained on all copies.
      10             :  * Permission to modify the code and to distribute modified code is granted,
      11             :  * provided the above notices are retained, and a notice that the code was
      12             :  * modified is included with the above copyright notice.
      13             :  */
      14             : 
      15             : #include "private/gc_priv.h"
      16             : 
      17             : #include <stdio.h>
      18             : 
      19             : /* Data structure for list of root sets.                                */
      20             : /* We keep a hash table, so that we can filter out duplicate additions. */
      21             : /* Under Win32, we need to do a better job of filtering overlaps, so    */
      22             : /* we resort to sequential search, and pay the price.                   */
      23             : /* This is really declared in gc_priv.h:
      24             : struct roots {
      25             :         ptr_t r_start;
      26             :         ptr_t r_end;
      27             : #       if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
      28             :           struct roots * r_next;
      29             : #       endif
      30             :         GC_bool r_tmp;
      31             :                 -- Delete before registering new dynamic libraries
      32             : };
      33             : 
      34             : struct roots GC_static_roots[MAX_ROOT_SETS];
      35             : */
      36             : 
      37             : int GC_no_dls = 0;      /* Register dynamic library data segments.      */
      38             : 
      39             : static int n_root_sets = 0;
      40             :         /* GC_static_roots[0..n_root_sets) contains the valid root sets. */
      41             : 
      42             : #if !defined(NO_DEBUGGING)
      43             :   /* For debugging:     */
      44           0 :   void GC_print_static_roots(void)
      45             :   {
      46             :     int i;
      47           0 :     size_t total = 0;
      48             : 
      49           0 :     for (i = 0; i < n_root_sets; i++) {
      50           0 :         GC_printf("From %p to %p%s\n",
      51             :                   GC_static_roots[i].r_start,
      52             :                   GC_static_roots[i].r_end,
      53           0 :                   GC_static_roots[i].r_tmp ? " (temporary)" : "");
      54           0 :         total += GC_static_roots[i].r_end - GC_static_roots[i].r_start;
      55             :     }
      56           0 :     GC_printf("Total size: %ld\n", (unsigned long) total);
      57           0 :     if (GC_root_size != total) {
      58           0 :         GC_err_printf("GC_root_size incorrect: %ld!!\n",
      59             :                       (long) GC_root_size);
      60             :     }
      61           0 :   }
      62             : #endif /* !NO_DEBUGGING */
      63             : 
      64             : #ifndef THREADS
      65             :   /* Primarily for debugging support:     */
      66             :   /* Is the address p in one of the registered static root sections?      */
      67             :   GC_INNER GC_bool GC_is_static_root(ptr_t p)
      68             :   {
      69             :     static int last_root_set = MAX_ROOT_SETS;
      70             :     int i;
      71             : 
      72             :     if (last_root_set < n_root_sets
      73             :         && p >= GC_static_roots[last_root_set].r_start
      74             :         && p < GC_static_roots[last_root_set].r_end) return(TRUE);
      75             :     for (i = 0; i < n_root_sets; i++) {
      76             :         if (p >= GC_static_roots[i].r_start
      77             :             && p < GC_static_roots[i].r_end) {
      78             :             last_root_set = i;
      79             :             return(TRUE);
      80             :         }
      81             :     }
      82             :     return(FALSE);
      83             :   }
      84             : #endif /* !THREADS */
      85             : 
      86             : #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
      87             : /*
      88             : #   define LOG_RT_SIZE 6
      89             : #   define RT_SIZE (1 << LOG_RT_SIZE)  -- Power of 2, may be != MAX_ROOT_SETS
      90             : 
      91             :     struct roots * GC_root_index[RT_SIZE];
      92             :         -- Hash table header.  Used only to check whether a range is
      93             :         -- already present.
      94             :         -- really defined in gc_priv.h
      95             : */
      96             : 
      97        5830 :   GC_INLINE int rt_hash(ptr_t addr)
      98             :   {
      99        5830 :     word result = (word) addr;
     100             : #   if CPP_WORDSZ > 8*LOG_RT_SIZE
     101        5830 :         result ^= result >> 8*LOG_RT_SIZE;
     102             : #   endif
     103             : #   if CPP_WORDSZ > 4*LOG_RT_SIZE
     104        5830 :         result ^= result >> 4*LOG_RT_SIZE;
     105             : #   endif
     106        5830 :     result ^= result >> 2*LOG_RT_SIZE;
     107        5830 :     result ^= result >> LOG_RT_SIZE;
     108        5830 :     result &= (RT_SIZE-1);
     109        5830 :     return(result);
     110             :   }
     111             : 
     112             :   /* Is a range starting at b already in the table? If so return a      */
     113             :   /* pointer to it, else NULL.                                          */
     114        2915 :   GC_INNER void * GC_roots_present(ptr_t b)
     115             :   {
     116        2915 :     int h = rt_hash(b);
     117        2915 :     struct roots *p = GC_root_index[h];
     118             : 
     119        6122 :     while (p != 0) {
     120         292 :         if (p -> r_start == (ptr_t)b) return(p);
     121         292 :         p = p -> r_next;
     122             :     }
     123        2915 :     return NULL;
     124             :   }
     125             : 
     126             :   /* Add the given root structure to the index. */
     127        2915 :   GC_INLINE void add_roots_to_index(struct roots *p)
     128             :   {
     129        2915 :     int h = rt_hash(p -> r_start);
     130             : 
     131        2915 :     p -> r_next = GC_root_index[h];
     132        2915 :     GC_root_index[h] = p;
     133        2915 :   }
     134             : #endif /* !MSWIN32 && !MSWINCE && !CYGWIN32 */
     135             : 
     136             : GC_INNER word GC_root_size = 0;
     137             : 
     138           0 : GC_API void GC_CALL GC_add_roots(void *b, void *e)
     139             : {
     140             :     DCL_LOCK_STATE;
     141             : 
     142           0 :     if (!GC_is_initialized) GC_init();
     143           0 :     LOCK();
     144           0 :     GC_add_roots_inner((ptr_t)b, (ptr_t)e, FALSE);
     145           0 :     UNLOCK();
     146           0 : }
     147             : 
     148             : 
     149             : /* Add [b,e) to the root set.  Adding the same interval a second time   */
     150             : /* is a moderately fast no-op, and hence benign.  We do not handle      */
     151             : /* different but overlapping intervals efficiently.  (We do handle      */
     152             : /* them correctly.)                                                     */
     153             : /* Tmp specifies that the interval may be deleted before                */
     154             : /* re-registering dynamic libraries.                                    */
     155        2915 : void GC_add_roots_inner(ptr_t b, ptr_t e, GC_bool tmp)
     156             : {
     157             :     struct roots * old;
     158             : 
     159             :     GC_ASSERT(b <= e);
     160        2915 :     b = (ptr_t)(((word)b + (sizeof(word) - 1)) & ~(sizeof(word) - 1));
     161             :                                         /* round b up to word boundary */
     162        2915 :     e = (ptr_t)((word)e & ~(sizeof(word) - 1));
     163             :                                         /* round e down to word boundary */
     164        2915 :     if (b >= e) return; /* nothing to do */
     165             : 
     166             : #   if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
     167             :       /* Spend the time to ensure that there are no overlapping */
     168             :       /* or adjacent intervals.                                 */
     169             :       /* This could be done faster with e.g. a                  */
     170             :       /* balanced tree.  But the execution time here is         */
     171             :       /* virtually guaranteed to be dominated by the time it    */
     172             :       /* takes to scan the roots.                               */
     173             :       {
     174             :         register int i;
     175             :         old = 0; /* initialized to prevent warning. */
     176             :         for (i = 0; i < n_root_sets; i++) {
     177             :             old = GC_static_roots + i;
     178             :             if (b <= old -> r_end && e >= old -> r_start) {
     179             :                 if (b < old -> r_start) {
     180             :                     GC_root_size += old->r_start - b;
     181             :                     old -> r_start = b;
     182             :                 }
     183             :                 if (e > old -> r_end) {
     184             :                     GC_root_size += e - old->r_end;
     185             :                     old -> r_end = e;
     186             :                 }
     187             :                 old -> r_tmp &= tmp;
     188             :                 break;
     189             :             }
     190             :         }
     191             :         if (i < n_root_sets) {
     192             :           /* merge other overlapping intervals */
     193             :             struct roots *other;
     194             : 
     195             :             for (i++; i < n_root_sets; i++) {
     196             :               other = GC_static_roots + i;
     197             :               b = other -> r_start;
     198             :               e = other -> r_end;
     199             :               if (b <= old -> r_end && e >= old -> r_start) {
     200             :                 if (b < old -> r_start) {
     201             :                     GC_root_size += old->r_start - b;
     202             :                     old -> r_start = b;
     203             :                 }
     204             :                 if (e > old -> r_end) {
     205             :                     GC_root_size += e - old->r_end;
     206             :                     old -> r_end = e;
     207             :                 }
     208             :                 old -> r_tmp &= other -> r_tmp;
     209             :                 /* Delete this entry. */
     210             :                   GC_root_size -= (other -> r_end - other -> r_start);
     211             :                   other -> r_start = GC_static_roots[n_root_sets-1].r_start;
     212             :                   other -> r_end = GC_static_roots[n_root_sets-1].r_end;
     213             :                   n_root_sets--;
     214             :               }
     215             :             }
     216             :           return;
     217             :         }
     218             :       }
     219             : #   else
     220        2915 :       old = (struct roots *)GC_roots_present(b);
     221        2915 :       if (old != 0) {
     222           0 :         if (e <= old -> r_end) /* already there */ return;
     223             :         /* else extend */
     224           0 :         GC_root_size += e - old -> r_end;
     225           0 :         old -> r_end = e;
     226           0 :         return;
     227             :       }
     228             : #   endif
     229        2915 :     if (n_root_sets == MAX_ROOT_SETS) {
     230           0 :         ABORT("Too many root sets");
     231             :     }
     232        2915 :     GC_static_roots[n_root_sets].r_start = (ptr_t)b;
     233        2915 :     GC_static_roots[n_root_sets].r_end = (ptr_t)e;
     234        2915 :     GC_static_roots[n_root_sets].r_tmp = tmp;
     235             : #   if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
     236        2915 :       GC_static_roots[n_root_sets].r_next = 0;
     237        2915 :       add_roots_to_index(GC_static_roots + n_root_sets);
     238             : #   endif
     239        2915 :     GC_root_size += e - b;
     240        2915 :     n_root_sets++;
     241             : }
     242             : 
     243             : static GC_bool roots_were_cleared = FALSE;
     244             : 
     245           0 : GC_API void GC_CALL GC_clear_roots(void)
     246             : {
     247             :     DCL_LOCK_STATE;
     248             : 
     249           0 :     if (!GC_is_initialized) GC_init();
     250           0 :     LOCK();
     251           0 :     roots_were_cleared = TRUE;
     252           0 :     n_root_sets = 0;
     253           0 :     GC_root_size = 0;
     254             : #   if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
     255           0 :       BZERO(GC_root_index, RT_SIZE * sizeof(void *));
     256             : #   endif
     257           0 :     UNLOCK();
     258           0 : }
     259             : 
     260             : /* Internal use only; lock held.        */
     261        1242 : STATIC void GC_remove_root_at_pos(int i)
     262             : {
     263        1242 :     GC_root_size -= (GC_static_roots[i].r_end - GC_static_roots[i].r_start);
     264        1242 :     GC_static_roots[i].r_start = GC_static_roots[n_root_sets-1].r_start;
     265        1242 :     GC_static_roots[i].r_end = GC_static_roots[n_root_sets-1].r_end;
     266        1242 :     GC_static_roots[i].r_tmp = GC_static_roots[n_root_sets-1].r_tmp;
     267        1242 :     n_root_sets--;
     268        1242 : }
     269             : 
     270             : #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
     271         252 :   STATIC void GC_rebuild_root_index(void)
     272             :   {
     273             :     int i;
     274         252 :     BZERO(GC_root_index, RT_SIZE * sizeof(void *));
     275         252 :     for (i = 0; i < n_root_sets; i++)
     276           0 :         add_roots_to_index(GC_static_roots + i);
     277         252 :   }
     278             : #endif
     279             : 
     280             : #if defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
     281             :      || defined(PCR) || defined(CYGWIN32)
     282             : /* Internal use only; lock held.        */
     283         252 : STATIC void GC_remove_tmp_roots(void)
     284             : {
     285             :     int i;
     286             : 
     287        1746 :     for (i = 0; i < n_root_sets; ) {
     288        1242 :         if (GC_static_roots[i].r_tmp) {
     289        1242 :             GC_remove_root_at_pos(i);
     290             :         } else {
     291           0 :             i++;
     292             :         }
     293             :     }
     294             : #   if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
     295         252 :       GC_rebuild_root_index();
     296             : #   endif
     297         252 : }
     298             : #endif
     299             : 
     300             : #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32)
     301             :   STATIC void GC_remove_roots_inner(ptr_t b, ptr_t e);
     302             : 
     303           0 :   GC_API void GC_CALL GC_remove_roots(void *b, void *e)
     304             :   {
     305             :     DCL_LOCK_STATE;
     306             : 
     307             :     /* Quick check whether has nothing to do */
     308           0 :     if ((((word)b + (sizeof(word) - 1)) & ~(sizeof(word) - 1)) >=
     309           0 :         ((word)e & ~(sizeof(word) - 1)))
     310           0 :       return;
     311             : 
     312           0 :     LOCK();
     313           0 :     GC_remove_roots_inner((ptr_t)b, (ptr_t)e);
     314           0 :     UNLOCK();
     315             :   }
     316             : 
     317             :   /* Should only be called when the lock is held */
     318           0 :   STATIC void GC_remove_roots_inner(ptr_t b, ptr_t e)
     319             :   {
     320             :     int i;
     321           0 :     for (i = 0; i < n_root_sets; ) {
     322           0 :         if (GC_static_roots[i].r_start >= b
     323           0 :             && GC_static_roots[i].r_end <= e) {
     324           0 :             GC_remove_root_at_pos(i);
     325             :         } else {
     326           0 :             i++;
     327             :         }
     328             :     }
     329           0 :     GC_rebuild_root_index();
     330           0 :   }
     331             : #endif /* !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32) */
     332             : 
     333             : #if (defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)) \
     334             :     && !defined(NO_DEBUGGING)
     335             :   /* Not used at present (except for, may be, debugging purpose).       */
     336             :   /* Workaround for the OS mapping and unmapping behind our back:       */
     337             :   /* Is the address p in one of the temporary static root sections?     */
     338             :   GC_bool GC_is_tmp_root(ptr_t p)
     339             :   {
     340             :     static int last_root_set = MAX_ROOT_SETS;
     341             :     register int i;
     342             : 
     343             :     if (last_root_set < n_root_sets
     344             :         && p >= GC_static_roots[last_root_set].r_start
     345             :         && p < GC_static_roots[last_root_set].r_end)
     346             :         return GC_static_roots[last_root_set].r_tmp;
     347             :     for (i = 0; i < n_root_sets; i++) {
     348             :         if (p >= GC_static_roots[i].r_start
     349             :             && p < GC_static_roots[i].r_end) {
     350             :             last_root_set = i;
     351             :             return GC_static_roots[i].r_tmp;
     352             :         }
     353             :     }
     354             :     return(FALSE);
     355             :   }
     356             : #endif /* MSWIN32 || MSWINCE || CYGWIN32 */
     357             : 
     358      170462 : GC_INNER ptr_t GC_approx_sp(void)
     359             : {
     360             :     volatile word sp;
     361      170462 :     sp = (word)&sp;
     362             :                 /* Also force stack to grow if necessary. Otherwise the */
     363             :                 /* later accesses might cause the kernel to think we're */
     364             :                 /* doing something wrong.                               */
     365      170462 :     return((ptr_t)sp);
     366             :                 /* GNU C: alternatively, we may return the value of     */
     367             :                 /*__builtin_frame_address(0).                           */
     368             : }
     369             : 
     370             : /*
     371             :  * Data structure for excluded static roots.
     372             :  * Real declaration is in gc_priv.h.
     373             : 
     374             : struct exclusion {
     375             :     ptr_t e_start;
     376             :     ptr_t e_end;
     377             : };
     378             : 
     379             : struct exclusion GC_excl_table[MAX_EXCLUSIONS];
     380             :                                         -- Array of exclusions, ascending
     381             :                                         -- address order.
     382             : */
     383             : 
     384             : STATIC size_t GC_excl_table_entries = 0;/* Number of entries in use.      */
     385             : 
     386             : /* Return the first exclusion range that includes an address >= start_addr */
     387             : /* Assumes the exclusion table contains at least one entry (namely the     */
     388             : /* GC data structures).                                                    */
     389        4105 : STATIC struct exclusion * GC_next_exclusion(ptr_t start_addr)
     390             : {
     391        4105 :     size_t low = 0;
     392        4105 :     size_t high = GC_excl_table_entries - 1;
     393             :     size_t mid;
     394             : 
     395       14990 :     while (high > low) {
     396        6780 :         mid = (low + high) >> 1;
     397             :         /* low <= mid < high    */
     398        6780 :         if ((word) GC_excl_table[mid].e_end <= (word) start_addr) {
     399        1362 :             low = mid + 1;
     400             :         } else {
     401        5418 :             high = mid;
     402             :         }
     403             :     }
     404        4105 :     if ((word) GC_excl_table[low].e_end <= (word) start_addr) return 0;
     405        3422 :     return GC_excl_table + low;
     406             : }
     407             : 
     408             : /* Should only be called when the lock is held.  The range boundaries   */
     409             : /* should be properly aligned and valid.                                */
     410         489 : GC_INNER void GC_exclude_static_roots_inner(void *start, void *finish)
     411             : {
     412             :     struct exclusion * next;
     413             :     size_t next_index, i;
     414             : 
     415             :     GC_ASSERT((word)start % sizeof(word) == 0);
     416             :     GC_ASSERT(start < finish);
     417             : 
     418         489 :     if (0 == GC_excl_table_entries) {
     419         163 :         next = 0;
     420             :     } else {
     421         326 :         next = GC_next_exclusion(start);
     422             :     }
     423         489 :     if (0 != next) {
     424         326 :       if ((word)(next -> e_start) < (word) finish) {
     425             :         /* incomplete error check. */
     426           0 :         ABORT("Exclusion ranges overlap");
     427             :       }
     428         326 :       if ((word)(next -> e_start) == (word) finish) {
     429             :         /* extend old range backwards   */
     430           0 :           next -> e_start = (ptr_t)start;
     431           0 :           return;
     432             :       }
     433         326 :       next_index = next - GC_excl_table;
     434         652 :       for (i = GC_excl_table_entries; i > next_index; --i) {
     435         326 :         GC_excl_table[i] = GC_excl_table[i-1];
     436             :       }
     437             :     } else {
     438         163 :       next_index = GC_excl_table_entries;
     439             :     }
     440         489 :     if (GC_excl_table_entries == MAX_EXCLUSIONS) ABORT("Too many exclusions");
     441         489 :     GC_excl_table[next_index].e_start = (ptr_t)start;
     442         489 :     GC_excl_table[next_index].e_end = (ptr_t)finish;
     443         489 :     ++GC_excl_table_entries;
     444             : }
     445             : 
     446           0 : GC_API void GC_CALL GC_exclude_static_roots(void *b, void *e)
     447             : {
     448             :     DCL_LOCK_STATE;
     449             : 
     450             :     /* Adjust the upper boundary for safety (round down) */
     451           0 :     e = (void *)((word)e & ~(sizeof(word) - 1));
     452             : 
     453           0 :     if (b == e) return;  /* nothing to exclude? */
     454             : 
     455           0 :     LOCK();
     456           0 :     GC_exclude_static_roots_inner(b, e);
     457           0 :     UNLOCK();
     458             : }
     459             : 
     460             : /* Invoke push_conditional on ranges that are not excluded. */
     461             : /*ARGSUSED*/
     462        3005 : STATIC void GC_push_conditional_with_exclusions(ptr_t bottom, ptr_t top,
     463             :                                                 GC_bool all)
     464             : {
     465             :     struct exclusion * next;
     466             :     ptr_t excl_start;
     467             : 
     468        6784 :     while (bottom < top) {
     469        3779 :         next = GC_next_exclusion(bottom);
     470        3779 :         if (0 == next || (excl_start = next -> e_start) >= top) {
     471        3005 :             GC_push_conditional(bottom, top, all);
     472        3005 :             return;
     473             :         }
     474         774 :         if (excl_start > bottom) GC_push_conditional(bottom, excl_start, all);
     475         774 :         bottom = next -> e_end;
     476             :     }
     477             : }
     478             : 
     479             : #ifdef IA64
     480             :   /* Similar to GC_push_all_stack_sections() but for IA-64 registers store. */
     481             :   GC_INNER void GC_push_all_register_sections(ptr_t bs_lo, ptr_t bs_hi,
     482             :                   int eager, struct GC_traced_stack_sect_s *traced_stack_sect)
     483             :   {
     484             :     while (traced_stack_sect != NULL) {
     485             :         ptr_t frame_bs_lo = traced_stack_sect -> backing_store_end;
     486             :         GC_ASSERT(frame_bs_lo <= bs_hi);
     487             :         if (eager) {
     488             :             GC_push_all_eager(frame_bs_lo, bs_hi);
     489             :         } else {
     490             :             GC_push_all_stack(frame_bs_lo, bs_hi);
     491             :         }
     492             :         bs_hi = traced_stack_sect -> saved_backing_store_ptr;
     493             :         traced_stack_sect = traced_stack_sect -> prev;
     494             :     }
     495             :     GC_ASSERT(bs_lo <= bs_hi);
     496             :     if (eager) {
     497             :         GC_push_all_eager(bs_lo, bs_hi);
     498             :     } else {
     499             :         GC_push_all_stack(bs_lo, bs_hi);
     500             :     }
     501             :   }
     502             : #endif /* IA64 */
     503             : 
     504             : #ifdef THREADS
     505             : 
     506         543 : GC_INNER void GC_push_all_stack_sections(ptr_t lo, ptr_t hi,
     507             :                         struct GC_traced_stack_sect_s *traced_stack_sect)
     508             : {
     509        1086 :     while (traced_stack_sect != NULL) {
     510             :         GC_ASSERT(lo HOTTER_THAN (ptr_t)traced_stack_sect);
     511             : #       ifdef STACK_GROWS_UP
     512             :             GC_push_all_stack((ptr_t)traced_stack_sect, lo);
     513             : #       else /* STACK_GROWS_DOWN */
     514           0 :             GC_push_all_stack(lo, (ptr_t)traced_stack_sect);
     515             : #       endif
     516           0 :         lo = traced_stack_sect -> saved_stack_ptr;
     517             :         GC_ASSERT(lo != NULL);
     518           0 :         traced_stack_sect = traced_stack_sect -> prev;
     519             :     }
     520             :     GC_ASSERT(!(hi HOTTER_THAN lo));
     521             : #   ifdef STACK_GROWS_UP
     522             :         /* We got them backwards! */
     523             :         GC_push_all_stack(hi, lo);
     524             : #   else /* STACK_GROWS_DOWN */
     525         543 :         GC_push_all_stack(lo, hi);
     526             : #   endif
     527         543 : }
     528             : 
     529             : #else /* !THREADS */
     530             : 
     531             : # ifdef TRACE_BUF
     532             :     /* Defined in mark.c.       */
     533             :     void GC_add_trace_entry(char *kind, word arg1, word arg2);
     534             : # endif
     535             : 
     536             :                         /* Similar to GC_push_all_eager, but only the   */
     537             :                         /* part hotter than cold_gc_frame is scanned    */
     538             :                         /* immediately.  Needed to ensure that callee-  */
     539             :                         /* save registers are not missed.               */
     540             : /*
     541             :  * A version of GC_push_all that treats all interior pointers as valid
     542             :  * and scans part of the area immediately, to make sure that saved
     543             :  * register values are not lost.
     544             :  * Cold_gc_frame delimits the stack section that must be scanned
     545             :  * eagerly.  A zero value indicates that no eager scanning is needed.
     546             :  * We don't need to worry about the MANUAL_VDB case here, since this
     547             :  * is only called in the single-threaded case.  We assume that we
     548             :  * cannot collect between an assignment and the corresponding
     549             :  * GC_dirty() call.
     550             :  */
     551             : STATIC void GC_push_all_stack_partially_eager(ptr_t bottom, ptr_t top,
     552             :                                               ptr_t cold_gc_frame)
     553             : {
     554             :   if (!NEED_FIXUP_POINTER && GC_all_interior_pointers) {
     555             :     /* Push the hot end of the stack eagerly, so that register values   */
     556             :     /* saved inside GC frames are marked before they disappear.         */
     557             :     /* The rest of the marking can be deferred until later.             */
     558             :     if (0 == cold_gc_frame) {
     559             :         GC_push_all_stack(bottom, top);
     560             :         return;
     561             :     }
     562             :     GC_ASSERT(bottom <= cold_gc_frame && cold_gc_frame <= top);
     563             : #   ifdef STACK_GROWS_DOWN
     564             :         GC_push_all(cold_gc_frame - sizeof(ptr_t), top);
     565             :         GC_push_all_eager(bottom, cold_gc_frame);
     566             : #   else /* STACK_GROWS_UP */
     567             :         GC_push_all(bottom, cold_gc_frame + sizeof(ptr_t));
     568             :         GC_push_all_eager(cold_gc_frame, top);
     569             : #   endif /* STACK_GROWS_UP */
     570             :   } else {
     571             :     GC_push_all_eager(bottom, top);
     572             :   }
     573             : # ifdef TRACE_BUF
     574             :       GC_add_trace_entry("GC_push_all_stack", bottom, top);
     575             : # endif
     576             : }
     577             : 
     578             : /* Similar to GC_push_all_stack_sections() but also uses cold_gc_frame. */
     579             : STATIC void GC_push_all_stack_part_eager_sections(ptr_t lo, ptr_t hi,
     580             :         ptr_t cold_gc_frame, struct GC_traced_stack_sect_s *traced_stack_sect)
     581             : {
     582             :     GC_ASSERT(traced_stack_sect == NULL || cold_gc_frame == NULL ||
     583             :                 cold_gc_frame HOTTER_THAN (ptr_t)traced_stack_sect);
     584             : 
     585             :     while (traced_stack_sect != NULL) {
     586             :         GC_ASSERT(lo HOTTER_THAN (ptr_t)traced_stack_sect);
     587             : #       ifdef STACK_GROWS_UP
     588             :             GC_push_all_stack_partially_eager((ptr_t)traced_stack_sect, lo,
     589             :                                               cold_gc_frame);
     590             : #       else /* STACK_GROWS_DOWN */
     591             :             GC_push_all_stack_partially_eager(lo, (ptr_t)traced_stack_sect,
     592             :                                               cold_gc_frame);
     593             : #       endif
     594             :         lo = traced_stack_sect -> saved_stack_ptr;
     595             :         GC_ASSERT(lo != NULL);
     596             :         traced_stack_sect = traced_stack_sect -> prev;
     597             :         cold_gc_frame = NULL; /* Use at most once.      */
     598             :     }
     599             : 
     600             :     GC_ASSERT(!(hi HOTTER_THAN lo));
     601             : #   ifdef STACK_GROWS_UP
     602             :         /* We got them backwards! */
     603             :         GC_push_all_stack_partially_eager(hi, lo, cold_gc_frame);
     604             : #   else /* STACK_GROWS_DOWN */
     605             :         GC_push_all_stack_partially_eager(lo, hi, cold_gc_frame);
     606             : #   endif
     607             : }
     608             : 
     609             : #endif /* !THREADS */
     610             : 
     611             :                         /* Push enough of the current stack eagerly to  */
     612             :                         /* ensure that callee-save registers saved in   */
     613             :                         /* GC frames are scanned.                       */
     614             :                         /* In the non-threads case, schedule entire     */
     615             :                         /* stack for scanning.                          */
     616             :                         /* The second argument is a pointer to the      */
     617             :                         /* (possibly null) thread context, for          */
     618             :                         /* (currently hypothetical) more precise        */
     619             :                         /* stack scanning.                              */
     620             : /*
     621             :  * In the absence of threads, push the stack contents.
     622             :  * In the presence of threads, push enough of the current stack
     623             :  * to ensure that callee-save registers saved in collector frames have been
     624             :  * seen.
     625             :  * FIXME: Merge with per-thread stuff.
     626             :  */
     627             : /*ARGSUSED*/
     628         258 : STATIC void GC_push_current_stack(ptr_t cold_gc_frame, void * context)
     629             : {
     630             : #   if defined(THREADS)
     631         258 :         if (0 == cold_gc_frame) return;
     632             : #       ifdef STACK_GROWS_DOWN
     633         258 :           GC_push_all_eager(GC_approx_sp(), cold_gc_frame);
     634             :           /* For IA64, the register stack backing store is handled      */
     635             :           /* in the thread-specific code.                               */
     636             : #       else
     637             :           GC_push_all_eager(cold_gc_frame, GC_approx_sp());
     638             : #       endif
     639             : #   else
     640             :         GC_push_all_stack_part_eager_sections(GC_approx_sp(), GC_stackbottom,
     641             :                                         cold_gc_frame, GC_traced_stack_sect);
     642             : #       ifdef IA64
     643             :               /* We also need to push the register stack backing store. */
     644             :               /* This should really be done in the same way as the      */
     645             :               /* regular stack.  For now we fudge it a bit.             */
     646             :               /* Note that the backing store grows up, so we can't use  */
     647             :               /* GC_push_all_stack_partially_eager.                     */
     648             :               {
     649             :                 ptr_t bsp = GC_save_regs_ret_val;
     650             :                 ptr_t cold_gc_bs_pointer = bsp - 2048;
     651             :                 if (GC_all_interior_pointers &&
     652             :                     cold_gc_bs_pointer > BACKING_STORE_BASE) {
     653             :                   /* Adjust cold_gc_bs_pointer if below our innermost   */
     654             :                   /* "traced stack section" in backing store.           */
     655             :                   if (GC_traced_stack_sect != NULL && cold_gc_bs_pointer <
     656             :                                 GC_traced_stack_sect->backing_store_end)
     657             :                     cold_gc_bs_pointer =
     658             :                                 GC_traced_stack_sect->backing_store_end;
     659             :                   GC_push_all_register_sections(BACKING_STORE_BASE,
     660             :                         cold_gc_bs_pointer, FALSE, GC_traced_stack_sect);
     661             :                   GC_push_all_eager(cold_gc_bs_pointer, bsp);
     662             :                 } else {
     663             :                   GC_push_all_register_sections(BACKING_STORE_BASE, bsp,
     664             :                                 TRUE /* eager */, GC_traced_stack_sect);
     665             :                 }
     666             :                 /* All values should be sufficiently aligned that we    */
     667             :                 /* don't have to worry about the boundary.              */
     668             :               }
     669             : #       endif
     670             : #   endif /* !THREADS */
     671             : }
     672             : 
     673             : GC_INNER void (*GC_push_typed_structures)(void) = 0;
     674             : 
     675             :                         /* Push GC internal roots.  These are normally  */
     676             :                         /* included in the static data segment, and     */
     677             :                         /* Thus implicitly pushed.  But we must do this */
     678             :                         /* explicitly if normal root processing is      */
     679             :                         /* disabled.                                    */
     680             : /*
     681             :  * Push GC internal roots.  Only called if there is some reason to believe
     682             :  * these would not otherwise get registered.
     683             :  */
     684           0 : STATIC void GC_push_gc_structures(void)
     685             : {
     686           0 :     GC_push_finalizer_structures();
     687             : #   if defined(THREADS)
     688           0 :       GC_push_thread_structures();
     689             : #   endif
     690           0 :     if( GC_push_typed_structures )
     691           0 :       GC_push_typed_structures();
     692           0 : }
     693             : 
     694         252 : GC_INNER void GC_cond_register_dynamic_libraries(void)
     695             : {
     696             : # if defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
     697             :      || defined(CYGWIN32) || defined(PCR)
     698         252 :     GC_remove_tmp_roots();
     699         252 :     if (!GC_no_dls) GC_register_dynamic_libraries();
     700             : # else
     701             :     GC_no_dls = TRUE;
     702             : # endif
     703         252 : }
     704             : 
     705         258 : STATIC void GC_push_regs_and_stack(ptr_t cold_gc_frame)
     706             : {
     707         258 :     GC_with_callee_saves_pushed(GC_push_current_stack, cold_gc_frame);
     708         258 : }
     709             : 
     710             : /*
     711             :  * Call the mark routines (GC_tl_push for a single pointer, GC_push_conditional
     712             :  * on groups of pointers) on every top level accessible pointer.
     713             :  * If all is FALSE, arrange to push only possibly altered values.
     714             :  * Cold_gc_frame is an address inside a GC frame that
     715             :  * remains valid until all marking is complete.
     716             :  * A zero value indicates that it's OK to miss some
     717             :  * register values.
     718             :  */
     719         258 : GC_INNER void GC_push_roots(GC_bool all, ptr_t cold_gc_frame)
     720             : {
     721             :     int i;
     722             :     unsigned kind;
     723             : 
     724             :     /*
     725             :      * Next push static data.  This must happen early on, since it's
     726             :      * not robust against mark stack overflow.
     727             :      */
     728             :      /* Re-register dynamic libraries, in case one got added.           */
     729             :      /* There is some argument for doing this as late as possible,      */
     730             :      /* especially on win32, where it can change asynchronously.        */
     731             :      /* In those cases, we do it here.  But on other platforms, it's    */
     732             :      /* not safe with the world stopped, so we do it earlier.           */
     733             : #      if !defined(REGISTER_LIBRARIES_EARLY)
     734             :          GC_cond_register_dynamic_libraries();
     735             : #      endif
     736             : 
     737             :      /* Mark everything in static data areas                             */
     738        3263 :        for (i = 0; i < n_root_sets; i++) {
     739        3005 :          GC_push_conditional_with_exclusions(
     740             :                              GC_static_roots[i].r_start,
     741             :                              GC_static_roots[i].r_end, all);
     742             :        }
     743             : 
     744             :      /* Mark all free list header blocks, if those were allocated from  */
     745             :      /* the garbage collected heap.  This makes sure they don't         */
     746             :      /* disappear if we are not marking from static data.  It also      */
     747             :      /* saves us the trouble of scanning them, and possibly that of     */
     748             :      /* marking the freelists.                                          */
     749        1290 :        for (kind = 0; kind < GC_n_kinds; kind++) {
     750        1032 :          void *base = GC_base(GC_obj_kinds[kind].ok_freelist);
     751        1032 :          if (0 != base) {
     752           0 :            GC_set_mark_bit(base);
     753             :          }
     754             :        }
     755             : 
     756             :      /* Mark from GC internal roots if those might otherwise have       */
     757             :      /* been excluded.                                                  */
     758         258 :        if (GC_no_dls || roots_were_cleared) {
     759           0 :            GC_push_gc_structures();
     760             :        }
     761             : 
     762             :      /* Mark thread local free lists, even if their mark        */
     763             :      /* descriptor excludes the link field.                     */
     764             :      /* If the world is not stopped, this is unsafe.  It is     */
     765             :      /* also unnecessary, since we will do this again with the  */
     766             :      /* world stopped.                                          */
     767             : #      if defined(THREAD_LOCAL_ALLOC)
     768         258 :          if (GC_world_stopped) GC_mark_thread_local_free_lists();
     769             : #      endif
     770             : 
     771             :     /*
     772             :      * Now traverse stacks, and mark from register contents.
     773             :      * These must be done last, since they can legitimately overflow
     774             :      * the mark stack.
     775             :      * This is usually done by saving the current context on the
     776             :      * stack, and then just tracing from the stack.
     777             :      */
     778         258 :       GC_push_regs_and_stack(cold_gc_frame);
     779             : 
     780         258 :     if (GC_push_other_roots != 0) (*GC_push_other_roots)();
     781             :         /* In the threads case, this also pushes thread stacks. */
     782             :         /* Note that without interior pointer recognition lots  */
     783             :         /* of stuff may have been pushed already, and this      */
     784             :         /* should be careful about mark stack overflows.        */
     785         258 : }

Generated by: LCOV version 1.11