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