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