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.
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_mem_found
; /* Number of reclaimed longwords */
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
GC_PROTO((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
GC_PROTO((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
) {
131 if (GC_print_stats
) {
132 GC_printf0("Abandoning stopped marking after ");
133 GC_printf1("%lu msecs", (unsigned long)time_diff
);
134 GC_printf1("(attempt %ld)\n", (unsigned long) GC_n_attempts
);
141 #endif /* !SMALL_CONFIG */
143 /* Return the minimum number of words that must be allocated between */
144 /* collections to amortize the collection cost. */
145 static word
min_words_allocd()
148 /* We punt, for now. */
149 register signed_word stack_size
= 10000;
152 register signed_word stack_size
= (ptr_t
)(&dummy
) - GC_stackbottom
;
154 word total_root_size
; /* includes double stack size, */
155 /* since the stack is expensive */
157 word scan_size
; /* Estimate of memory to be scanned */
158 /* during normal GC. */
160 if (stack_size
< 0) stack_size
= -stack_size
;
161 total_root_size
= 2 * stack_size
+ GC_root_size
;
162 scan_size
= BYTES_TO_WORDS(GC_heapsize
- GC_large_free_bytes
163 + (GC_large_free_bytes
>> 2)
164 /* use a bit more of large empty heap */
166 if (TRUE_INCREMENTAL
) {
167 return scan_size
/ (2 * GC_free_space_divisor
);
169 return scan_size
/ GC_free_space_divisor
;
173 /* Return the number of words allocated, adjusted for explicit storage */
174 /* management, etc.. This number is used in deciding when to trigger */
176 word
GC_adj_words_allocd()
178 register signed_word result
;
179 register signed_word expl_managed
=
180 BYTES_TO_WORDS((long)GC_non_gc_bytes
181 - (long)GC_non_gc_bytes_at_gc
);
183 /* Don't count what was explicitly freed, or newly allocated for */
184 /* explicit management. Note that deallocating an explicitly */
185 /* managed object should not alter result, assuming the client */
186 /* is playing by the rules. */
187 result
= (signed_word
)GC_words_allocd
188 - (signed_word
)GC_mem_freed
189 + (signed_word
)GC_finalizer_mem_freed
- expl_managed
;
190 if (result
> (signed_word
)GC_words_allocd
) {
191 result
= GC_words_allocd
;
192 /* probably client bug or unfortunate scheduling */
194 result
+= GC_words_finalized
;
195 /* We count objects enqueued for finalization as though they */
196 /* had been reallocated this round. Finalization is user */
197 /* visible progress. And if we don't count this, we have */
198 /* stability problems for programs that finalize all objects. */
199 result
+= GC_words_wasted
;
200 /* This doesn't reflect useful work. But if there is lots of */
201 /* new fragmentation, the same is probably true of the heap, */
202 /* and the collection will be correspondingly cheaper. */
203 if (result
< (signed_word
)(GC_words_allocd
>> 3)) {
204 /* Always count at least 1/8 of the allocations. We don't want */
205 /* to collect too infrequently, since that would inhibit */
206 /* coalescing of free storage blocks. */
207 /* This also makes us partially robust against client bugs. */
208 return(GC_words_allocd
>> 3);
215 /* Clear up a few frames worth of garbage left at the top of the stack. */
216 /* This is used to prevent us from accidentally treating garbade left */
217 /* on the stack by other parts of the collector as roots. This */
218 /* differs from the code in misc.c, which actually tries to keep the */
219 /* stack clear of long-lived, client-generated garbage. */
220 void GC_clear_a_few_frames()
226 for (i
= 0; i
< NWORDS
; i
++) frames
[i
] = 0;
229 /* Heap size at which we need a collection to avoid expanding past */
230 /* limits used by blacklisting. */
231 static word GC_collect_at_heapsize
= (word
)(-1);
233 /* Have we allocated enough to amortize a collection? */
234 GC_bool
GC_should_collect()
236 return(GC_adj_words_allocd() >= min_words_allocd()
237 || GC_heapsize
>= GC_collect_at_heapsize
);
241 void GC_notify_full_gc()
243 if (GC_start_call_back
!= (void (*) GC_PROTO((void)))0) {
244 (*GC_start_call_back
)();
248 GC_bool GC_is_full_gc
= FALSE
;
251 * Initiate a garbage collection if appropriate.
253 * between partial, full, and stop-world collections.
254 * Assumes lock held, signals disabled.
258 static int n_partial_gcs
= 0;
260 if (GC_should_collect()) {
261 if (!GC_incremental
) {
266 # ifdef PARALLEL_MARK
267 GC_wait_for_reclaim();
269 if (GC_need_full_gc
|| n_partial_gcs
>= GC_full_freq
) {
271 if (GC_print_stats
) {
273 "***>Full mark for collection %lu after %ld allocd bytes\n",
274 (unsigned long) GC_gc_no
+1,
275 (long)WORDS_TO_BYTES(GC_words_allocd
));
278 GC_promote_black_lists();
279 (void)GC_reclaim_all((GC_stop_func
)0, TRUE
);
283 GC_is_full_gc
= TRUE
;
288 /* We try to mark with the world stopped. */
289 /* If we run out of time, this turns into */
290 /* incremental marking. */
292 if (GC_time_limit
!= GC_TIME_UNLIMITED
) { GET_TIME(GC_start_time
); }
294 if (GC_stopped_mark(GC_time_limit
== GC_TIME_UNLIMITED
?
295 GC_never_stop_func
: GC_timeout_stop_func
)) {
296 # ifdef SAVE_CALL_CHAIN
297 GC_save_callers(GC_last_stack
);
299 GC_finish_collection();
301 if (!GC_is_full_gc
) {
302 /* Count this as the first attempt */
311 * Stop the world garbage collection. Assumes lock held, signals disabled.
312 * If stop_func is not GC_never_stop_func, then abort if stop_func returns TRUE.
313 * Return TRUE if we successfully completed the collection.
315 GC_bool
GC_try_to_collect_inner(stop_func
)
316 GC_stop_func stop_func
;
319 CLOCK_TYPE start_time
, current_time
;
321 if (GC_dont_gc
) return FALSE
;
322 if (GC_incremental
&& GC_collection_in_progress()) {
324 if (GC_print_stats
) {
326 "GC_try_to_collect_inner: finishing collection in progress\n");
328 # endif /* CONDPRINT */
329 /* Just finish collection already in progress. */
330 while(GC_collection_in_progress()) {
331 if (stop_func()) return(FALSE
);
332 GC_collect_a_little_inner(1);
335 if (stop_func
== GC_never_stop_func
) GC_notify_full_gc();
337 if (GC_print_stats
) {
338 if (GC_print_stats
) GET_TIME(start_time
);
340 "Initiating full world-stop collection %lu after %ld allocd bytes\n",
341 (unsigned long) GC_gc_no
+1,
342 (long)WORDS_TO_BYTES(GC_words_allocd
));
345 GC_promote_black_lists();
346 /* Make sure all blocks have been reclaimed, so sweep routines */
347 /* don't see cleared mark bits. */
348 /* If we're guaranteed to finish, then this is unnecessary. */
349 /* In the find_leak case, we have to finish to guarantee that */
350 /* previously unmarked objects are not reported as leaks. */
351 # ifdef PARALLEL_MARK
352 GC_wait_for_reclaim();
354 if ((GC_find_leak
|| stop_func
!= GC_never_stop_func
)
355 && !GC_reclaim_all(stop_func
, FALSE
)) {
356 /* Aborted. So far everything is still consistent. */
359 GC_invalidate_mark_state(); /* Flush mark stack. */
361 # ifdef SAVE_CALL_CHAIN
362 GC_save_callers(GC_last_stack
);
364 GC_is_full_gc
= TRUE
;
365 if (!GC_stopped_mark(stop_func
)) {
366 if (!GC_incremental
) {
367 /* We're partially done and have no way to complete or use */
368 /* current work. Reestablish invariants as cheaply as */
370 GC_invalidate_mark_state();
371 GC_unpromote_black_lists();
372 } /* else we claim the world is already still consistent. We'll */
373 /* finish incrementally. */
376 GC_finish_collection();
377 # if defined(CONDPRINT)
378 if (GC_print_stats
) {
379 GET_TIME(current_time
);
380 GC_printf1("Complete collection took %lu msecs\n",
381 MS_TIME_DIFF(current_time
,start_time
));
390 * Perform n units of garbage collection work. A unit is intended to touch
391 * roughly GC_RATE pages. Every once in a while, we do more than that.
392 * This needa to be a fairly large number with our current incremental
393 * GC strategy, since otherwise we allocate too much during GC, and the
394 * cleanup gets expensive.
397 # define MAX_PRIOR_ATTEMPTS 1
398 /* Maximum number of prior attempts at world stop marking */
399 /* A value of 1 means that we finish the second time, no matter */
400 /* how long it takes. Doesn't count the initial root scan */
403 int GC_deficit
= 0; /* The number of extra calls to GC_mark_some */
404 /* that we have made. */
406 void GC_collect_a_little_inner(n
)
411 if (GC_dont_gc
) return;
412 if (GC_incremental
&& GC_collection_in_progress()) {
413 for (i
= GC_deficit
; i
< GC_RATE
*n
; i
++) {
414 if (GC_mark_some((ptr_t
)0)) {
415 /* Need to finish a collection */
416 # ifdef SAVE_CALL_CHAIN
417 GC_save_callers(GC_last_stack
);
419 # ifdef PARALLEL_MARK
420 GC_wait_for_reclaim();
422 if (GC_n_attempts
< MAX_PRIOR_ATTEMPTS
423 && GC_time_limit
!= GC_TIME_UNLIMITED
) {
424 GET_TIME(GC_start_time
);
425 if (!GC_stopped_mark(GC_timeout_stop_func
)) {
430 (void)GC_stopped_mark(GC_never_stop_func
);
432 GC_finish_collection();
436 if (GC_deficit
> 0) GC_deficit
-= GC_RATE
*n
;
437 if (GC_deficit
< 0) GC_deficit
= 0;
443 int GC_collect_a_little
GC_PROTO(())
450 GC_collect_a_little_inner(1);
451 result
= (int)GC_collection_in_progress();
454 if (!result
&& GC_debugging_started
) GC_print_all_smashed();
459 * Assumes lock is held, signals are disabled.
461 * If stop_func() ever returns TRUE, we may fail and return FALSE.
462 * Increment GC_gc_no if we succeed.
464 GC_bool
GC_stopped_mark(stop_func
)
465 GC_stop_func stop_func
;
469 # if defined(PRINTTIMES) || defined(CONDPRINT)
470 CLOCK_TYPE start_time
, current_time
;
474 GET_TIME(start_time
);
476 # if defined(CONDPRINT) && !defined(PRINTTIMES)
477 if (GC_print_stats
) GET_TIME(start_time
);
479 # if defined(REGISTER_LIBRARIES_EARLY)
480 GC_cond_register_dynamic_libraries();
483 IF_THREADS(GC_world_stopped
= TRUE
);
485 if (GC_print_stats
) {
486 GC_printf1("--> Marking for collection %lu ",
487 (unsigned long) GC_gc_no
+ 1);
488 GC_printf2("after %lu allocd bytes + %lu wasted bytes\n",
489 (unsigned long) WORDS_TO_BYTES(GC_words_allocd
),
490 (unsigned long) WORDS_TO_BYTES(GC_words_wasted
));
493 # ifdef MAKE_BACK_GRAPH
494 if (GC_print_back_height
) {
495 GC_build_back_graph();
499 /* Mark from all roots. */
500 /* Minimize junk left in my registers and on the stack */
501 GC_clear_a_few_frames();
502 GC_noop(0,0,0,0,0,0);
505 if ((*stop_func
)()) {
507 if (GC_print_stats
) {
508 GC_printf0("Abandoned stopped marking after ");
509 GC_printf1("%lu iterations\n",
513 GC_deficit
= i
; /* Give the mutator a chance. */
514 IF_THREADS(GC_world_stopped
= FALSE
);
518 if (GC_mark_some((ptr_t
)(&dummy
))) break;
523 GC_printf2("Collection %lu reclaimed %ld bytes",
524 (unsigned long) GC_gc_no
- 1,
525 (long)WORDS_TO_BYTES(GC_mem_found
));
528 if (GC_print_stats
) {
529 GC_printf1("Collection %lu finished", (unsigned long) GC_gc_no
- 1);
532 # endif /* !PRINTSTATS */
534 if (GC_print_stats
) {
535 GC_printf1(" ---> heapsize = %lu bytes\n",
536 (unsigned long) GC_heapsize
);
537 /* Printf arguments may be pushed in funny places. Clear the */
541 # endif /* CONDPRINT */
543 /* Check all debugged objects for consistency */
544 if (GC_debugging_started
) {
548 IF_THREADS(GC_world_stopped
= FALSE
);
551 GET_TIME(current_time
);
552 GC_printf1("World-stopped marking took %lu msecs\n",
553 MS_TIME_DIFF(current_time
,start_time
));
556 if (GC_print_stats
) {
557 GET_TIME(current_time
);
558 GC_printf1("World-stopped marking took %lu msecs\n",
559 MS_TIME_DIFF(current_time
,start_time
));
566 /* Set all mark bits for the free list whose first entry is q */
568 void GC_set_fl_marks(ptr_t q
)
570 void GC_set_fl_marks(q
)
575 struct hblk
* h
, * last_h
= 0;
579 for (p
= q
; p
!= 0; p
= obj_link(p
)){
585 word_no
= (((word
*)p
) - ((word
*)h
));
586 set_mark_bit_from_hdr(hhdr
, word_no
);
590 /* Clear all mark bits for the free list whose first entry is q */
591 /* Decrement GC_mem_found by number of words on free list. */
593 void GC_clear_fl_marks(ptr_t q
)
595 void GC_clear_fl_marks(q
)
600 struct hblk
* h
, * last_h
= 0;
604 for (p
= q
; p
!= 0; p
= obj_link(p
)){
610 word_no
= (((word
*)p
) - ((word
*)h
));
611 clear_mark_bit_from_hdr(hhdr
, word_no
);
613 GC_mem_found
-= hhdr
-> hb_sz
;
618 /* Finish up a collection. Assumes lock is held, signals are disabled, */
619 /* but the world is otherwise running. */
620 void GC_finish_collection()
623 CLOCK_TYPE start_time
;
624 CLOCK_TYPE finalize_time
;
625 CLOCK_TYPE done_time
;
627 GET_TIME(start_time
);
628 finalize_time
= start_time
;
634 # if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
635 if (getenv("GC_PRINT_ADDRESS_MAP") != 0) {
636 GC_print_address_map();
641 /* Mark all objects on the free list. All objects should be */
642 /* marked when we're done. */
644 register word size
; /* current object size */
648 for (kind
= 0; kind
< GC_n_kinds
; kind
++) {
649 for (size
= 1; size
<= MAXOBJSZ
; size
++) {
650 q
= GC_obj_kinds
[kind
].ok_freelist
[size
];
651 if (q
!= 0) GC_set_fl_marks(q
);
655 GC_start_reclaim(TRUE
);
656 /* The above just checks; it doesn't really reclaim anything. */
660 # ifdef STUBBORN_ALLOC
661 GC_clean_changing_list();
665 GET_TIME(finalize_time
);
668 if (GC_print_back_height
) {
669 # ifdef MAKE_BACK_GRAPH
670 GC_traverse_back_graph();
672 # ifndef SMALL_CONFIG
673 GC_err_printf0("Back height not available: "
674 "Rebuild collector with -DMAKE_BACK_GRAPH\n");
679 /* Clear free list mark bits, in case they got accidentally marked */
680 /* (or GC_find_leak is set and they were intentionally marked). */
681 /* Also subtract memory remaining from GC_mem_found count. */
682 /* Note that composite objects on free list are cleared. */
683 /* Thus accidentally marking a free list is not a problem; only */
684 /* objects on the list itself will be marked, and that's fixed here. */
686 register word size
; /* current object size */
687 register ptr_t q
; /* pointer to current object */
690 for (kind
= 0; kind
< GC_n_kinds
; kind
++) {
691 for (size
= 1; size
<= MAXOBJSZ
; size
++) {
692 q
= GC_obj_kinds
[kind
].ok_freelist
[size
];
693 if (q
!= 0) GC_clear_fl_marks(q
);
700 GC_printf1("Bytes recovered before sweep - f.l. count = %ld\n",
701 (long)WORDS_TO_BYTES(GC_mem_found
));
703 /* Reconstruct free lists to contain everything not marked */
704 GC_start_reclaim(FALSE
);
706 GC_used_heap_size_after_full
= USED_HEAP_SIZE
;
707 GC_need_full_gc
= FALSE
;
710 BYTES_TO_WORDS(USED_HEAP_SIZE
- GC_used_heap_size_after_full
)
711 > min_words_allocd();
716 "Immediately reclaimed %ld bytes in heap of size %lu bytes",
717 (long)WORDS_TO_BYTES(GC_mem_found
),
718 (unsigned long)GC_heapsize
);
720 GC_printf1("(%lu unmapped)", GC_unmapped_bytes
);
723 "\n%lu (atomic) + %lu (composite) collectable bytes in use\n",
724 (unsigned long)WORDS_TO_BYTES(GC_atomic_in_use
),
725 (unsigned long)WORDS_TO_BYTES(GC_composite_in_use
));
729 GC_is_full_gc
= FALSE
;
730 /* Reset or increment counters for next cycle */
731 GC_words_allocd_before_gc
+= GC_words_allocd
;
732 GC_non_gc_bytes_at_gc
= GC_non_gc_bytes
;
736 GC_finalizer_mem_freed
= 0;
743 GC_printf2("Finalize + initiate sweep took %lu + %lu msecs\n",
744 MS_TIME_DIFF(finalize_time
,start_time
),
745 MS_TIME_DIFF(done_time
,finalize_time
));
749 /* Externally callable routine to invoke full, stop-world collection */
750 # if defined(__STDC__) || defined(__cplusplus)
751 int GC_try_to_collect(GC_stop_func stop_func
)
753 int GC_try_to_collect(stop_func
)
754 GC_stop_func stop_func
;
760 if (GC_debugging_started
) GC_print_all_smashed();
761 GC_INVOKE_FINALIZERS();
765 if (!GC_is_initialized
) GC_init_inner();
766 /* Minimize junk left in my registers */
767 GC_noop(0,0,0,0,0,0);
768 result
= (int)GC_try_to_collect_inner(stop_func
);
773 if (GC_debugging_started
) GC_print_all_smashed();
774 GC_INVOKE_FINALIZERS();
779 void GC_gcollect
GC_PROTO(())
781 (void)GC_try_to_collect(GC_never_stop_func
);
782 if (GC_have_errors
) GC_print_all_errors();
785 word GC_n_heap_sects
= 0; /* Number of sections currently in heap. */
788 * Use the chunk of memory starting at p of size bytes as part of the heap.
789 * Assumes p is HBLKSIZE aligned, and bytes is a multiple of HBLKSIZE.
791 void GC_add_to_heap(p
, bytes
)
798 if (GC_n_heap_sects
>= MAX_HEAP_SECTS
) {
799 ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");
801 phdr
= GC_install_header(p
);
803 /* This is extremely unlikely. Can't add it. This will */
804 /* almost certainly result in a 0 return from the allocator, */
805 /* which is entirely appropriate. */
808 GC_heap_sects
[GC_n_heap_sects
].hs_start
= (ptr_t
)p
;
809 GC_heap_sects
[GC_n_heap_sects
].hs_bytes
= bytes
;
811 words
= BYTES_TO_WORDS(bytes
);
812 phdr
-> hb_sz
= words
;
813 phdr
-> hb_map
= (unsigned char *)1; /* A value != GC_invalid_map */
814 phdr
-> hb_flags
= 0;
816 GC_heapsize
+= bytes
;
817 if ((ptr_t
)p
<= (ptr_t
)GC_least_plausible_heap_addr
818 || GC_least_plausible_heap_addr
== 0) {
819 GC_least_plausible_heap_addr
= (GC_PTR
)((ptr_t
)p
- sizeof(word
));
820 /* Making it a little smaller than necessary prevents */
821 /* us from getting a false hit from the variable */
822 /* itself. There's some unintentional reflection */
825 if ((ptr_t
)p
+ bytes
>= (ptr_t
)GC_greatest_plausible_heap_addr
) {
826 GC_greatest_plausible_heap_addr
= (GC_PTR
)((ptr_t
)p
+ bytes
);
830 # if !defined(NO_DEBUGGING)
831 void GC_print_heap_sects()
835 GC_printf1("Total heap size: %lu\n", (unsigned long) GC_heapsize
);
836 for (i
= 0; i
< GC_n_heap_sects
; i
++) {
837 unsigned long start
= (unsigned long) GC_heap_sects
[i
].hs_start
;
838 unsigned long len
= (unsigned long) GC_heap_sects
[i
].hs_bytes
;
842 GC_printf3("Section %ld from 0x%lx to 0x%lx ", (unsigned long)i
,
843 start
, (unsigned long)(start
+ len
));
844 for (h
= (struct hblk
*)start
; h
< (struct hblk
*)(start
+ len
); h
++) {
845 if (GC_is_black_listed(h
, HBLKSIZE
)) nbl
++;
847 GC_printf2("%lu/%lu blacklisted\n", (unsigned long)nbl
,
848 (unsigned long)(len
/HBLKSIZE
));
853 GC_PTR GC_least_plausible_heap_addr
= (GC_PTR
)ONES
;
854 GC_PTR GC_greatest_plausible_heap_addr
= 0;
859 return(x
> y
? x
: y
);
865 return(x
< y
? x
: y
);
868 # if defined(__STDC__) || defined(__cplusplus)
869 void GC_set_max_heap_size(GC_word n
)
871 void GC_set_max_heap_size(n
)
878 GC_word GC_max_retries
= 0;
881 * this explicitly increases the size of the heap. It is used
882 * internally, but may also be invoked from GC_expand_hp by the user.
883 * The argument is in units of HBLKSIZE.
884 * Tiny values of n are rounded up.
885 * Returns FALSE on failure.
887 GC_bool
GC_expand_hp_inner(n
)
892 word expansion_slop
; /* Number of bytes by which we expect the */
893 /* heap to expand soon. */
895 if (n
< MINHINCR
) n
= MINHINCR
;
896 bytes
= n
* HBLKSIZE
;
897 /* Make sure bytes is a multiple of GC_page_size */
899 word mask
= GC_page_size
- 1;
904 if (GC_max_heapsize
!= 0 && GC_heapsize
+ bytes
> GC_max_heapsize
) {
905 /* Exceeded self-imposed limit */
908 space
= GET_MEM(bytes
);
911 if (GC_print_stats
) {
912 GC_printf1("Failed to expand heap by %ld bytes\n",
913 (unsigned long)bytes
);
919 if (GC_print_stats
) {
920 GC_printf2("Increasing heap size by %lu after %lu allocated bytes\n",
921 (unsigned long)bytes
,
922 (unsigned long)WORDS_TO_BYTES(GC_words_allocd
));
924 GC_printf1("Root size = %lu\n", GC_root_size
);
925 GC_print_block_list(); GC_print_hblkfreelist();
930 expansion_slop
= WORDS_TO_BYTES(min_words_allocd()) + 4*MAXHINCR
*HBLKSIZE
;
931 if (GC_last_heap_addr
== 0 && !((word
)space
& SIGNB
)
932 || GC_last_heap_addr
!= 0 && GC_last_heap_addr
< (ptr_t
)space
) {
933 /* Assume the heap is growing up */
934 GC_greatest_plausible_heap_addr
=
935 (GC_PTR
)GC_max((ptr_t
)GC_greatest_plausible_heap_addr
,
936 (ptr_t
)space
+ bytes
+ expansion_slop
);
938 /* Heap is growing down */
939 GC_least_plausible_heap_addr
=
940 (GC_PTR
)GC_min((ptr_t
)GC_least_plausible_heap_addr
,
941 (ptr_t
)space
- expansion_slop
);
943 # if defined(LARGE_CONFIG)
944 if (((ptr_t
)GC_greatest_plausible_heap_addr
<= (ptr_t
)space
+ bytes
945 || (ptr_t
)GC_least_plausible_heap_addr
>= (ptr_t
)space
)
946 && GC_heapsize
> 0) {
947 /* GC_add_to_heap will fix this, but ... */
948 WARN("Too close to address space limit: blacklisting ineffective\n", 0);
951 GC_prev_heap_addr
= GC_last_heap_addr
;
952 GC_last_heap_addr
= (ptr_t
)space
;
953 GC_add_to_heap(space
, bytes
);
954 /* Force GC before we are likely to allocate past expansion_slop */
955 GC_collect_at_heapsize
=
956 GC_heapsize
+ expansion_slop
- 2*MAXHINCR
*HBLKSIZE
;
957 # if defined(LARGE_CONFIG)
958 if (GC_collect_at_heapsize
< GC_heapsize
/* wrapped */)
959 GC_collect_at_heapsize
= (word
)(-1);
964 /* Really returns a bool, but it's externally visible, so that's clumsy. */
965 /* Arguments is in bytes. */
966 # if defined(__STDC__) || defined(__cplusplus)
967 int GC_expand_hp(size_t bytes
)
969 int GC_expand_hp(bytes
)
978 if (!GC_is_initialized
) GC_init_inner();
979 result
= (int)GC_expand_hp_inner(divHBLKSZ((word
)bytes
));
980 if (result
) GC_requested_heapsize
+= bytes
;
986 unsigned GC_fail_count
= 0;
987 /* How many consecutive GC/expansion failures? */
988 /* Reset by GC_allochblk. */
990 GC_bool
GC_collect_or_expand(needed_blocks
, ignore_off_page
)
992 GC_bool ignore_off_page
;
994 if (!GC_incremental
&& !GC_dont_gc
&&
995 (GC_dont_expand
&& GC_words_allocd
> 0 || GC_should_collect())) {
998 word blocks_to_get
= GC_heapsize
/(HBLKSIZE
*GC_free_space_divisor
)
1001 if (blocks_to_get
> MAXHINCR
) {
1004 if (ignore_off_page
) {
1007 slop
= 2*divHBLKSZ(BL_LIMIT
);
1008 if (slop
> needed_blocks
) slop
= needed_blocks
;
1010 if (needed_blocks
+ slop
> MAXHINCR
) {
1011 blocks_to_get
= needed_blocks
+ slop
;
1013 blocks_to_get
= MAXHINCR
;
1016 if (!GC_expand_hp_inner(blocks_to_get
)
1017 && !GC_expand_hp_inner(needed_blocks
)) {
1018 if (GC_fail_count
++ < GC_max_retries
) {
1019 WARN("Out of Memory! Trying to continue ...\n", 0);
1020 GC_gcollect_inner();
1022 # if !defined(AMIGA) || !defined(GC_AMIGA_FASTALLOC)
1023 WARN("Out of Memory! Returning NIL!\n", 0);
1029 if (GC_fail_count
&& GC_print_stats
) {
1030 GC_printf0("Memory available again ...\n");
1039 * Make sure the object free list for sz is not empty.
1040 * Return a pointer to the first object on the free list.
1041 * The object MUST BE REMOVED FROM THE FREE LIST BY THE CALLER.
1042 * Assumes we hold the allocator lock and signals are disabled.
1045 ptr_t
GC_allocobj(sz
, kind
)
1049 ptr_t
* flh
= &(GC_obj_kinds
[kind
].ok_freelist
[sz
]);
1050 GC_bool tried_minor
= FALSE
;
1052 if (sz
== 0) return(0);
1056 /* Do our share of marking work */
1057 if(TRUE_INCREMENTAL
) GC_collect_a_little_inner(1);
1058 /* Sweep blocks for objects of this size */
1059 GC_continue_reclaim(sz
, kind
);
1062 GC_new_hblk(sz
, kind
);
1066 if (GC_incremental
&& GC_time_limit
== GC_TIME_UNLIMITED
1067 && ! tried_minor
) {
1068 GC_collect_a_little_inner(1);
1071 if (!GC_collect_or_expand((word
)1,FALSE
)) {
1079 /* Successful allocation; reset failure count. */