* toplev.c (lang_independent_f_options): Remove
[official-gcc.git] / boehm-gc / alloc.c
blob7b923885b96cf35201e105f9d6effc83cc0a6177
1 /*
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 "gc_priv.h"
21 # include <stdio.h>
22 # ifndef MACOS
23 # include <signal.h>
24 # include <sys/types.h>
25 # endif
28 * Separate free lists are maintained for different sized objects
29 * up to MAXOBJSZ.
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);
38 * ptr = *opp;
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
44 * their first word.
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
55 * allocator.
58 word GC_non_gc_bytes = 0; /* Number of bytes not intended to be collected */
60 word GC_gc_no = 0;
62 #ifndef SMALL_CONFIG
63 int GC_incremental = 0; /* By default, stop the world. */
64 #endif
66 int GC_full_freq = 19; /* Every 20th collection is a full */
67 /* collection, whether we need it */
68 /* or not. */
70 GC_bool GC_need_full_gc = FALSE;
71 /* Need full GC do to heap growth. */
73 word GC_used_heap_size_after_full = 0;
75 char * GC_copyright[] =
76 {"Copyright 1988,1989 Hans-J. Boehm and Alan J. Demers ",
77 "Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved. ",
78 "Copyright (c) 1996-1998 by Silicon Graphics. All rights reserved. ",
79 "THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY",
80 " EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.",
81 "See source code for details." };
83 # include "version.h"
85 /* some more variables */
87 extern signed_word GC_mem_found; /* Number of reclaimed longwords */
88 /* after garbage collection */
90 GC_bool GC_dont_expand = 0;
92 word GC_free_space_divisor = 3;
94 extern GC_bool GC_collection_in_progress();
95 /* Collection is in progress, or was abandoned. */
97 int GC_never_stop_func GC_PROTO((void)) { return(0); }
99 CLOCK_TYPE GC_start_time; /* Time at which we stopped world. */
100 /* used only in GC_timeout_stop_func. */
102 int GC_n_attempts = 0; /* Number of attempts at finishing */
103 /* collection within TIME_LIMIT */
105 #ifdef SMALL_CONFIG
106 # define GC_timeout_stop_func GC_never_stop_func
107 #else
108 int GC_timeout_stop_func GC_PROTO((void))
110 CLOCK_TYPE current_time;
111 static unsigned count = 0;
112 unsigned long time_diff;
114 if ((count++ & 3) != 0) return(0);
115 #ifndef NO_CLOCK
116 GET_TIME(current_time);
117 time_diff = MS_TIME_DIFF(current_time,GC_start_time);
118 if (time_diff >= TIME_LIMIT) {
119 # ifdef PRINTSTATS
120 GC_printf0("Abandoning stopped marking after ");
121 GC_printf1("%lu msecs", (unsigned long)time_diff);
122 GC_printf1("(attempt %d)\n", (unsigned long) GC_n_attempts);
123 # endif
124 return(1);
126 #endif
127 return(0);
129 #endif /* !SMALL_CONFIG */
131 /* Return the minimum number of words that must be allocated between */
132 /* collections to amortize the collection cost. */
133 static word min_words_allocd()
135 # ifdef THREADS
136 /* We punt, for now. */
137 register signed_word stack_size = 10000;
138 # else
139 int dummy;
140 register signed_word stack_size = (ptr_t)(&dummy) - GC_stackbottom;
141 # endif
142 word total_root_size; /* includes double stack size, */
143 /* since the stack is expensive */
144 /* to scan. */
145 word scan_size; /* Estimate of memory to be scanned */
146 /* during normal GC. */
148 if (stack_size < 0) stack_size = -stack_size;
149 total_root_size = 2 * stack_size + GC_root_size;
150 scan_size = BYTES_TO_WORDS(GC_heapsize - GC_large_free_bytes
151 + (GC_large_free_bytes >> 2)
152 /* use a bit more of large empty heap */
153 + total_root_size);
154 if (GC_incremental) {
155 return scan_size / (2 * GC_free_space_divisor);
156 } else {
157 return scan_size / GC_free_space_divisor;
161 /* Return the number of words allocated, adjusted for explicit storage */
162 /* management, etc.. This number is used in deciding when to trigger */
163 /* collections. */
164 word GC_adj_words_allocd()
166 register signed_word result;
167 register signed_word expl_managed =
168 BYTES_TO_WORDS((long)GC_non_gc_bytes
169 - (long)GC_non_gc_bytes_at_gc);
171 /* Don't count what was explicitly freed, or newly allocated for */
172 /* explicit management. Note that deallocating an explicitly */
173 /* managed object should not alter result, assuming the client */
174 /* is playing by the rules. */
175 result = (signed_word)GC_words_allocd
176 - (signed_word)GC_mem_freed - expl_managed;
177 if (result > (signed_word)GC_words_allocd) {
178 result = GC_words_allocd;
179 /* probably client bug or unfortunate scheduling */
181 result += GC_words_finalized;
182 /* We count objects enqueued for finalization as though they */
183 /* had been reallocated this round. Finalization is user */
184 /* visible progress. And if we don't count this, we have */
185 /* stability problems for programs that finalize all objects. */
186 result += GC_words_wasted;
187 /* This doesn't reflect useful work. But if there is lots of */
188 /* new fragmentation, the same is probably true of the heap, */
189 /* and the collection will be correspondingly cheaper. */
190 if (result < (signed_word)(GC_words_allocd >> 3)) {
191 /* Always count at least 1/8 of the allocations. We don't want */
192 /* to collect too infrequently, since that would inhibit */
193 /* coalescing of free storage blocks. */
194 /* This also makes us partially robust against client bugs. */
195 return(GC_words_allocd >> 3);
196 } else {
197 return(result);
202 /* Clear up a few frames worth of garbage left at the top of the stack. */
203 /* This is used to prevent us from accidentally treating garbade left */
204 /* on the stack by other parts of the collector as roots. This */
205 /* differs from the code in misc.c, which actually tries to keep the */
206 /* stack clear of long-lived, client-generated garbage. */
207 void GC_clear_a_few_frames()
209 # define NWORDS 64
210 word frames[NWORDS];
211 register int i;
213 for (i = 0; i < NWORDS; i++) frames[i] = 0;
216 /* Have we allocated enough to amortize a collection? */
217 GC_bool GC_should_collect()
219 return(GC_adj_words_allocd() >= min_words_allocd());
223 void GC_notify_full_gc()
225 if (GC_start_call_back != (void (*)())0) {
226 (*GC_start_call_back)();
230 GC_bool GC_is_full_gc = FALSE;
233 * Initiate a garbage collection if appropriate.
234 * Choose judiciously
235 * between partial, full, and stop-world collections.
236 * Assumes lock held, signals disabled.
238 void GC_maybe_gc()
240 static int n_partial_gcs = 0;
242 if (GC_should_collect()) {
243 if (!GC_incremental) {
244 GC_notify_full_gc();
245 GC_gcollect_inner();
246 n_partial_gcs = 0;
247 return;
248 } else if (GC_need_full_gc || n_partial_gcs >= GC_full_freq) {
249 # ifdef PRINTSTATS
250 GC_printf2(
251 "***>Full mark for collection %lu after %ld allocd bytes\n",
252 (unsigned long) GC_gc_no+1,
253 (long)WORDS_TO_BYTES(GC_words_allocd));
254 # endif
255 GC_promote_black_lists();
256 (void)GC_reclaim_all((GC_stop_func)0, TRUE);
257 GC_clear_marks();
258 n_partial_gcs = 0;
259 GC_notify_full_gc();
260 GC_is_full_gc = TRUE;
261 } else {
262 n_partial_gcs++;
264 /* We try to mark with the world stopped. */
265 /* If we run out of time, this turns into */
266 /* incremental marking. */
267 #ifndef NO_CLOCK
268 GET_TIME(GC_start_time);
269 #endif
270 if (GC_stopped_mark(GC_timeout_stop_func)) {
271 # ifdef SAVE_CALL_CHAIN
272 GC_save_callers(GC_last_stack);
273 # endif
274 GC_finish_collection();
275 } else {
276 if (!GC_is_full_gc) {
277 /* Count this as the first attempt */
278 GC_n_attempts++;
286 * Stop the world garbage collection. Assumes lock held, signals disabled.
287 * If stop_func is not GC_never_stop_func, then abort if stop_func returns TRUE.
289 GC_bool GC_try_to_collect_inner(stop_func)
290 GC_stop_func stop_func;
292 if (GC_incremental && GC_collection_in_progress()) {
293 # ifdef PRINTSTATS
294 GC_printf0(
295 "GC_try_to_collect_inner: finishing collection in progress\n");
296 # endif /* PRINTSTATS */
297 /* Just finish collection already in progress. */
298 while(GC_collection_in_progress()) {
299 if (stop_func()) return(FALSE);
300 GC_collect_a_little_inner(1);
303 # ifdef PRINTSTATS
304 GC_printf2(
305 "Initiating full world-stop collection %lu after %ld allocd bytes\n",
306 (unsigned long) GC_gc_no+1,
307 (long)WORDS_TO_BYTES(GC_words_allocd));
308 # endif
309 GC_promote_black_lists();
310 /* Make sure all blocks have been reclaimed, so sweep routines */
311 /* don't see cleared mark bits. */
312 /* If we're guaranteed to finish, then this is unnecessary. */
313 if (stop_func != GC_never_stop_func
314 && !GC_reclaim_all(stop_func, FALSE)) {
315 /* Aborted. So far everything is still consistent. */
316 return(FALSE);
318 GC_invalidate_mark_state(); /* Flush mark stack. */
319 GC_clear_marks();
320 # ifdef SAVE_CALL_CHAIN
321 GC_save_callers(GC_last_stack);
322 # endif
323 GC_is_full_gc = TRUE;
324 if (!GC_stopped_mark(stop_func)) {
325 if (!GC_incremental) {
326 /* We're partially done and have no way to complete or use */
327 /* current work. Reestablish invariants as cheaply as */
328 /* possible. */
329 GC_invalidate_mark_state();
330 GC_unpromote_black_lists();
331 } /* else we claim the world is already still consistent. We'll */
332 /* finish incrementally. */
333 return(FALSE);
335 GC_finish_collection();
336 return(TRUE);
342 * Perform n units of garbage collection work. A unit is intended to touch
343 * roughly GC_RATE pages. Every once in a while, we do more than that.
344 * This needa to be a fairly large number with our current incremental
345 * GC strategy, since otherwise we allocate too much during GC, and the
346 * cleanup gets expensive.
348 # define GC_RATE 10
349 # define MAX_PRIOR_ATTEMPTS 1
350 /* Maximum number of prior attempts at world stop marking */
351 /* A value of 1 means that we finish the seconf time, no matter */
352 /* how long it takes. Doesn't count the initial root scan */
353 /* for a full GC. */
355 int GC_deficit = 0; /* The number of extra calls to GC_mark_some */
356 /* that we have made. */
358 void GC_collect_a_little_inner(n)
359 int n;
361 register int i;
363 if (GC_incremental && GC_collection_in_progress()) {
364 for (i = GC_deficit; i < GC_RATE*n; i++) {
365 if (GC_mark_some((ptr_t)0)) {
366 /* Need to finish a collection */
367 # ifdef SAVE_CALL_CHAIN
368 GC_save_callers(GC_last_stack);
369 # endif
370 if (GC_n_attempts < MAX_PRIOR_ATTEMPTS) {
371 GET_TIME(GC_start_time);
372 if (!GC_stopped_mark(GC_timeout_stop_func)) {
373 GC_n_attempts++;
374 break;
376 } else {
377 (void)GC_stopped_mark(GC_never_stop_func);
379 GC_finish_collection();
380 break;
383 if (GC_deficit > 0) GC_deficit -= GC_RATE*n;
384 if (GC_deficit < 0) GC_deficit = 0;
385 } else {
386 GC_maybe_gc();
390 int GC_collect_a_little GC_PROTO(())
392 int result;
393 DCL_LOCK_STATE;
395 DISABLE_SIGNALS();
396 LOCK();
397 GC_collect_a_little_inner(1);
398 result = (int)GC_collection_in_progress();
399 UNLOCK();
400 ENABLE_SIGNALS();
401 return(result);
405 * Assumes lock is held, signals are disabled.
406 * We stop the world.
407 * If stop_func() ever returns TRUE, we may fail and return FALSE.
408 * Increment GC_gc_no if we succeed.
410 GC_bool GC_stopped_mark(stop_func)
411 GC_stop_func stop_func;
413 register int i;
414 int dummy;
415 # ifdef PRINTSTATS
416 CLOCK_TYPE start_time, current_time;
417 # endif
419 STOP_WORLD();
420 # ifdef PRINTSTATS
421 GET_TIME(start_time);
422 GC_printf1("--> Marking for collection %lu ",
423 (unsigned long) GC_gc_no + 1);
424 GC_printf2("after %lu allocd bytes + %lu wasted bytes\n",
425 (unsigned long) WORDS_TO_BYTES(GC_words_allocd),
426 (unsigned long) WORDS_TO_BYTES(GC_words_wasted));
427 # endif
429 /* Mark from all roots. */
430 /* Minimize junk left in my registers and on the stack */
431 GC_clear_a_few_frames();
432 GC_noop(0,0,0,0,0,0);
433 GC_initiate_gc();
434 for(i = 0;;i++) {
435 if ((*stop_func)()) {
436 # ifdef PRINTSTATS
437 GC_printf0("Abandoned stopped marking after ");
438 GC_printf1("%lu iterations\n",
439 (unsigned long)i);
440 # endif
441 GC_deficit = i; /* Give the mutator a chance. */
442 START_WORLD();
443 return(FALSE);
445 if (GC_mark_some((ptr_t)(&dummy))) break;
448 GC_gc_no++;
449 # ifdef PRINTSTATS
450 GC_printf2("Collection %lu reclaimed %ld bytes",
451 (unsigned long) GC_gc_no - 1,
452 (long)WORDS_TO_BYTES(GC_mem_found));
453 GC_printf1(" ---> heapsize = %lu bytes\n",
454 (unsigned long) GC_heapsize);
455 /* Printf arguments may be pushed in funny places. Clear the */
456 /* space. */
457 GC_printf0("");
458 # endif
460 /* Check all debugged objects for consistency */
461 if (GC_debugging_started) {
462 (*GC_check_heap)();
465 # ifdef PRINTTIMES
466 GET_TIME(current_time);
467 GC_printf1("World-stopped marking took %lu msecs\n",
468 MS_TIME_DIFF(current_time,start_time));
469 # endif
470 START_WORLD();
471 return(TRUE);
475 /* Finish up a collection. Assumes lock is held, signals are disabled, */
476 /* but the world is otherwise running. */
477 void GC_finish_collection()
479 # ifdef PRINTTIMES
480 CLOCK_TYPE start_time;
481 CLOCK_TYPE finalize_time;
482 CLOCK_TYPE done_time;
484 GET_TIME(start_time);
485 finalize_time = start_time;
486 # endif
488 # ifdef GATHERSTATS
489 GC_mem_found = 0;
490 # endif
491 if (GC_find_leak) {
492 /* Mark all objects on the free list. All objects should be */
493 /* marked when we're done. */
495 register word size; /* current object size */
496 register ptr_t p; /* pointer to current object */
497 register struct hblk * h; /* pointer to block containing *p */
498 register hdr * hhdr;
499 register int word_no; /* "index" of *p in *q */
500 int kind;
502 for (kind = 0; kind < GC_n_kinds; kind++) {
503 for (size = 1; size <= MAXOBJSZ; size++) {
504 for (p= GC_obj_kinds[kind].ok_freelist[size];
505 p != 0; p=obj_link(p)){
506 h = HBLKPTR(p);
507 hhdr = HDR(h);
508 word_no = (((word *)p) - ((word *)h));
509 set_mark_bit_from_hdr(hhdr, word_no);
514 GC_start_reclaim(TRUE);
515 /* The above just checks; it doesn't really reclaim anything. */
518 GC_finalize();
519 # ifdef STUBBORN_ALLOC
520 GC_clean_changing_list();
521 # endif
523 # ifdef PRINTTIMES
524 GET_TIME(finalize_time);
525 # endif
527 /* Clear free list mark bits, in case they got accidentally marked */
528 /* Note: HBLKPTR(p) == pointer to head of block containing *p */
529 /* (or GC_find_leak is set and they were intentionally marked.) */
530 /* Also subtract memory remaining from GC_mem_found count. */
531 /* Note that composite objects on free list are cleared. */
532 /* Thus accidentally marking a free list is not a problem; only */
533 /* objects on the list itself will be marked, and that's fixed here. */
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 */
538 register hdr * hhdr;
539 register int word_no; /* "index" of *p in *q */
540 int kind;
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)){
546 h = HBLKPTR(p);
547 hhdr = HDR(h);
548 word_no = (((word *)p) - ((word *)h));
549 clear_mark_bit_from_hdr(hhdr, word_no);
550 # ifdef GATHERSTATS
551 GC_mem_found -= size;
552 # endif
559 # ifdef PRINTSTATS
560 GC_printf1("Bytes recovered before sweep - f.l. count = %ld\n",
561 (long)WORDS_TO_BYTES(GC_mem_found));
562 # endif
563 /* Reconstruct free lists to contain everything not marked */
564 GC_start_reclaim(FALSE);
565 if (GC_is_full_gc) {
566 GC_used_heap_size_after_full = USED_HEAP_SIZE;
567 GC_need_full_gc = FALSE;
568 } else {
569 GC_need_full_gc =
570 BYTES_TO_WORDS(USED_HEAP_SIZE - GC_used_heap_size_after_full)
571 > min_words_allocd();
574 # ifdef PRINTSTATS
575 GC_printf2(
576 "Immediately reclaimed %ld bytes in heap of size %lu bytes",
577 (long)WORDS_TO_BYTES(GC_mem_found),
578 (unsigned long)GC_heapsize);
579 # ifdef USE_MUNMAP
580 GC_printf1("(%lu unmapped)", GC_unmapped_bytes);
581 # endif
582 GC_printf2(
583 "\n%lu (atomic) + %lu (composite) collectable bytes in use\n",
584 (unsigned long)WORDS_TO_BYTES(GC_atomic_in_use),
585 (unsigned long)WORDS_TO_BYTES(GC_composite_in_use));
586 # endif
588 GC_n_attempts = 0;
589 GC_is_full_gc = FALSE;
590 /* Reset or increment counters for next cycle */
591 GC_words_allocd_before_gc += GC_words_allocd;
592 GC_non_gc_bytes_at_gc = GC_non_gc_bytes;
593 GC_words_allocd = 0;
594 GC_words_wasted = 0;
595 GC_mem_freed = 0;
597 # ifdef USE_MUNMAP
598 GC_unmap_old();
599 # endif
600 # ifdef PRINTTIMES
601 GET_TIME(done_time);
602 GC_printf2("Finalize + initiate sweep took %lu + %lu msecs\n",
603 MS_TIME_DIFF(finalize_time,start_time),
604 MS_TIME_DIFF(done_time,finalize_time));
605 # endif
608 /* Externally callable routine to invoke full, stop-world collection */
609 # if defined(__STDC__) || defined(__cplusplus)
610 int GC_try_to_collect(GC_stop_func stop_func)
611 # else
612 int GC_try_to_collect(stop_func)
613 GC_stop_func stop_func;
614 # endif
616 int result;
617 DCL_LOCK_STATE;
619 GC_INVOKE_FINALIZERS();
620 DISABLE_SIGNALS();
621 LOCK();
622 ENTER_GC();
623 if (!GC_is_initialized) GC_init_inner();
624 /* Minimize junk left in my registers */
625 GC_noop(0,0,0,0,0,0);
626 result = (int)GC_try_to_collect_inner(stop_func);
627 EXIT_GC();
628 UNLOCK();
629 ENABLE_SIGNALS();
630 if(result) GC_INVOKE_FINALIZERS();
631 return(result);
634 void GC_gcollect GC_PROTO(())
636 GC_notify_full_gc();
637 (void)GC_try_to_collect(GC_never_stop_func);
640 word GC_n_heap_sects = 0; /* Number of sections currently in heap. */
643 * Use the chunk of memory starting at p of size bytes as part of the heap.
644 * Assumes p is HBLKSIZE aligned, and bytes is a multiple of HBLKSIZE.
646 void GC_add_to_heap(p, bytes)
647 struct hblk *p;
648 word bytes;
650 word words;
651 hdr * phdr;
653 if (GC_n_heap_sects >= MAX_HEAP_SECTS) {
654 ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");
656 phdr = GC_install_header(p);
657 if (0 == phdr) {
658 /* This is extremely unlikely. Can't add it. This will */
659 /* almost certainly result in a 0 return from the allocator, */
660 /* which is entirely appropriate. */
661 return;
663 GC_heap_sects[GC_n_heap_sects].hs_start = (ptr_t)p;
664 GC_heap_sects[GC_n_heap_sects].hs_bytes = bytes;
665 GC_n_heap_sects++;
666 words = BYTES_TO_WORDS(bytes - HDR_BYTES);
667 phdr -> hb_sz = words;
668 phdr -> hb_map = (char *)1; /* A value != GC_invalid_map */
669 phdr -> hb_flags = 0;
670 GC_freehblk(p);
671 GC_heapsize += bytes;
672 if ((ptr_t)p <= GC_least_plausible_heap_addr
673 || GC_least_plausible_heap_addr == 0) {
674 GC_least_plausible_heap_addr = (ptr_t)p - sizeof(word);
675 /* Making it a little smaller than necessary prevents */
676 /* us from getting a false hit from the variable */
677 /* itself. There's some unintentional reflection */
678 /* here. */
680 if ((ptr_t)p + bytes >= GC_greatest_plausible_heap_addr) {
681 GC_greatest_plausible_heap_addr = (ptr_t)p + bytes;
685 # if !defined(NO_DEBUGGING)
686 void GC_print_heap_sects()
688 register unsigned i;
690 GC_printf1("Total heap size: %lu\n", (unsigned long) GC_heapsize);
691 for (i = 0; i < GC_n_heap_sects; i++) {
692 unsigned long start = (unsigned long) GC_heap_sects[i].hs_start;
693 unsigned long len = (unsigned long) GC_heap_sects[i].hs_bytes;
694 struct hblk *h;
695 unsigned nbl = 0;
697 GC_printf3("Section %ld from 0x%lx to 0x%lx ", (unsigned long)i,
698 start, (unsigned long)(start + len));
699 for (h = (struct hblk *)start; h < (struct hblk *)(start + len); h++) {
700 if (GC_is_black_listed(h, HBLKSIZE)) nbl++;
702 GC_printf2("%lu/%lu blacklisted\n", (unsigned long)nbl,
703 (unsigned long)(len/HBLKSIZE));
706 # endif
708 ptr_t GC_least_plausible_heap_addr = (ptr_t)ONES;
709 ptr_t GC_greatest_plausible_heap_addr = 0;
711 ptr_t GC_max(x,y)
712 ptr_t x, y;
714 return(x > y? x : y);
717 ptr_t GC_min(x,y)
718 ptr_t x, y;
720 return(x < y? x : y);
723 # if defined(__STDC__) || defined(__cplusplus)
724 void GC_set_max_heap_size(GC_word n)
725 # else
726 void GC_set_max_heap_size(n)
727 GC_word n;
728 # endif
730 GC_max_heapsize = n;
733 GC_word GC_max_retries = 0;
736 * this explicitly increases the size of the heap. It is used
737 * internally, but may also be invoked from GC_expand_hp by the user.
738 * The argument is in units of HBLKSIZE.
739 * Tiny values of n are rounded up.
740 * Returns FALSE on failure.
742 GC_bool GC_expand_hp_inner(n)
743 word n;
745 word bytes;
746 struct hblk * space;
747 word expansion_slop; /* Number of bytes by which we expect the */
748 /* heap to expand soon. */
750 if (n < MINHINCR) n = MINHINCR;
751 bytes = n * HBLKSIZE;
752 /* Make sure bytes is a multiple of GC_page_size */
754 word mask = GC_page_size - 1;
755 bytes += mask;
756 bytes &= ~mask;
759 if (GC_max_heapsize != 0 && GC_heapsize + bytes > GC_max_heapsize) {
760 /* Exceeded self-imposed limit */
761 return(FALSE);
763 space = GET_MEM(bytes);
764 if( space == 0 ) {
765 return(FALSE);
767 # ifdef PRINTSTATS
768 GC_printf2("Increasing heap size by %lu after %lu allocated bytes\n",
769 (unsigned long)bytes,
770 (unsigned long)WORDS_TO_BYTES(GC_words_allocd));
771 # ifdef UNDEFINED
772 GC_printf1("Root size = %lu\n", GC_root_size);
773 GC_print_block_list(); GC_print_hblkfreelist();
774 GC_printf0("\n");
775 # endif
776 # endif
777 expansion_slop = 8 * WORDS_TO_BYTES(min_words_allocd());
778 if (5 * HBLKSIZE * MAXHINCR > expansion_slop) {
779 expansion_slop = 5 * HBLKSIZE * MAXHINCR;
781 if (GC_last_heap_addr == 0 && !((word)space & SIGNB)
782 || GC_last_heap_addr != 0 && GC_last_heap_addr < (ptr_t)space) {
783 /* Assume the heap is growing up */
784 GC_greatest_plausible_heap_addr =
785 GC_max(GC_greatest_plausible_heap_addr,
786 (ptr_t)space + bytes + expansion_slop);
787 } else {
788 /* Heap is growing down */
789 GC_least_plausible_heap_addr =
790 GC_min(GC_least_plausible_heap_addr,
791 (ptr_t)space - expansion_slop);
793 GC_prev_heap_addr = GC_last_heap_addr;
794 GC_last_heap_addr = (ptr_t)space;
795 GC_add_to_heap(space, bytes);
796 return(TRUE);
799 /* Really returns a bool, but it's externally visible, so that's clumsy. */
800 /* Arguments is in bytes. */
801 # if defined(__STDC__) || defined(__cplusplus)
802 int GC_expand_hp(size_t bytes)
803 # else
804 int GC_expand_hp(bytes)
805 size_t bytes;
806 # endif
808 int result;
809 DCL_LOCK_STATE;
811 DISABLE_SIGNALS();
812 LOCK();
813 if (!GC_is_initialized) GC_init_inner();
814 result = (int)GC_expand_hp_inner(divHBLKSZ((word)bytes));
815 if (result) GC_requested_heapsize += bytes;
816 UNLOCK();
817 ENABLE_SIGNALS();
818 return(result);
821 unsigned GC_fail_count = 0;
822 /* How many consecutive GC/expansion failures? */
823 /* Reset by GC_allochblk. */
825 GC_bool GC_collect_or_expand(needed_blocks, ignore_off_page)
826 word needed_blocks;
827 GC_bool ignore_off_page;
829 if (!GC_incremental && !GC_dont_gc &&
830 (GC_dont_expand && GC_words_allocd > 0 || GC_should_collect())) {
831 GC_notify_full_gc();
832 GC_gcollect_inner();
833 } else {
834 word blocks_to_get = GC_heapsize/(HBLKSIZE*GC_free_space_divisor)
835 + needed_blocks;
837 if (blocks_to_get > MAXHINCR) {
838 word slop;
840 if (ignore_off_page) {
841 slop = 4;
842 } else {
843 slop = 2*divHBLKSZ(BL_LIMIT);
844 if (slop > needed_blocks) slop = needed_blocks;
846 if (needed_blocks + slop > MAXHINCR) {
847 blocks_to_get = needed_blocks + slop;
848 } else {
849 blocks_to_get = MAXHINCR;
852 if (!GC_expand_hp_inner(blocks_to_get)
853 && !GC_expand_hp_inner(needed_blocks)) {
854 if (GC_fail_count++ < GC_max_retries) {
855 WARN("Out of Memory! Trying to continue ...\n", 0);
856 GC_notify_full_gc();
857 GC_gcollect_inner();
858 } else {
859 WARN("Out of Memory! Returning NIL!\n", 0);
860 return(FALSE);
862 } else {
863 # ifdef PRINTSTATS
864 if (GC_fail_count) {
865 GC_printf0("Memory available again ...\n");
867 # endif
870 return(TRUE);
874 * Make sure the object free list for sz is not empty.
875 * Return a pointer to the first object on the free list.
876 * The object MUST BE REMOVED FROM THE FREE LIST BY THE CALLER.
877 * Assumes we hold the allocator lock and signals are disabled.
880 ptr_t GC_allocobj(sz, kind)
881 word sz;
882 int kind;
884 register ptr_t * flh = &(GC_obj_kinds[kind].ok_freelist[sz]);
886 if (sz == 0) return(0);
888 while (*flh == 0) {
889 ENTER_GC();
890 /* Do our share of marking work */
891 if(GC_incremental && !GC_dont_gc) GC_collect_a_little_inner(1);
892 /* Sweep blocks for objects of this size */
893 GC_continue_reclaim(sz, kind);
894 EXIT_GC();
895 if (*flh == 0) {
896 GC_new_hblk(sz, kind);
898 if (*flh == 0) {
899 ENTER_GC();
900 if (!GC_collect_or_expand((word)1,FALSE)) {
901 EXIT_GC();
902 return(0);
904 EXIT_GC();
908 return(*flh);