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.
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
17 #include "private/gc_priv.h"
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. */
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 */
31 #endif /* PARALLEL_MARK */
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. */
37 # define MAX_LEAKED 40
39 STATIC ptr_t GC_leaked[MAX_LEAKED] = { NULL };
40 STATIC unsigned GC_n_leaked = 0;
42 GC_INNER GC_bool GC_have_errors = FALSE;
44 GC_INLINE void GC_add_leaked(ptr_t leaked)
46 GC_have_errors = TRUE;
47 /* FIXME: Prevent adding an object while printing leaked ones. */
48 if (GC_n_leaked < MAX_LEAKED) {
49 GC_leaked[GC_n_leaked++] = leaked;
50 /* Make sure it's not reclaimed this cycle */
51 GC_set_mark_bit(leaked);
55 /* Print all objects on the list after printing any smashed objects. */
56 /* Clear both lists. Called without the allocation lock held. */
57 GC_INNER void GC_print_all_errors(void)
59 static GC_bool printing_errors = FALSE;
65 if (printing_errors) {
69 have_errors = GC_have_errors;
70 printing_errors = TRUE;
73 if (GC_debugging_started) {
74 GC_print_all_smashed();
79 for (i = 0; i < GC_n_leaked; ++i) {
80 ptr_t p = GC_leaked[i];
81 if (HDR(p) -> hb_obj_kind == PTRFREE) {
82 GC_err_printf("Leaked atomic object at ");
84 GC_err_printf("Leaked composite object at ");
95 # ifndef GC_ABORT_ON_LEAK
96 && GETENV("GC_ABORT_ON_LEAK") != NULL
99 ABORT("Leaked or smashed objects encountered");
102 printing_errors = FALSE;
111 /* Test whether a block is completely empty, i.e. contains no marked */
112 /* objects. This does not require the block to be in physical memory. */
113 GC_INNER GC_bool GC_block_empty(hdr *hhdr)
115 return (hhdr -> hb_n_marks == 0);
118 STATIC GC_bool GC_block_nearly_full(hdr *hhdr)
120 return (hhdr -> hb_n_marks > 7 * HBLK_OBJS(hhdr -> hb_sz)/8);
123 /* FIXME: This should perhaps again be specialized for USE_MARK_BYTES */
124 /* and USE_MARK_BITS cases. */
127 * Restore unmarked small objects in h of size sz to the object
128 * free list. Returns the new list.
129 * Clears unmarked objects. Sz is in bytes.
131 STATIC ptr_t GC_reclaim_clear(struct hblk *hbp, hdr *hhdr, size_t sz,
132 ptr_t list, signed_word *count)
136 signed_word n_bytes_found = 0;
138 GC_ASSERT(hhdr == GC_find_header((ptr_t)hbp));
139 GC_ASSERT(sz == hhdr -> hb_sz);
140 GC_ASSERT((sz & (BYTES_PER_WORD-1)) == 0);
141 p = (word *)(hbp->hb_body);
142 plim = (word *)(hbp->hb_body + HBLKSIZE - sz);
144 /* go through all words in block */
146 if( mark_bit_from_hdr(hhdr, bit_no) ) {
147 p = (word *)((ptr_t)p + sz);
150 /* object is available - put on list */
153 /* Clear object, advance p to next object in the process */
154 q = (word *)((ptr_t)p + sz);
155 # ifdef USE_MARK_BYTES
157 && !((word)p & (2 * sizeof(word) - 1)));
165 p++; /* Skip link field */
171 bit_no += MARK_BIT_OFFSET(sz);
173 *count += n_bytes_found;
177 /* The same thing, but don't clear objects: */
178 STATIC ptr_t GC_reclaim_uninit(struct hblk *hbp, hdr *hhdr, size_t sz,
179 ptr_t list, signed_word *count)
183 signed_word n_bytes_found = 0;
185 GC_ASSERT(sz == hhdr -> hb_sz);
186 p = (word *)(hbp->hb_body);
187 plim = (word *)((ptr_t)hbp + HBLKSIZE - sz);
189 /* go through all words in block */
191 if( !mark_bit_from_hdr(hhdr, bit_no) ) {
193 /* object is available - put on list */
197 p = (word *)((ptr_t)p + sz);
198 bit_no += MARK_BIT_OFFSET(sz);
200 *count += n_bytes_found;
204 /* Don't really reclaim objects, just check for unmarked ones: */
205 STATIC void GC_reclaim_check(struct hblk *hbp, hdr *hhdr, word sz)
209 GC_ASSERT(sz == hhdr -> hb_sz);
211 /* go through all words in block */
213 plim = p + HBLKSIZE - sz;
214 for (bit_no = 0; p <= plim; p += sz, bit_no += MARK_BIT_OFFSET(sz)) {
215 if (!mark_bit_from_hdr(hhdr, bit_no)) {
222 * Generic procedure to rebuild a free list in hbp.
223 * Also called directly from GC_malloc_many.
224 * Sz is now in bytes.
226 GC_INNER ptr_t GC_reclaim_generic(struct hblk * hbp, hdr *hhdr, size_t sz,
227 GC_bool init, ptr_t list,
232 GC_ASSERT(GC_find_header((ptr_t)hbp) == hhdr);
233 # ifndef GC_DISABLE_INCREMENTAL
234 GC_remove_protection(hbp, 1, (hhdr)->hb_descr == 0 /* Pointer-free? */);
236 if (init || GC_debugging_started) {
237 result = GC_reclaim_clear(hbp, hhdr, sz, list, count);
239 GC_ASSERT((hhdr)->hb_descr == 0 /* Pointer-free block */);
240 result = GC_reclaim_uninit(hbp, hhdr, sz, list, count);
242 if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) GC_set_hdr_marks(hhdr);
247 * Restore unmarked small objects in the block pointed to by hbp
248 * to the appropriate object free list.
249 * If entirely empty blocks are to be completely deallocated, then
250 * caller should perform that check.
252 STATIC void GC_reclaim_small_nonempty_block(struct hblk *hbp,
253 GC_bool report_if_found)
255 hdr *hhdr = HDR(hbp);
256 size_t sz = hhdr -> hb_sz;
257 int kind = hhdr -> hb_obj_kind;
258 struct obj_kind * ok = &GC_obj_kinds[kind];
259 void **flh = &(ok -> ok_freelist[BYTES_TO_GRANULES(sz)]);
261 hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
263 if (report_if_found) {
264 GC_reclaim_check(hbp, hhdr, sz);
266 *flh = GC_reclaim_generic(hbp, hhdr, sz,
268 *flh, &GC_bytes_found);
273 * Restore an unmarked large object or an entirely empty blocks of small objects
274 * to the heap block free list.
275 * Otherwise enqueue the block for later processing
276 * by GC_reclaim_small_nonempty_block.
277 * If report_if_found is TRUE, then process any block immediately, and
278 * simply report free objects; do not actually reclaim them.
280 STATIC void GC_reclaim_block(struct hblk *hbp, word report_if_found)
282 hdr * hhdr = HDR(hbp);
283 size_t sz = hhdr -> hb_sz; /* size of objects in current block */
284 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
287 if( sz > MAXOBJBYTES ) { /* 1 big object */
288 if( !mark_bit_from_hdr(hhdr, 0) ) {
289 if (report_if_found) {
290 GC_add_leaked((ptr_t)hbp);
292 size_t blocks = OBJ_SZ_TO_BLOCKS(sz);
294 GC_large_allocd_bytes -= blocks * HBLKSIZE;
296 GC_bytes_found += sz;
300 if (hhdr -> hb_descr != 0) {
301 GC_composite_in_use += sz;
303 GC_atomic_in_use += sz;
307 GC_bool empty = GC_block_empty(hhdr);
308 # ifdef PARALLEL_MARK
309 /* Count can be low or one too high because we sometimes */
310 /* have to ignore decrements. Objects can also potentially */
311 /* be repeatedly marked by each marker. */
312 /* Here we assume two markers, but this is extremely */
313 /* unlikely to fail spuriously with more. And if it does, it */
314 /* should be looked at. */
315 GC_ASSERT(hhdr -> hb_n_marks <= 2 * (HBLKSIZE/sz + 1) + 16);
317 GC_ASSERT(sz * hhdr -> hb_n_marks <= HBLKSIZE);
319 if (hhdr -> hb_descr != 0) {
320 GC_composite_in_use += sz * hhdr -> hb_n_marks;
322 GC_atomic_in_use += sz * hhdr -> hb_n_marks;
324 if (report_if_found) {
325 GC_reclaim_small_nonempty_block(hbp, (GC_bool)report_if_found);
327 GC_bytes_found += HBLKSIZE;
329 } else if (GC_find_leak || !GC_block_nearly_full(hhdr)) {
330 /* group of smaller objects, enqueue the real work */
331 rlh = &(ok -> ok_reclaim_list[BYTES_TO_GRANULES(sz)]);
332 hhdr -> hb_next = *rlh;
334 } /* else not worth salvaging. */
335 /* We used to do the nearly_full check later, but we */
336 /* already have the right cache context here. Also */
337 /* doing it here avoids some silly lock contention in */
338 /* GC_malloc_many. */
342 #if !defined(NO_DEBUGGING)
343 /* Routines to gather and print heap block info */
344 /* intended for debugging. Otherwise should be called */
349 size_t number_of_blocks;
353 #ifdef USE_MARK_BYTES
355 /* Return the number of set mark bits in the given header */
356 STATIC int GC_n_set_marks(hdr *hhdr)
360 size_t sz = hhdr -> hb_sz;
361 int offset = (int)MARK_BIT_OFFSET(sz);
362 int limit = (int)FINAL_MARK_BIT(sz);
364 for (i = 0; i < limit; i += offset) {
365 result += hhdr -> hb_marks[i];
367 GC_ASSERT(hhdr -> hb_marks[limit]);
373 /* Number of set bits in a word. Not performance critical. */
374 static int set_bits(word n)
386 /* Return the number of set mark bits in the given header */
387 STATIC int GC_n_set_marks(hdr *hhdr)
392 # ifdef MARK_BIT_PER_OBJ
393 int n_objs = (int)HBLK_OBJS(hhdr -> hb_sz);
395 if (0 == n_objs) n_objs = 1;
396 n_mark_words = divWORDSZ(n_objs + WORDSZ - 1);
397 # else /* MARK_BIT_PER_GRANULE */
398 n_mark_words = MARK_BITS_SZ;
400 for (i = 0; i < n_mark_words - 1; i++) {
401 result += set_bits(hhdr -> hb_marks[i]);
403 # ifdef MARK_BIT_PER_OBJ
404 result += set_bits((hhdr -> hb_marks[n_mark_words - 1])
405 << (n_mark_words * WORDSZ - n_objs));
407 result += set_bits(hhdr -> hb_marks[n_mark_words - 1]);
412 #endif /* !USE_MARK_BYTES */
414 STATIC void GC_print_block_descr(struct hblk *h,
415 word /* struct PrintStats */ raw_ps)
418 size_t bytes = hhdr -> hb_sz;
419 struct Print_stats *ps;
420 unsigned n_marks = GC_n_set_marks(hhdr);
422 if (hhdr -> hb_n_marks != n_marks) {
423 GC_printf("(%u:%u,%u!=%u)", hhdr -> hb_obj_kind, (unsigned)bytes,
424 (unsigned)hhdr -> hb_n_marks, n_marks);
426 GC_printf("(%u:%u,%u)", hhdr -> hb_obj_kind,
427 (unsigned)bytes, n_marks);
430 bytes &= ~(HBLKSIZE-1);
432 ps = (struct Print_stats *)raw_ps;
433 ps->total_bytes += bytes;
434 ps->number_of_blocks++;
437 void GC_print_block_list(void)
439 struct Print_stats pstats;
441 GC_printf("(kind(0=ptrfree,1=normal,2=unc.):size_in_bytes, #_marks_set)\n");
442 pstats.number_of_blocks = 0;
443 pstats.total_bytes = 0;
444 GC_apply_to_all_blocks(GC_print_block_descr, (word)&pstats);
445 GC_printf("\nblocks = %lu, bytes = %lu\n",
446 (unsigned long)pstats.number_of_blocks,
447 (unsigned long)pstats.total_bytes);
450 /* Currently for debugger use only: */
451 void GC_print_free_list(int kind, size_t sz_in_granules)
453 struct obj_kind * ok = &GC_obj_kinds[kind];
454 ptr_t flh = ok -> ok_freelist[sz_in_granules];
455 struct hblk *lastBlock = 0;
458 for (n = 1; flh; n++) {
459 struct hblk *block = HBLKPTR(flh);
460 if (block != lastBlock) {
461 GC_printf("\nIn heap block at %p:\n\t", block);
464 GC_printf("%d: %p;", n, flh);
469 #endif /* !NO_DEBUGGING */
472 * Clear all obj_link pointers in the list of free objects *flp.
474 * This must be done before dropping a list of free gcj-style objects,
475 * since may otherwise end up with dangling "descriptor" pointers.
476 * It may help for other pointer-containing objects.
478 STATIC void GC_clear_fl_links(void **flp)
484 flp = &(obj_link(next));
490 * Perform GC_reclaim_block on the entire heap, after first clearing
491 * small object free lists (if we are not just looking for leaks).
493 GC_INNER void GC_start_reclaim(GC_bool report_if_found)
497 # if defined(PARALLEL_MARK)
498 GC_ASSERT(0 == GC_fl_builder_count);
500 /* Reset in use counters. GC_reclaim_block recomputes them. */
501 GC_composite_in_use = 0;
502 GC_atomic_in_use = 0;
503 /* Clear reclaim- and free-lists */
504 for (kind = 0; kind < GC_n_kinds; kind++) {
509 struct hblk ** rlist = GC_obj_kinds[kind].ok_reclaim_list;
510 GC_bool should_clobber = (GC_obj_kinds[kind].ok_descriptor != 0);
512 if (rlist == 0) continue; /* This kind not used. */
513 if (!report_if_found) {
514 lim = &(GC_obj_kinds[kind].ok_freelist[MAXOBJGRANULES+1]);
515 for( fop = GC_obj_kinds[kind].ok_freelist; fop < lim; fop++ ) {
517 if (should_clobber) {
518 GC_clear_fl_links(fop);
524 } /* otherwise free list objects are marked, */
525 /* and its safe to leave them */
526 rlim = rlist + MAXOBJGRANULES+1;
527 for( rlp = rlist; rlp < rlim; rlp++ ) {
533 /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
534 /* or enqueue the block for later processing. */
535 GC_apply_to_all_blocks(GC_reclaim_block, (word)report_if_found);
538 /* This is a very stupid thing to do. We make it possible anyway, */
539 /* so that you can convince yourself that it really is very stupid. */
540 GC_reclaim_all((GC_stop_func)0, FALSE);
542 # if defined(PARALLEL_MARK)
543 GC_ASSERT(0 == GC_fl_builder_count);
549 * Sweep blocks of the indicated object size and kind until either the
550 * appropriate free list is nonempty, or there are no more blocks to
553 GC_INNER void GC_continue_reclaim(size_t sz /* granules */, int kind)
557 struct obj_kind * ok = &(GC_obj_kinds[kind]);
558 struct hblk ** rlh = ok -> ok_reclaim_list;
559 void **flh = &(ok -> ok_freelist[sz]);
561 if (rlh == 0) return; /* No blocks of this kind. */
563 while ((hbp = *rlh) != 0) {
565 *rlh = hhdr -> hb_next;
566 GC_reclaim_small_nonempty_block(hbp, FALSE);
567 if (*flh != 0) break;
572 * Reclaim all small blocks waiting to be reclaimed.
573 * Abort and return FALSE when/if (*stop_func)() returns TRUE.
574 * If this returns TRUE, then it's safe to restart the world
575 * with incorrectly cleared mark bits.
576 * If ignore_old is TRUE, then reclaim only blocks that have been
577 * recently reclaimed, and discard the rest.
578 * Stop_func may be 0.
580 GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old)
586 struct obj_kind * ok;
589 # ifndef SMALL_CONFIG
590 CLOCK_TYPE start_time = 0; /* initialized to prevent warning. */
591 CLOCK_TYPE done_time;
593 if (GC_print_stats == VERBOSE)
594 GET_TIME(start_time);
597 for (kind = 0; kind < GC_n_kinds; kind++) {
598 ok = &(GC_obj_kinds[kind]);
599 rlp = ok -> ok_reclaim_list;
600 if (rlp == 0) continue;
601 for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
603 while ((hbp = *rlh) != 0) {
604 if (stop_func != (GC_stop_func)0 && (*stop_func)()) {
608 *rlh = hhdr -> hb_next;
609 if (!ignore_old || hhdr -> hb_last_reclaimed == GC_gc_no - 1) {
610 /* It's likely we'll need it this time, too */
611 /* It's been touched recently, so this */
612 /* shouldn't trigger paging. */
613 GC_reclaim_small_nonempty_block(hbp, FALSE);
618 # ifndef SMALL_CONFIG
619 if (GC_print_stats == VERBOSE) {
621 GC_log_printf("Disposing of reclaim lists took %lu msecs\n",
622 MS_TIME_DIFF(done_time,start_time));