2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1996 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1998 by Silicon Graphics. All rights reserved.
5 * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
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.
20 # include "private/gc_priv.h"
23 # if !defined(MACOS) && !defined(MSWINCE)
25 # include <sys/types.h>
29 * Separate free lists are maintained for different sized objects
31 * The call GC_allocobj(i,k) ensures that the freelist for
32 * kind k objects of size i points to a non-empty
33 * free list. It returns a pointer to the first entry on the free list.
34 * In a single-threaded world, GC_allocobj may be called to allocate
35 * an object of (small) size i as follows:
37 * opp = &(GC_objfreelist[i]);
38 * if (*opp == 0) GC_allocobj(i, NORMAL);
40 * *opp = obj_link(ptr);
42 * Note that this is very fast if the free list is non-empty; it should
43 * only involve the execution of 4 or 5 simple instructions.
44 * All composite objects on freelists are cleared, except for
49 * The allocator uses GC_allochblk to allocate large chunks of objects.
50 * These chunks all start on addresses which are multiples of
51 * HBLKSZ. Each allocated chunk has an associated header,
52 * which can be located quickly based on the address of the chunk.
53 * (See headers.c for details.)
54 * This makes it possible to check quickly whether an
55 * arbitrary address corresponds to an object administered by the
59 word GC_non_gc_bytes = 0; /* Number of bytes not intended to be collected */
64 int GC_incremental = 0; /* By default, stop the world. */
67 int GC_parallel = FALSE; /* By default, parallel GC is off. */
69 int GC_full_freq = 19; /* Every 20th collection is a full */
70 /* collection, whether we need it */
73 GC_bool GC_need_full_gc = FALSE;
74 /* Need full GC do to heap growth. */
77 GC_bool GC_world_stopped = FALSE;
78 # define IF_THREADS(x) x
80 # define IF_THREADS(x)
83 word GC_used_heap_size_after_full = 0;
85 char * GC_copyright[] =
86 {"Copyright 1988,1989 Hans-J. Boehm and Alan J. Demers ",
87 "Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved. ",
88 "Copyright (c) 1996-1998 by Silicon Graphics. All rights reserved. ",
89 "Copyright (c) 1999-2001 by Hewlett-Packard Company. All rights reserved. ",
90 "THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY",
91 " EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.",
92 "See source code for details." };
96 /* some more variables */
98 extern signed_word GC_mem_found; /* Number of reclaimed longwords */
99 /* after garbage collection */
101 GC_bool GC_dont_expand = 0;
103 word GC_free_space_divisor = 3;
105 extern GC_bool GC_collection_in_progress();
106 /* Collection is in progress, or was abandoned. */
108 extern GC_bool GC_print_back_height;
110 int GC_never_stop_func GC_PROTO((void)) { return(0); }
112 unsigned long GC_time_limit = TIME_LIMIT;
114 CLOCK_TYPE GC_start_time; /* Time at which we stopped world. */
115 /* used only in GC_timeout_stop_func. */
117 int GC_n_attempts = 0; /* Number of attempts at finishing */
118 /* collection within GC_time_limit. */
120 #if defined(SMALL_CONFIG) || defined(NO_CLOCK)
121 # define GC_timeout_stop_func GC_never_stop_func
123 int GC_timeout_stop_func GC_PROTO((void))
125 CLOCK_TYPE current_time;
126 static unsigned count = 0;
127 unsigned long time_diff;
129 if ((count++ & 3) != 0) return(0);
130 GET_TIME(current_time);
131 time_diff = MS_TIME_DIFF(current_time,GC_start_time);
132 if (time_diff >= GC_time_limit) {
134 if (GC_print_stats) {
135 GC_printf0("Abandoning stopped marking after ");
136 GC_printf1("%lu msecs", (unsigned long)time_diff);
137 GC_printf1("(attempt %ld)\n", (unsigned long) GC_n_attempts);
144 #endif /* !SMALL_CONFIG */
146 /* Return the minimum number of words that must be allocated between */
147 /* collections to amortize the collection cost. */
148 static word min_words_allocd()
151 /* We punt, for now. */
152 register signed_word stack_size = 10000;
155 register signed_word stack_size = (ptr_t)(&dummy) - GC_stackbottom;
157 word total_root_size; /* includes double stack size, */
158 /* since the stack is expensive */
160 word scan_size; /* Estimate of memory to be scanned */
161 /* during normal GC. */
163 if (stack_size < 0) stack_size = -stack_size;
164 total_root_size = 2 * stack_size + GC_root_size;
165 scan_size = BYTES_TO_WORDS(GC_heapsize - GC_large_free_bytes
166 + (GC_large_free_bytes >> 2)
167 /* use a bit more of large empty heap */
169 if (TRUE_INCREMENTAL) {
170 return scan_size / (2 * GC_free_space_divisor);
172 return scan_size / GC_free_space_divisor;
176 /* Return the number of words allocated, adjusted for explicit storage */
177 /* management, etc.. This number is used in deciding when to trigger */
179 word GC_adj_words_allocd()
181 register signed_word result;
182 register signed_word expl_managed =
183 BYTES_TO_WORDS((long)GC_non_gc_bytes
184 - (long)GC_non_gc_bytes_at_gc);
186 /* Don't count what was explicitly freed, or newly allocated for */
187 /* explicit management. Note that deallocating an explicitly */
188 /* managed object should not alter result, assuming the client */
189 /* is playing by the rules. */
190 result = (signed_word)GC_words_allocd
191 - (signed_word)GC_mem_freed
192 + (signed_word)GC_finalizer_mem_freed - expl_managed;
193 if (result > (signed_word)GC_words_allocd) {
194 result = GC_words_allocd;
195 /* probably client bug or unfortunate scheduling */
197 result += GC_words_finalized;
198 /* We count objects enqueued for finalization as though they */
199 /* had been reallocated this round. Finalization is user */
200 /* visible progress. And if we don't count this, we have */
201 /* stability problems for programs that finalize all objects. */
202 result += GC_words_wasted;
203 /* This doesn't reflect useful work. But if there is lots of */
204 /* new fragmentation, the same is probably true of the heap, */
205 /* and the collection will be correspondingly cheaper. */
206 if (result < (signed_word)(GC_words_allocd >> 3)) {
207 /* Always count at least 1/8 of the allocations. We don't want */
208 /* to collect too infrequently, since that would inhibit */
209 /* coalescing of free storage blocks. */
210 /* This also makes us partially robust against client bugs. */
211 return(GC_words_allocd >> 3);
218 /* Clear up a few frames worth of garbage left at the top of the stack. */
219 /* This is used to prevent us from accidentally treating garbade left */
220 /* on the stack by other parts of the collector as roots. This */
221 /* differs from the code in misc.c, which actually tries to keep the */
222 /* stack clear of long-lived, client-generated garbage. */
223 void GC_clear_a_few_frames()
229 for (i = 0; i < NWORDS; i++) frames[i] = 0;
232 /* Heap size at which we need a collection to avoid expanding past */
233 /* limits used by blacklisting. */
234 static word GC_collect_at_heapsize = (word)(-1);
236 /* Have we allocated enough to amortize a collection? */
237 GC_bool GC_should_collect()
239 return(GC_adj_words_allocd() >= min_words_allocd()
240 || GC_heapsize >= GC_collect_at_heapsize);
244 void GC_notify_full_gc()
246 if (GC_start_call_back != (void (*) GC_PROTO((void)))0) {
247 (*GC_start_call_back)();
251 GC_bool GC_is_full_gc = FALSE;
254 * Initiate a garbage collection if appropriate.
256 * between partial, full, and stop-world collections.
257 * Assumes lock held, signals disabled.
261 static int n_partial_gcs = 0;
263 if (GC_should_collect()) {
264 if (!GC_incremental) {
269 # ifdef PARALLEL_MARK
270 GC_wait_for_reclaim();
272 if (GC_need_full_gc || n_partial_gcs >= GC_full_freq) {
274 if (GC_print_stats) {
276 "***>Full mark for collection %lu after %ld allocd bytes\n",
277 (unsigned long) GC_gc_no+1,
278 (long)WORDS_TO_BYTES(GC_words_allocd));
281 GC_promote_black_lists();
282 (void)GC_reclaim_all((GC_stop_func)0, TRUE);
286 GC_is_full_gc = TRUE;
291 /* We try to mark with the world stopped. */
292 /* If we run out of time, this turns into */
293 /* incremental marking. */
295 if (GC_time_limit != GC_TIME_UNLIMITED) { GET_TIME(GC_start_time); }
297 if (GC_stopped_mark(GC_time_limit == GC_TIME_UNLIMITED?
298 GC_never_stop_func : GC_timeout_stop_func)) {
299 # ifdef SAVE_CALL_CHAIN
300 GC_save_callers(GC_last_stack);
302 GC_finish_collection();
304 if (!GC_is_full_gc) {
305 /* Count this as the first attempt */
314 * Stop the world garbage collection. Assumes lock held, signals disabled.
315 * If stop_func is not GC_never_stop_func, then abort if stop_func returns TRUE.
316 * Return TRUE if we successfully completed the collection.
318 GC_bool GC_try_to_collect_inner(stop_func)
319 GC_stop_func stop_func;
322 CLOCK_TYPE start_time, current_time;
324 if (GC_dont_gc) return FALSE;
325 if (GC_incremental && GC_collection_in_progress()) {
327 if (GC_print_stats) {
329 "GC_try_to_collect_inner: finishing collection in progress\n");
331 # endif /* CONDPRINT */
332 /* Just finish collection already in progress. */
333 while(GC_collection_in_progress()) {
334 if (stop_func()) return(FALSE);
335 GC_collect_a_little_inner(1);
338 if (stop_func == GC_never_stop_func) GC_notify_full_gc();
340 if (GC_print_stats) {
341 if (GC_print_stats) GET_TIME(start_time);
343 "Initiating full world-stop collection %lu after %ld allocd bytes\n",
344 (unsigned long) GC_gc_no+1,
345 (long)WORDS_TO_BYTES(GC_words_allocd));
348 GC_promote_black_lists();
349 /* Make sure all blocks have been reclaimed, so sweep routines */
350 /* don't see cleared mark bits. */
351 /* If we're guaranteed to finish, then this is unnecessary. */
352 /* In the find_leak case, we have to finish to guarantee that */
353 /* previously unmarked objects are not reported as leaks. */
354 # ifdef PARALLEL_MARK
355 GC_wait_for_reclaim();
357 if ((GC_find_leak || stop_func != GC_never_stop_func)
358 && !GC_reclaim_all(stop_func, FALSE)) {
359 /* Aborted. So far everything is still consistent. */
362 GC_invalidate_mark_state(); /* Flush mark stack. */
364 # ifdef SAVE_CALL_CHAIN
365 GC_save_callers(GC_last_stack);
367 GC_is_full_gc = TRUE;
368 if (!GC_stopped_mark(stop_func)) {
369 if (!GC_incremental) {
370 /* We're partially done and have no way to complete or use */
371 /* current work. Reestablish invariants as cheaply as */
373 GC_invalidate_mark_state();
374 GC_unpromote_black_lists();
375 } /* else we claim the world is already still consistent. We'll */
376 /* finish incrementally. */
379 GC_finish_collection();
380 # if defined(CONDPRINT)
381 if (GC_print_stats) {
382 GET_TIME(current_time);
383 GC_printf1("Complete collection took %lu msecs\n",
384 MS_TIME_DIFF(current_time,start_time));
393 * Perform n units of garbage collection work. A unit is intended to touch
394 * roughly GC_RATE pages. Every once in a while, we do more than that.
395 * This needa to be a fairly large number with our current incremental
396 * GC strategy, since otherwise we allocate too much during GC, and the
397 * cleanup gets expensive.
400 # define MAX_PRIOR_ATTEMPTS 1
401 /* Maximum number of prior attempts at world stop marking */
402 /* A value of 1 means that we finish the second time, no matter */
403 /* how long it takes. Doesn't count the initial root scan */
406 int GC_deficit = 0; /* The number of extra calls to GC_mark_some */
407 /* that we have made. */
409 void GC_collect_a_little_inner(n)
414 if (GC_dont_gc) return;
415 if (GC_incremental && GC_collection_in_progress()) {
416 for (i = GC_deficit; i < GC_RATE*n; i++) {
417 if (GC_mark_some((ptr_t)0)) {
418 /* Need to finish a collection */
419 # ifdef SAVE_CALL_CHAIN
420 GC_save_callers(GC_last_stack);
422 # ifdef PARALLEL_MARK
423 GC_wait_for_reclaim();
425 if (GC_n_attempts < MAX_PRIOR_ATTEMPTS
426 && GC_time_limit != GC_TIME_UNLIMITED) {
427 GET_TIME(GC_start_time);
428 if (!GC_stopped_mark(GC_timeout_stop_func)) {
433 (void)GC_stopped_mark(GC_never_stop_func);
435 GC_finish_collection();
439 if (GC_deficit > 0) GC_deficit -= GC_RATE*n;
440 if (GC_deficit < 0) GC_deficit = 0;
446 int GC_collect_a_little GC_PROTO(())
453 GC_collect_a_little_inner(1);
454 result = (int)GC_collection_in_progress();
457 if (!result && GC_debugging_started) GC_print_all_smashed();
462 * Assumes lock is held, signals are disabled.
464 * If stop_func() ever returns TRUE, we may fail and return FALSE.
465 * Increment GC_gc_no if we succeed.
467 GC_bool GC_stopped_mark(stop_func)
468 GC_stop_func stop_func;
472 # if defined(PRINTTIMES) || defined(CONDPRINT)
473 CLOCK_TYPE start_time, current_time;
477 GET_TIME(start_time);
479 # if defined(CONDPRINT) && !defined(PRINTTIMES)
480 if (GC_print_stats) GET_TIME(start_time);
482 # if defined(REGISTER_LIBRARIES_EARLY)
483 GC_cond_register_dynamic_libraries();
486 IF_THREADS(GC_world_stopped = TRUE);
488 if (GC_print_stats) {
489 GC_printf1("--> Marking for collection %lu ",
490 (unsigned long) GC_gc_no + 1);
491 GC_printf2("after %lu allocd bytes + %lu wasted bytes\n",
492 (unsigned long) WORDS_TO_BYTES(GC_words_allocd),
493 (unsigned long) WORDS_TO_BYTES(GC_words_wasted));
496 # ifdef MAKE_BACK_GRAPH
497 if (GC_print_back_height) {
498 GC_build_back_graph();
502 /* Mark from all roots. */
503 /* Minimize junk left in my registers and on the stack */
504 GC_clear_a_few_frames();
505 GC_noop(0,0,0,0,0,0);
508 if ((*stop_func)()) {
510 if (GC_print_stats) {
511 GC_printf0("Abandoned stopped marking after ");
512 GC_printf1("%lu iterations\n",
516 GC_deficit = i; /* Give the mutator a chance. */
517 IF_THREADS(GC_world_stopped = FALSE);
521 if (GC_mark_some((ptr_t)(&dummy))) break;
526 GC_printf2("Collection %lu reclaimed %ld bytes",
527 (unsigned long) GC_gc_no - 1,
528 (long)WORDS_TO_BYTES(GC_mem_found));
531 if (GC_print_stats) {
532 GC_printf1("Collection %lu finished", (unsigned long) GC_gc_no - 1);
535 # endif /* !PRINTSTATS */
537 if (GC_print_stats) {
538 GC_printf1(" ---> heapsize = %lu bytes\n",
539 (unsigned long) GC_heapsize);
540 /* Printf arguments may be pushed in funny places. Clear the */
544 # endif /* CONDPRINT */
546 /* Check all debugged objects for consistency */
547 if (GC_debugging_started) {
551 IF_THREADS(GC_world_stopped = FALSE);
554 GET_TIME(current_time);
555 GC_printf1("World-stopped marking took %lu msecs\n",
556 MS_TIME_DIFF(current_time,start_time));
559 if (GC_print_stats) {
560 GET_TIME(current_time);
561 GC_printf1("World-stopped marking took %lu msecs\n",
562 MS_TIME_DIFF(current_time,start_time));
569 /* Set all mark bits for the free list whose first entry is q */
571 void GC_set_fl_marks(ptr_t q)
573 void GC_set_fl_marks(q)
578 struct hblk * h, * last_h = 0;
582 for (p = q; p != 0; p = obj_link(p)){
588 word_no = (((word *)p) - ((word *)h));
589 set_mark_bit_from_hdr(hhdr, word_no);
593 /* Clear all mark bits for the free list whose first entry is q */
594 /* Decrement GC_mem_found by number of words on free list. */
596 void GC_clear_fl_marks(ptr_t q)
598 void GC_clear_fl_marks(q)
603 struct hblk * h, * last_h = 0;
607 for (p = q; p != 0; p = obj_link(p)){
613 word_no = (((word *)p) - ((word *)h));
614 clear_mark_bit_from_hdr(hhdr, word_no);
616 GC_mem_found -= hhdr -> hb_sz;
621 /* Finish up a collection. Assumes lock is held, signals are disabled, */
622 /* but the world is otherwise running. */
623 void GC_finish_collection()
626 CLOCK_TYPE start_time;
627 CLOCK_TYPE finalize_time;
628 CLOCK_TYPE done_time;
630 GET_TIME(start_time);
631 finalize_time = start_time;
637 # if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
638 if (getenv("GC_PRINT_ADDRESS_MAP") != 0) {
639 GC_print_address_map();
644 /* Mark all objects on the free list. All objects should be */
645 /* marked when we're done. */
647 register word size; /* current object size */
651 for (kind = 0; kind < GC_n_kinds; kind++) {
652 for (size = 1; size <= MAXOBJSZ; size++) {
653 q = GC_obj_kinds[kind].ok_freelist[size];
654 if (q != 0) GC_set_fl_marks(q);
658 GC_start_reclaim(TRUE);
659 /* The above just checks; it doesn't really reclaim anything. */
663 # ifdef STUBBORN_ALLOC
664 GC_clean_changing_list();
668 GET_TIME(finalize_time);
671 if (GC_print_back_height) {
672 # ifdef MAKE_BACK_GRAPH
673 GC_traverse_back_graph();
675 # ifndef SMALL_CONFIG
676 GC_err_printf0("Back height not available: "
677 "Rebuild collector with -DMAKE_BACK_GRAPH\n");
682 /* Clear free list mark bits, in case they got accidentally marked */
683 /* (or GC_find_leak is set and they were intentionally marked). */
684 /* Also subtract memory remaining from GC_mem_found count. */
685 /* Note that composite objects on free list are cleared. */
686 /* Thus accidentally marking a free list is not a problem; only */
687 /* objects on the list itself will be marked, and that's fixed here. */
689 register word size; /* current object size */
690 register ptr_t q; /* pointer to current object */
693 for (kind = 0; kind < GC_n_kinds; kind++) {
694 for (size = 1; size <= MAXOBJSZ; size++) {
695 q = GC_obj_kinds[kind].ok_freelist[size];
696 if (q != 0) GC_clear_fl_marks(q);
703 GC_printf1("Bytes recovered before sweep - f.l. count = %ld\n",
704 (long)WORDS_TO_BYTES(GC_mem_found));
706 /* Reconstruct free lists to contain everything not marked */
707 GC_start_reclaim(FALSE);
709 GC_used_heap_size_after_full = USED_HEAP_SIZE;
710 GC_need_full_gc = FALSE;
713 BYTES_TO_WORDS(USED_HEAP_SIZE - GC_used_heap_size_after_full)
714 > min_words_allocd();
719 "Immediately reclaimed %ld bytes in heap of size %lu bytes",
720 (long)WORDS_TO_BYTES(GC_mem_found),
721 (unsigned long)GC_heapsize);
723 GC_printf1("(%lu unmapped)", GC_unmapped_bytes);
726 "\n%lu (atomic) + %lu (composite) collectable bytes in use\n",
727 (unsigned long)WORDS_TO_BYTES(GC_atomic_in_use),
728 (unsigned long)WORDS_TO_BYTES(GC_composite_in_use));
732 GC_is_full_gc = FALSE;
733 /* Reset or increment counters for next cycle */
734 GC_words_allocd_before_gc += GC_words_allocd;
735 GC_non_gc_bytes_at_gc = GC_non_gc_bytes;
739 GC_finalizer_mem_freed = 0;
746 GC_printf2("Finalize + initiate sweep took %lu + %lu msecs\n",
747 MS_TIME_DIFF(finalize_time,start_time),
748 MS_TIME_DIFF(done_time,finalize_time));
752 /* Externally callable routine to invoke full, stop-world collection */
753 # if defined(__STDC__) || defined(__cplusplus)
754 int GC_try_to_collect(GC_stop_func stop_func)
756 int GC_try_to_collect(stop_func)
757 GC_stop_func stop_func;
763 if (GC_debugging_started) GC_print_all_smashed();
764 GC_INVOKE_FINALIZERS();
768 if (!GC_is_initialized) GC_init_inner();
769 /* Minimize junk left in my registers */
770 GC_noop(0,0,0,0,0,0);
771 result = (int)GC_try_to_collect_inner(stop_func);
776 if (GC_debugging_started) GC_print_all_smashed();
777 GC_INVOKE_FINALIZERS();
782 void GC_gcollect GC_PROTO(())
784 (void)GC_try_to_collect(GC_never_stop_func);
785 if (GC_have_errors) GC_print_all_errors();
788 word GC_n_heap_sects = 0; /* Number of sections currently in heap. */
791 * Use the chunk of memory starting at p of size bytes as part of the heap.
792 * Assumes p is HBLKSIZE aligned, and bytes is a multiple of HBLKSIZE.
794 void GC_add_to_heap(p, bytes)
801 if (GC_n_heap_sects >= MAX_HEAP_SECTS) {
802 ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");
804 phdr = GC_install_header(p);
806 /* This is extremely unlikely. Can't add it. This will */
807 /* almost certainly result in a 0 return from the allocator, */
808 /* which is entirely appropriate. */
811 GC_heap_sects[GC_n_heap_sects].hs_start = (ptr_t)p;
812 GC_heap_sects[GC_n_heap_sects].hs_bytes = bytes;
814 words = BYTES_TO_WORDS(bytes);
815 phdr -> hb_sz = words;
816 phdr -> hb_map = (unsigned char *)1; /* A value != GC_invalid_map */
817 phdr -> hb_flags = 0;
819 GC_heapsize += bytes;
820 if ((ptr_t)p <= (ptr_t)GC_least_plausible_heap_addr
821 || GC_least_plausible_heap_addr == 0) {
822 GC_least_plausible_heap_addr = (GC_PTR)((ptr_t)p - sizeof(word));
823 /* Making it a little smaller than necessary prevents */
824 /* us from getting a false hit from the variable */
825 /* itself. There's some unintentional reflection */
828 if ((ptr_t)p + bytes >= (ptr_t)GC_greatest_plausible_heap_addr) {
829 GC_greatest_plausible_heap_addr = (GC_PTR)((ptr_t)p + bytes);
833 # if !defined(NO_DEBUGGING)
834 void GC_print_heap_sects()
838 GC_printf1("Total heap size: %lu\n", (unsigned long) GC_heapsize);
839 for (i = 0; i < GC_n_heap_sects; i++) {
840 unsigned long start = (unsigned long) GC_heap_sects[i].hs_start;
841 unsigned long len = (unsigned long) GC_heap_sects[i].hs_bytes;
845 GC_printf3("Section %ld from 0x%lx to 0x%lx ", (unsigned long)i,
846 start, (unsigned long)(start + len));
847 for (h = (struct hblk *)start; h < (struct hblk *)(start + len); h++) {
848 if (GC_is_black_listed(h, HBLKSIZE)) nbl++;
850 GC_printf2("%lu/%lu blacklisted\n", (unsigned long)nbl,
851 (unsigned long)(len/HBLKSIZE));
856 GC_PTR GC_least_plausible_heap_addr = (GC_PTR)ONES;
857 GC_PTR GC_greatest_plausible_heap_addr = 0;
862 return(x > y? x : y);
868 return(x < y? x : y);
871 # if defined(__STDC__) || defined(__cplusplus)
872 void GC_set_max_heap_size(GC_word n)
874 void GC_set_max_heap_size(n)
881 word GC_get_max_heap_size()
883 return GC_max_heapsize;
886 GC_word GC_max_retries = 0;
889 * this explicitly increases the size of the heap. It is used
890 * internally, but may also be invoked from GC_expand_hp by the user.
891 * The argument is in units of HBLKSIZE.
892 * Tiny values of n are rounded up.
893 * Returns FALSE on failure.
895 GC_bool GC_expand_hp_inner(n)
900 word expansion_slop; /* Number of bytes by which we expect the */
901 /* heap to expand soon. */
903 if (n < MINHINCR) n = MINHINCR;
904 bytes = n * HBLKSIZE;
905 /* Make sure bytes is a multiple of GC_page_size */
907 word mask = GC_page_size - 1;
912 if (GC_max_heapsize != 0 && GC_heapsize + bytes > GC_max_heapsize) {
913 /* Exceeded self-imposed limit */
916 space = GET_MEM(bytes);
919 if (GC_print_stats) {
920 GC_printf1("Failed to expand heap by %ld bytes\n",
921 (unsigned long)bytes);
927 if (GC_print_stats) {
928 GC_printf2("Increasing heap size by %lu after %lu allocated bytes\n",
929 (unsigned long)bytes,
930 (unsigned long)WORDS_TO_BYTES(GC_words_allocd));
932 GC_printf1("Root size = %lu\n", GC_root_size);
933 GC_print_block_list(); GC_print_hblkfreelist();
938 expansion_slop = WORDS_TO_BYTES(min_words_allocd()) + 4*MAXHINCR*HBLKSIZE;
939 if (GC_last_heap_addr == 0 && !((word)space & SIGNB)
940 || GC_last_heap_addr != 0 && GC_last_heap_addr < (ptr_t)space) {
941 /* Assume the heap is growing up */
942 GC_greatest_plausible_heap_addr =
943 (GC_PTR)GC_max((ptr_t)GC_greatest_plausible_heap_addr,
944 (ptr_t)space + bytes + expansion_slop);
946 /* Heap is growing down */
947 GC_least_plausible_heap_addr =
948 (GC_PTR)GC_min((ptr_t)GC_least_plausible_heap_addr,
949 (ptr_t)space - expansion_slop);
951 # if defined(LARGE_CONFIG)
952 if (((ptr_t)GC_greatest_plausible_heap_addr <= (ptr_t)space + bytes
953 || (ptr_t)GC_least_plausible_heap_addr >= (ptr_t)space)
954 && GC_heapsize > 0) {
955 /* GC_add_to_heap will fix this, but ... */
956 WARN("Too close to address space limit: blacklisting ineffective\n", 0);
959 GC_prev_heap_addr = GC_last_heap_addr;
960 GC_last_heap_addr = (ptr_t)space;
961 GC_add_to_heap(space, bytes);
962 /* Force GC before we are likely to allocate past expansion_slop */
963 GC_collect_at_heapsize =
964 GC_heapsize + expansion_slop - 2*MAXHINCR*HBLKSIZE;
965 # if defined(LARGE_CONFIG)
966 if (GC_collect_at_heapsize < GC_heapsize /* wrapped */)
967 GC_collect_at_heapsize = (word)(-1);
972 /* Really returns a bool, but it's externally visible, so that's clumsy. */
973 /* Arguments is in bytes. */
974 # if defined(__STDC__) || defined(__cplusplus)
975 int GC_expand_hp(size_t bytes)
977 int GC_expand_hp(bytes)
986 if (!GC_is_initialized) GC_init_inner();
987 result = (int)GC_expand_hp_inner(divHBLKSZ((word)bytes));
988 if (result) GC_requested_heapsize += bytes;
994 unsigned GC_fail_count = 0;
995 /* How many consecutive GC/expansion failures? */
996 /* Reset by GC_allochblk. */
998 GC_bool GC_collect_or_expand(needed_blocks, ignore_off_page)
1000 GC_bool ignore_off_page;
1002 if (!GC_incremental && !GC_dont_gc &&
1003 (GC_dont_expand && GC_words_allocd > 0 || GC_should_collect())) {
1004 GC_gcollect_inner();
1006 word blocks_to_get = GC_heapsize/(HBLKSIZE*GC_free_space_divisor)
1009 if (blocks_to_get > MAXHINCR) {
1012 if (ignore_off_page) {
1015 slop = 2*divHBLKSZ(BL_LIMIT);
1016 if (slop > needed_blocks) slop = needed_blocks;
1018 if (needed_blocks + slop > MAXHINCR) {
1019 blocks_to_get = needed_blocks + slop;
1021 blocks_to_get = MAXHINCR;
1024 if (!GC_expand_hp_inner(blocks_to_get)
1025 && !GC_expand_hp_inner(needed_blocks)) {
1026 if (GC_fail_count++ < GC_max_retries) {
1027 WARN("Out of Memory! Trying to continue ...\n", 0);
1028 GC_gcollect_inner();
1030 # if !defined(AMIGA) || !defined(GC_AMIGA_FASTALLOC)
1031 WARN("Out of Memory! Returning NIL!\n", 0);
1037 if (GC_fail_count && GC_print_stats) {
1038 GC_printf0("Memory available again ...\n");
1047 * Make sure the object free list for sz is not empty.
1048 * Return a pointer to the first object on the free list.
1049 * The object MUST BE REMOVED FROM THE FREE LIST BY THE CALLER.
1050 * Assumes we hold the allocator lock and signals are disabled.
1053 ptr_t GC_allocobj(sz, kind)
1057 ptr_t * flh = &(GC_obj_kinds[kind].ok_freelist[sz]);
1058 GC_bool tried_minor = FALSE;
1060 if (sz == 0) return(0);
1064 /* Do our share of marking work */
1065 if(TRUE_INCREMENTAL) GC_collect_a_little_inner(1);
1066 /* Sweep blocks for objects of this size */
1067 GC_continue_reclaim(sz, kind);
1070 GC_new_hblk(sz, kind);
1074 if (GC_incremental && GC_time_limit == GC_TIME_UNLIMITED
1075 && ! tried_minor ) {
1076 GC_collect_a_little_inner(1);
1079 if (!GC_collect_or_expand((word)1,FALSE)) {
1087 /* Successful allocation; reset failure count. */