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-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.
19 # include "private/gc_priv.h"
22 # if !defined(MACOS) && !defined(MSWINCE)
24 # include <sys/types.h>
28 * Separate free lists are maintained for different sized objects
30 * The call GC_allocobj(i,k) ensures that the freelist for
31 * kind k objects of size i points to a non-empty
32 * free list. It returns a pointer to the first entry on the free list.
33 * In a single-threaded world, GC_allocobj may be called to allocate
34 * an object of (small) size i as follows:
36 * opp = &(GC_objfreelist[i]);
37 * if (*opp == 0) GC_allocobj(i, NORMAL);
39 * *opp = obj_link(ptr);
41 * Note that this is very fast if the free list is non-empty; it should
42 * only involve the execution of 4 or 5 simple instructions.
43 * All composite objects on freelists are cleared, except for
48 * The allocator uses GC_allochblk to allocate large chunks of objects.
49 * These chunks all start on addresses which are multiples of
50 * HBLKSZ. Each allocated chunk has an associated header,
51 * which can be located quickly based on the address of the chunk.
52 * (See headers.c for details.)
53 * This makes it possible to check quickly whether an
54 * arbitrary address corresponds to an object administered by the
58 word GC_non_gc_bytes
= 0; /* Number of bytes not intended to be collected */
63 int GC_incremental
= 0; /* By default, stop the world. */
66 int GC_parallel
= FALSE
; /* By default, parallel GC is off. */
68 int GC_full_freq
= 19; /* Every 20th collection is a full */
69 /* collection, whether we need it */
72 GC_bool GC_need_full_gc
= FALSE
;
73 /* Need full GC do to heap growth. */
76 GC_bool GC_world_stopped
= FALSE
;
77 # define IF_THREADS(x) x
79 # define IF_THREADS(x)
82 word GC_used_heap_size_after_full
= 0;
84 char * GC_copyright
[] =
85 {"Copyright 1988,1989 Hans-J. Boehm and Alan J. Demers ",
86 "Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved. ",
87 "Copyright (c) 1996-1998 by Silicon Graphics. All rights reserved. ",
88 "Copyright (c) 1999-2001 by Hewlett-Packard Company. All rights reserved. ",
89 "THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY",
90 " EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.",
91 "See source code for details." };
95 /* some more variables */
97 extern signed_word GC_bytes_found
; /* Number of reclaimed bytes */
98 /* after garbage collection */
100 GC_bool GC_dont_expand
= 0;
102 word GC_free_space_divisor
= 3;
104 extern GC_bool
GC_collection_in_progress();
105 /* Collection is in progress, or was abandoned. */
107 int GC_never_stop_func (void) { return(0); }
109 unsigned long GC_time_limit
= TIME_LIMIT
;
111 CLOCK_TYPE GC_start_time
; /* Time at which we stopped world. */
112 /* used only in GC_timeout_stop_func. */
114 int GC_n_attempts
= 0; /* Number of attempts at finishing */
115 /* collection within GC_time_limit. */
117 #if defined(SMALL_CONFIG) || defined(NO_CLOCK)
118 # define GC_timeout_stop_func GC_never_stop_func
120 int GC_timeout_stop_func (void)
122 CLOCK_TYPE current_time
;
123 static unsigned count
= 0;
124 unsigned long time_diff
;
126 if ((count
++ & 3) != 0) return(0);
127 GET_TIME(current_time
);
128 time_diff
= MS_TIME_DIFF(current_time
,GC_start_time
);
129 if (time_diff
>= GC_time_limit
) {
130 if (GC_print_stats
) {
131 GC_log_printf("Abandoning stopped marking after ");
132 GC_log_printf("%lu msecs", time_diff
);
133 GC_log_printf("(attempt %d)\n", GC_n_attempts
);
139 #endif /* !SMALL_CONFIG */
141 /* Return the minimum number of words that must be allocated between */
142 /* collections to amortize the collection cost. */
143 static word
min_bytes_allocd()
146 /* We punt, for now. */
147 signed_word stack_size
= 10000;
150 signed_word stack_size
= (ptr_t
)(&dummy
) - GC_stackbottom
;
152 word total_root_size
; /* includes double stack size, */
153 /* since the stack is expensive */
155 word scan_size
; /* Estimate of memory to be scanned */
156 /* during normal GC. */
158 if (stack_size
< 0) stack_size
= -stack_size
;
159 total_root_size
= 2 * stack_size
+ GC_root_size
;
160 scan_size
= 2 * GC_composite_in_use
+ GC_atomic_in_use
162 if (TRUE_INCREMENTAL
) {
163 return scan_size
/ (2 * GC_free_space_divisor
);
165 return scan_size
/ GC_free_space_divisor
;
169 /* Return the number of bytes allocated, adjusted for explicit storage */
170 /* management, etc.. This number is used in deciding when to trigger */
172 word
GC_adj_bytes_allocd(void)
175 signed_word expl_managed
=
176 (signed_word
)GC_non_gc_bytes
177 - (signed_word
)GC_non_gc_bytes_at_gc
;
179 /* Don't count what was explicitly freed, or newly allocated for */
180 /* explicit management. Note that deallocating an explicitly */
181 /* managed object should not alter result, assuming the client */
182 /* is playing by the rules. */
183 result
= (signed_word
)GC_bytes_allocd
184 - (signed_word
)GC_bytes_freed
185 + (signed_word
)GC_finalizer_bytes_freed
187 if (result
> (signed_word
)GC_bytes_allocd
) {
188 result
= GC_bytes_allocd
;
189 /* probably client bug or unfortunate scheduling */
191 result
+= GC_bytes_finalized
;
192 /* We count objects enqueued for finalization as though they */
193 /* had been reallocated this round. Finalization is user */
194 /* visible progress. And if we don't count this, we have */
195 /* stability problems for programs that finalize all objects. */
196 if (result
< (signed_word
)(GC_bytes_allocd
>> 3)) {
197 /* Always count at least 1/8 of the allocations. We don't want */
198 /* to collect too infrequently, since that would inhibit */
199 /* coalescing of free storage blocks. */
200 /* This also makes us partially robust against client bugs. */
201 return(GC_bytes_allocd
>> 3);
208 /* Clear up a few frames worth of garbage left at the top of the stack. */
209 /* This is used to prevent us from accidentally treating garbade left */
210 /* on the stack by other parts of the collector as roots. This */
211 /* differs from the code in misc.c, which actually tries to keep the */
212 /* stack clear of long-lived, client-generated garbage. */
213 void GC_clear_a_few_frames()
219 for (i
= 0; i
< NWORDS
; i
++) frames
[i
] = 0;
222 /* Heap size at which we need a collection to avoid expanding past */
223 /* limits used by blacklisting. */
224 static word GC_collect_at_heapsize
= (word
)(-1);
226 /* Have we allocated enough to amortize a collection? */
227 GC_bool
GC_should_collect(void)
229 return(GC_adj_bytes_allocd() >= min_bytes_allocd()
230 || GC_heapsize
>= GC_collect_at_heapsize
);
234 void GC_notify_full_gc(void)
236 if (GC_start_call_back
!= (void (*) (void))0) {
237 (*GC_start_call_back
)();
241 GC_bool GC_is_full_gc
= FALSE
;
244 * Initiate a garbage collection if appropriate.
246 * between partial, full, and stop-world collections.
247 * Assumes lock held, signals disabled.
249 void GC_maybe_gc(void)
251 static int n_partial_gcs
= 0;
253 if (GC_should_collect()) {
254 if (!GC_incremental
) {
259 # ifdef PARALLEL_MARK
260 GC_wait_for_reclaim();
262 if (GC_need_full_gc
|| n_partial_gcs
>= GC_full_freq
) {
263 if (GC_print_stats
) {
265 "***>Full mark for collection %lu after %ld allocd bytes\n",
266 (unsigned long)GC_gc_no
+1,
267 (long)GC_bytes_allocd
);
269 GC_promote_black_lists();
270 (void)GC_reclaim_all((GC_stop_func
)0, TRUE
);
274 GC_is_full_gc
= TRUE
;
279 /* We try to mark with the world stopped. */
280 /* If we run out of time, this turns into */
281 /* incremental marking. */
283 if (GC_time_limit
!= GC_TIME_UNLIMITED
) { GET_TIME(GC_start_time
); }
285 if (GC_stopped_mark(GC_time_limit
== GC_TIME_UNLIMITED
?
286 GC_never_stop_func
: GC_timeout_stop_func
)) {
287 # ifdef SAVE_CALL_CHAIN
288 GC_save_callers(GC_last_stack
);
290 GC_finish_collection();
292 if (!GC_is_full_gc
) {
293 /* Count this as the first attempt */
302 * Stop the world garbage collection. Assumes lock held, signals disabled.
303 * If stop_func is not GC_never_stop_func, then abort if stop_func returns TRUE.
304 * Return TRUE if we successfully completed the collection.
306 GC_bool
GC_try_to_collect_inner(GC_stop_func stop_func
)
308 CLOCK_TYPE start_time
, current_time
;
309 if (GC_dont_gc
) return FALSE
;
310 if (GC_incremental
&& GC_collection_in_progress()) {
311 if (GC_print_stats
) {
313 "GC_try_to_collect_inner: finishing collection in progress\n");
315 /* Just finish collection already in progress. */
316 while(GC_collection_in_progress()) {
317 if (stop_func()) return(FALSE
);
318 GC_collect_a_little_inner(1);
321 if (stop_func
== GC_never_stop_func
) GC_notify_full_gc();
322 if (GC_print_stats
) {
323 GET_TIME(start_time
);
325 "Initiating full world-stop collection %lu after %ld allocd bytes\n",
326 (unsigned long)GC_gc_no
+1, (long)GC_bytes_allocd
);
328 GC_promote_black_lists();
329 /* Make sure all blocks have been reclaimed, so sweep routines */
330 /* don't see cleared mark bits. */
331 /* If we're guaranteed to finish, then this is unnecessary. */
332 /* In the find_leak case, we have to finish to guarantee that */
333 /* previously unmarked objects are not reported as leaks. */
334 # ifdef PARALLEL_MARK
335 GC_wait_for_reclaim();
337 if ((GC_find_leak
|| stop_func
!= GC_never_stop_func
)
338 && !GC_reclaim_all(stop_func
, FALSE
)) {
339 /* Aborted. So far everything is still consistent. */
342 GC_invalidate_mark_state(); /* Flush mark stack. */
344 # ifdef SAVE_CALL_CHAIN
345 GC_save_callers(GC_last_stack
);
347 GC_is_full_gc
= TRUE
;
348 if (!GC_stopped_mark(stop_func
)) {
349 if (!GC_incremental
) {
350 /* We're partially done and have no way to complete or use */
351 /* current work. Reestablish invariants as cheaply as */
353 GC_invalidate_mark_state();
354 GC_unpromote_black_lists();
355 } /* else we claim the world is already still consistent. We'll */
356 /* finish incrementally. */
359 GC_finish_collection();
360 if (GC_print_stats
) {
361 GET_TIME(current_time
);
362 GC_log_printf("Complete collection took %lu msecs\n",
363 MS_TIME_DIFF(current_time
,start_time
));
371 * Perform n units of garbage collection work. A unit is intended to touch
372 * roughly GC_RATE pages. Every once in a while, we do more than that.
373 * This needs to be a fairly large number with our current incremental
374 * GC strategy, since otherwise we allocate too much during GC, and the
375 * cleanup gets expensive.
378 # define MAX_PRIOR_ATTEMPTS 1
379 /* Maximum number of prior attempts at world stop marking */
380 /* A value of 1 means that we finish the second time, no matter */
381 /* how long it takes. Doesn't count the initial root scan */
384 int GC_deficit
= 0; /* The number of extra calls to GC_mark_some */
385 /* that we have made. */
387 void GC_collect_a_little_inner(int n
)
391 if (GC_dont_gc
) return;
392 if (GC_incremental
&& GC_collection_in_progress()) {
393 for (i
= GC_deficit
; i
< GC_RATE
*n
; i
++) {
394 if (GC_mark_some((ptr_t
)0)) {
395 /* Need to finish a collection */
396 # ifdef SAVE_CALL_CHAIN
397 GC_save_callers(GC_last_stack
);
399 # ifdef PARALLEL_MARK
400 GC_wait_for_reclaim();
402 if (GC_n_attempts
< MAX_PRIOR_ATTEMPTS
403 && GC_time_limit
!= GC_TIME_UNLIMITED
) {
404 GET_TIME(GC_start_time
);
405 if (!GC_stopped_mark(GC_timeout_stop_func
)) {
410 (void)GC_stopped_mark(GC_never_stop_func
);
412 GC_finish_collection();
416 if (GC_deficit
> 0) GC_deficit
-= GC_RATE
*n
;
417 if (GC_deficit
< 0) GC_deficit
= 0;
423 int GC_collect_a_little(void)
429 GC_collect_a_little_inner(1);
430 result
= (int)GC_collection_in_progress();
432 if (!result
&& GC_debugging_started
) GC_print_all_smashed();
437 * Assumes lock is held, signals are disabled.
439 * If stop_func() ever returns TRUE, we may fail and return FALSE.
440 * Increment GC_gc_no if we succeed.
442 GC_bool
GC_stopped_mark(GC_stop_func stop_func
)
446 CLOCK_TYPE start_time
, current_time
;
449 GET_TIME(start_time
);
451 # if defined(REGISTER_LIBRARIES_EARLY)
452 GC_cond_register_dynamic_libraries();
455 IF_THREADS(GC_world_stopped
= TRUE
);
456 if (GC_print_stats
) {
457 GC_log_printf("--> Marking for collection %lu ",
458 (unsigned long)GC_gc_no
+ 1);
459 GC_log_printf("after %lu allocd bytes\n",
460 (unsigned long) GC_bytes_allocd
);
462 # ifdef MAKE_BACK_GRAPH
463 if (GC_print_back_height
) {
464 GC_build_back_graph();
468 /* Mark from all roots. */
469 /* Minimize junk left in my registers and on the stack */
470 GC_clear_a_few_frames();
471 GC_noop(0,0,0,0,0,0);
474 if ((*stop_func
)()) {
475 if (GC_print_stats
) {
476 GC_log_printf("Abandoned stopped marking after ");
477 GC_log_printf("%u iterations\n", i
);
479 GC_deficit
= i
; /* Give the mutator a chance. */
480 IF_THREADS(GC_world_stopped
= FALSE
);
484 if (GC_mark_some((ptr_t
)(&dummy
))) break;
488 if (GC_print_stats
) {
489 GC_log_printf("Collection %lu reclaimed %ld bytes",
490 (unsigned long)GC_gc_no
- 1,
491 (long)GC_bytes_found
);
492 GC_log_printf(" ---> heapsize = %lu bytes\n",
493 (unsigned long) GC_heapsize
);
494 /* Printf arguments may be pushed in funny places. Clear the */
499 /* Check all debugged objects for consistency */
500 if (GC_debugging_started
) {
504 IF_THREADS(GC_world_stopped
= FALSE
);
506 if (GC_print_stats
) {
507 GET_TIME(current_time
);
508 GC_log_printf("World-stopped marking took %lu msecs\n",
509 MS_TIME_DIFF(current_time
,start_time
));
514 /* Set all mark bits for the free list whose first entry is q */
515 void GC_set_fl_marks(ptr_t q
)
518 struct hblk
* h
, * last_h
= 0;
519 hdr
*hhdr
; /* gcc "might be uninitialized" warning is bogus. */
520 IF_PER_OBJ(size_t sz
;)
523 for (p
= q
; p
!= 0; p
= obj_link(p
)){
528 IF_PER_OBJ(sz
= hhdr
->hb_sz
;)
530 bit_no
= MARK_BIT_NO((ptr_t
)p
- (ptr_t
)h
, sz
);
531 if (!mark_bit_from_hdr(hhdr
, bit_no
)) {
532 set_mark_bit_from_hdr(hhdr
, bit_no
);
533 ++hhdr
-> hb_n_marks
;
539 /* Check that all mark bits for the free list whose first entry is q */
541 void GC_check_fl_marks(ptr_t q
)
545 for (p
= q
; p
!= 0; p
= obj_link(p
)){
546 if (!GC_is_marked(p
)) {
547 GC_err_printf("Unmarked object %p on list %p\n", p
, q
);
548 ABORT("Unmarked local free list entry.");
554 /* Clear all mark bits for the free list whose first entry is q */
555 /* Decrement GC_bytes_found by number of bytes on free list. */
556 void GC_clear_fl_marks(ptr_t q
)
559 struct hblk
* h
, * last_h
= 0;
564 for (p
= q
; p
!= 0; p
= obj_link(p
)){
569 sz
= hhdr
->hb_sz
; /* Normally set only once. */
571 bit_no
= MARK_BIT_NO((ptr_t
)p
- (ptr_t
)h
, sz
);
572 if (mark_bit_from_hdr(hhdr
, bit_no
)) {
573 size_t n_marks
= hhdr
-> hb_n_marks
- 1;
574 clear_mark_bit_from_hdr(hhdr
, bit_no
);
575 # ifdef PARALLEL_MARK
576 /* Appr. count, don't decrement to zero! */
578 hhdr
-> hb_n_marks
= n_marks
;
581 hhdr
-> hb_n_marks
= n_marks
;
584 GC_bytes_found
-= sz
;
588 #if defined(GC_ASSERTIONS) && defined(THREADS) && defined(THREAD_LOCAL_ALLOC)
589 extern void GC_check_tls(void);
592 /* Finish up a collection. Assumes lock is held, signals are disabled, */
593 /* but the world is otherwise running. */
594 void GC_finish_collection()
596 CLOCK_TYPE start_time
;
597 CLOCK_TYPE finalize_time
;
598 CLOCK_TYPE done_time
;
600 # if defined(GC_ASSERTIONS) && defined(THREADS) \
601 && defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
602 /* Check that we marked some of our own data. */
603 /* FIXME: Add more checks. */
608 GET_TIME(start_time
);
611 # if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
612 if (getenv("GC_PRINT_ADDRESS_MAP") != 0) {
613 GC_print_address_map();
618 /* Mark all objects on the free list. All objects should be */
619 /* marked when we're done. */
621 word size
; /* current object size */
625 for (kind
= 0; kind
< GC_n_kinds
; kind
++) {
626 for (size
= 1; size
<= MAXOBJGRANULES
; size
++) {
627 q
= GC_obj_kinds
[kind
].ok_freelist
[size
];
628 if (q
!= 0) GC_set_fl_marks(q
);
632 GC_start_reclaim(TRUE
);
633 /* The above just checks; it doesn't really reclaim anything. */
637 # ifdef STUBBORN_ALLOC
638 GC_clean_changing_list();
642 GET_TIME(finalize_time
);
644 if (GC_print_back_height
) {
645 # ifdef MAKE_BACK_GRAPH
646 GC_traverse_back_graph();
648 # ifndef SMALL_CONFIG
649 GC_err_printf("Back height not available: "
650 "Rebuild collector with -DMAKE_BACK_GRAPH\n");
655 /* Clear free list mark bits, in case they got accidentally marked */
656 /* (or GC_find_leak is set and they were intentionally marked). */
657 /* Also subtract memory remaining from GC_bytes_found count. */
658 /* Note that composite objects on free list are cleared. */
659 /* Thus accidentally marking a free list is not a problem; only */
660 /* objects on the list itself will be marked, and that's fixed here. */
662 word size
; /* current object size */
663 ptr_t q
; /* pointer to current object */
666 for (kind
= 0; kind
< GC_n_kinds
; kind
++) {
667 for (size
= 1; size
<= MAXOBJGRANULES
; size
++) {
668 q
= GC_obj_kinds
[kind
].ok_freelist
[size
];
669 if (q
!= 0) GC_clear_fl_marks(q
);
675 if (GC_print_stats
== VERBOSE
)
676 GC_log_printf("Bytes recovered before sweep - f.l. count = %ld\n",
677 (long)GC_bytes_found
);
679 /* Reconstruct free lists to contain everything not marked */
680 GC_start_reclaim(FALSE
);
681 if (GC_print_stats
) {
682 GC_log_printf("Heap contains %lu pointer-containing "
683 "+ %lu pointer-free reachable bytes\n",
684 (unsigned long)GC_composite_in_use
,
685 (unsigned long)GC_atomic_in_use
);
688 GC_used_heap_size_after_full
= USED_HEAP_SIZE
;
689 GC_need_full_gc
= FALSE
;
692 USED_HEAP_SIZE
- GC_used_heap_size_after_full
693 > min_bytes_allocd();
696 if (GC_print_stats
== VERBOSE
) {
698 "Immediately reclaimed %ld bytes in heap of size %lu bytes",
699 (long)GC_bytes_found
,
700 (unsigned long)GC_heapsize
);
702 GC_log_printf("(%lu unmapped)", (unsigned long)GC_unmapped_bytes
);
707 /* Reset or increment counters for next cycle */
709 GC_is_full_gc
= FALSE
;
710 GC_bytes_allocd_before_gc
+= GC_bytes_allocd
;
711 GC_non_gc_bytes_at_gc
= GC_non_gc_bytes
;
714 GC_finalizer_bytes_freed
= 0;
719 if (GC_print_stats
) {
721 GC_log_printf("Finalize + initiate sweep took %lu + %lu msecs\n",
722 MS_TIME_DIFF(finalize_time
,start_time
),
723 MS_TIME_DIFF(done_time
,finalize_time
));
727 /* Externally callable routine to invoke full, stop-world collection */
728 int GC_try_to_collect(GC_stop_func stop_func
)
733 if (!GC_is_initialized
) GC_init();
734 if (GC_debugging_started
) GC_print_all_smashed();
735 GC_INVOKE_FINALIZERS();
738 if (!GC_is_initialized
) GC_init_inner();
739 /* Minimize junk left in my registers */
740 GC_noop(0,0,0,0,0,0);
741 result
= (int)GC_try_to_collect_inner(stop_func
);
745 if (GC_debugging_started
) GC_print_all_smashed();
746 GC_INVOKE_FINALIZERS();
751 void GC_gcollect(void)
753 (void)GC_try_to_collect(GC_never_stop_func
);
754 if (GC_have_errors
) GC_print_all_errors();
757 word GC_n_heap_sects
= 0; /* Number of sections currently in heap. */
760 * Use the chunk of memory starting at p of size bytes as part of the heap.
761 * Assumes p is HBLKSIZE aligned, and bytes is a multiple of HBLKSIZE.
763 void GC_add_to_heap(struct hblk
*p
, size_t bytes
)
767 if (GC_n_heap_sects
>= MAX_HEAP_SECTS
) {
768 ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");
770 phdr
= GC_install_header(p
);
772 /* This is extremely unlikely. Can't add it. This will */
773 /* almost certainly result in a 0 return from the allocator, */
774 /* which is entirely appropriate. */
777 GC_heap_sects
[GC_n_heap_sects
].hs_start
= (ptr_t
)p
;
778 GC_heap_sects
[GC_n_heap_sects
].hs_bytes
= bytes
;
780 phdr
-> hb_sz
= bytes
;
781 phdr
-> hb_flags
= 0;
783 GC_heapsize
+= bytes
;
784 if ((ptr_t
)p
<= (ptr_t
)GC_least_plausible_heap_addr
785 || GC_least_plausible_heap_addr
== 0) {
786 GC_least_plausible_heap_addr
= (void *)((ptr_t
)p
- sizeof(word
));
787 /* Making it a little smaller than necessary prevents */
788 /* us from getting a false hit from the variable */
789 /* itself. There's some unintentional reflection */
792 if ((ptr_t
)p
+ bytes
>= (ptr_t
)GC_greatest_plausible_heap_addr
) {
793 GC_greatest_plausible_heap_addr
= (void *)((ptr_t
)p
+ bytes
);
797 # if !defined(NO_DEBUGGING)
798 void GC_print_heap_sects(void)
802 GC_printf("Total heap size: %lu\n", (unsigned long) GC_heapsize
);
803 for (i
= 0; i
< GC_n_heap_sects
; i
++) {
804 ptr_t start
= GC_heap_sects
[i
].hs_start
;
805 size_t len
= GC_heap_sects
[i
].hs_bytes
;
809 GC_printf("Section %d from %p to %p ", i
,
811 for (h
= (struct hblk
*)start
; h
< (struct hblk
*)(start
+ len
); h
++) {
812 if (GC_is_black_listed(h
, HBLKSIZE
)) nbl
++;
814 GC_printf("%lu/%lu blacklisted\n", (unsigned long)nbl
,
815 (unsigned long)(len
/HBLKSIZE
));
820 void * GC_least_plausible_heap_addr
= (void *)ONES
;
821 void * GC_greatest_plausible_heap_addr
= 0;
823 static INLINE ptr_t
GC_max(ptr_t x
, ptr_t y
)
825 return(x
> y
? x
: y
);
828 static INLINE ptr_t
GC_min(ptr_t x
, ptr_t y
)
830 return(x
< y
? x
: y
);
833 void GC_set_max_heap_size(GC_word n
)
838 GC_word GC_max_retries
= 0;
841 * this explicitly increases the size of the heap. It is used
842 * internally, but may also be invoked from GC_expand_hp by the user.
843 * The argument is in units of HBLKSIZE.
844 * Tiny values of n are rounded up.
845 * Returns FALSE on failure.
847 GC_bool
GC_expand_hp_inner(word n
)
851 word expansion_slop
; /* Number of bytes by which we expect the */
852 /* heap to expand soon. */
854 if (n
< MINHINCR
) n
= MINHINCR
;
855 bytes
= n
* HBLKSIZE
;
856 /* Make sure bytes is a multiple of GC_page_size */
858 word mask
= GC_page_size
- 1;
863 if (GC_max_heapsize
!= 0 && GC_heapsize
+ bytes
> GC_max_heapsize
) {
864 /* Exceeded self-imposed limit */
867 space
= GET_MEM(bytes
);
869 if (GC_print_stats
) {
870 GC_log_printf("Failed to expand heap by %ld bytes\n",
871 (unsigned long)bytes
);
875 if (GC_print_stats
) {
876 GC_log_printf("Increasing heap size by %lu after %lu allocated bytes\n",
877 (unsigned long)bytes
,
878 (unsigned long)GC_bytes_allocd
);
880 expansion_slop
= min_bytes_allocd() + 4*MAXHINCR
*HBLKSIZE
;
881 if ((GC_last_heap_addr
== 0 && !((word
)space
& SIGNB
))
882 || (GC_last_heap_addr
!= 0 && GC_last_heap_addr
< (ptr_t
)space
)) {
883 /* Assume the heap is growing up */
884 GC_greatest_plausible_heap_addr
=
885 (void *)GC_max((ptr_t
)GC_greatest_plausible_heap_addr
,
886 (ptr_t
)space
+ bytes
+ expansion_slop
);
888 /* Heap is growing down */
889 GC_least_plausible_heap_addr
=
890 (void *)GC_min((ptr_t
)GC_least_plausible_heap_addr
,
891 (ptr_t
)space
- expansion_slop
);
893 # if defined(LARGE_CONFIG)
894 if (((ptr_t
)GC_greatest_plausible_heap_addr
<= (ptr_t
)space
+ bytes
895 || (ptr_t
)GC_least_plausible_heap_addr
>= (ptr_t
)space
)
896 && GC_heapsize
> 0) {
897 /* GC_add_to_heap will fix this, but ... */
898 WARN("Too close to address space limit: blacklisting ineffective\n", 0);
901 GC_prev_heap_addr
= GC_last_heap_addr
;
902 GC_last_heap_addr
= (ptr_t
)space
;
903 GC_add_to_heap(space
, bytes
);
904 /* Force GC before we are likely to allocate past expansion_slop */
905 GC_collect_at_heapsize
=
906 GC_heapsize
+ expansion_slop
- 2*MAXHINCR
*HBLKSIZE
;
907 # if defined(LARGE_CONFIG)
908 if (GC_collect_at_heapsize
< GC_heapsize
/* wrapped */)
909 GC_collect_at_heapsize
= (word
)(-1);
914 /* Really returns a bool, but it's externally visible, so that's clumsy. */
915 /* Arguments is in bytes. */
916 int GC_expand_hp(size_t bytes
)
922 if (!GC_is_initialized
) GC_init_inner();
923 result
= (int)GC_expand_hp_inner(divHBLKSZ((word
)bytes
));
924 if (result
) GC_requested_heapsize
+= bytes
;
929 unsigned GC_fail_count
= 0;
930 /* How many consecutive GC/expansion failures? */
931 /* Reset by GC_allochblk. */
933 GC_bool
GC_collect_or_expand(word needed_blocks
, GC_bool ignore_off_page
)
935 if (!GC_incremental
&& !GC_dont_gc
&&
936 ((GC_dont_expand
&& GC_bytes_allocd
> 0) || GC_should_collect())) {
939 word blocks_to_get
= GC_heapsize
/(HBLKSIZE
*GC_free_space_divisor
)
942 if (blocks_to_get
> MAXHINCR
) {
945 /* Get the minimum required to make it likely that we */
946 /* can satisfy the current request in the presence of black- */
947 /* listing. This will probably be more than MAXHINCR. */
948 if (ignore_off_page
) {
951 slop
= 2*divHBLKSZ(BL_LIMIT
);
952 if (slop
> needed_blocks
) slop
= needed_blocks
;
954 if (needed_blocks
+ slop
> MAXHINCR
) {
955 blocks_to_get
= needed_blocks
+ slop
;
957 blocks_to_get
= MAXHINCR
;
960 if (!GC_expand_hp_inner(blocks_to_get
)
961 && !GC_expand_hp_inner(needed_blocks
)) {
962 if (GC_fail_count
++ < GC_max_retries
) {
963 WARN("Out of Memory! Trying to continue ...\n", 0);
966 # if !defined(AMIGA) || !defined(GC_AMIGA_FASTALLOC)
967 WARN("Out of Memory! Returning NIL!\n", 0);
972 if (GC_fail_count
&& GC_print_stats
) {
973 GC_printf("Memory available again ...\n");
981 * Make sure the object free list for size gran (in granules) is not empty.
982 * Return a pointer to the first object on the free list.
983 * The object MUST BE REMOVED FROM THE FREE LIST BY THE CALLER.
984 * Assumes we hold the allocator lock and signals are disabled.
987 ptr_t
GC_allocobj(size_t gran
, int kind
)
989 void ** flh
= &(GC_obj_kinds
[kind
].ok_freelist
[gran
]);
990 GC_bool tried_minor
= FALSE
;
992 if (gran
== 0) return(0);
996 /* Do our share of marking work */
997 if(TRUE_INCREMENTAL
) GC_collect_a_little_inner(1);
998 /* Sweep blocks for objects of this size */
999 GC_continue_reclaim(gran
, kind
);
1002 GC_new_hblk(gran
, kind
);
1006 if (GC_incremental
&& GC_time_limit
== GC_TIME_UNLIMITED
1007 && ! tried_minor
) {
1008 GC_collect_a_little_inner(1);
1011 if (!GC_collect_or_expand((word
)1,FALSE
)) {
1019 /* Successful allocation; reset failure count. */