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_incremental
&& GC_collection_in_progress()) {
311 if (GC_print_stats
) {
313 "GC_try_to_collect_inner: finishing collection in progress\n");
315 # endif /* CONDPRINT */
316 /* Just finish collection already in progress. */
317 while(GC_collection_in_progress()) {
318 if (stop_func()) return(FALSE
);
319 GC_collect_a_little_inner(1);
323 if (GC_print_stats
) {
325 "Initiating full world-stop collection %lu after %ld allocd bytes\n",
326 (unsigned long) GC_gc_no
+1,
327 (long)WORDS_TO_BYTES(GC_words_allocd
));
330 GC_promote_black_lists();
331 /* Make sure all blocks have been reclaimed, so sweep routines */
332 /* don't see cleared mark bits. */
333 /* If we're guaranteed to finish, then this is unnecessary. */
334 /* In the find_leak case, we have to finish to guarantee that */
335 /* previously unmarked objects are not reported as leaks. */
336 # ifdef PARALLEL_MARK
337 GC_wait_for_reclaim();
339 if ((GC_find_leak
|| stop_func
!= GC_never_stop_func
)
340 && !GC_reclaim_all(stop_func
, FALSE
)) {
341 /* Aborted. So far everything is still consistent. */
344 GC_invalidate_mark_state(); /* Flush mark stack. */
346 # ifdef SAVE_CALL_CHAIN
347 GC_save_callers(GC_last_stack
);
349 GC_is_full_gc
= TRUE
;
350 if (!GC_stopped_mark(stop_func
)) {
351 if (!GC_incremental
) {
352 /* We're partially done and have no way to complete or use */
353 /* current work. Reestablish invariants as cheaply as */
355 GC_invalidate_mark_state();
356 GC_unpromote_black_lists();
357 } /* else we claim the world is already still consistent. We'll */
358 /* finish incrementally. */
361 GC_finish_collection();
368 * Perform n units of garbage collection work. A unit is intended to touch
369 * roughly GC_RATE pages. Every once in a while, we do more than that.
370 * This needa to be a fairly large number with our current incremental
371 * GC strategy, since otherwise we allocate too much during GC, and the
372 * cleanup gets expensive.
375 # define MAX_PRIOR_ATTEMPTS 1
376 /* Maximum number of prior attempts at world stop marking */
377 /* A value of 1 means that we finish the second time, no matter */
378 /* how long it takes. Doesn't count the initial root scan */
381 int GC_deficit
= 0; /* The number of extra calls to GC_mark_some */
382 /* that we have made. */
384 void GC_collect_a_little_inner(n
)
389 if (GC_incremental
&& GC_collection_in_progress()) {
390 for (i
= GC_deficit
; i
< GC_RATE
*n
; i
++) {
391 if (GC_mark_some((ptr_t
)0)) {
392 /* Need to finish a collection */
393 # ifdef SAVE_CALL_CHAIN
394 GC_save_callers(GC_last_stack
);
396 # ifdef PARALLEL_MARK
397 GC_wait_for_reclaim();
399 if (GC_n_attempts
< MAX_PRIOR_ATTEMPTS
400 && GC_time_limit
!= GC_TIME_UNLIMITED
) {
401 GET_TIME(GC_start_time
);
402 if (!GC_stopped_mark(GC_timeout_stop_func
)) {
407 (void)GC_stopped_mark(GC_never_stop_func
);
409 GC_finish_collection();
413 if (GC_deficit
> 0) GC_deficit
-= GC_RATE
*n
;
414 if (GC_deficit
< 0) GC_deficit
= 0;
420 int GC_collect_a_little
GC_PROTO(())
427 GC_collect_a_little_inner(1);
428 result
= (int)GC_collection_in_progress();
435 * Assumes lock is held, signals are disabled.
437 * If stop_func() ever returns TRUE, we may fail and return FALSE.
438 * Increment GC_gc_no if we succeed.
440 GC_bool
GC_stopped_mark(stop_func
)
441 GC_stop_func stop_func
;
445 # if defined(PRINTTIMES) || defined(CONDPRINT)
446 CLOCK_TYPE start_time
, current_time
;
451 GET_TIME(start_time
);
453 # if defined(CONDPRINT) && !defined(PRINTTIMES)
454 if (GC_print_stats
) GET_TIME(start_time
);
457 if (GC_print_stats
) {
458 GC_printf1("--> Marking for collection %lu ",
459 (unsigned long) GC_gc_no
+ 1);
460 GC_printf2("after %lu allocd bytes + %lu wasted bytes\n",
461 (unsigned long) WORDS_TO_BYTES(GC_words_allocd
),
462 (unsigned long) WORDS_TO_BYTES(GC_words_wasted
));
465 # ifdef MAKE_BACK_GRAPH
466 if (GC_print_back_height
) {
467 GC_build_back_graph();
471 /* Mark from all roots. */
472 /* Minimize junk left in my registers and on the stack */
473 GC_clear_a_few_frames();
474 GC_noop(0,0,0,0,0,0);
477 if ((*stop_func
)()) {
479 if (GC_print_stats
) {
480 GC_printf0("Abandoned stopped marking after ");
481 GC_printf1("%lu iterations\n",
485 GC_deficit
= i
; /* Give the mutator a chance. */
489 if (GC_mark_some((ptr_t
)(&dummy
))) break;
494 GC_printf2("Collection %lu reclaimed %ld bytes",
495 (unsigned long) GC_gc_no
- 1,
496 (long)WORDS_TO_BYTES(GC_mem_found
));
499 if (GC_print_stats
) {
500 GC_printf1("Collection %lu finished", (unsigned long) GC_gc_no
- 1);
503 # endif /* !PRINTSTATS */
505 if (GC_print_stats
) {
506 GC_printf1(" ---> heapsize = %lu bytes\n",
507 (unsigned long) GC_heapsize
);
508 /* Printf arguments may be pushed in funny places. Clear the */
512 # endif /* CONDPRINT */
514 /* Check all debugged objects for consistency */
515 if (GC_debugging_started
) {
520 GET_TIME(current_time
);
521 GC_printf1("World-stopped marking took %lu msecs\n",
522 MS_TIME_DIFF(current_time
,start_time
));
525 if (GC_print_stats
) {
526 GET_TIME(current_time
);
527 GC_printf1("World-stopped marking took %lu msecs\n",
528 MS_TIME_DIFF(current_time
,start_time
));
536 /* Set all mark bits for the free list whose first entry is q */
538 void GC_set_fl_marks(ptr_t q
)
540 void GC_set_fl_marks(q
)
545 struct hblk
* h
, * last_h
= 0;
549 for (p
= q
; p
!= 0; p
= obj_link(p
)){
555 word_no
= (((word
*)p
) - ((word
*)h
));
556 set_mark_bit_from_hdr(hhdr
, word_no
);
560 /* Clear all mark bits for the free list whose first entry is q */
561 /* Decrement GC_mem_found by number of words on free list. */
563 void GC_clear_fl_marks(ptr_t q
)
565 void GC_clear_fl_marks(q
)
570 struct hblk
* h
, * last_h
= 0;
574 for (p
= q
; p
!= 0; p
= obj_link(p
)){
580 word_no
= (((word
*)p
) - ((word
*)h
));
581 clear_mark_bit_from_hdr(hhdr
, word_no
);
583 GC_mem_found
-= hhdr
-> hb_sz
;
588 /* Finish up a collection. Assumes lock is held, signals are disabled, */
589 /* but the world is otherwise running. */
590 void GC_finish_collection()
593 CLOCK_TYPE start_time
;
594 CLOCK_TYPE finalize_time
;
595 CLOCK_TYPE done_time
;
597 GET_TIME(start_time
);
598 finalize_time
= start_time
;
604 # if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
605 if (getenv("GC_PRINT_ADDRESS_MAP") != 0) {
606 GC_print_address_map();
610 /* Mark all objects on the free list. All objects should be */
611 /* marked when we're done. */
613 register word size
; /* current object size */
617 for (kind
= 0; kind
< GC_n_kinds
; kind
++) {
618 for (size
= 1; size
<= MAXOBJSZ
; size
++) {
619 q
= GC_obj_kinds
[kind
].ok_freelist
[size
];
620 if (q
!= 0) GC_set_fl_marks(q
);
624 GC_start_reclaim(TRUE
);
625 /* The above just checks; it doesn't really reclaim anything. */
629 # ifdef STUBBORN_ALLOC
630 GC_clean_changing_list();
634 GET_TIME(finalize_time
);
637 if (GC_print_back_height
) {
638 # ifdef MAKE_BACK_GRAPH
639 GC_traverse_back_graph();
641 # ifndef SMALL_CONFIG
642 GC_err_printf0("Back height not available: "
643 "Rebuild collector with -DMAKE_BACK_GRAPH\n");
648 /* Clear free list mark bits, in case they got accidentally marked */
649 /* (or GC_find_leak is set and they were intentionally marked). */
650 /* Also subtract memory remaining from GC_mem_found count. */
651 /* Note that composite objects on free list are cleared. */
652 /* Thus accidentally marking a free list is not a problem; only */
653 /* objects on the list itself will be marked, and that's fixed here. */
655 register word size
; /* current object size */
656 register ptr_t q
; /* pointer to current object */
659 for (kind
= 0; kind
< GC_n_kinds
; kind
++) {
660 for (size
= 1; size
<= MAXOBJSZ
; size
++) {
661 q
= GC_obj_kinds
[kind
].ok_freelist
[size
];
662 if (q
!= 0) GC_clear_fl_marks(q
);
669 GC_printf1("Bytes recovered before sweep - f.l. count = %ld\n",
670 (long)WORDS_TO_BYTES(GC_mem_found
));
672 /* Reconstruct free lists to contain everything not marked */
673 GC_start_reclaim(FALSE
);
675 GC_used_heap_size_after_full
= USED_HEAP_SIZE
;
676 GC_need_full_gc
= FALSE
;
679 BYTES_TO_WORDS(USED_HEAP_SIZE
- GC_used_heap_size_after_full
)
680 > min_words_allocd();
685 "Immediately reclaimed %ld bytes in heap of size %lu bytes",
686 (long)WORDS_TO_BYTES(GC_mem_found
),
687 (unsigned long)GC_heapsize
);
689 GC_printf1("(%lu unmapped)", GC_unmapped_bytes
);
692 "\n%lu (atomic) + %lu (composite) collectable bytes in use\n",
693 (unsigned long)WORDS_TO_BYTES(GC_atomic_in_use
),
694 (unsigned long)WORDS_TO_BYTES(GC_composite_in_use
));
698 GC_is_full_gc
= FALSE
;
699 /* Reset or increment counters for next cycle */
700 GC_words_allocd_before_gc
+= GC_words_allocd
;
701 GC_non_gc_bytes_at_gc
= GC_non_gc_bytes
;
711 GC_printf2("Finalize + initiate sweep took %lu + %lu msecs\n",
712 MS_TIME_DIFF(finalize_time
,start_time
),
713 MS_TIME_DIFF(done_time
,finalize_time
));
717 /* Externally callable routine to invoke full, stop-world collection */
718 # if defined(__STDC__) || defined(__cplusplus)
719 int GC_try_to_collect(GC_stop_func stop_func
)
721 int GC_try_to_collect(stop_func
)
722 GC_stop_func stop_func
;
728 GC_INVOKE_FINALIZERS();
732 if (!GC_is_initialized
) GC_init_inner();
733 /* Minimize junk left in my registers */
734 GC_noop(0,0,0,0,0,0);
735 result
= (int)GC_try_to_collect_inner(stop_func
);
739 if(result
) GC_INVOKE_FINALIZERS();
743 void GC_gcollect
GC_PROTO(())
746 (void)GC_try_to_collect(GC_never_stop_func
);
749 word GC_n_heap_sects
= 0; /* Number of sections currently in heap. */
752 * Use the chunk of memory starting at p of size bytes as part of the heap.
753 * Assumes p is HBLKSIZE aligned, and bytes is a multiple of HBLKSIZE.
755 void GC_add_to_heap(p
, bytes
)
762 if (GC_n_heap_sects
>= MAX_HEAP_SECTS
) {
763 ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");
765 phdr
= GC_install_header(p
);
767 /* This is extremely unlikely. Can't add it. This will */
768 /* almost certainly result in a 0 return from the allocator, */
769 /* which is entirely appropriate. */
772 GC_heap_sects
[GC_n_heap_sects
].hs_start
= (ptr_t
)p
;
773 GC_heap_sects
[GC_n_heap_sects
].hs_bytes
= bytes
;
775 words
= BYTES_TO_WORDS(bytes
);
776 phdr
-> hb_sz
= words
;
777 phdr
-> hb_map
= (unsigned char *)1; /* A value != GC_invalid_map */
778 phdr
-> hb_flags
= 0;
780 GC_heapsize
+= bytes
;
781 if ((ptr_t
)p
<= (ptr_t
)GC_least_plausible_heap_addr
782 || GC_least_plausible_heap_addr
== 0) {
783 GC_least_plausible_heap_addr
= (GC_PTR
)((ptr_t
)p
- sizeof(word
));
784 /* Making it a little smaller than necessary prevents */
785 /* us from getting a false hit from the variable */
786 /* itself. There's some unintentional reflection */
789 if ((ptr_t
)p
+ bytes
>= (ptr_t
)GC_greatest_plausible_heap_addr
) {
790 GC_greatest_plausible_heap_addr
= (GC_PTR
)((ptr_t
)p
+ bytes
);
794 # if !defined(NO_DEBUGGING)
795 void GC_print_heap_sects()
799 GC_printf1("Total heap size: %lu\n", (unsigned long) GC_heapsize
);
800 for (i
= 0; i
< GC_n_heap_sects
; i
++) {
801 unsigned long start
= (unsigned long) GC_heap_sects
[i
].hs_start
;
802 unsigned long len
= (unsigned long) GC_heap_sects
[i
].hs_bytes
;
806 GC_printf3("Section %ld from 0x%lx to 0x%lx ", (unsigned long)i
,
807 start
, (unsigned long)(start
+ len
));
808 for (h
= (struct hblk
*)start
; h
< (struct hblk
*)(start
+ len
); h
++) {
809 if (GC_is_black_listed(h
, HBLKSIZE
)) nbl
++;
811 GC_printf2("%lu/%lu blacklisted\n", (unsigned long)nbl
,
812 (unsigned long)(len
/HBLKSIZE
));
817 GC_PTR GC_least_plausible_heap_addr
= (GC_PTR
)ONES
;
818 GC_PTR GC_greatest_plausible_heap_addr
= 0;
823 return(x
> y
? x
: y
);
829 return(x
< y
? x
: y
);
832 # if defined(__STDC__) || defined(__cplusplus)
833 void GC_set_max_heap_size(GC_word n
)
835 void GC_set_max_heap_size(n
)
842 GC_word GC_max_retries
= 0;
845 * this explicitly increases the size of the heap. It is used
846 * internally, but may also be invoked from GC_expand_hp by the user.
847 * The argument is in units of HBLKSIZE.
848 * Tiny values of n are rounded up.
849 * Returns FALSE on failure.
851 GC_bool
GC_expand_hp_inner(n
)
856 word expansion_slop
; /* Number of bytes by which we expect the */
857 /* heap to expand soon. */
859 if (n
< MINHINCR
) n
= MINHINCR
;
860 bytes
= n
* HBLKSIZE
;
861 /* Make sure bytes is a multiple of GC_page_size */
863 word mask
= GC_page_size
- 1;
868 if (GC_max_heapsize
!= 0 && GC_heapsize
+ bytes
> GC_max_heapsize
) {
869 /* Exceeded self-imposed limit */
872 space
= GET_MEM(bytes
);
875 if (GC_print_stats
) {
876 GC_printf1("Failed to expand heap by %ld bytes\n",
877 (unsigned long)bytes
);
883 if (GC_print_stats
) {
884 GC_printf2("Increasing heap size by %lu after %lu allocated bytes\n",
885 (unsigned long)bytes
,
886 (unsigned long)WORDS_TO_BYTES(GC_words_allocd
));
888 GC_printf1("Root size = %lu\n", GC_root_size
);
889 GC_print_block_list(); GC_print_hblkfreelist();
894 expansion_slop
= 8 * WORDS_TO_BYTES(min_words_allocd());
895 if (5 * HBLKSIZE
* MAXHINCR
> expansion_slop
) {
896 expansion_slop
= 5 * HBLKSIZE
* MAXHINCR
;
898 if (GC_last_heap_addr
== 0 && !((word
)space
& SIGNB
)
899 || GC_last_heap_addr
!= 0 && GC_last_heap_addr
< (ptr_t
)space
) {
900 /* Assume the heap is growing up */
901 GC_greatest_plausible_heap_addr
=
902 GC_max(GC_greatest_plausible_heap_addr
,
903 (ptr_t
)space
+ bytes
+ expansion_slop
);
905 /* Heap is growing down */
906 GC_least_plausible_heap_addr
=
907 GC_min(GC_least_plausible_heap_addr
,
908 (ptr_t
)space
- expansion_slop
);
910 GC_prev_heap_addr
= GC_last_heap_addr
;
911 GC_last_heap_addr
= (ptr_t
)space
;
912 GC_add_to_heap(space
, bytes
);
916 /* Really returns a bool, but it's externally visible, so that's clumsy. */
917 /* Arguments is in bytes. */
918 # if defined(__STDC__) || defined(__cplusplus)
919 int GC_expand_hp(size_t bytes
)
921 int GC_expand_hp(bytes
)
930 if (!GC_is_initialized
) GC_init_inner();
931 result
= (int)GC_expand_hp_inner(divHBLKSZ((word
)bytes
));
932 if (result
) GC_requested_heapsize
+= bytes
;
938 unsigned GC_fail_count
= 0;
939 /* How many consecutive GC/expansion failures? */
940 /* Reset by GC_allochblk. */
942 GC_bool
GC_collect_or_expand(needed_blocks
, ignore_off_page
)
944 GC_bool ignore_off_page
;
946 if (!GC_incremental
&& !GC_dont_gc
&&
947 (GC_dont_expand
&& GC_words_allocd
> 0 || GC_should_collect())) {
951 word blocks_to_get
= GC_heapsize
/(HBLKSIZE
*GC_free_space_divisor
)
954 if (blocks_to_get
> MAXHINCR
) {
957 if (ignore_off_page
) {
960 slop
= 2*divHBLKSZ(BL_LIMIT
);
961 if (slop
> needed_blocks
) slop
= needed_blocks
;
963 if (needed_blocks
+ slop
> MAXHINCR
) {
964 blocks_to_get
= needed_blocks
+ slop
;
966 blocks_to_get
= MAXHINCR
;
969 if (!GC_expand_hp_inner(blocks_to_get
)
970 && !GC_expand_hp_inner(needed_blocks
)) {
971 if (GC_fail_count
++ < GC_max_retries
) {
972 WARN("Out of Memory! Trying to continue ...\n", 0);
976 # if !defined(AMIGA) || !defined(GC_AMIGA_FASTALLOC)
977 WARN("Out of Memory! Returning NIL!\n", 0);
983 if (GC_fail_count
&& GC_print_stats
) {
984 GC_printf0("Memory available again ...\n");
993 * Make sure the object free list for sz is not empty.
994 * Return a pointer to the first object on the free list.
995 * The object MUST BE REMOVED FROM THE FREE LIST BY THE CALLER.
996 * Assumes we hold the allocator lock and signals are disabled.
999 ptr_t
GC_allocobj(sz
, kind
)
1003 register ptr_t
* flh
= &(GC_obj_kinds
[kind
].ok_freelist
[sz
]);
1005 if (sz
== 0) return(0);
1009 /* Do our share of marking work */
1010 if(GC_incremental
&& !GC_dont_gc
) GC_collect_a_little_inner(1);
1011 /* Sweep blocks for objects of this size */
1012 GC_continue_reclaim(sz
, kind
);
1015 GC_new_hblk(sz
, kind
);
1019 if (!GC_collect_or_expand((word
)1,FALSE
)) {