]> granicus.if.org Git - gc/blob - reclaim.c
Fixed memory leak issue with unconditional marking.
[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 #include "gc_disclaim.h"
19
20 #include <stdio.h>
21
22 GC_INNER signed_word GC_bytes_found = 0;
23                         /* Number of bytes of memory reclaimed     */
24                         /* minus the number of bytes originally    */
25                         /* on free lists which we had to drop.     */
26
27 #if defined(PARALLEL_MARK)
28   GC_INNER word GC_fl_builder_count = 0;
29         /* Number of threads currently building free lists without      */
30         /* holding GC lock.  It is not safe to collect if this is       */
31         /* nonzero.                                                     */
32 #endif /* PARALLEL_MARK */
33
34 /* We defer printing of leaked objects until we're done with the GC     */
35 /* cycle, since the routine for printing objects needs to run outside   */
36 /* the collector, e.g. without the allocation lock.                     */
37 #ifndef MAX_LEAKED
38 # define MAX_LEAKED 40
39 #endif
40 STATIC ptr_t GC_leaked[MAX_LEAKED] = { NULL };
41 STATIC unsigned GC_n_leaked = 0;
42
43 GC_INNER GC_bool GC_have_errors = FALSE;
44
45 #if !defined(EAGER_SWEEP) && defined(MARK_UNCONDITIONALLY)
46 void GC_reclaim_unconditionally_marked(void);
47 #endif
48
49 GC_INLINE void GC_add_leaked(ptr_t leaked)
50 {
51 #  ifndef SHORT_DBG_HDRS
52      if (GC_findleak_delay_free && !GC_check_leaked(leaked))
53        return;
54 #  endif
55
56     GC_have_errors = TRUE;
57     /* FIXME: Prevent adding an object while printing leaked ones.      */
58     if (GC_n_leaked < MAX_LEAKED) {
59       GC_leaked[GC_n_leaked++] = leaked;
60       /* Make sure it's not reclaimed this cycle */
61       GC_set_mark_bit(leaked);
62     }
63 }
64
65 /* Print all objects on the list after printing any smashed objects.    */
66 /* Clear both lists.  Called without the allocation lock held.          */
67 GC_INNER void GC_print_all_errors(void)
68 {
69     static GC_bool printing_errors = FALSE;
70     GC_bool have_errors;
71     unsigned i;
72     DCL_LOCK_STATE;
73
74     LOCK();
75     if (printing_errors) {
76         UNLOCK();
77         return;
78     }
79     have_errors = GC_have_errors;
80     printing_errors = TRUE;
81     UNLOCK();
82
83     if (GC_debugging_started) {
84       GC_print_all_smashed();
85     } else {
86       have_errors = FALSE;
87     }
88
89     for (i = 0; i < GC_n_leaked; ++i) {
90         ptr_t p = GC_leaked[i];
91         if (HDR(p) -> hb_obj_kind == PTRFREE) {
92             GC_err_printf("Leaked atomic object at ");
93         } else {
94             GC_err_printf("Leaked composite object at ");
95         }
96         GC_print_heap_obj(p);
97         GC_err_printf("\n");
98         GC_free(p);
99         GC_leaked[i] = 0;
100         have_errors = TRUE;
101     }
102     GC_n_leaked = 0;
103
104     if (have_errors
105 #       ifndef GC_ABORT_ON_LEAK
106           && GETENV("GC_ABORT_ON_LEAK") != NULL
107 #       endif
108         ) {
109       ABORT("Leaked or smashed objects encountered");
110     }
111
112     printing_errors = FALSE;
113 }
114
115
116 /*
117  * reclaim phase
118  *
119  */
120
121 /* Test whether a block is completely empty, i.e. contains no marked    */
122 /* objects.  This does not require the block to be in physical memory.  */
123 GC_INNER GC_bool GC_block_empty(hdr *hhdr)
124 {
125     /* XXX: Only if reclaim notifiers have not been run. */
126 #   ifdef ENABLE_DISCLAIM
127         if (hhdr -> hb_flags & HAS_DISCLAIM)
128             return FALSE;
129 #   endif
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 (p <= 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 (p < q) {
176                         CLEAR_DOUBLE(p);
177                         p += 2;
178                       }
179 #                   else
180                       p++; /* Skip link field */
181                       while (p < 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 (p <= 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 ptr_t GC_disclaim_and_reclaim(struct hblk *hbp, hdr *hhdr, size_t sz,
223                               ptr_t list, signed_word *count)
224 {
225     register int bit_no = 0;
226     register 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 (*proc)(void *, void *) = ok -> ok_disclaim_proc;
230     void *cd = ok -> ok_disclaim_cd;
231
232     GC_ASSERT(sz == hhdr -> hb_sz);
233     p = (word *)(hbp -> hb_body);
234     plim = (word *)((ptr_t)p + HBLKSIZE - sz);
235
236     while (p <= plim) {
237         if (mark_bit_from_hdr(hhdr, bit_no) || (*proc)(p, cd))
238             p = (word *)((ptr_t)p + sz);
239         else {
240                 n_bytes_found += sz;
241                 /* object is available - put on list */
242                     obj_link(p) = list;
243                     list = ((ptr_t)p);
244                 /* Clear object, advance p to next object in the process */
245                     q = (word *)((ptr_t)p + sz);
246 #                   ifdef USE_MARK_BYTES
247                       GC_ASSERT(!(sz & 1)
248                                 && !((word)p & (2 * sizeof(word) - 1)));
249                       p[1] = 0;
250                       p += 2;
251                       while (p < q) {
252                         CLEAR_DOUBLE(p);
253                         p += 2;
254                       }
255 #                   else
256                       p++; /* Skip link field */
257                       while (p < q) {
258                         *p++ = 0;
259                       }
260 #                   endif
261         }
262         bit_no += MARK_BIT_OFFSET(sz);
263     }
264     *count += n_bytes_found;
265     return list;
266 }
267 #endif /* ENABLE_DISCLAIM */
268
269 /* Don't really reclaim objects, just check for unmarked ones: */
270 STATIC void GC_reclaim_check(struct hblk *hbp, hdr *hhdr, word sz)
271 {
272     word bit_no;
273     ptr_t p, plim;
274     GC_ASSERT(sz == hhdr -> hb_sz);
275
276     /* go through all words in block */
277     p = hbp->hb_body;
278     plim = p + HBLKSIZE - sz;
279     for (bit_no = 0; p <= plim; p += sz, bit_no += MARK_BIT_OFFSET(sz)) {
280       if (!mark_bit_from_hdr(hhdr, bit_no)) {
281         GC_add_leaked(p);
282       }
283     }
284 }
285
286 /*
287  * Generic procedure to rebuild a free list in hbp.
288  * Also called directly from GC_malloc_many.
289  * Sz is now in bytes.
290  */
291 GC_INNER ptr_t GC_reclaim_generic(struct hblk * hbp, hdr *hhdr, size_t sz,
292                                   GC_bool init, ptr_t list,
293                                   signed_word *count)
294 {
295     ptr_t result;
296
297     GC_ASSERT(GC_find_header((ptr_t)hbp) == hhdr);
298 #   ifndef GC_DISABLE_INCREMENTAL
299       GC_remove_protection(hbp, 1, (hhdr)->hb_descr == 0 /* Pointer-free? */);
300 #   endif
301 #   ifdef ENABLE_DISCLAIM
302     if (hhdr -> hb_flags & HAS_DISCLAIM)
303       result = GC_disclaim_and_reclaim(hbp, hhdr, sz, list, count);
304     else
305 #   endif
306     if (init || GC_debugging_started) {
307       result = GC_reclaim_clear(hbp, hhdr, sz, list, count);
308     } else {
309       GC_ASSERT((hhdr)->hb_descr == 0 /* Pointer-free block */);
310       result = GC_reclaim_uninit(hbp, hhdr, sz, list, count);
311     }
312     if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) GC_set_hdr_marks(hhdr);
313     return result;
314 }
315
316 /*
317  * Restore unmarked small objects in the block pointed to by hbp
318  * to the appropriate object free list.
319  * If entirely empty blocks are to be completely deallocated, then
320  * caller should perform that check.
321  */
322 STATIC void GC_reclaim_small_nonempty_block(struct hblk *hbp,
323                                             GC_bool report_if_found)
324 {
325     hdr *hhdr = HDR(hbp);
326     size_t sz = hhdr -> hb_sz;
327     struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
328     void **flh = &(ok -> ok_freelist[BYTES_TO_GRANULES(sz)]);
329
330     hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
331
332     if (report_if_found) {
333         GC_reclaim_check(hbp, hhdr, sz);
334     } else {
335         *flh = GC_reclaim_generic(hbp, hhdr, sz, ok -> ok_init,
336                                   *flh, &GC_bytes_found);
337     }
338 }
339
340 /*
341  * Restore an unmarked large object or an entirely empty blocks of small objects
342  * to the heap block free list.
343  * Otherwise enqueue the block for later processing
344  * by GC_reclaim_small_nonempty_block.
345  * If report_if_found is TRUE, then process any block immediately, and
346  * simply report free objects; do not actually reclaim them.
347  */
348 STATIC void GC_reclaim_block(struct hblk *hbp, word report_if_found)
349 {
350     hdr * hhdr = HDR(hbp);
351     size_t sz = hhdr -> hb_sz;  /* size of objects in current block     */
352     struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
353     struct hblk ** rlh;
354
355     if( sz > MAXOBJBYTES ) {  /* 1 big object */
356         if( !mark_bit_from_hdr(hhdr, 0) ) {
357             if (report_if_found) {
358               GC_add_leaked((ptr_t)hbp);
359             } else {
360               size_t blocks = OBJ_SZ_TO_BLOCKS(sz);
361 #             ifdef ENABLE_DISCLAIM
362                 if (EXPECT(hhdr->hb_flags & HAS_DISCLAIM, 0)) {
363                   struct obj_kind *ok = &GC_obj_kinds[hhdr->hb_obj_kind];
364                   int resurrect;
365                   resurrect = (*ok->ok_disclaim_proc)(hbp, ok->ok_disclaim_cd);
366                   if (resurrect) {
367                     set_mark_bit_from_hdr(hhdr, 0);
368                     /* excuse me, */ goto in_use;
369                   }
370                 }
371 #             endif
372               if (blocks > 1) {
373                 GC_large_allocd_bytes -= blocks * HBLKSIZE;
374               }
375               GC_bytes_found += sz;
376               GC_freehblk(hbp);
377             }
378         } else {
379           in_use:
380             if (hhdr -> hb_descr != 0) {
381               GC_composite_in_use += sz;
382             } else {
383               GC_atomic_in_use += sz;
384             }
385         }
386     } else {
387         GC_bool empty = GC_block_empty(hhdr);
388 #       ifdef PARALLEL_MARK
389           /* Count can be low or one too high because we sometimes      */
390           /* have to ignore decrements.  Objects can also potentially   */
391           /* be repeatedly marked by each marker.                       */
392           /* Here we assume two markers, but this is extremely          */
393           /* unlikely to fail spuriously with more.  And if it does, it */
394           /* should be looked at.                                       */
395           GC_ASSERT(hhdr -> hb_n_marks <= 2 * (HBLKSIZE/sz + 1) + 16);
396 #       else
397           GC_ASSERT(sz * hhdr -> hb_n_marks <= HBLKSIZE);
398 #       endif
399         if (hhdr -> hb_descr != 0) {
400           GC_composite_in_use += sz * hhdr -> hb_n_marks;
401         } else {
402           GC_atomic_in_use += sz * hhdr -> hb_n_marks;
403         }
404         if (report_if_found) {
405           GC_reclaim_small_nonempty_block(hbp, TRUE /* report_if_found */);
406         } else if (empty) {
407           GC_bytes_found += HBLKSIZE;
408           GC_freehblk(hbp);
409         } else if (GC_find_leak || !GC_block_nearly_full(hhdr)) {
410           /* group of smaller objects, enqueue the real work */
411           rlh = &(ok -> ok_reclaim_list[BYTES_TO_GRANULES(sz)]);
412           hhdr -> hb_next = *rlh;
413           *rlh = hbp;
414         } /* else not worth salvaging. */
415         /* We used to do the nearly_full check later, but we    */
416         /* already have the right cache context here.  Also     */
417         /* doing it here avoids some silly lock contention in   */
418         /* GC_malloc_many.                                      */
419     }
420 }
421
422 #if !defined(NO_DEBUGGING)
423 /* Routines to gather and print heap block info         */
424 /* intended for debugging.  Otherwise should be called  */
425 /* with lock.                                           */
426
427 struct Print_stats
428 {
429         size_t number_of_blocks;
430         size_t total_bytes;
431 };
432
433 #ifdef USE_MARK_BYTES
434
435 /* Return the number of set mark bits in the given header       */
436 STATIC int GC_n_set_marks(hdr *hhdr)
437 {
438     int result = 0;
439     int i;
440     size_t sz = hhdr -> hb_sz;
441     int offset = (int)MARK_BIT_OFFSET(sz);
442     int limit = (int)FINAL_MARK_BIT(sz);
443
444     for (i = 0; i < limit; i += offset) {
445         result += hhdr -> hb_marks[i];
446     }
447     GC_ASSERT(hhdr -> hb_marks[limit]);
448     return(result);
449 }
450
451 #else
452
453 /* Number of set bits in a word.  Not performance critical.     */
454 static int set_bits(word n)
455 {
456     word m = n;
457     int result = 0;
458
459     while (m > 0) {
460         if (m & 1) result++;
461         m >>= 1;
462     }
463     return(result);
464 }
465
466 /* Return the number of set mark bits in the given header       */
467 STATIC int GC_n_set_marks(hdr *hhdr)
468 {
469     int result = 0;
470     int i;
471     int n_mark_words;
472 #   ifdef MARK_BIT_PER_OBJ
473       int n_objs = (int)HBLK_OBJS(hhdr -> hb_sz);
474
475       if (0 == n_objs) n_objs = 1;
476       n_mark_words = divWORDSZ(n_objs + WORDSZ - 1);
477 #   else /* MARK_BIT_PER_GRANULE */
478       n_mark_words = MARK_BITS_SZ;
479 #   endif
480     for (i = 0; i < n_mark_words - 1; i++) {
481         result += set_bits(hhdr -> hb_marks[i]);
482     }
483 #   ifdef MARK_BIT_PER_OBJ
484       result += set_bits((hhdr -> hb_marks[n_mark_words - 1])
485                          << (n_mark_words * WORDSZ - n_objs));
486 #   else
487       result += set_bits(hhdr -> hb_marks[n_mark_words - 1]);
488 #   endif
489     return(result - 1);
490 }
491
492 #endif /* !USE_MARK_BYTES  */
493
494 STATIC void GC_print_block_descr(struct hblk *h,
495                                  word /* struct PrintStats */ raw_ps)
496 {
497     hdr * hhdr = HDR(h);
498     size_t bytes = hhdr -> hb_sz;
499     struct Print_stats *ps;
500     unsigned n_marks = GC_n_set_marks(hhdr);
501
502     if (hhdr -> hb_n_marks != n_marks) {
503       GC_printf("(%u:%u,%u!=%u)", hhdr -> hb_obj_kind, (unsigned)bytes,
504                 (unsigned)hhdr -> hb_n_marks, n_marks);
505     } else {
506       GC_printf("(%u:%u,%u)", hhdr -> hb_obj_kind,
507                 (unsigned)bytes, n_marks);
508     }
509     bytes += HBLKSIZE-1;
510     bytes &= ~(HBLKSIZE-1);
511
512     ps = (struct Print_stats *)raw_ps;
513     ps->total_bytes += bytes;
514     ps->number_of_blocks++;
515 }
516
517 void GC_print_block_list(void)
518 {
519     struct Print_stats pstats;
520
521     GC_printf("(kind(0=ptrfree,1=normal,2=unc.):size_in_bytes, #_marks_set)\n");
522     pstats.number_of_blocks = 0;
523     pstats.total_bytes = 0;
524     GC_apply_to_all_blocks(GC_print_block_descr, (word)&pstats);
525     GC_printf("\nblocks = %lu, bytes = %lu\n",
526               (unsigned long)pstats.number_of_blocks,
527               (unsigned long)pstats.total_bytes);
528 }
529
530 /* Currently for debugger use only: */
531 void GC_print_free_list(int kind, size_t sz_in_granules)
532 {
533     struct obj_kind * ok = &GC_obj_kinds[kind];
534     ptr_t flh = ok -> ok_freelist[sz_in_granules];
535     struct hblk *lastBlock = 0;
536     int n;
537
538     for (n = 1; flh; n++) {
539         struct hblk *block = HBLKPTR(flh);
540         if (block != lastBlock) {
541           GC_printf("\nIn heap block at %p:\n\t", (void *)block);
542           lastBlock = block;
543         }
544         GC_printf("%d: %p;", n, flh);
545         flh = obj_link(flh);
546     }
547 }
548
549 #endif /* !NO_DEBUGGING */
550
551 /*
552  * Clear all obj_link pointers in the list of free objects *flp.
553  * Clear *flp.
554  * This must be done before dropping a list of free gcj-style objects,
555  * since may otherwise end up with dangling "descriptor" pointers.
556  * It may help for other pointer-containing objects.
557  */
558 STATIC void GC_clear_fl_links(void **flp)
559 {
560     void *next = *flp;
561
562     while (0 != next) {
563        *flp = 0;
564        flp = &(obj_link(next));
565        next = *flp;
566     }
567 }
568
569 /*
570  * Perform GC_reclaim_block on the entire heap, after first clearing
571  * small object free lists (if we are not just looking for leaks).
572  */
573 GC_INNER void GC_start_reclaim(GC_bool report_if_found)
574 {
575     unsigned kind;
576
577 #   if defined(PARALLEL_MARK)
578       GC_ASSERT(0 == GC_fl_builder_count);
579 #   endif
580     /* Reset in use counters.  GC_reclaim_block recomputes them. */
581       GC_composite_in_use = 0;
582       GC_atomic_in_use = 0;
583     /* Clear reclaim- and free-lists */
584       for (kind = 0; kind < GC_n_kinds; kind++) {
585         void **fop;
586         void **lim;
587         struct hblk ** rlist = GC_obj_kinds[kind].ok_reclaim_list;
588         GC_bool should_clobber = (GC_obj_kinds[kind].ok_descriptor != 0);
589
590         if (rlist == 0) continue;       /* This kind not used.  */
591         if (!report_if_found) {
592             lim = &(GC_obj_kinds[kind].ok_freelist[MAXOBJGRANULES+1]);
593             for( fop = GC_obj_kinds[kind].ok_freelist; fop < lim; fop++ ) {
594               if (*fop != 0) {
595                 if (should_clobber) {
596                   GC_clear_fl_links(fop);
597                 } else {
598                   *fop = 0;
599                 }
600               }
601             }
602         } /* otherwise free list objects are marked,    */
603           /* and its safe to leave them                 */
604         BZERO(rlist, (MAXOBJGRANULES + 1) * sizeof(void *));
605       }
606
607
608   /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
609   /* or enqueue the block for later processing.                            */
610     GC_apply_to_all_blocks(GC_reclaim_block, (word)report_if_found);
611
612 # ifdef EAGER_SWEEP
613     /* This is a very stupid thing to do.  We make it possible anyway,  */
614     /* so that you can convince yourself that it really is very stupid. */
615     GC_reclaim_all((GC_stop_func)0, FALSE);
616 # elif defined(MARK_UNCONDITIONALLY)
617     /* However, make sure to clear reclaimable objects of kinds with    */
618     /* unconditional marking enabled before we do any significant       */
619     /* marking work.                                                    */
620     GC_reclaim_unconditionally_marked();
621 # endif
622 # if defined(PARALLEL_MARK)
623     GC_ASSERT(0 == GC_fl_builder_count);
624 # endif
625
626 }
627
628 /*
629  * Sweep blocks of the indicated object size and kind until either the
630  * appropriate free list is nonempty, or there are no more blocks to
631  * sweep.
632  */
633 GC_INNER void GC_continue_reclaim(size_t sz /* granules */, int kind)
634 {
635     hdr * hhdr;
636     struct hblk * hbp;
637     struct obj_kind * ok = &(GC_obj_kinds[kind]);
638     struct hblk ** rlh = ok -> ok_reclaim_list;
639     void **flh = &(ok -> ok_freelist[sz]);
640
641     if (rlh == 0) return;       /* No blocks of this kind.      */
642     rlh += sz;
643     while ((hbp = *rlh) != 0) {
644         hhdr = HDR(hbp);
645         *rlh = hhdr -> hb_next;
646         GC_reclaim_small_nonempty_block(hbp, FALSE);
647         if (*flh != 0) break;
648     }
649 }
650
651 /*
652  * Reclaim all small blocks waiting to be reclaimed.
653  * Abort and return FALSE when/if (*stop_func)() returns TRUE.
654  * If this returns TRUE, then it's safe to restart the world
655  * with incorrectly cleared mark bits.
656  * If ignore_old is TRUE, then reclaim only blocks that have been
657  * recently reclaimed, and discard the rest.
658  * Stop_func may be 0.
659  */
660 GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old)
661 {
662     word sz;
663     unsigned kind;
664     hdr * hhdr;
665     struct hblk * hbp;
666     struct obj_kind * ok;
667     struct hblk ** rlp;
668     struct hblk ** rlh;
669 #   ifndef SMALL_CONFIG
670       CLOCK_TYPE start_time = 0; /* initialized to prevent warning. */
671       CLOCK_TYPE done_time;
672
673       if (GC_print_stats == VERBOSE)
674         GET_TIME(start_time);
675 #   endif
676
677     for (kind = 0; kind < GC_n_kinds; kind++) {
678         ok = &(GC_obj_kinds[kind]);
679         rlp = ok -> ok_reclaim_list;
680         if (rlp == 0) continue;
681         for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
682             rlh = rlp + sz;
683             while ((hbp = *rlh) != 0) {
684                 if (stop_func != (GC_stop_func)0 && (*stop_func)()) {
685                     return(FALSE);
686                 }
687                 hhdr = HDR(hbp);
688                 *rlh = hhdr -> hb_next;
689                 if (!ignore_old || hhdr -> hb_last_reclaimed == GC_gc_no - 1) {
690                     /* It's likely we'll need it this time, too */
691                     /* It's been touched recently, so this      */
692                     /* shouldn't trigger paging.                */
693                     GC_reclaim_small_nonempty_block(hbp, FALSE);
694                 }
695             }
696         }
697     }
698 #   ifndef SMALL_CONFIG
699       if (GC_print_stats == VERBOSE) {
700         GET_TIME(done_time);
701         GC_log_printf("Disposing of reclaim lists took %lu msecs\n",
702                       MS_TIME_DIFF(done_time,start_time));
703       }
704 #   endif
705     return(TRUE);
706 }
707
708 #if !defined(EAGER_SWEEP) && defined(MARK_UNCONDITIONALLY)
709 /* Part of disclaim patch.  This could be merged with the above, but I don't
710  * want to clobber the original source too much by changing the prototype and
711  * testing for defined(MARK_UNCONDITIONALLY). */
712 void GC_reclaim_unconditionally_marked(void)
713 {
714     word sz;
715     unsigned kind;
716     hdr * hhdr;
717     struct hblk * hbp;
718     struct obj_kind * ok;
719     struct hblk ** rlp;
720     struct hblk ** rlh;
721     for (kind = 0; kind < GC_n_kinds; kind++) {
722         ok = &(GC_obj_kinds[kind]);
723         if (!ok->ok_mark_unconditionally) continue;
724         rlp = ok->ok_reclaim_list;
725         if (rlp == 0) continue;
726         for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
727             rlh = rlp + sz;
728             while ((hbp = *rlh) != 0) {
729                 hhdr = HDR(hbp);
730                 *rlh = hhdr->hb_next;
731                 GC_reclaim_small_nonempty_block(hbp, FALSE, &GC_bytes_found);
732             }
733         }
734     }
735 }
736 #endif