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