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