]> granicus.if.org Git - gc/blob - reclaim.c
Eliminate 'possible loss of data' MS VC warning in disclaim_and_reclaim
[gc] / reclaim.c
1 /*
2  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991-1996 by Xerox Corporation.  All rights reserved.
4  * Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
5  * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
6  *
7  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
9  *
10  * Permission is hereby granted to use or copy this program
11  * for any purpose,  provided the above notices are retained on all copies.
12  * Permission to modify the code and to distribute modified code is granted,
13  * provided the above notices are retained, and a notice that the code was
14  * modified is included with the above copyright notice.
15  */
16
17 #include "private/gc_priv.h"
18
19 #ifdef ENABLE_DISCLAIM
20 #  include "gc_disclaim.h"
21 #endif
22
23 #include <stdio.h>
24
25 GC_INNER signed_word GC_bytes_found = 0;
26                         /* Number of bytes of memory reclaimed     */
27                         /* minus the number of bytes originally    */
28                         /* on free lists which we had to drop.     */
29
30 #if defined(PARALLEL_MARK)
31   GC_INNER word GC_fl_builder_count = 0;
32         /* Number of threads currently building free lists without      */
33         /* holding GC lock.  It is not safe to collect if this is       */
34         /* nonzero.                                                     */
35 #endif /* PARALLEL_MARK */
36
37 /* We defer printing of leaked objects until we're done with the GC     */
38 /* cycle, since the routine for printing objects needs to run outside   */
39 /* the collector, e.g. without the allocation lock.                     */
40 #ifndef MAX_LEAKED
41 # define MAX_LEAKED 40
42 #endif
43 STATIC ptr_t GC_leaked[MAX_LEAKED] = { NULL };
44 STATIC unsigned GC_n_leaked = 0;
45
46 GC_INNER GC_bool GC_have_errors = FALSE;
47
48 #if !defined(EAGER_SWEEP) && defined(ENABLE_DISCLAIM)
49   STATIC void GC_reclaim_unconditionally_marked(void);
50 #endif
51
52 GC_INLINE void GC_add_leaked(ptr_t leaked)
53 {
54 #  ifndef SHORT_DBG_HDRS
55      if (GC_findleak_delay_free && !GC_check_leaked(leaked))
56        return;
57 #  endif
58
59     GC_have_errors = TRUE;
60     if (GC_n_leaked < MAX_LEAKED) {
61       GC_leaked[GC_n_leaked++] = leaked;
62       /* Make sure it's not reclaimed this cycle */
63       GC_set_mark_bit(leaked);
64     }
65 }
66
67 /* Print all objects on the list after printing any smashed objects.    */
68 /* Clear both lists.  Called without the allocation lock held.          */
69 GC_INNER void GC_print_all_errors(void)
70 {
71     static GC_bool printing_errors = FALSE;
72     GC_bool have_errors;
73     unsigned i, n_leaked;
74     ptr_t leaked[MAX_LEAKED];
75     DCL_LOCK_STATE;
76
77     LOCK();
78     if (printing_errors) {
79         UNLOCK();
80         return;
81     }
82     have_errors = GC_have_errors;
83     printing_errors = TRUE;
84     n_leaked = GC_n_leaked;
85     GC_ASSERT(n_leaked <= MAX_LEAKED);
86     BCOPY(GC_leaked, leaked, n_leaked * sizeof(ptr_t));
87     GC_n_leaked = 0;
88     BZERO(GC_leaked, n_leaked * sizeof(ptr_t));
89     UNLOCK();
90
91     if (GC_debugging_started) {
92       GC_print_all_smashed();
93     } else {
94       have_errors = FALSE;
95     }
96
97     if (n_leaked > 0) {
98         GC_err_printf("Found %u leaked objects:\n", n_leaked);
99         have_errors = TRUE;
100     }
101     for (i = 0; i < n_leaked; i++) {
102         ptr_t p = leaked[i];
103         GC_print_heap_obj(p);
104         GC_free(p);
105     }
106
107     if (have_errors
108 #       ifndef GC_ABORT_ON_LEAK
109           && GETENV("GC_ABORT_ON_LEAK") != NULL
110 #       endif
111         ) {
112       ABORT("Leaked or smashed objects encountered");
113     }
114
115     LOCK();
116     printing_errors = FALSE;
117     UNLOCK();
118 }
119
120
121 /*
122  * reclaim phase
123  *
124  */
125
126 /* Test whether a block is completely empty, i.e. contains no marked    */
127 /* objects.  This does not require the block to be in physical memory.  */
128 GC_INNER GC_bool GC_block_empty(hdr *hhdr)
129 {
130     return (hhdr -> hb_n_marks == 0);
131 }
132
133 STATIC GC_bool GC_block_nearly_full(hdr *hhdr)
134 {
135     return (hhdr -> hb_n_marks > 7 * HBLK_OBJS(hhdr -> hb_sz)/8);
136 }
137
138 /* FIXME: This should perhaps again be specialized for USE_MARK_BYTES   */
139 /* and USE_MARK_BITS cases.                                             */
140
141 /*
142  * Restore unmarked small objects in h of size sz to the object
143  * free list.  Returns the new list.
144  * Clears unmarked objects.  Sz is in bytes.
145  */
146 STATIC ptr_t GC_reclaim_clear(struct hblk *hbp, hdr *hhdr, size_t sz,
147                               ptr_t list, signed_word *count)
148 {
149     word bit_no = 0;
150     word *p, *q, *plim;
151     signed_word n_bytes_found = 0;
152
153     GC_ASSERT(hhdr == GC_find_header((ptr_t)hbp));
154     GC_ASSERT(sz == hhdr -> hb_sz);
155     GC_ASSERT((sz & (BYTES_PER_WORD-1)) == 0);
156     p = (word *)(hbp->hb_body);
157     plim = (word *)(hbp->hb_body + HBLKSIZE - sz);
158
159     /* go through all words in block */
160         while ((word)p <= (word)plim) {
161             if (mark_bit_from_hdr(hhdr, bit_no)) {
162                 p = (word *)((ptr_t)p + sz);
163             } else {
164                 n_bytes_found += sz;
165                 /* object is available - put on list */
166                     obj_link(p) = list;
167                     list = ((ptr_t)p);
168                 /* Clear object, advance p to next object in the process */
169                     q = (word *)((ptr_t)p + sz);
170 #                   ifdef USE_MARK_BYTES
171                       GC_ASSERT(!(sz & 1)
172                                 && !((word)p & (2 * sizeof(word) - 1)));
173                       p[1] = 0;
174                       p += 2;
175                       while ((word)p < (word)q) {
176                         CLEAR_DOUBLE(p);
177                         p += 2;
178                       }
179 #                   else
180                       p++; /* Skip link field */
181                       while ((word)p < (word)q) {
182                         *p++ = 0;
183                       }
184 #                   endif
185             }
186             bit_no += MARK_BIT_OFFSET(sz);
187         }
188     *count += n_bytes_found;
189     return(list);
190 }
191
192 /* The same thing, but don't clear objects: */
193 STATIC ptr_t GC_reclaim_uninit(struct hblk *hbp, hdr *hhdr, size_t sz,
194                                ptr_t list, signed_word *count)
195 {
196     word bit_no = 0;
197     word *p, *plim;
198     signed_word n_bytes_found = 0;
199
200     GC_ASSERT(sz == hhdr -> hb_sz);
201     p = (word *)(hbp->hb_body);
202     plim = (word *)((ptr_t)hbp + HBLKSIZE - sz);
203
204     /* go through all words in block */
205         while ((word)p <= (word)plim) {
206             if (!mark_bit_from_hdr(hhdr, bit_no)) {
207                 n_bytes_found += sz;
208                 /* object is available - put on list */
209                     obj_link(p) = list;
210                     list = ((ptr_t)p);
211             }
212             p = (word *)((ptr_t)p + sz);
213             bit_no += MARK_BIT_OFFSET(sz);
214         }
215     *count += n_bytes_found;
216     return(list);
217 }
218
219 #ifdef ENABLE_DISCLAIM
220   /* Call reclaim notifier for block's kind on each unmarked object in  */
221   /* block, all within a pair of corresponding enter/leave callbacks.   */
222   STATIC ptr_t GC_disclaim_and_reclaim(struct hblk *hbp, hdr *hhdr, size_t sz,
223                                        ptr_t list, signed_word *count)
224   {
225     word bit_no = 0;
226     word *p, *q, *plim;
227     signed_word n_bytes_found = 0;
228     struct obj_kind *ok = &GC_obj_kinds[hhdr->hb_obj_kind];
229     int (GC_CALLBACK *disclaim)(void *) = ok->ok_disclaim_proc;
230
231     GC_ASSERT(sz == hhdr -> hb_sz);
232     p = (word *)(hbp -> hb_body);
233     plim = (word *)((ptr_t)p + HBLKSIZE - sz);
234
235     while ((word)p <= (word)plim) {
236         int marked = mark_bit_from_hdr(hhdr, bit_no);
237         if (!marked && (*disclaim)(p)) {
238             hhdr -> hb_n_marks++;
239             marked = 1;
240         }
241         if (marked)
242             p = (word *)((ptr_t)p + sz);
243         else {
244                 n_bytes_found += sz;
245                 /* object is available - put on list */
246                     obj_link(p) = list;
247                     list = ((ptr_t)p);
248                 /* Clear object, advance p to next object in the process */
249                     q = (word *)((ptr_t)p + sz);
250 #                   ifdef USE_MARK_BYTES
251                       GC_ASSERT((sz & 1) == 0);
252                       GC_ASSERT(((word)p & (2 * sizeof(word) - 1)) == 0);
253                       p[1] = 0;
254                       p += 2;
255                       while ((word)p < (word)q) {
256                         CLEAR_DOUBLE(p);
257                         p += 2;
258                       }
259 #                   else
260                       p++; /* Skip link field */
261                       while ((word)p < (word)q) {
262                         *p++ = 0;
263                       }
264 #                   endif
265         }
266         bit_no += MARK_BIT_OFFSET(sz);
267     }
268     *count += n_bytes_found;
269     return list;
270   }
271 #endif /* ENABLE_DISCLAIM */
272
273 /* Don't really reclaim objects, just check for unmarked ones: */
274 STATIC void GC_reclaim_check(struct hblk *hbp, hdr *hhdr, word sz)
275 {
276     word bit_no;
277     ptr_t p, plim;
278     GC_ASSERT(sz == hhdr -> hb_sz);
279
280     /* go through all words in block */
281     p = hbp->hb_body;
282     plim = p + HBLKSIZE - sz;
283     for (bit_no = 0; (word)p <= (word)plim;
284          p += sz, bit_no += MARK_BIT_OFFSET(sz)) {
285       if (!mark_bit_from_hdr(hhdr, bit_no)) {
286         GC_add_leaked(p);
287       }
288     }
289 }
290
291 /*
292  * Generic procedure to rebuild a free list in hbp.
293  * Also called directly from GC_malloc_many.
294  * Sz is now in bytes.
295  */
296 GC_INNER ptr_t GC_reclaim_generic(struct hblk * hbp, hdr *hhdr, size_t sz,
297                                   GC_bool init, ptr_t list,
298                                   signed_word *count)
299 {
300     ptr_t result;
301
302     GC_ASSERT(GC_find_header((ptr_t)hbp) == hhdr);
303 #   ifndef GC_DISABLE_INCREMENTAL
304       GC_remove_protection(hbp, 1, (hhdr)->hb_descr == 0 /* Pointer-free? */);
305 #   endif
306 #   ifdef ENABLE_DISCLAIM
307       if ((hhdr -> hb_flags & HAS_DISCLAIM) != 0) {
308         result = GC_disclaim_and_reclaim(hbp, hhdr, sz, list, count);
309       } else
310 #   endif
311     /* else */ if (init || GC_debugging_started) {
312       result = GC_reclaim_clear(hbp, hhdr, sz, list, count);
313     } else {
314       GC_ASSERT((hhdr)->hb_descr == 0 /* Pointer-free block */);
315       result = GC_reclaim_uninit(hbp, hhdr, sz, list, count);
316     }
317     if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) GC_set_hdr_marks(hhdr);
318     return result;
319 }
320
321 /*
322  * Restore unmarked small objects in the block pointed to by hbp
323  * to the appropriate object free list.
324  * If entirely empty blocks are to be completely deallocated, then
325  * caller should perform that check.
326  */
327 STATIC void GC_reclaim_small_nonempty_block(struct hblk *hbp,
328                                             GC_bool report_if_found)
329 {
330     hdr *hhdr = HDR(hbp);
331     size_t sz = hhdr -> hb_sz;
332     struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
333     void **flh = &(ok -> ok_freelist[BYTES_TO_GRANULES(sz)]);
334
335     hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
336
337     if (report_if_found) {
338         GC_reclaim_check(hbp, hhdr, sz);
339     } else {
340         *flh = GC_reclaim_generic(hbp, hhdr, sz, ok -> ok_init,
341                                   *flh, &GC_bytes_found);
342     }
343 }
344
345 #ifdef ENABLE_DISCLAIM
346   STATIC void GC_disclaim_and_reclaim_or_free_small_block(struct hblk *hbp)
347   {
348     hdr *hhdr = HDR(hbp);
349     size_t sz = hhdr -> hb_sz;
350     struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
351     void **flh = &(ok -> ok_freelist[BYTES_TO_GRANULES(sz)]);
352     void *flh_next;
353
354     hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
355     flh_next = GC_reclaim_generic(hbp, hhdr, sz, ok -> ok_init,
356                                   *flh, &GC_bytes_found);
357     if (hhdr -> hb_n_marks)
358         *flh = flh_next;
359     else {
360         GC_bytes_found += HBLKSIZE;
361         GC_freehblk(hbp);
362     }
363   }
364 #endif /* ENABLE_DISCLAIM */
365
366 /*
367  * Restore an unmarked large object or an entirely empty blocks of small objects
368  * to the heap block free list.
369  * Otherwise enqueue the block for later processing
370  * by GC_reclaim_small_nonempty_block.
371  * If report_if_found is TRUE, then process any block immediately, and
372  * simply report free objects; do not actually reclaim them.
373  */
374 STATIC void GC_reclaim_block(struct hblk *hbp, word report_if_found)
375 {
376     hdr * hhdr = HDR(hbp);
377     size_t sz = hhdr -> hb_sz;  /* size of objects in current block     */
378     struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
379
380     if( sz > MAXOBJBYTES ) {  /* 1 big object */
381         if( !mark_bit_from_hdr(hhdr, 0) ) {
382             if (report_if_found) {
383               GC_add_leaked((ptr_t)hbp);
384             } else {
385               size_t blocks;
386
387 #             ifdef ENABLE_DISCLAIM
388                 if (EXPECT(hhdr->hb_flags & HAS_DISCLAIM, 0)) {
389                   struct obj_kind *ok = &GC_obj_kinds[hhdr->hb_obj_kind];
390                   if ((*ok->ok_disclaim_proc)(hbp)) {
391                     /* Not disclaimed => resurrect the object. */
392                     set_mark_bit_from_hdr(hhdr, 0);
393                     goto in_use;
394                   }
395                 }
396 #             endif
397               blocks = OBJ_SZ_TO_BLOCKS(sz);
398               if (blocks > 1) {
399                 GC_large_allocd_bytes -= blocks * HBLKSIZE;
400               }
401               GC_bytes_found += sz;
402               GC_freehblk(hbp);
403             }
404         } else {
405 #        ifdef ENABLE_DISCLAIM
406            in_use:
407 #        endif
408             if (hhdr -> hb_descr != 0) {
409               GC_composite_in_use += sz;
410             } else {
411               GC_atomic_in_use += sz;
412             }
413         }
414     } else {
415         GC_bool empty = GC_block_empty(hhdr);
416 #       ifdef PARALLEL_MARK
417           /* Count can be low or one too high because we sometimes      */
418           /* have to ignore decrements.  Objects can also potentially   */
419           /* be repeatedly marked by each marker.                       */
420           /* Here we assume two markers, but this is extremely          */
421           /* unlikely to fail spuriously with more.  And if it does, it */
422           /* should be looked at.                                       */
423           GC_ASSERT(hhdr -> hb_n_marks <= 2 * (HBLKSIZE/sz + 1) + 16);
424 #       else
425           GC_ASSERT(sz * hhdr -> hb_n_marks <= HBLKSIZE);
426 #       endif
427         if (report_if_found) {
428           GC_reclaim_small_nonempty_block(hbp, TRUE /* report_if_found */);
429         } else if (empty) {
430 #       ifdef ENABLE_DISCLAIM
431           if ((hhdr -> hb_flags & HAS_DISCLAIM) != 0) {
432             GC_disclaim_and_reclaim_or_free_small_block(hbp);
433           } else
434 #       endif
435           /* else */ {
436             GC_bytes_found += HBLKSIZE;
437             GC_freehblk(hbp);
438           }
439         } else if (GC_find_leak || !GC_block_nearly_full(hhdr)) {
440           /* group of smaller objects, enqueue the real work */
441           struct hblk **rlh = ok -> ok_reclaim_list + BYTES_TO_GRANULES(sz);
442
443           hhdr -> hb_next = *rlh;
444           *rlh = hbp;
445         } /* else not worth salvaging. */
446         /* We used to do the nearly_full check later, but we    */
447         /* already have the right cache context here.  Also     */
448         /* doing it here avoids some silly lock contention in   */
449         /* GC_malloc_many.                                      */
450
451         if (hhdr -> hb_descr != 0) {
452           GC_composite_in_use += sz * hhdr -> hb_n_marks;
453         } else {
454           GC_atomic_in_use += sz * hhdr -> hb_n_marks;
455         }
456     }
457 }
458
459 #if !defined(NO_DEBUGGING)
460 /* Routines to gather and print heap block info         */
461 /* intended for debugging.  Otherwise should be called  */
462 /* with lock.                                           */
463
464 struct Print_stats
465 {
466         size_t number_of_blocks;
467         size_t total_bytes;
468 };
469
470 #ifdef USE_MARK_BYTES
471
472 /* Return the number of set mark bits in the given header.      */
473 /* Remains externally visible as used by GNU GCJ currently.     */
474 int GC_n_set_marks(hdr *hhdr)
475 {
476     int result = 0;
477     int i;
478     size_t sz = hhdr -> hb_sz;
479     int offset = (int)MARK_BIT_OFFSET(sz);
480     int limit = (int)FINAL_MARK_BIT(sz);
481
482     for (i = 0; i < limit; i += offset) {
483         result += hhdr -> hb_marks[i];
484     }
485     GC_ASSERT(hhdr -> hb_marks[limit]);
486     return(result);
487 }
488
489 #else
490
491 /* Number of set bits in a word.  Not performance critical.     */
492 static int set_bits(word n)
493 {
494     word m = n;
495     int result = 0;
496
497     while (m > 0) {
498         if (m & 1) result++;
499         m >>= 1;
500     }
501     return(result);
502 }
503
504 int GC_n_set_marks(hdr *hhdr)
505 {
506     int result = 0;
507     int i;
508     int n_mark_words;
509 #   ifdef MARK_BIT_PER_OBJ
510       int n_objs = (int)HBLK_OBJS(hhdr -> hb_sz);
511
512       if (0 == n_objs) n_objs = 1;
513       n_mark_words = divWORDSZ(n_objs + WORDSZ - 1);
514 #   else /* MARK_BIT_PER_GRANULE */
515       n_mark_words = MARK_BITS_SZ;
516 #   endif
517     for (i = 0; i < n_mark_words - 1; i++) {
518         result += set_bits(hhdr -> hb_marks[i]);
519     }
520 #   ifdef MARK_BIT_PER_OBJ
521       result += set_bits((hhdr -> hb_marks[n_mark_words - 1])
522                          << (n_mark_words * WORDSZ - n_objs));
523 #   else
524       result += set_bits(hhdr -> hb_marks[n_mark_words - 1]);
525 #   endif
526     return(result - 1);
527 }
528
529 #endif /* !USE_MARK_BYTES  */
530
531 STATIC void GC_print_block_descr(struct hblk *h,
532                                  word /* struct PrintStats */ raw_ps)
533 {
534     hdr * hhdr = HDR(h);
535     size_t bytes = hhdr -> hb_sz;
536     struct Print_stats *ps;
537     unsigned n_marks = GC_n_set_marks(hhdr);
538     unsigned n_objs = HBLK_OBJS((unsigned)bytes);
539
540     if (0 == n_objs) n_objs = 1;
541     if (hhdr -> hb_n_marks != n_marks) {
542       GC_printf("%u,%u,%u!=%u,%u\n", hhdr->hb_obj_kind, (unsigned)bytes,
543                 (unsigned)hhdr->hb_n_marks, n_marks, n_objs);
544     } else {
545       GC_printf("%u,%u,%u,%u\n", hhdr->hb_obj_kind, (unsigned)bytes,
546                 n_marks, n_objs);
547     }
548
549     ps = (struct Print_stats *)raw_ps;
550     ps->total_bytes += (bytes + (HBLKSIZE-1)) & ~(HBLKSIZE-1); /* round up */
551     ps->number_of_blocks++;
552 }
553
554 void GC_print_block_list(void)
555 {
556     struct Print_stats pstats;
557
558     GC_printf("kind(0=ptrfree,1=normal,2=unc.),"
559               "size_in_bytes,#_marks_set,#objs\n");
560     pstats.number_of_blocks = 0;
561     pstats.total_bytes = 0;
562     GC_apply_to_all_blocks(GC_print_block_descr, (word)&pstats);
563     GC_printf("blocks= %lu, bytes= %lu\n",
564               (unsigned long)pstats.number_of_blocks,
565               (unsigned long)pstats.total_bytes);
566 }
567
568 #include "gc_inline.h" /* for GC_print_free_list prototype */
569
570 /* Currently for debugger use only: */
571 GC_API void GC_CALL GC_print_free_list(int kind, size_t sz_in_granules)
572 {
573     ptr_t flh;
574     int n;
575
576     GC_ASSERT(kind < MAXOBJKINDS);
577     GC_ASSERT(sz_in_granules <= MAXOBJGRANULES);
578     flh = GC_obj_kinds[kind].ok_freelist[sz_in_granules];
579     for (n = 0; flh; n++) {
580         struct hblk *block = HBLKPTR(flh);
581         GC_printf("Free object in heap block %p [%d]: %p\n",
582                   (void *)block, n, (void *)flh);
583         flh = obj_link(flh);
584     }
585 }
586
587 #endif /* !NO_DEBUGGING */
588
589 /*
590  * Clear all obj_link pointers in the list of free objects *flp.
591  * Clear *flp.
592  * This must be done before dropping a list of free gcj-style objects,
593  * since may otherwise end up with dangling "descriptor" pointers.
594  * It may help for other pointer-containing objects.
595  */
596 STATIC void GC_clear_fl_links(void **flp)
597 {
598     void *next = *flp;
599
600     while (0 != next) {
601        *flp = 0;
602        flp = &(obj_link(next));
603        next = *flp;
604     }
605 }
606
607 /*
608  * Perform GC_reclaim_block on the entire heap, after first clearing
609  * small object free lists (if we are not just looking for leaks).
610  */
611 GC_INNER void GC_start_reclaim(GC_bool report_if_found)
612 {
613     unsigned kind;
614
615 #   if defined(PARALLEL_MARK)
616       GC_ASSERT(0 == GC_fl_builder_count);
617 #   endif
618     /* Reset in use counters.  GC_reclaim_block recomputes them. */
619       GC_composite_in_use = 0;
620       GC_atomic_in_use = 0;
621     /* Clear reclaim- and free-lists */
622       for (kind = 0; kind < GC_n_kinds; kind++) {
623         struct hblk ** rlist = GC_obj_kinds[kind].ok_reclaim_list;
624         GC_bool should_clobber = (GC_obj_kinds[kind].ok_descriptor != 0);
625
626         if (rlist == 0) continue;       /* This kind not used.  */
627         if (!report_if_found) {
628             void **fop;
629             void **lim = &(GC_obj_kinds[kind].ok_freelist[MAXOBJGRANULES+1]);
630
631             for (fop = GC_obj_kinds[kind].ok_freelist;
632                  (word)fop < (word)lim; (*(word **)&fop)++) {
633               if (*fop != 0) {
634                 if (should_clobber) {
635                   GC_clear_fl_links(fop);
636                 } else {
637                   *fop = 0;
638                 }
639               }
640             }
641         } /* otherwise free list objects are marked,    */
642           /* and its safe to leave them                 */
643         BZERO(rlist, (MAXOBJGRANULES + 1) * sizeof(void *));
644       }
645
646
647   /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
648   /* or enqueue the block for later processing.                            */
649     GC_apply_to_all_blocks(GC_reclaim_block, (word)report_if_found);
650
651 # ifdef EAGER_SWEEP
652     /* This is a very stupid thing to do.  We make it possible anyway,  */
653     /* so that you can convince yourself that it really is very stupid. */
654     GC_reclaim_all((GC_stop_func)0, FALSE);
655 # elif defined(ENABLE_DISCLAIM)
656     /* However, make sure to clear reclaimable objects of kinds with    */
657     /* unconditional marking enabled before we do any significant       */
658     /* marking work.                                                    */
659     GC_reclaim_unconditionally_marked();
660 # endif
661 # if defined(PARALLEL_MARK)
662     GC_ASSERT(0 == GC_fl_builder_count);
663 # endif
664
665 }
666
667 /*
668  * Sweep blocks of the indicated object size and kind until either the
669  * appropriate free list is nonempty, or there are no more blocks to
670  * sweep.
671  */
672 GC_INNER void GC_continue_reclaim(size_t sz /* granules */, int kind)
673 {
674     hdr * hhdr;
675     struct hblk * hbp;
676     struct obj_kind * ok = &(GC_obj_kinds[kind]);
677     struct hblk ** rlh = ok -> ok_reclaim_list;
678     void **flh = &(ok -> ok_freelist[sz]);
679
680     if (rlh == 0) return;       /* No blocks of this kind.      */
681     rlh += sz;
682     while ((hbp = *rlh) != 0) {
683         hhdr = HDR(hbp);
684         *rlh = hhdr -> hb_next;
685         GC_reclaim_small_nonempty_block(hbp, FALSE);
686         if (*flh != 0) break;
687     }
688 }
689
690 /*
691  * Reclaim all small blocks waiting to be reclaimed.
692  * Abort and return FALSE when/if (*stop_func)() returns TRUE.
693  * If this returns TRUE, then it's safe to restart the world
694  * with incorrectly cleared mark bits.
695  * If ignore_old is TRUE, then reclaim only blocks that have been
696  * recently reclaimed, and discard the rest.
697  * Stop_func may be 0.
698  */
699 GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old)
700 {
701     word sz;
702     unsigned kind;
703     hdr * hhdr;
704     struct hblk * hbp;
705     struct obj_kind * ok;
706     struct hblk ** rlp;
707     struct hblk ** rlh;
708 #   ifndef SMALL_CONFIG
709       CLOCK_TYPE start_time = 0; /* initialized to prevent warning. */
710
711       if (GC_print_stats == VERBOSE)
712         GET_TIME(start_time);
713 #   endif
714
715     for (kind = 0; kind < GC_n_kinds; kind++) {
716         ok = &(GC_obj_kinds[kind]);
717         rlp = ok -> ok_reclaim_list;
718         if (rlp == 0) continue;
719         for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
720             rlh = rlp + sz;
721             while ((hbp = *rlh) != 0) {
722                 if (stop_func != (GC_stop_func)0 && (*stop_func)()) {
723                     return(FALSE);
724                 }
725                 hhdr = HDR(hbp);
726                 *rlh = hhdr -> hb_next;
727                 if (!ignore_old || hhdr -> hb_last_reclaimed == GC_gc_no - 1) {
728                     /* It's likely we'll need it this time, too */
729                     /* It's been touched recently, so this      */
730                     /* shouldn't trigger paging.                */
731                     GC_reclaim_small_nonempty_block(hbp, FALSE);
732                 }
733             }
734         }
735     }
736 #   ifndef SMALL_CONFIG
737       if (GC_print_stats == VERBOSE) {
738         CLOCK_TYPE done_time;
739
740         GET_TIME(done_time);
741         GC_verbose_log_printf("Disposing of reclaim lists took %lu msecs\n",
742                               MS_TIME_DIFF(done_time,start_time));
743       }
744 #   endif
745     return(TRUE);
746 }
747
748 #if !defined(EAGER_SWEEP) && defined(ENABLE_DISCLAIM)
749 /* We do an eager sweep on heap blocks where unconditional marking has  */
750 /* been enabled, so that any reclaimable objects have been reclaimed    */
751 /* before we start marking.  This is a simplified GC_reclaim_all        */
752 /* restricted to kinds where ok_mark_unconditionally is true.           */
753   STATIC void GC_reclaim_unconditionally_marked(void)
754   {
755     word sz;
756     unsigned kind;
757     hdr * hhdr;
758     struct hblk * hbp;
759     struct obj_kind * ok;
760     struct hblk ** rlp;
761     struct hblk ** rlh;
762
763     for (kind = 0; kind < GC_n_kinds; kind++) {
764         ok = &(GC_obj_kinds[kind]);
765         if (!ok->ok_mark_unconditionally)
766           continue;
767         rlp = ok->ok_reclaim_list;
768         if (rlp == 0)
769           continue;
770         for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
771             rlh = rlp + sz;
772             while ((hbp = *rlh) != 0) {
773                 hhdr = HDR(hbp);
774                 *rlh = hhdr->hb_next;
775                 GC_reclaim_small_nonempty_block(hbp, FALSE);
776             }
777         }
778     }
779   }
780 #endif /* !EAGER_SWEEP && ENABLE_DISCLAIM */
781
782 struct enumerate_reachable_s {
783   GC_reachable_object_proc proc;
784   void *client_data;
785 };
786
787 STATIC void GC_do_enumerate_reachable_objects(struct hblk *hbp, word ped)
788 {
789   struct hblkhdr *hhdr = HDR(hbp);
790   size_t sz = hhdr -> hb_sz;
791   size_t bit_no;
792   char *p, *plim;
793
794   if (GC_block_empty(hhdr)) {
795     return;
796   }
797
798   p = hbp->hb_body;
799   if (sz > MAXOBJBYTES) { /* one big object */
800     plim = p;
801   } else {
802     plim = hbp->hb_body + HBLKSIZE - sz;
803   }
804   /* Go through all words in block. */
805   for (bit_no = 0; p <= plim; bit_no += MARK_BIT_OFFSET(sz), p += sz) {
806     if (mark_bit_from_hdr(hhdr, bit_no)) {
807       ((struct enumerate_reachable_s *)ped)->proc(p, sz,
808                         ((struct enumerate_reachable_s *)ped)->client_data);
809     }
810   }
811 }
812
813 GC_API void GC_CALL GC_enumerate_reachable_objects_inner(
814                                                 GC_reachable_object_proc proc,
815                                                 void *client_data)
816 {
817   struct enumerate_reachable_s ed;
818
819   GC_ASSERT(I_HOLD_LOCK());
820   ed.proc = proc;
821   ed.client_data = client_data;
822   GC_apply_to_all_blocks(GC_do_enumerate_reachable_objects, (word)&ed);
823 }