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-2000 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 int GC_never_stop_func
GC_PROTO((void)) { return(0); }
102 CLOCK_TYPE GC_start_time
; /* Time at which we stopped world. */
103 /* used only in GC_timeout_stop_func. */
105 int GC_n_attempts
= 0; /* Number of attempts at finishing */
106 /* collection within TIME_LIMIT */
109 # define GC_timeout_stop_func GC_never_stop_func
111 int GC_timeout_stop_func
GC_PROTO((void))
113 CLOCK_TYPE current_time
;
114 static unsigned count
= 0;
115 unsigned long time_diff
;
117 if ((count
++ & 3) != 0) return(0);
119 GET_TIME(current_time
);
120 time_diff
= MS_TIME_DIFF(current_time
,GC_start_time
);
121 if (time_diff
>= TIME_LIMIT
) {
123 if (GC_print_stats
) {
124 GC_printf0("Abandoning stopped marking after ");
125 GC_printf1("%lu msecs", (unsigned long)time_diff
);
126 GC_printf1("(attempt %d)\n", (unsigned long) GC_n_attempts
);
134 #endif /* !SMALL_CONFIG */
136 /* Return the minimum number of words that must be allocated between */
137 /* collections to amortize the collection cost. */
138 static word
min_words_allocd()
141 /* We punt, for now. */
142 register signed_word stack_size
= 10000;
145 register signed_word stack_size
= (ptr_t
)(&dummy
) - GC_stackbottom
;
147 word total_root_size
; /* includes double stack size, */
148 /* since the stack is expensive */
150 word scan_size
; /* Estimate of memory to be scanned */
151 /* during normal GC. */
153 if (stack_size
< 0) stack_size
= -stack_size
;
154 total_root_size
= 2 * stack_size
+ GC_root_size
;
155 scan_size
= BYTES_TO_WORDS(GC_heapsize
- GC_large_free_bytes
156 + (GC_large_free_bytes
>> 2)
157 /* use a bit more of large empty heap */
159 if (GC_incremental
) {
160 return scan_size
/ (2 * GC_free_space_divisor
);
162 return scan_size
/ GC_free_space_divisor
;
166 /* Return the number of words allocated, adjusted for explicit storage */
167 /* management, etc.. This number is used in deciding when to trigger */
169 word
GC_adj_words_allocd()
171 register signed_word result
;
172 register signed_word expl_managed
=
173 BYTES_TO_WORDS((long)GC_non_gc_bytes
174 - (long)GC_non_gc_bytes_at_gc
);
176 /* Don't count what was explicitly freed, or newly allocated for */
177 /* explicit management. Note that deallocating an explicitly */
178 /* managed object should not alter result, assuming the client */
179 /* is playing by the rules. */
180 result
= (signed_word
)GC_words_allocd
181 - (signed_word
)GC_mem_freed
- expl_managed
;
182 if (result
> (signed_word
)GC_words_allocd
) {
183 result
= GC_words_allocd
;
184 /* probably client bug or unfortunate scheduling */
186 result
+= GC_words_finalized
;
187 /* We count objects enqueued for finalization as though they */
188 /* had been reallocated this round. Finalization is user */
189 /* visible progress. And if we don't count this, we have */
190 /* stability problems for programs that finalize all objects. */
191 result
+= GC_words_wasted
;
192 /* This doesn't reflect useful work. But if there is lots of */
193 /* new fragmentation, the same is probably true of the heap, */
194 /* and the collection will be correspondingly cheaper. */
195 if (result
< (signed_word
)(GC_words_allocd
>> 3)) {
196 /* Always count at least 1/8 of the allocations. We don't want */
197 /* to collect too infrequently, since that would inhibit */
198 /* coalescing of free storage blocks. */
199 /* This also makes us partially robust against client bugs. */
200 return(GC_words_allocd
>> 3);
207 /* Clear up a few frames worth of garbage left at the top of the stack. */
208 /* This is used to prevent us from accidentally treating garbade left */
209 /* on the stack by other parts of the collector as roots. This */
210 /* differs from the code in misc.c, which actually tries to keep the */
211 /* stack clear of long-lived, client-generated garbage. */
212 void GC_clear_a_few_frames()
218 for (i
= 0; i
< NWORDS
; i
++) frames
[i
] = 0;
221 /* Have we allocated enough to amortize a collection? */
222 GC_bool
GC_should_collect()
224 return(GC_adj_words_allocd() >= min_words_allocd());
228 void GC_notify_full_gc()
230 if (GC_start_call_back
!= (void (*) GC_PROTO((void)))0) {
231 (*GC_start_call_back
)();
235 GC_bool GC_is_full_gc
= FALSE
;
238 * Initiate a garbage collection if appropriate.
240 * between partial, full, and stop-world collections.
241 * Assumes lock held, signals disabled.
245 static int n_partial_gcs
= 0;
247 if (GC_should_collect()) {
248 if (!GC_incremental
) {
253 } else if (GC_need_full_gc
|| n_partial_gcs
>= GC_full_freq
) {
255 if (GC_print_stats
) {
257 "***>Full mark for collection %lu after %ld allocd bytes\n",
258 (unsigned long) GC_gc_no
+1,
259 (long)WORDS_TO_BYTES(GC_words_allocd
));
262 GC_promote_black_lists();
263 # ifdef PARALLEL_MARK
264 GC_wait_for_reclaim();
266 (void)GC_reclaim_all((GC_stop_func
)0, TRUE
);
270 GC_is_full_gc
= TRUE
;
274 /* We try to mark with the world stopped. */
275 /* If we run out of time, this turns into */
276 /* incremental marking. */
278 GET_TIME(GC_start_time
);
280 if (GC_stopped_mark(GC_timeout_stop_func
)) {
281 # ifdef SAVE_CALL_CHAIN
282 GC_save_callers(GC_last_stack
);
284 GC_finish_collection();
286 if (!GC_is_full_gc
) {
287 /* Count this as the first attempt */
296 * Stop the world garbage collection. Assumes lock held, signals disabled.
297 * If stop_func is not GC_never_stop_func, then abort if stop_func returns TRUE.
299 GC_bool
GC_try_to_collect_inner(stop_func
)
300 GC_stop_func stop_func
;
302 if (GC_incremental
&& GC_collection_in_progress()) {
304 if (GC_print_stats
) {
306 "GC_try_to_collect_inner: finishing collection in progress\n");
308 # endif /* CONDPRINT */
309 /* Just finish collection already in progress. */
310 while(GC_collection_in_progress()) {
311 if (stop_func()) return(FALSE
);
312 GC_collect_a_little_inner(1);
316 if (GC_print_stats
) {
318 "Initiating full world-stop collection %lu after %ld allocd bytes\n",
319 (unsigned long) GC_gc_no
+1,
320 (long)WORDS_TO_BYTES(GC_words_allocd
));
323 GC_promote_black_lists();
324 /* Make sure all blocks have been reclaimed, so sweep routines */
325 /* don't see cleared mark bits. */
326 /* If we're guaranteed to finish, then this is unnecessary. */
327 /* In the find_leak case, we have to finish to guarantee that */
328 /* previously unmarked objects are not reported as leaks. */
329 # ifdef PARALLEL_MARK
330 GC_wait_for_reclaim();
332 if ((GC_find_leak
|| stop_func
!= GC_never_stop_func
)
333 && !GC_reclaim_all(stop_func
, FALSE
)) {
334 /* Aborted. So far everything is still consistent. */
337 GC_invalidate_mark_state(); /* Flush mark stack. */
339 # ifdef SAVE_CALL_CHAIN
340 GC_save_callers(GC_last_stack
);
342 GC_is_full_gc
= TRUE
;
343 if (!GC_stopped_mark(stop_func
)) {
344 if (!GC_incremental
) {
345 /* We're partially done and have no way to complete or use */
346 /* current work. Reestablish invariants as cheaply as */
348 GC_invalidate_mark_state();
349 GC_unpromote_black_lists();
350 } /* else we claim the world is already still consistent. We'll */
351 /* finish incrementally. */
354 GC_finish_collection();
361 * Perform n units of garbage collection work. A unit is intended to touch
362 * roughly GC_RATE pages. Every once in a while, we do more than that.
363 * This needa to be a fairly large number with our current incremental
364 * GC strategy, since otherwise we allocate too much during GC, and the
365 * cleanup gets expensive.
368 # define MAX_PRIOR_ATTEMPTS 1
369 /* Maximum number of prior attempts at world stop marking */
370 /* A value of 1 means that we finish the seconf time, no matter */
371 /* how long it takes. Doesn't count the initial root scan */
374 int GC_deficit
= 0; /* The number of extra calls to GC_mark_some */
375 /* that we have made. */
377 void GC_collect_a_little_inner(n
)
382 if (GC_incremental
&& GC_collection_in_progress()) {
383 for (i
= GC_deficit
; i
< GC_RATE
*n
; i
++) {
384 if (GC_mark_some((ptr_t
)0)) {
385 /* Need to finish a collection */
386 # ifdef SAVE_CALL_CHAIN
387 GC_save_callers(GC_last_stack
);
389 if (GC_n_attempts
< MAX_PRIOR_ATTEMPTS
) {
390 GET_TIME(GC_start_time
);
391 if (!GC_stopped_mark(GC_timeout_stop_func
)) {
396 (void)GC_stopped_mark(GC_never_stop_func
);
398 GC_finish_collection();
402 if (GC_deficit
> 0) GC_deficit
-= GC_RATE
*n
;
403 if (GC_deficit
< 0) GC_deficit
= 0;
409 int GC_collect_a_little
GC_PROTO(())
416 GC_collect_a_little_inner(1);
417 result
= (int)GC_collection_in_progress();
424 * Assumes lock is held, signals are disabled.
426 * If stop_func() ever returns TRUE, we may fail and return FALSE.
427 * Increment GC_gc_no if we succeed.
429 GC_bool
GC_stopped_mark(stop_func
)
430 GC_stop_func stop_func
;
435 CLOCK_TYPE start_time
, current_time
;
440 GET_TIME(start_time
);
443 if (GC_print_stats
) {
444 GC_printf1("--> Marking for collection %lu ",
445 (unsigned long) GC_gc_no
+ 1);
446 GC_printf2("after %lu allocd bytes + %lu wasted bytes\n",
447 (unsigned long) WORDS_TO_BYTES(GC_words_allocd
),
448 (unsigned long) WORDS_TO_BYTES(GC_words_wasted
));
452 /* Mark from all roots. */
453 /* Minimize junk left in my registers and on the stack */
454 GC_clear_a_few_frames();
455 GC_noop(0,0,0,0,0,0);
458 if ((*stop_func
)()) {
460 if (GC_print_stats
) {
461 GC_printf0("Abandoned stopped marking after ");
462 GC_printf1("%lu iterations\n",
466 GC_deficit
= i
; /* Give the mutator a chance. */
470 if (GC_mark_some((ptr_t
)(&dummy
))) break;
475 GC_printf2("Collection %lu reclaimed %ld bytes",
476 (unsigned long) GC_gc_no
- 1,
477 (long)WORDS_TO_BYTES(GC_mem_found
));
480 if (GC_print_stats
) {
481 GC_printf1("Collection %lu finished", (unsigned long) GC_gc_no
- 1);
484 # endif /* !PRINTSTATS */
486 if (GC_print_stats
) {
487 GC_printf1(" ---> heapsize = %lu bytes\n",
488 (unsigned long) GC_heapsize
);
489 /* Printf arguments may be pushed in funny places. Clear the */
493 # endif /* CONDPRINT */
495 /* Check all debugged objects for consistency */
496 if (GC_debugging_started
) {
501 GET_TIME(current_time
);
502 GC_printf1("World-stopped marking took %lu msecs\n",
503 MS_TIME_DIFF(current_time
,start_time
));
510 /* Finish up a collection. Assumes lock is held, signals are disabled, */
511 /* but the world is otherwise running. */
512 void GC_finish_collection()
515 CLOCK_TYPE start_time
;
516 CLOCK_TYPE finalize_time
;
517 CLOCK_TYPE done_time
;
519 GET_TIME(start_time
);
520 finalize_time
= start_time
;
526 # if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
527 if (getenv("GC_PRINT_ADDRESS_MAP") != 0) {
528 GC_print_address_map();
532 /* Mark all objects on the free list. All objects should be */
533 /* marked when we're done. */
535 register word size
; /* current object size */
536 register ptr_t p
; /* pointer to current object */
537 register struct hblk
* h
; /* pointer to block containing *p */
539 register int word_no
; /* "index" of *p in *q */
542 for (kind
= 0; kind
< GC_n_kinds
; kind
++) {
543 for (size
= 1; size
<= MAXOBJSZ
; size
++) {
544 for (p
= GC_obj_kinds
[kind
].ok_freelist
[size
];
545 p
!= 0; p
=obj_link(p
)){
548 word_no
= (((word
*)p
) - ((word
*)h
));
549 set_mark_bit_from_hdr(hhdr
, word_no
);
554 GC_start_reclaim(TRUE
);
555 /* The above just checks; it doesn't really reclaim anything. */
559 # ifdef STUBBORN_ALLOC
560 GC_clean_changing_list();
564 GET_TIME(finalize_time
);
567 /* Clear free list mark bits, in case they got accidentally marked */
568 /* Note: HBLKPTR(p) == pointer to head of block containing *p */
569 /* (or GC_find_leak is set and they were intentionally marked.) */
570 /* Also subtract memory remaining from GC_mem_found count. */
571 /* Note that composite objects on free list are cleared. */
572 /* Thus accidentally marking a free list is not a problem; only */
573 /* objects on the list itself will be marked, and that's fixed here. */
575 register word size
; /* current object size */
576 register ptr_t p
; /* pointer to current object */
577 register struct hblk
* h
; /* pointer to block containing *p */
579 register int word_no
; /* "index" of *p in *q */
582 for (kind
= 0; kind
< GC_n_kinds
; kind
++) {
583 for (size
= 1; size
<= MAXOBJSZ
; size
++) {
584 for (p
= GC_obj_kinds
[kind
].ok_freelist
[size
];
585 p
!= 0; p
=obj_link(p
)){
588 word_no
= (((word
*)p
) - ((word
*)h
));
589 clear_mark_bit_from_hdr(hhdr
, word_no
);
591 GC_mem_found
-= size
;
600 GC_printf1("Bytes recovered before sweep - f.l. count = %ld\n",
601 (long)WORDS_TO_BYTES(GC_mem_found
));
603 /* Reconstruct free lists to contain everything not marked */
604 GC_start_reclaim(FALSE
);
606 GC_used_heap_size_after_full
= USED_HEAP_SIZE
;
607 GC_need_full_gc
= FALSE
;
610 BYTES_TO_WORDS(USED_HEAP_SIZE
- GC_used_heap_size_after_full
)
611 > min_words_allocd();
616 "Immediately reclaimed %ld bytes in heap of size %lu bytes",
617 (long)WORDS_TO_BYTES(GC_mem_found
),
618 (unsigned long)GC_heapsize
);
620 GC_printf1("(%lu unmapped)", GC_unmapped_bytes
);
623 "\n%lu (atomic) + %lu (composite) collectable bytes in use\n",
624 (unsigned long)WORDS_TO_BYTES(GC_atomic_in_use
),
625 (unsigned long)WORDS_TO_BYTES(GC_composite_in_use
));
629 GC_is_full_gc
= FALSE
;
630 /* Reset or increment counters for next cycle */
631 GC_words_allocd_before_gc
+= GC_words_allocd
;
632 GC_non_gc_bytes_at_gc
= GC_non_gc_bytes
;
642 GC_printf2("Finalize + initiate sweep took %lu + %lu msecs\n",
643 MS_TIME_DIFF(finalize_time
,start_time
),
644 MS_TIME_DIFF(done_time
,finalize_time
));
648 /* Externally callable routine to invoke full, stop-world collection */
649 # if defined(__STDC__) || defined(__cplusplus)
650 int GC_try_to_collect(GC_stop_func stop_func
)
652 int GC_try_to_collect(stop_func
)
653 GC_stop_func stop_func
;
659 GC_INVOKE_FINALIZERS();
663 if (!GC_is_initialized
) GC_init_inner();
664 /* Minimize junk left in my registers */
665 GC_noop(0,0,0,0,0,0);
666 result
= (int)GC_try_to_collect_inner(stop_func
);
670 if(result
) GC_INVOKE_FINALIZERS();
674 void GC_gcollect
GC_PROTO(())
677 (void)GC_try_to_collect(GC_never_stop_func
);
680 word GC_n_heap_sects
= 0; /* Number of sections currently in heap. */
683 * Use the chunk of memory starting at p of size bytes as part of the heap.
684 * Assumes p is HBLKSIZE aligned, and bytes is a multiple of HBLKSIZE.
686 void GC_add_to_heap(p
, bytes
)
693 if (GC_n_heap_sects
>= MAX_HEAP_SECTS
) {
694 ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");
696 phdr
= GC_install_header(p
);
698 /* This is extremely unlikely. Can't add it. This will */
699 /* almost certainly result in a 0 return from the allocator, */
700 /* which is entirely appropriate. */
703 GC_heap_sects
[GC_n_heap_sects
].hs_start
= (ptr_t
)p
;
704 GC_heap_sects
[GC_n_heap_sects
].hs_bytes
= bytes
;
706 words
= BYTES_TO_WORDS(bytes
);
707 phdr
-> hb_sz
= words
;
708 phdr
-> hb_map
= (unsigned char *)1; /* A value != GC_invalid_map */
709 phdr
-> hb_flags
= 0;
711 GC_heapsize
+= bytes
;
712 if ((ptr_t
)p
<= (ptr_t
)GC_least_plausible_heap_addr
713 || GC_least_plausible_heap_addr
== 0) {
714 GC_least_plausible_heap_addr
= (GC_PTR
)((ptr_t
)p
- sizeof(word
));
715 /* Making it a little smaller than necessary prevents */
716 /* us from getting a false hit from the variable */
717 /* itself. There's some unintentional reflection */
720 if ((ptr_t
)p
+ bytes
>= (ptr_t
)GC_greatest_plausible_heap_addr
) {
721 GC_greatest_plausible_heap_addr
= (GC_PTR
)((ptr_t
)p
+ bytes
);
725 # if !defined(NO_DEBUGGING)
726 void GC_print_heap_sects()
730 GC_printf1("Total heap size: %lu\n", (unsigned long) GC_heapsize
);
731 for (i
= 0; i
< GC_n_heap_sects
; i
++) {
732 unsigned long start
= (unsigned long) GC_heap_sects
[i
].hs_start
;
733 unsigned long len
= (unsigned long) GC_heap_sects
[i
].hs_bytes
;
737 GC_printf3("Section %ld from 0x%lx to 0x%lx ", (unsigned long)i
,
738 start
, (unsigned long)(start
+ len
));
739 for (h
= (struct hblk
*)start
; h
< (struct hblk
*)(start
+ len
); h
++) {
740 if (GC_is_black_listed(h
, HBLKSIZE
)) nbl
++;
742 GC_printf2("%lu/%lu blacklisted\n", (unsigned long)nbl
,
743 (unsigned long)(len
/HBLKSIZE
));
748 GC_PTR GC_least_plausible_heap_addr
= (GC_PTR
)ONES
;
749 GC_PTR GC_greatest_plausible_heap_addr
= 0;
754 return(x
> y
? x
: y
);
760 return(x
< y
? x
: y
);
763 # if defined(__STDC__) || defined(__cplusplus)
764 void GC_set_max_heap_size(GC_word n
)
766 void GC_set_max_heap_size(n
)
773 GC_word GC_max_retries
= 0;
776 * this explicitly increases the size of the heap. It is used
777 * internally, but may also be invoked from GC_expand_hp by the user.
778 * The argument is in units of HBLKSIZE.
779 * Tiny values of n are rounded up.
780 * Returns FALSE on failure.
782 GC_bool
GC_expand_hp_inner(n
)
787 word expansion_slop
; /* Number of bytes by which we expect the */
788 /* heap to expand soon. */
790 if (n
< MINHINCR
) n
= MINHINCR
;
791 bytes
= n
* HBLKSIZE
;
792 /* Make sure bytes is a multiple of GC_page_size */
794 word mask
= GC_page_size
- 1;
799 if (GC_max_heapsize
!= 0 && GC_heapsize
+ bytes
> GC_max_heapsize
) {
800 /* Exceeded self-imposed limit */
803 space
= GET_MEM(bytes
);
806 if (GC_print_stats
) {
807 GC_printf1("Failed to expand heap by %ld bytes\n",
808 (unsigned long)bytes
);
814 if (GC_print_stats
) {
815 GC_printf2("Increasing heap size by %lu after %lu allocated bytes\n",
816 (unsigned long)bytes
,
817 (unsigned long)WORDS_TO_BYTES(GC_words_allocd
));
819 GC_printf1("Root size = %lu\n", GC_root_size
);
820 GC_print_block_list(); GC_print_hblkfreelist();
825 expansion_slop
= 8 * WORDS_TO_BYTES(min_words_allocd());
826 if (5 * HBLKSIZE
* MAXHINCR
> expansion_slop
) {
827 expansion_slop
= 5 * HBLKSIZE
* MAXHINCR
;
829 if (GC_last_heap_addr
== 0 && !((word
)space
& SIGNB
)
830 || GC_last_heap_addr
!= 0 && GC_last_heap_addr
< (ptr_t
)space
) {
831 /* Assume the heap is growing up */
832 GC_greatest_plausible_heap_addr
=
833 GC_max(GC_greatest_plausible_heap_addr
,
834 (ptr_t
)space
+ bytes
+ expansion_slop
);
836 /* Heap is growing down */
837 GC_least_plausible_heap_addr
=
838 GC_min(GC_least_plausible_heap_addr
,
839 (ptr_t
)space
- expansion_slop
);
841 GC_prev_heap_addr
= GC_last_heap_addr
;
842 GC_last_heap_addr
= (ptr_t
)space
;
843 GC_add_to_heap(space
, bytes
);
847 /* Really returns a bool, but it's externally visible, so that's clumsy. */
848 /* Arguments is in bytes. */
849 # if defined(__STDC__) || defined(__cplusplus)
850 int GC_expand_hp(size_t bytes
)
852 int GC_expand_hp(bytes
)
861 if (!GC_is_initialized
) GC_init_inner();
862 result
= (int)GC_expand_hp_inner(divHBLKSZ((word
)bytes
));
863 if (result
) GC_requested_heapsize
+= bytes
;
869 unsigned GC_fail_count
= 0;
870 /* How many consecutive GC/expansion failures? */
871 /* Reset by GC_allochblk. */
873 GC_bool
GC_collect_or_expand(needed_blocks
, ignore_off_page
)
875 GC_bool ignore_off_page
;
877 if (!GC_incremental
&& !GC_dont_gc
&&
878 (GC_dont_expand
&& GC_words_allocd
> 0 || GC_should_collect())) {
882 word blocks_to_get
= GC_heapsize
/(HBLKSIZE
*GC_free_space_divisor
)
885 if (blocks_to_get
> MAXHINCR
) {
888 if (ignore_off_page
) {
891 slop
= 2*divHBLKSZ(BL_LIMIT
);
892 if (slop
> needed_blocks
) slop
= needed_blocks
;
894 if (needed_blocks
+ slop
> MAXHINCR
) {
895 blocks_to_get
= needed_blocks
+ slop
;
897 blocks_to_get
= MAXHINCR
;
900 if (!GC_expand_hp_inner(blocks_to_get
)
901 && !GC_expand_hp_inner(needed_blocks
)) {
902 if (GC_fail_count
++ < GC_max_retries
) {
903 WARN("Out of Memory! Trying to continue ...\n", 0);
907 # if !defined(AMIGA) || !defined(GC_AMIGA_FASTALLOC)
908 WARN("Out of Memory! Returning NIL!\n", 0);
914 if (GC_fail_count
&& GC_print_stats
) {
915 GC_printf0("Memory available again ...\n");
924 * Make sure the object free list for sz is not empty.
925 * Return a pointer to the first object on the free list.
926 * The object MUST BE REMOVED FROM THE FREE LIST BY THE CALLER.
927 * Assumes we hold the allocator lock and signals are disabled.
930 ptr_t
GC_allocobj(sz
, kind
)
934 register ptr_t
* flh
= &(GC_obj_kinds
[kind
].ok_freelist
[sz
]);
936 if (sz
== 0) return(0);
940 /* Do our share of marking work */
941 if(GC_incremental
&& !GC_dont_gc
) GC_collect_a_little_inner(1);
942 /* Sweep blocks for objects of this size */
943 GC_continue_reclaim(sz
, kind
);
946 GC_new_hblk(sz
, kind
);
950 if (!GC_collect_or_expand((word
)1,FALSE
)) {