]> granicus.if.org Git - gc/blob - reclaim.c
Improve GC error printing atomicity in GC_add_to_black_list_normal/stack
[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     /* 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 ((word)p <= (word)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 ((word)p < (word)q) {
174                         CLEAR_DOUBLE(p);
175                         p += 2;
176                       }
177 #                   else
178                       p++; /* Skip link field */
179                       while ((word)p < (word)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 ((word)p <= (word)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 (GC_CALLBACK *disclaim)(void *) = ok->ok_disclaim_proc;
228
229     GC_ASSERT(sz == hhdr -> hb_sz);
230     p = (word *)(hbp -> hb_body);
231     plim = (word *)((ptr_t)p + HBLKSIZE - sz);
232
233     while ((word)p <= (word)plim) {
234         int marked = mark_bit_from_hdr(hhdr, bit_no);
235         if (!marked && (*disclaim)(p)) {
236             hhdr -> hb_n_marks++;
237             marked = 1;
238         }
239         if (marked)
240             p = (word *)((ptr_t)p + sz);
241         else {
242                 n_bytes_found += sz;
243                 /* object is available - put on list */
244                     obj_link(p) = list;
245                     list = ((ptr_t)p);
246                 /* Clear object, advance p to next object in the process */
247                     q = (word *)((ptr_t)p + sz);
248 #                   ifdef USE_MARK_BYTES
249                       GC_ASSERT((sz & 1) == 0);
250                       GC_ASSERT(((word)p & (2 * sizeof(word) - 1)) == 0);
251                       p[1] = 0;
252                       p += 2;
253                       while ((word)p < (word)q) {
254                         CLEAR_DOUBLE(p);
255                         p += 2;
256                       }
257 #                   else
258                       p++; /* Skip link field */
259                       while ((word)p < (word)q) {
260                         *p++ = 0;
261                       }
262 #                   endif
263         }
264         bit_no += MARK_BIT_OFFSET(sz);
265     }
266     *count += n_bytes_found;
267     return list;
268   }
269 #endif /* ENABLE_DISCLAIM */
270
271 /* Don't really reclaim objects, just check for unmarked ones: */
272 STATIC void GC_reclaim_check(struct hblk *hbp, hdr *hhdr, word sz)
273 {
274     word bit_no;
275     ptr_t p, plim;
276     GC_ASSERT(sz == hhdr -> hb_sz);
277
278     /* go through all words in block */
279     p = hbp->hb_body;
280     plim = p + HBLKSIZE - sz;
281     for (bit_no = 0; (word)p <= (word)plim;
282          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) != 0) {
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;
385
386 #             ifdef ENABLE_DISCLAIM
387                 if (EXPECT(hhdr->hb_flags & HAS_DISCLAIM, 0)) {
388                   struct obj_kind *ok = &GC_obj_kinds[hhdr->hb_obj_kind];
389                   if ((*ok->ok_disclaim_proc)(hbp)) {
390                     /* Not disclaimed => resurrect the object. */
391                     set_mark_bit_from_hdr(hhdr, 0);
392                     goto in_use;
393                   }
394                 }
395 #             endif
396               blocks = OBJ_SZ_TO_BLOCKS(sz);
397               if (blocks > 1) {
398                 GC_large_allocd_bytes -= blocks * HBLKSIZE;
399               }
400               GC_bytes_found += sz;
401               GC_freehblk(hbp);
402             }
403         } else {
404 #        ifdef ENABLE_DISCLAIM
405            in_use:
406 #        endif
407             if (hhdr -> hb_descr != 0) {
408               GC_composite_in_use += sz;
409             } else {
410               GC_atomic_in_use += sz;
411             }
412         }
413     } else {
414         GC_bool empty = GC_block_empty(hhdr);
415 #       ifdef PARALLEL_MARK
416           /* Count can be low or one too high because we sometimes      */
417           /* have to ignore decrements.  Objects can also potentially   */
418           /* be repeatedly marked by each marker.                       */
419           /* Here we assume two markers, but this is extremely          */
420           /* unlikely to fail spuriously with more.  And if it does, it */
421           /* should be looked at.                                       */
422           GC_ASSERT(hhdr -> hb_n_marks <= 2 * (HBLKSIZE/sz + 1) + 16);
423 #       else
424           GC_ASSERT(sz * hhdr -> hb_n_marks <= HBLKSIZE);
425 #       endif
426         if (report_if_found) {
427           GC_reclaim_small_nonempty_block(hbp, TRUE /* report_if_found */);
428         } else if (empty) {
429 #       ifdef ENABLE_DISCLAIM
430           if ((hhdr -> hb_flags & HAS_DISCLAIM) != 0) {
431             GC_disclaim_and_reclaim_or_free_small_block(hbp);
432           } else
433 #       endif
434           /* else */ {
435             GC_bytes_found += HBLKSIZE;
436             GC_freehblk(hbp);
437           }
438         } else if (GC_find_leak || !GC_block_nearly_full(hhdr)) {
439           /* group of smaller objects, enqueue the real work */
440           rlh = &(ok -> ok_reclaim_list[BYTES_TO_GRANULES(sz)]);
441           hhdr -> hb_next = *rlh;
442           *rlh = hbp;
443         } /* else not worth salvaging. */
444         /* We used to do the nearly_full check later, but we    */
445         /* already have the right cache context here.  Also     */
446         /* doing it here avoids some silly lock contention in   */
447         /* GC_malloc_many.                                      */
448
449         if (hhdr -> hb_descr != 0) {
450           GC_composite_in_use += sz * hhdr -> hb_n_marks;
451         } else {
452           GC_atomic_in_use += sz * hhdr -> hb_n_marks;
453         }
454     }
455 }
456
457 #if !defined(NO_DEBUGGING)
458 /* Routines to gather and print heap block info         */
459 /* intended for debugging.  Otherwise should be called  */
460 /* with lock.                                           */
461
462 struct Print_stats
463 {
464         size_t number_of_blocks;
465         size_t total_bytes;
466 };
467
468 #ifdef USE_MARK_BYTES
469
470 /* Return the number of set mark bits in the given header.      */
471 /* Remains externally visible as used by GNU GCJ currently.     */
472 int GC_n_set_marks(hdr *hhdr)
473 {
474     int result = 0;
475     int i;
476     size_t sz = hhdr -> hb_sz;
477     int offset = (int)MARK_BIT_OFFSET(sz);
478     int limit = (int)FINAL_MARK_BIT(sz);
479
480     for (i = 0; i < limit; i += offset) {
481         result += hhdr -> hb_marks[i];
482     }
483     GC_ASSERT(hhdr -> hb_marks[limit]);
484     return(result);
485 }
486
487 #else
488
489 /* Number of set bits in a word.  Not performance critical.     */
490 static int set_bits(word n)
491 {
492     word m = n;
493     int result = 0;
494
495     while (m > 0) {
496         if (m & 1) result++;
497         m >>= 1;
498     }
499     return(result);
500 }
501
502 int GC_n_set_marks(hdr *hhdr)
503 {
504     int result = 0;
505     int i;
506     int n_mark_words;
507 #   ifdef MARK_BIT_PER_OBJ
508       int n_objs = (int)HBLK_OBJS(hhdr -> hb_sz);
509
510       if (0 == n_objs) n_objs = 1;
511       n_mark_words = divWORDSZ(n_objs + WORDSZ - 1);
512 #   else /* MARK_BIT_PER_GRANULE */
513       n_mark_words = MARK_BITS_SZ;
514 #   endif
515     for (i = 0; i < n_mark_words - 1; i++) {
516         result += set_bits(hhdr -> hb_marks[i]);
517     }
518 #   ifdef MARK_BIT_PER_OBJ
519       result += set_bits((hhdr -> hb_marks[n_mark_words - 1])
520                          << (n_mark_words * WORDSZ - n_objs));
521 #   else
522       result += set_bits(hhdr -> hb_marks[n_mark_words - 1]);
523 #   endif
524     return(result - 1);
525 }
526
527 #endif /* !USE_MARK_BYTES  */
528
529 STATIC void GC_print_block_descr(struct hblk *h,
530                                  word /* struct PrintStats */ raw_ps)
531 {
532     hdr * hhdr = HDR(h);
533     size_t bytes = hhdr -> hb_sz;
534     struct Print_stats *ps;
535     unsigned n_marks = GC_n_set_marks(hhdr);
536
537     if (hhdr -> hb_n_marks != n_marks) {
538       GC_printf("(%u:%u,%u!=%u)\n", hhdr->hb_obj_kind, (unsigned)bytes,
539                 (unsigned)hhdr->hb_n_marks, n_marks);
540     } else {
541       GC_printf("(%u:%u,%u)\n", hhdr->hb_obj_kind,
542                 (unsigned)bytes, n_marks);
543     }
544     bytes += HBLKSIZE-1;
545     bytes &= ~(HBLKSIZE-1);
546
547     ps = (struct Print_stats *)raw_ps;
548     ps->total_bytes += bytes;
549     ps->number_of_blocks++;
550 }
551
552 void GC_print_block_list(void)
553 {
554     struct Print_stats pstats;
555
556     GC_printf("(kind(0=ptrfree,1=normal,2=unc.):size_in_bytes, #_marks_set)\n");
557     pstats.number_of_blocks = 0;
558     pstats.total_bytes = 0;
559     GC_apply_to_all_blocks(GC_print_block_descr, (word)&pstats);
560     GC_printf("blocks= %lu, bytes= %lu\n",
561               (unsigned long)pstats.number_of_blocks,
562               (unsigned long)pstats.total_bytes);
563 }
564
565 /* Currently for debugger use only: */
566 void GC_print_free_list(int kind, size_t sz_in_granules)
567 {
568     struct obj_kind * ok = &GC_obj_kinds[kind];
569     ptr_t flh = ok -> ok_freelist[sz_in_granules];
570     int n;
571
572     for (n = 0; flh; n++) {
573         struct hblk *block = HBLKPTR(flh);
574         GC_printf("Free object in heap block %p [%d]: %p\n",
575                   (void *)block, 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;
625                  (word)fop < (word)lim; fop++) {
626               if (*fop != 0) {
627                 if (should_clobber) {
628                   GC_clear_fl_links(fop);
629                 } else {
630                   *fop = 0;
631                 }
632               }
633             }
634         } /* otherwise free list objects are marked,    */
635           /* and its safe to leave them                 */
636         BZERO(rlist, (MAXOBJGRANULES + 1) * sizeof(void *));
637       }
638
639
640   /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
641   /* or enqueue the block for later processing.                            */
642     GC_apply_to_all_blocks(GC_reclaim_block, (word)report_if_found);
643
644 # ifdef EAGER_SWEEP
645     /* This is a very stupid thing to do.  We make it possible anyway,  */
646     /* so that you can convince yourself that it really is very stupid. */
647     GC_reclaim_all((GC_stop_func)0, FALSE);
648 # elif defined(ENABLE_DISCLAIM)
649     /* However, make sure to clear reclaimable objects of kinds with    */
650     /* unconditional marking enabled before we do any significant       */
651     /* marking work.                                                    */
652     GC_reclaim_unconditionally_marked();
653 # endif
654 # if defined(PARALLEL_MARK)
655     GC_ASSERT(0 == GC_fl_builder_count);
656 # endif
657
658 }
659
660 /*
661  * Sweep blocks of the indicated object size and kind until either the
662  * appropriate free list is nonempty, or there are no more blocks to
663  * sweep.
664  */
665 GC_INNER void GC_continue_reclaim(size_t sz /* granules */, int kind)
666 {
667     hdr * hhdr;
668     struct hblk * hbp;
669     struct obj_kind * ok = &(GC_obj_kinds[kind]);
670     struct hblk ** rlh = ok -> ok_reclaim_list;
671     void **flh = &(ok -> ok_freelist[sz]);
672
673     if (rlh == 0) return;       /* No blocks of this kind.      */
674     rlh += sz;
675     while ((hbp = *rlh) != 0) {
676         hhdr = HDR(hbp);
677         *rlh = hhdr -> hb_next;
678         GC_reclaim_small_nonempty_block(hbp, FALSE);
679         if (*flh != 0) break;
680     }
681 }
682
683 /*
684  * Reclaim all small blocks waiting to be reclaimed.
685  * Abort and return FALSE when/if (*stop_func)() returns TRUE.
686  * If this returns TRUE, then it's safe to restart the world
687  * with incorrectly cleared mark bits.
688  * If ignore_old is TRUE, then reclaim only blocks that have been
689  * recently reclaimed, and discard the rest.
690  * Stop_func may be 0.
691  */
692 GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old)
693 {
694     word sz;
695     unsigned kind;
696     hdr * hhdr;
697     struct hblk * hbp;
698     struct obj_kind * ok;
699     struct hblk ** rlp;
700     struct hblk ** rlh;
701 #   ifndef SMALL_CONFIG
702       CLOCK_TYPE start_time = 0; /* initialized to prevent warning. */
703       CLOCK_TYPE done_time;
704
705       if (GC_print_stats == VERBOSE)
706         GET_TIME(start_time);
707 #   endif
708
709     for (kind = 0; kind < GC_n_kinds; kind++) {
710         ok = &(GC_obj_kinds[kind]);
711         rlp = ok -> ok_reclaim_list;
712         if (rlp == 0) continue;
713         for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
714             rlh = rlp + sz;
715             while ((hbp = *rlh) != 0) {
716                 if (stop_func != (GC_stop_func)0 && (*stop_func)()) {
717                     return(FALSE);
718                 }
719                 hhdr = HDR(hbp);
720                 *rlh = hhdr -> hb_next;
721                 if (!ignore_old || hhdr -> hb_last_reclaimed == GC_gc_no - 1) {
722                     /* It's likely we'll need it this time, too */
723                     /* It's been touched recently, so this      */
724                     /* shouldn't trigger paging.                */
725                     GC_reclaim_small_nonempty_block(hbp, FALSE);
726                 }
727             }
728         }
729     }
730 #   ifndef SMALL_CONFIG
731       if (GC_print_stats == VERBOSE) {
732         GET_TIME(done_time);
733         GC_log_printf("Disposing of reclaim lists took %lu msecs\n",
734                       MS_TIME_DIFF(done_time,start_time));
735       }
736 #   endif
737     return(TRUE);
738 }
739
740 #if !defined(EAGER_SWEEP) && defined(ENABLE_DISCLAIM)
741 /* We do an eager sweep on heap blocks where unconditional marking has  */
742 /* been enabled, so that any reclaimable objects have been reclaimed    */
743 /* before we start marking.  This is a simplified GC_reclaim_all        */
744 /* restricted to kinds where ok_mark_unconditionally is true.           */
745   STATIC void GC_reclaim_unconditionally_marked(void)
746   {
747     word sz;
748     unsigned kind;
749     hdr * hhdr;
750     struct hblk * hbp;
751     struct obj_kind * ok;
752     struct hblk ** rlp;
753     struct hblk ** rlh;
754
755     for (kind = 0; kind < GC_n_kinds; kind++) {
756         ok = &(GC_obj_kinds[kind]);
757         if (!ok->ok_mark_unconditionally)
758           continue;
759         rlp = ok->ok_reclaim_list;
760         if (rlp == 0)
761           continue;
762         for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
763             rlh = rlp + sz;
764             while ((hbp = *rlh) != 0) {
765                 hhdr = HDR(hbp);
766                 *rlh = hhdr->hb_next;
767                 GC_reclaim_small_nonempty_block(hbp, FALSE);
768             }
769         }
770     }
771   }
772 #endif /* !EAGER_SWEEP && ENABLE_DISCLAIM */