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