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. */
75 word GC_used_heap_size_after_full
= 0;
77 char * GC_copyright
[] =
78 {"Copyright 1988,1989 Hans-J. Boehm and Alan J. Demers ",
79 "Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved. ",
80 "Copyright (c) 1996-1998 by Silicon Graphics. All rights reserved. ",
81 "Copyright (c) 1999-2001 by Hewlett-Packard Company. All rights reserved. ",
82 "THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY",
83 " EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.",
84 "See source code for details." };
88 /* some more variables */
90 extern signed_word GC_mem_found
; /* Number of reclaimed longwords */
91 /* after garbage collection */
93 GC_bool GC_dont_expand
= 0;
95 word GC_free_space_divisor
= 3;
97 extern GC_bool
GC_collection_in_progress();
98 /* Collection is in progress, or was abandoned. */
100 extern GC_bool GC_print_back_height
;
102 int GC_never_stop_func
GC_PROTO((void)) { return(0); }
104 unsigned long GC_time_limit
= TIME_LIMIT
;
106 CLOCK_TYPE GC_start_time
; /* Time at which we stopped world. */
107 /* used only in GC_timeout_stop_func. */
109 int GC_n_attempts
= 0; /* Number of attempts at finishing */
110 /* collection within GC_time_limit. */
112 #if defined(SMALL_CONFIG) || defined(NO_CLOCK)
113 # define GC_timeout_stop_func GC_never_stop_func
115 int GC_timeout_stop_func
GC_PROTO((void))
117 CLOCK_TYPE current_time
;
118 static unsigned count
= 0;
119 unsigned long time_diff
;
121 if ((count
++ & 3) != 0) return(0);
123 GET_TIME(current_time
);
124 time_diff
= MS_TIME_DIFF(current_time
,GC_start_time
);
125 if (time_diff
>= GC_time_limit
) {
127 if (GC_print_stats
) {
128 GC_printf0("Abandoning stopped marking after ");
129 GC_printf1("%lu msecs", (unsigned long)time_diff
);
130 GC_printf1("(attempt %d)\n", (unsigned long) GC_n_attempts
);
138 #endif /* !SMALL_CONFIG */
140 /* Return the minimum number of words that must be allocated between */
141 /* collections to amortize the collection cost. */
142 static word
min_words_allocd()
145 /* We punt, for now. */
146 register signed_word stack_size
= 10000;
149 register signed_word stack_size
= (ptr_t
)(&dummy
) - GC_stackbottom
;
151 word total_root_size
; /* includes double stack size, */
152 /* since the stack is expensive */
154 word scan_size
; /* Estimate of memory to be scanned */
155 /* during normal GC. */
157 if (stack_size
< 0) stack_size
= -stack_size
;
158 total_root_size
= 2 * stack_size
+ GC_root_size
;
159 scan_size
= BYTES_TO_WORDS(GC_heapsize
- GC_large_free_bytes
160 + (GC_large_free_bytes
>> 2)
161 /* use a bit more of large empty heap */
163 if (GC_incremental
) {
164 return scan_size
/ (2 * GC_free_space_divisor
);
166 return scan_size
/ GC_free_space_divisor
;
170 /* Return the number of words allocated, adjusted for explicit storage */
171 /* management, etc.. This number is used in deciding when to trigger */
173 word
GC_adj_words_allocd()
175 register signed_word result
;
176 register signed_word expl_managed
=
177 BYTES_TO_WORDS((long)GC_non_gc_bytes
178 - (long)GC_non_gc_bytes_at_gc
);
180 /* Don't count what was explicitly freed, or newly allocated for */
181 /* explicit management. Note that deallocating an explicitly */
182 /* managed object should not alter result, assuming the client */
183 /* is playing by the rules. */
184 result
= (signed_word
)GC_words_allocd
185 - (signed_word
)GC_mem_freed
- expl_managed
;
186 if (result
> (signed_word
)GC_words_allocd
) {
187 result
= GC_words_allocd
;
188 /* probably client bug or unfortunate scheduling */
190 result
+= GC_words_finalized
;
191 /* We count objects enqueued for finalization as though they */
192 /* had been reallocated this round. Finalization is user */
193 /* visible progress. And if we don't count this, we have */
194 /* stability problems for programs that finalize all objects. */
195 result
+= GC_words_wasted
;
196 /* This doesn't reflect useful work. But if there is lots of */
197 /* new fragmentation, the same is probably true of the heap, */
198 /* and the collection will be correspondingly cheaper. */
199 if (result
< (signed_word
)(GC_words_allocd
>> 3)) {
200 /* Always count at least 1/8 of the allocations. We don't want */
201 /* to collect too infrequently, since that would inhibit */
202 /* coalescing of free storage blocks. */
203 /* This also makes us partially robust against client bugs. */
204 return(GC_words_allocd
>> 3);
211 /* Clear up a few frames worth of garbage left at the top of the stack. */
212 /* This is used to prevent us from accidentally treating garbade left */
213 /* on the stack by other parts of the collector as roots. This */
214 /* differs from the code in misc.c, which actually tries to keep the */
215 /* stack clear of long-lived, client-generated garbage. */
216 void GC_clear_a_few_frames()
222 for (i
= 0; i
< NWORDS
; i
++) frames
[i
] = 0;
225 /* Have we allocated enough to amortize a collection? */
226 GC_bool
GC_should_collect()
228 return(GC_adj_words_allocd() >= min_words_allocd());
232 void GC_notify_full_gc()
234 if (GC_start_call_back
!= (void (*) GC_PROTO((void)))0) {
235 (*GC_start_call_back
)();
239 GC_bool GC_is_full_gc
= FALSE
;
242 * Initiate a garbage collection if appropriate.
244 * between partial, full, and stop-world collections.
245 * Assumes lock held, signals disabled.
249 static int n_partial_gcs
= 0;
251 if (GC_should_collect()) {
252 if (!GC_incremental
) {
258 # ifdef PARALLEL_MARK
259 GC_wait_for_reclaim();
261 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)WORDS_TO_BYTES(GC_words_allocd
));
270 GC_promote_black_lists();
271 (void)GC_reclaim_all((GC_stop_func
)0, TRUE
);
275 GC_is_full_gc
= TRUE
;
280 /* We try to mark with the world stopped. */
281 /* If we run out of time, this turns into */
282 /* incremental marking. */
284 if (GC_time_limit
!= GC_TIME_UNLIMITED
) { GET_TIME(GC_start_time
); }
286 if (GC_stopped_mark(GC_time_limit
== GC_TIME_UNLIMITED
?
287 GC_never_stop_func
: GC_timeout_stop_func
)) {
288 # ifdef SAVE_CALL_CHAIN
289 GC_save_callers(GC_last_stack
);
291 GC_finish_collection();
293 if (!GC_is_full_gc
) {
294 /* Count this as the first attempt */
303 * Stop the world garbage collection. Assumes lock held, signals disabled.
304 * If stop_func is not GC_never_stop_func, then abort if stop_func returns TRUE.
306 GC_bool
GC_try_to_collect_inner(stop_func
)
307 GC_stop_func stop_func
;
309 if (GC_dont_gc
) return FALSE
;
310 if (GC_incremental
&& GC_collection_in_progress()) {
312 if (GC_print_stats
) {
314 "GC_try_to_collect_inner: finishing collection in progress\n");
316 # endif /* CONDPRINT */
317 /* Just finish collection already in progress. */
318 while(GC_collection_in_progress()) {
319 if (stop_func()) return(FALSE
);
320 GC_collect_a_little_inner(1);
324 if (GC_print_stats
) {
326 "Initiating full world-stop collection %lu after %ld allocd bytes\n",
327 (unsigned long) GC_gc_no
+1,
328 (long)WORDS_TO_BYTES(GC_words_allocd
));
331 GC_promote_black_lists();
332 /* Make sure all blocks have been reclaimed, so sweep routines */
333 /* don't see cleared mark bits. */
334 /* If we're guaranteed to finish, then this is unnecessary. */
335 /* In the find_leak case, we have to finish to guarantee that */
336 /* previously unmarked objects are not reported as leaks. */
337 # ifdef PARALLEL_MARK
338 GC_wait_for_reclaim();
340 if ((GC_find_leak
|| stop_func
!= GC_never_stop_func
)
341 && !GC_reclaim_all(stop_func
, FALSE
)) {
342 /* Aborted. So far everything is still consistent. */
345 GC_invalidate_mark_state(); /* Flush mark stack. */
347 # ifdef SAVE_CALL_CHAIN
348 GC_save_callers(GC_last_stack
);
350 GC_is_full_gc
= TRUE
;
351 if (!GC_stopped_mark(stop_func
)) {
352 if (!GC_incremental
) {
353 /* We're partially done and have no way to complete or use */
354 /* current work. Reestablish invariants as cheaply as */
356 GC_invalidate_mark_state();
357 GC_unpromote_black_lists();
358 } /* else we claim the world is already still consistent. We'll */
359 /* finish incrementally. */
362 GC_finish_collection();
369 * Perform n units of garbage collection work. A unit is intended to touch
370 * roughly GC_RATE pages. Every once in a while, we do more than that.
371 * This needa to be a fairly large number with our current incremental
372 * GC strategy, since otherwise we allocate too much during GC, and the
373 * cleanup gets expensive.
376 # define MAX_PRIOR_ATTEMPTS 1
377 /* Maximum number of prior attempts at world stop marking */
378 /* A value of 1 means that we finish the second time, no matter */
379 /* how long it takes. Doesn't count the initial root scan */
382 int GC_deficit
= 0; /* The number of extra calls to GC_mark_some */
383 /* that we have made. */
385 void GC_collect_a_little_inner(n
)
390 if (GC_dont_gc
) return;
391 if (GC_incremental
&& GC_collection_in_progress()) {
392 for (i
= GC_deficit
; i
< GC_RATE
*n
; i
++) {
393 if (GC_mark_some((ptr_t
)0)) {
394 /* Need to finish a collection */
395 # ifdef SAVE_CALL_CHAIN
396 GC_save_callers(GC_last_stack
);
398 # ifdef PARALLEL_MARK
399 GC_wait_for_reclaim();
401 if (GC_n_attempts
< MAX_PRIOR_ATTEMPTS
402 && GC_time_limit
!= GC_TIME_UNLIMITED
) {
403 GET_TIME(GC_start_time
);
404 if (!GC_stopped_mark(GC_timeout_stop_func
)) {
409 (void)GC_stopped_mark(GC_never_stop_func
);
411 GC_finish_collection();
415 if (GC_deficit
> 0) GC_deficit
-= GC_RATE
*n
;
416 if (GC_deficit
< 0) GC_deficit
= 0;
422 int GC_collect_a_little
GC_PROTO(())
429 GC_collect_a_little_inner(1);
430 result
= (int)GC_collection_in_progress();
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(stop_func
)
443 GC_stop_func stop_func
;
447 # if defined(PRINTTIMES) || defined(CONDPRINT)
448 CLOCK_TYPE start_time
, current_time
;
451 # if defined(REGISTER_LIBRARIES_EARLY)
452 GC_cond_register_dynamic_libraries();
456 GET_TIME(start_time
);
458 # if defined(CONDPRINT) && !defined(PRINTTIMES)
459 if (GC_print_stats
) GET_TIME(start_time
);
462 if (GC_print_stats
) {
463 GC_printf1("--> Marking for collection %lu ",
464 (unsigned long) GC_gc_no
+ 1);
465 GC_printf2("after %lu allocd bytes + %lu wasted bytes\n",
466 (unsigned long) WORDS_TO_BYTES(GC_words_allocd
),
467 (unsigned long) WORDS_TO_BYTES(GC_words_wasted
));
470 # ifdef MAKE_BACK_GRAPH
471 if (GC_print_back_height
) {
472 GC_build_back_graph();
476 /* Mark from all roots. */
477 /* Minimize junk left in my registers and on the stack */
478 GC_clear_a_few_frames();
479 GC_noop(0,0,0,0,0,0);
482 if ((*stop_func
)()) {
484 if (GC_print_stats
) {
485 GC_printf0("Abandoned stopped marking after ");
486 GC_printf1("%lu iterations\n",
490 GC_deficit
= i
; /* Give the mutator a chance. */
494 if (GC_mark_some((ptr_t
)(&dummy
))) break;
499 GC_printf2("Collection %lu reclaimed %ld bytes",
500 (unsigned long) GC_gc_no
- 1,
501 (long)WORDS_TO_BYTES(GC_mem_found
));
504 if (GC_print_stats
) {
505 GC_printf1("Collection %lu finished", (unsigned long) GC_gc_no
- 1);
508 # endif /* !PRINTSTATS */
510 if (GC_print_stats
) {
511 GC_printf1(" ---> heapsize = %lu bytes\n",
512 (unsigned long) GC_heapsize
);
513 /* Printf arguments may be pushed in funny places. Clear the */
517 # endif /* CONDPRINT */
519 /* Check all debugged objects for consistency */
520 if (GC_debugging_started
) {
525 GET_TIME(current_time
);
526 GC_printf1("World-stopped marking took %lu msecs\n",
527 MS_TIME_DIFF(current_time
,start_time
));
530 if (GC_print_stats
) {
531 GET_TIME(current_time
);
532 GC_printf1("World-stopped marking took %lu msecs\n",
533 MS_TIME_DIFF(current_time
,start_time
));
541 /* Set all mark bits for the free list whose first entry is q */
543 void GC_set_fl_marks(ptr_t q
)
545 void GC_set_fl_marks(q
)
550 struct hblk
* h
, * last_h
= 0;
554 for (p
= q
; p
!= 0; p
= obj_link(p
)){
560 word_no
= (((word
*)p
) - ((word
*)h
));
561 set_mark_bit_from_hdr(hhdr
, word_no
);
565 /* Clear all mark bits for the free list whose first entry is q */
566 /* Decrement GC_mem_found by number of words on free list. */
568 void GC_clear_fl_marks(ptr_t q
)
570 void GC_clear_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 clear_mark_bit_from_hdr(hhdr
, word_no
);
588 GC_mem_found
-= hhdr
-> hb_sz
;
593 /* Finish up a collection. Assumes lock is held, signals are disabled, */
594 /* but the world is otherwise running. */
595 void GC_finish_collection()
598 CLOCK_TYPE start_time
;
599 CLOCK_TYPE finalize_time
;
600 CLOCK_TYPE done_time
;
602 GET_TIME(start_time
);
603 finalize_time
= start_time
;
609 # if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
610 if (getenv("GC_PRINT_ADDRESS_MAP") != 0) {
611 GC_print_address_map();
615 /* Mark all objects on the free list. All objects should be */
616 /* marked when we're done. */
618 register word size
; /* current object size */
622 for (kind
= 0; kind
< GC_n_kinds
; kind
++) {
623 for (size
= 1; size
<= MAXOBJSZ
; size
++) {
624 q
= GC_obj_kinds
[kind
].ok_freelist
[size
];
625 if (q
!= 0) GC_set_fl_marks(q
);
629 GC_start_reclaim(TRUE
);
630 /* The above just checks; it doesn't really reclaim anything. */
634 # ifdef STUBBORN_ALLOC
635 GC_clean_changing_list();
639 GET_TIME(finalize_time
);
642 if (GC_print_back_height
) {
643 # ifdef MAKE_BACK_GRAPH
644 GC_traverse_back_graph();
646 # ifndef SMALL_CONFIG
647 GC_err_printf0("Back height not available: "
648 "Rebuild collector with -DMAKE_BACK_GRAPH\n");
653 /* Clear free list mark bits, in case they got accidentally marked */
654 /* (or GC_find_leak is set and they were intentionally marked). */
655 /* Also subtract memory remaining from GC_mem_found count. */
656 /* Note that composite objects on free list are cleared. */
657 /* Thus accidentally marking a free list is not a problem; only */
658 /* objects on the list itself will be marked, and that's fixed here. */
660 register word size
; /* current object size */
661 register ptr_t q
; /* pointer to current object */
664 for (kind
= 0; kind
< GC_n_kinds
; kind
++) {
665 for (size
= 1; size
<= MAXOBJSZ
; size
++) {
666 q
= GC_obj_kinds
[kind
].ok_freelist
[size
];
667 if (q
!= 0) GC_clear_fl_marks(q
);
674 GC_printf1("Bytes recovered before sweep - f.l. count = %ld\n",
675 (long)WORDS_TO_BYTES(GC_mem_found
));
677 /* Reconstruct free lists to contain everything not marked */
678 GC_start_reclaim(FALSE
);
680 GC_used_heap_size_after_full
= USED_HEAP_SIZE
;
681 GC_need_full_gc
= FALSE
;
684 BYTES_TO_WORDS(USED_HEAP_SIZE
- GC_used_heap_size_after_full
)
685 > min_words_allocd();
690 "Immediately reclaimed %ld bytes in heap of size %lu bytes",
691 (long)WORDS_TO_BYTES(GC_mem_found
),
692 (unsigned long)GC_heapsize
);
694 GC_printf1("(%lu unmapped)", GC_unmapped_bytes
);
697 "\n%lu (atomic) + %lu (composite) collectable bytes in use\n",
698 (unsigned long)WORDS_TO_BYTES(GC_atomic_in_use
),
699 (unsigned long)WORDS_TO_BYTES(GC_composite_in_use
));
703 GC_is_full_gc
= FALSE
;
704 /* Reset or increment counters for next cycle */
705 GC_words_allocd_before_gc
+= GC_words_allocd
;
706 GC_non_gc_bytes_at_gc
= GC_non_gc_bytes
;
716 GC_printf2("Finalize + initiate sweep took %lu + %lu msecs\n",
717 MS_TIME_DIFF(finalize_time
,start_time
),
718 MS_TIME_DIFF(done_time
,finalize_time
));
722 /* Externally callable routine to invoke full, stop-world collection */
723 # if defined(__STDC__) || defined(__cplusplus)
724 int GC_try_to_collect(GC_stop_func stop_func
)
726 int GC_try_to_collect(stop_func
)
727 GC_stop_func stop_func
;
733 GC_INVOKE_FINALIZERS();
737 if (!GC_is_initialized
) GC_init_inner();
738 /* Minimize junk left in my registers */
739 GC_noop(0,0,0,0,0,0);
740 result
= (int)GC_try_to_collect_inner(stop_func
);
744 if(result
) GC_INVOKE_FINALIZERS();
748 void GC_gcollect
GC_PROTO(())
751 (void)GC_try_to_collect(GC_never_stop_func
);
754 word GC_n_heap_sects
= 0; /* Number of sections currently in heap. */
757 * Use the chunk of memory starting at p of size bytes as part of the heap.
758 * Assumes p is HBLKSIZE aligned, and bytes is a multiple of HBLKSIZE.
760 void GC_add_to_heap(p
, 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 words
= BYTES_TO_WORDS(bytes
);
781 phdr
-> hb_sz
= words
;
782 phdr
-> hb_map
= (unsigned char *)1; /* A value != GC_invalid_map */
783 phdr
-> hb_flags
= 0;
785 GC_heapsize
+= bytes
;
786 if ((ptr_t
)p
<= (ptr_t
)GC_least_plausible_heap_addr
787 || GC_least_plausible_heap_addr
== 0) {
788 GC_least_plausible_heap_addr
= (GC_PTR
)((ptr_t
)p
- sizeof(word
));
789 /* Making it a little smaller than necessary prevents */
790 /* us from getting a false hit from the variable */
791 /* itself. There's some unintentional reflection */
794 if ((ptr_t
)p
+ bytes
>= (ptr_t
)GC_greatest_plausible_heap_addr
) {
795 GC_greatest_plausible_heap_addr
= (GC_PTR
)((ptr_t
)p
+ bytes
);
799 # if !defined(NO_DEBUGGING)
800 void GC_print_heap_sects()
804 GC_printf1("Total heap size: %lu\n", (unsigned long) GC_heapsize
);
805 for (i
= 0; i
< GC_n_heap_sects
; i
++) {
806 unsigned long start
= (unsigned long) GC_heap_sects
[i
].hs_start
;
807 unsigned long len
= (unsigned long) GC_heap_sects
[i
].hs_bytes
;
811 GC_printf3("Section %ld from 0x%lx to 0x%lx ", (unsigned long)i
,
812 start
, (unsigned long)(start
+ len
));
813 for (h
= (struct hblk
*)start
; h
< (struct hblk
*)(start
+ len
); h
++) {
814 if (GC_is_black_listed(h
, HBLKSIZE
)) nbl
++;
816 GC_printf2("%lu/%lu blacklisted\n", (unsigned long)nbl
,
817 (unsigned long)(len
/HBLKSIZE
));
822 GC_PTR GC_least_plausible_heap_addr
= (GC_PTR
)ONES
;
823 GC_PTR GC_greatest_plausible_heap_addr
= 0;
828 return(x
> y
? x
: y
);
834 return(x
< y
? x
: y
);
837 # if defined(__STDC__) || defined(__cplusplus)
838 void GC_set_max_heap_size(GC_word n
)
840 void GC_set_max_heap_size(n
)
847 GC_word GC_max_retries
= 0;
850 * this explicitly increases the size of the heap. It is used
851 * internally, but may also be invoked from GC_expand_hp by the user.
852 * The argument is in units of HBLKSIZE.
853 * Tiny values of n are rounded up.
854 * Returns FALSE on failure.
856 GC_bool
GC_expand_hp_inner(n
)
861 word expansion_slop
; /* Number of bytes by which we expect the */
862 /* heap to expand soon. */
864 if (n
< MINHINCR
) n
= MINHINCR
;
865 bytes
= n
* HBLKSIZE
;
866 /* Make sure bytes is a multiple of GC_page_size */
868 word mask
= GC_page_size
- 1;
873 if (GC_max_heapsize
!= 0 && GC_heapsize
+ bytes
> GC_max_heapsize
) {
874 /* Exceeded self-imposed limit */
877 space
= GET_MEM(bytes
);
880 if (GC_print_stats
) {
881 GC_printf1("Failed to expand heap by %ld bytes\n",
882 (unsigned long)bytes
);
888 if (GC_print_stats
) {
889 GC_printf2("Increasing heap size by %lu after %lu allocated bytes\n",
890 (unsigned long)bytes
,
891 (unsigned long)WORDS_TO_BYTES(GC_words_allocd
));
893 GC_printf1("Root size = %lu\n", GC_root_size
);
894 GC_print_block_list(); GC_print_hblkfreelist();
899 expansion_slop
= 8 * WORDS_TO_BYTES(min_words_allocd());
900 if (5 * HBLKSIZE
* MAXHINCR
> expansion_slop
) {
901 expansion_slop
= 5 * HBLKSIZE
* MAXHINCR
;
903 if (GC_last_heap_addr
== 0 && !((word
)space
& SIGNB
)
904 || GC_last_heap_addr
!= 0 && GC_last_heap_addr
< (ptr_t
)space
) {
905 /* Assume the heap is growing up */
906 GC_greatest_plausible_heap_addr
=
907 GC_max(GC_greatest_plausible_heap_addr
,
908 (ptr_t
)space
+ bytes
+ expansion_slop
);
910 /* Heap is growing down */
911 GC_least_plausible_heap_addr
=
912 GC_min(GC_least_plausible_heap_addr
,
913 (ptr_t
)space
- expansion_slop
);
915 GC_prev_heap_addr
= GC_last_heap_addr
;
916 GC_last_heap_addr
= (ptr_t
)space
;
917 GC_add_to_heap(space
, bytes
);
921 /* Really returns a bool, but it's externally visible, so that's clumsy. */
922 /* Arguments is in bytes. */
923 # if defined(__STDC__) || defined(__cplusplus)
924 int GC_expand_hp(size_t bytes
)
926 int GC_expand_hp(bytes
)
935 if (!GC_is_initialized
) GC_init_inner();
936 result
= (int)GC_expand_hp_inner(divHBLKSZ((word
)bytes
));
937 if (result
) GC_requested_heapsize
+= bytes
;
943 unsigned GC_fail_count
= 0;
944 /* How many consecutive GC/expansion failures? */
945 /* Reset by GC_allochblk. */
947 GC_bool
GC_collect_or_expand(needed_blocks
, ignore_off_page
)
949 GC_bool ignore_off_page
;
951 if (!GC_incremental
&& !GC_dont_gc
&&
952 (GC_dont_expand
&& GC_words_allocd
> 0 || GC_should_collect())) {
956 word blocks_to_get
= GC_heapsize
/(HBLKSIZE
*GC_free_space_divisor
)
959 if (blocks_to_get
> MAXHINCR
) {
962 if (ignore_off_page
) {
965 slop
= 2*divHBLKSZ(BL_LIMIT
);
966 if (slop
> needed_blocks
) slop
= needed_blocks
;
968 if (needed_blocks
+ slop
> MAXHINCR
) {
969 blocks_to_get
= needed_blocks
+ slop
;
971 blocks_to_get
= MAXHINCR
;
974 if (!GC_expand_hp_inner(blocks_to_get
)
975 && !GC_expand_hp_inner(needed_blocks
)) {
976 if (GC_fail_count
++ < GC_max_retries
) {
977 WARN("Out of Memory! Trying to continue ...\n", 0);
981 # if !defined(AMIGA) || !defined(GC_AMIGA_FASTALLOC)
982 WARN("Out of Memory! Returning NIL!\n", 0);
988 if (GC_fail_count
&& GC_print_stats
) {
989 GC_printf0("Memory available again ...\n");
998 * Make sure the object free list for sz is not empty.
999 * Return a pointer to the first object on the free list.
1000 * The object MUST BE REMOVED FROM THE FREE LIST BY THE CALLER.
1001 * Assumes we hold the allocator lock and signals are disabled.
1004 ptr_t
GC_allocobj(sz
, kind
)
1008 register ptr_t
* flh
= &(GC_obj_kinds
[kind
].ok_freelist
[sz
]);
1010 if (sz
== 0) return(0);
1014 /* Do our share of marking work */
1015 if(GC_incremental
&& !GC_dont_gc
) GC_collect_a_little_inner(1);
1016 /* Sweep blocks for objects of this size */
1017 GC_continue_reclaim(sz
, kind
);
1020 GC_new_hblk(sz
, kind
);
1024 if (!GC_collect_or_expand((word
)1,FALSE
)) {