comparison gc/misc.c @ 51488:5de98dce4bd1

*** empty log message ***
author Dave Love <fx@gnu.org>
date Thu, 05 Jun 2003 17:49:22 +0000
parents
children
comparison
equal deleted inserted replaced
51487:01d68b199093 51488:5de98dce4bd1
1 /*
2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1999-2001 by Hewlett-Packard Company. All rights reserved.
5 *
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 *
9 * Permission is hereby granted to use or copy this program
10 * for any purpose, provided the above notices are retained on all copies.
11 * Permission to modify the code and to distribute modified code is granted,
12 * provided the above notices are retained, and a notice that the code was
13 * modified is included with the above copyright notice.
14 */
15 /* Boehm, July 31, 1995 5:02 pm PDT */
16
17
18 #include <stdio.h>
19 #include <limits.h>
20 #ifndef _WIN32_WCE
21 #include <signal.h>
22 #endif
23
24 #define I_HIDE_POINTERS /* To make GC_call_with_alloc_lock visible */
25 #include "private/gc_pmark.h"
26
27 #ifdef GC_SOLARIS_THREADS
28 # include <sys/syscall.h>
29 #endif
30 #if defined(MSWIN32) || defined(MSWINCE)
31 # define WIN32_LEAN_AND_MEAN
32 # define NOSERVICE
33 # include <windows.h>
34 # include <tchar.h>
35 #endif
36
37 # ifdef THREADS
38 # ifdef PCR
39 # include "il/PCR_IL.h"
40 PCR_Th_ML GC_allocate_ml;
41 # else
42 # ifdef SRC_M3
43 /* Critical section counter is defined in the M3 runtime */
44 /* That's all we use. */
45 # else
46 # ifdef GC_SOLARIS_THREADS
47 mutex_t GC_allocate_ml; /* Implicitly initialized. */
48 # else
49 # if defined(GC_WIN32_THREADS)
50 # if defined(GC_PTHREADS)
51 pthread_mutex_t GC_allocate_ml = PTHREAD_MUTEX_INITIALIZER;
52 # elif !defined(GC_NOT_DLL) && (defined(_DLL) || defined(GC_DLL))
53 __declspec(dllexport) CRITICAL_SECTION GC_allocate_ml;
54 # else
55 CRITICAL_SECTION GC_allocate_ml;
56 # endif
57 # else
58 # if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS)
59 # if defined(USE_SPIN_LOCK)
60 pthread_t GC_lock_holder = NO_THREAD;
61 # else
62 pthread_mutex_t GC_allocate_ml = PTHREAD_MUTEX_INITIALIZER;
63 pthread_t GC_lock_holder = NO_THREAD;
64 /* Used only for assertions, and to prevent */
65 /* recursive reentry in the system call wrapper. */
66 # endif
67 # else
68 --> declare allocator lock here
69 # endif
70 # endif
71 # endif
72 # endif
73 # endif
74 # endif
75
76 #if defined(NOSYS) || defined(ECOS)
77 #undef STACKBASE
78 #endif
79
80 /* Dont unnecessarily call GC_register_main_static_data() in case */
81 /* dyn_load.c isn't linked in. */
82 #ifdef DYNAMIC_LOADING
83 # define GC_REGISTER_MAIN_STATIC_DATA() GC_register_main_static_data()
84 #else
85 # define GC_REGISTER_MAIN_STATIC_DATA() TRUE
86 #endif
87
88 GC_FAR struct _GC_arrays GC_arrays /* = { 0 } */;
89
90
91 GC_bool GC_debugging_started = FALSE;
92 /* defined here so we don't have to load debug_malloc.o */
93
94 void (*GC_check_heap) GC_PROTO((void)) = (void (*) GC_PROTO((void)))0;
95 void (*GC_print_all_smashed) GC_PROTO((void)) = (void (*) GC_PROTO((void)))0;
96
97 void (*GC_start_call_back) GC_PROTO((void)) = (void (*) GC_PROTO((void)))0;
98
99 ptr_t GC_stackbottom = 0;
100
101 #ifdef IA64
102 ptr_t GC_register_stackbottom = 0;
103 #endif
104
105 GC_bool GC_dont_gc = 0;
106
107 GC_bool GC_dont_precollect = 0;
108
109 GC_bool GC_quiet = 0;
110
111 GC_bool GC_print_stats = 0;
112
113 GC_bool GC_print_back_height = 0;
114
115 #ifndef NO_DEBUGGING
116 GC_bool GC_dump_regularly = 0; /* Generate regular debugging dumps. */
117 #endif
118
119 #ifdef FIND_LEAK
120 int GC_find_leak = 1;
121 #else
122 int GC_find_leak = 0;
123 #endif
124
125 #ifdef ALL_INTERIOR_POINTERS
126 int GC_all_interior_pointers = 1;
127 #else
128 int GC_all_interior_pointers = 0;
129 #endif
130
131 long GC_large_alloc_warn_interval = 5;
132 /* Interval between unsuppressed warnings. */
133
134 long GC_large_alloc_warn_suppressed = 0;
135 /* Number of warnings suppressed so far. */
136
137 /*ARGSUSED*/
138 GC_PTR GC_default_oom_fn GC_PROTO((size_t bytes_requested))
139 {
140 return(0);
141 }
142
143 GC_PTR (*GC_oom_fn) GC_PROTO((size_t bytes_requested)) = GC_default_oom_fn;
144
145 extern signed_word GC_mem_found;
146
147 void * GC_project2(arg1, arg2)
148 void *arg1;
149 void *arg2;
150 {
151 return arg2;
152 }
153
154 # ifdef MERGE_SIZES
155 /* Set things up so that GC_size_map[i] >= words(i), */
156 /* but not too much bigger */
157 /* and so that size_map contains relatively few distinct entries */
158 /* This is stolen from Russ Atkinson's Cedar quantization */
159 /* alogrithm (but we precompute it). */
160
161
162 void GC_init_size_map()
163 {
164 register unsigned i;
165
166 /* Map size 0 to something bigger. */
167 /* This avoids problems at lower levels. */
168 /* One word objects don't have to be 2 word aligned, */
169 /* unless we're using mark bytes. */
170 for (i = 0; i < sizeof(word); i++) {
171 GC_size_map[i] = MIN_WORDS;
172 }
173 # if MIN_WORDS > 1
174 GC_size_map[sizeof(word)] = MIN_WORDS;
175 # else
176 GC_size_map[sizeof(word)] = ROUNDED_UP_WORDS(sizeof(word));
177 # endif
178 for (i = sizeof(word) + 1; i <= 8 * sizeof(word); i++) {
179 GC_size_map[i] = ALIGNED_WORDS(i);
180 }
181 for (i = 8*sizeof(word) + 1; i <= 16 * sizeof(word); i++) {
182 GC_size_map[i] = (ROUNDED_UP_WORDS(i) + 1) & (~1);
183 }
184 # ifdef GC_GCJ_SUPPORT
185 /* Make all sizes up to 32 words predictable, so that a */
186 /* compiler can statically perform the same computation, */
187 /* or at least a computation that results in similar size */
188 /* classes. */
189 for (i = 16*sizeof(word) + 1; i <= 32 * sizeof(word); i++) {
190 GC_size_map[i] = (ROUNDED_UP_WORDS(i) + 3) & (~3);
191 }
192 # endif
193 /* We leave the rest of the array to be filled in on demand. */
194 }
195
196 /* Fill in additional entries in GC_size_map, including the ith one */
197 /* We assume the ith entry is currently 0. */
198 /* Note that a filled in section of the array ending at n always */
199 /* has length at least n/4. */
200 void GC_extend_size_map(i)
201 word i;
202 {
203 word orig_word_sz = ROUNDED_UP_WORDS(i);
204 word word_sz = orig_word_sz;
205 register word byte_sz = WORDS_TO_BYTES(word_sz);
206 /* The size we try to preserve. */
207 /* Close to to i, unless this would */
208 /* introduce too many distinct sizes. */
209 word smaller_than_i = byte_sz - (byte_sz >> 3);
210 word much_smaller_than_i = byte_sz - (byte_sz >> 2);
211 register word low_limit; /* The lowest indexed entry we */
212 /* initialize. */
213 register word j;
214
215 if (GC_size_map[smaller_than_i] == 0) {
216 low_limit = much_smaller_than_i;
217 while (GC_size_map[low_limit] != 0) low_limit++;
218 } else {
219 low_limit = smaller_than_i + 1;
220 while (GC_size_map[low_limit] != 0) low_limit++;
221 word_sz = ROUNDED_UP_WORDS(low_limit);
222 word_sz += word_sz >> 3;
223 if (word_sz < orig_word_sz) word_sz = orig_word_sz;
224 }
225 # ifdef ALIGN_DOUBLE
226 word_sz += 1;
227 word_sz &= ~1;
228 # endif
229 if (word_sz > MAXOBJSZ) {
230 word_sz = MAXOBJSZ;
231 }
232 /* If we can fit the same number of larger objects in a block, */
233 /* do so. */
234 {
235 size_t number_of_objs = BODY_SZ/word_sz;
236 word_sz = BODY_SZ/number_of_objs;
237 # ifdef ALIGN_DOUBLE
238 word_sz &= ~1;
239 # endif
240 }
241 byte_sz = WORDS_TO_BYTES(word_sz);
242 if (GC_all_interior_pointers) {
243 /* We need one extra byte; don't fill in GC_size_map[byte_sz] */
244 byte_sz--;
245 }
246
247 for (j = low_limit; j <= byte_sz; j++) GC_size_map[j] = word_sz;
248 }
249 # endif
250
251
252 /*
253 * The following is a gross hack to deal with a problem that can occur
254 * on machines that are sloppy about stack frame sizes, notably SPARC.
255 * Bogus pointers may be written to the stack and not cleared for
256 * a LONG time, because they always fall into holes in stack frames
257 * that are not written. We partially address this by clearing
258 * sections of the stack whenever we get control.
259 */
260 word GC_stack_last_cleared = 0; /* GC_no when we last did this */
261 # ifdef THREADS
262 # define BIG_CLEAR_SIZE 2048 /* Clear this much now and then. */
263 # define SMALL_CLEAR_SIZE 256 /* Clear this much every time. */
264 # endif
265 # define CLEAR_SIZE 213 /* Granularity for GC_clear_stack_inner */
266 # define DEGRADE_RATE 50
267
268 word GC_min_sp; /* Coolest stack pointer value from which we've */
269 /* already cleared the stack. */
270
271 word GC_high_water;
272 /* "hottest" stack pointer value we have seen */
273 /* recently. Degrades over time. */
274
275 word GC_words_allocd_at_reset;
276
277 #if defined(ASM_CLEAR_CODE)
278 extern ptr_t GC_clear_stack_inner();
279 #else
280 /* Clear the stack up to about limit. Return arg. */
281 /*ARGSUSED*/
282 ptr_t GC_clear_stack_inner(arg, limit)
283 ptr_t arg;
284 word limit;
285 {
286 word dummy[CLEAR_SIZE];
287
288 BZERO(dummy, CLEAR_SIZE*sizeof(word));
289 if ((word)(dummy) COOLER_THAN limit) {
290 (void) GC_clear_stack_inner(arg, limit);
291 }
292 /* Make sure the recursive call is not a tail call, and the bzero */
293 /* call is not recognized as dead code. */
294 GC_noop1((word)dummy);
295 return(arg);
296 }
297 #endif
298
299 /* Clear some of the inaccessible part of the stack. Returns its */
300 /* argument, so it can be used in a tail call position, hence clearing */
301 /* another frame. */
302 ptr_t GC_clear_stack(arg)
303 ptr_t arg;
304 {
305 register word sp = (word)GC_approx_sp(); /* Hotter than actual sp */
306 # ifdef THREADS
307 word dummy[SMALL_CLEAR_SIZE];
308 static unsigned random_no = 0;
309 /* Should be more random than it is ... */
310 /* Used to occasionally clear a bigger */
311 /* chunk. */
312 # endif
313 register word limit;
314
315 # define SLOP 400
316 /* Extra bytes we clear every time. This clears our own */
317 /* activation record, and should cause more frequent */
318 /* clearing near the cold end of the stack, a good thing. */
319 # define GC_SLOP 4000
320 /* We make GC_high_water this much hotter than we really saw */
321 /* saw it, to cover for GC noise etc. above our current frame. */
322 # define CLEAR_THRESHOLD 100000
323 /* We restart the clearing process after this many bytes of */
324 /* allocation. Otherwise very heavily recursive programs */
325 /* with sparse stacks may result in heaps that grow almost */
326 /* without bounds. As the heap gets larger, collection */
327 /* frequency decreases, thus clearing frequency would decrease, */
328 /* thus more junk remains accessible, thus the heap gets */
329 /* larger ... */
330 # ifdef THREADS
331 if (++random_no % 13 == 0) {
332 limit = sp;
333 MAKE_HOTTER(limit, BIG_CLEAR_SIZE*sizeof(word));
334 limit &= ~0xf; /* Make it sufficiently aligned for assembly */
335 /* implementations of GC_clear_stack_inner. */
336 return GC_clear_stack_inner(arg, limit);
337 } else {
338 BZERO(dummy, SMALL_CLEAR_SIZE*sizeof(word));
339 return arg;
340 }
341 # else
342 if (GC_gc_no > GC_stack_last_cleared) {
343 /* Start things over, so we clear the entire stack again */
344 if (GC_stack_last_cleared == 0) GC_high_water = (word) GC_stackbottom;
345 GC_min_sp = GC_high_water;
346 GC_stack_last_cleared = GC_gc_no;
347 GC_words_allocd_at_reset = GC_words_allocd;
348 }
349 /* Adjust GC_high_water */
350 MAKE_COOLER(GC_high_water, WORDS_TO_BYTES(DEGRADE_RATE) + GC_SLOP);
351 if (sp HOTTER_THAN GC_high_water) {
352 GC_high_water = sp;
353 }
354 MAKE_HOTTER(GC_high_water, GC_SLOP);
355 limit = GC_min_sp;
356 MAKE_HOTTER(limit, SLOP);
357 if (sp COOLER_THAN limit) {
358 limit &= ~0xf; /* Make it sufficiently aligned for assembly */
359 /* implementations of GC_clear_stack_inner. */
360 GC_min_sp = sp;
361 return(GC_clear_stack_inner(arg, limit));
362 } else if (WORDS_TO_BYTES(GC_words_allocd - GC_words_allocd_at_reset)
363 > CLEAR_THRESHOLD) {
364 /* Restart clearing process, but limit how much clearing we do. */
365 GC_min_sp = sp;
366 MAKE_HOTTER(GC_min_sp, CLEAR_THRESHOLD/4);
367 if (GC_min_sp HOTTER_THAN GC_high_water) GC_min_sp = GC_high_water;
368 GC_words_allocd_at_reset = GC_words_allocd;
369 }
370 return(arg);
371 # endif
372 }
373
374
375 /* Return a pointer to the base address of p, given a pointer to a */
376 /* an address within an object. Return 0 o.w. */
377 # ifdef __STDC__
378 GC_PTR GC_base(GC_PTR p)
379 # else
380 GC_PTR GC_base(p)
381 GC_PTR p;
382 # endif
383 {
384 register word r;
385 register struct hblk *h;
386 register bottom_index *bi;
387 register hdr *candidate_hdr;
388 register word limit;
389
390 r = (word)p;
391 if (!GC_is_initialized) return 0;
392 h = HBLKPTR(r);
393 GET_BI(r, bi);
394 candidate_hdr = HDR_FROM_BI(bi, r);
395 if (candidate_hdr == 0) return(0);
396 /* If it's a pointer to the middle of a large object, move it */
397 /* to the beginning. */
398 while (IS_FORWARDING_ADDR_OR_NIL(candidate_hdr)) {
399 h = FORWARDED_ADDR(h,candidate_hdr);
400 r = (word)h;
401 candidate_hdr = HDR(h);
402 }
403 if (candidate_hdr -> hb_map == GC_invalid_map) return(0);
404 /* Make sure r points to the beginning of the object */
405 r &= ~(WORDS_TO_BYTES(1) - 1);
406 {
407 register int offset = HBLKDISPL(r);
408 register signed_word sz = candidate_hdr -> hb_sz;
409 register signed_word map_entry;
410
411 map_entry = MAP_ENTRY((candidate_hdr -> hb_map), offset);
412 if (map_entry > CPP_MAX_OFFSET) {
413 map_entry = (signed_word)(BYTES_TO_WORDS(offset)) % sz;
414 }
415 r -= WORDS_TO_BYTES(map_entry);
416 limit = r + WORDS_TO_BYTES(sz);
417 if (limit > (word)(h + 1)
418 && sz <= BYTES_TO_WORDS(HBLKSIZE)) {
419 return(0);
420 }
421 if ((word)p >= limit) return(0);
422 }
423 return((GC_PTR)r);
424 }
425
426
427 /* Return the size of an object, given a pointer to its base. */
428 /* (For small obects this also happens to work from interior pointers, */
429 /* but that shouldn't be relied upon.) */
430 # ifdef __STDC__
431 size_t GC_size(GC_PTR p)
432 # else
433 size_t GC_size(p)
434 GC_PTR p;
435 # endif
436 {
437 register int sz;
438 register hdr * hhdr = HDR(p);
439
440 sz = WORDS_TO_BYTES(hhdr -> hb_sz);
441 return(sz);
442 }
443
444 size_t GC_get_heap_size GC_PROTO(())
445 {
446 return ((size_t) GC_heapsize);
447 }
448
449 size_t GC_get_free_bytes GC_PROTO(())
450 {
451 return ((size_t) GC_large_free_bytes);
452 }
453
454 size_t GC_get_bytes_since_gc GC_PROTO(())
455 {
456 return ((size_t) WORDS_TO_BYTES(GC_words_allocd));
457 }
458
459 size_t GC_get_total_bytes GC_PROTO(())
460 {
461 return ((size_t) WORDS_TO_BYTES(GC_words_allocd+GC_words_allocd_before_gc));
462 }
463
464 GC_bool GC_is_initialized = FALSE;
465
466 void GC_init()
467 {
468 DCL_LOCK_STATE;
469
470 DISABLE_SIGNALS();
471
472 #if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
473 if (!GC_is_initialized) InitializeCriticalSection(&GC_allocate_ml);
474 #endif /* MSWIN32 */
475
476 LOCK();
477 GC_init_inner();
478 UNLOCK();
479 ENABLE_SIGNALS();
480
481 # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
482 /* Make sure marker threads and started and thread local */
483 /* allocation is initialized, in case we didn't get */
484 /* called from GC_init_parallel(); */
485 {
486 extern void GC_init_parallel(void);
487 GC_init_parallel();
488 }
489 # endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
490 }
491
492 #if defined(MSWIN32) || defined(MSWINCE)
493 CRITICAL_SECTION GC_write_cs;
494 #endif
495
496 #ifdef MSWIN32
497 extern void GC_init_win32 GC_PROTO((void));
498 #endif
499
500 extern void GC_setpagesize();
501
502 #ifdef UNIX_LIKE
503
504 extern void GC_set_and_save_fault_handler GC_PROTO((void (*handler)(int)));
505
506 static void looping_handler(sig)
507 int sig;
508 {
509 GC_err_printf1("Caught signal %d: looping in handler\n", sig);
510 for(;;);
511 }
512 #endif
513
514 #ifdef MSWIN32
515 extern GC_bool GC_no_win32_dlls;
516 #else
517 # define GC_no_win32_dlls FALSE
518 #endif
519
520 void GC_exit_check GC_PROTO((void))
521 {
522 GC_gcollect();
523 }
524
525 #ifdef SEARCH_FOR_DATA_START
526 extern void GC_init_linux_data_start GC_PROTO((void));
527 #endif
528
529 void GC_init_inner()
530 {
531 # if !defined(THREADS) && defined(GC_ASSERTIONS)
532 word dummy;
533 # endif
534 word initial_heap_sz = (word)MINHINCR;
535
536 if (GC_is_initialized) return;
537 # ifdef PRINTSTATS
538 GC_print_stats = 1;
539 # endif
540 # if defined(MSWIN32) || defined(MSWINCE)
541 InitializeCriticalSection(&GC_write_cs);
542 # endif
543 if (0 != GETENV("GC_PRINT_STATS")) {
544 GC_print_stats = 1;
545 }
546 # ifndef NO_DEBUGGING
547 if (0 != GETENV("GC_DUMP_REGULARLY")) {
548 GC_dump_regularly = 1;
549 }
550 # endif
551 if (0 != GETENV("GC_FIND_LEAK")) {
552 GC_find_leak = 1;
553 # ifdef __STDC__
554 atexit(GC_exit_check);
555 # endif
556 }
557 if (0 != GETENV("GC_ALL_INTERIOR_POINTERS")) {
558 GC_all_interior_pointers = 1;
559 }
560 if (0 != GETENV("GC_DONT_GC")) {
561 GC_dont_gc = 1;
562 }
563 if (0 != GETENV("GC_PRINT_BACK_HEIGHT")) {
564 GC_print_back_height = 1;
565 }
566 if (0 != GETENV("GC_NO_BLACKLIST_WARNING")) {
567 GC_large_alloc_warn_interval = LONG_MAX;
568 }
569 {
570 char * time_limit_string = GETENV("GC_PAUSE_TIME_TARGET");
571 if (0 != time_limit_string) {
572 long time_limit = atol(time_limit_string);
573 if (time_limit < 5) {
574 WARN("GC_PAUSE_TIME_TARGET environment variable value too small "
575 "or bad syntax: Ignoring\n", 0);
576 } else {
577 GC_time_limit = time_limit;
578 }
579 }
580 }
581 {
582 char * interval_string = GETENV("GC_LARGE_ALLOC_WARN_INTERVAL");
583 if (0 != interval_string) {
584 long interval = atol(interval_string);
585 if (interval <= 0) {
586 WARN("GC_LARGE_ALLOC_WARN_INTERVAL environment variable has "
587 "bad value: Ignoring\n", 0);
588 } else {
589 GC_large_alloc_warn_interval = interval;
590 }
591 }
592 }
593 # ifdef UNIX_LIKE
594 if (0 != GETENV("GC_LOOP_ON_ABORT")) {
595 GC_set_and_save_fault_handler(looping_handler);
596 }
597 # endif
598 /* Adjust normal object descriptor for extra allocation. */
599 if (ALIGNMENT > GC_DS_TAGS && EXTRA_BYTES != 0) {
600 GC_obj_kinds[NORMAL].ok_descriptor = ((word)(-ALIGNMENT) | GC_DS_LENGTH);
601 }
602 GC_setpagesize();
603 GC_exclude_static_roots(beginGC_arrays, endGC_arrays);
604 GC_exclude_static_roots(beginGC_obj_kinds, endGC_obj_kinds);
605 # ifdef SEPARATE_GLOBALS
606 GC_exclude_static_roots(beginGC_objfreelist, endGC_objfreelist);
607 GC_exclude_static_roots(beginGC_aobjfreelist, endGC_aobjfreelist);
608 # endif
609 # ifdef MSWIN32
610 GC_init_win32();
611 # endif
612 # if defined(SEARCH_FOR_DATA_START)
613 GC_init_linux_data_start();
614 # endif
615 # if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__)
616 GC_init_netbsd_elf();
617 # endif
618 # if defined(GC_PTHREADS) || defined(GC_SOLARIS_THREADS)
619 GC_thr_init();
620 # endif
621 # ifdef GC_SOLARIS_THREADS
622 /* We need dirty bits in order to find live stack sections. */
623 GC_dirty_init();
624 # endif
625 # if !defined(THREADS) || defined(GC_PTHREADS) || defined(GC_WIN32_THREADS) \
626 || defined(GC_SOLARIS_THREADS)
627 if (GC_stackbottom == 0) {
628 GC_stackbottom = GC_get_stack_base();
629 # if defined(LINUX) && defined(IA64)
630 GC_register_stackbottom = GC_get_register_stack_base();
631 # endif
632 } else {
633 # if defined(LINUX) && defined(IA64)
634 if (GC_register_stackbottom == 0) {
635 WARN("GC_register_stackbottom should be set with GC_stackbottom", 0);
636 /* The following is likely to fail, since we rely on */
637 /* alignment properties that may not hold with a user set */
638 /* GC_stackbottom. */
639 GC_register_stackbottom = GC_get_register_stack_base();
640 }
641 # endif
642 }
643 # endif
644 GC_ASSERT(sizeof (ptr_t) == sizeof(word));
645 GC_ASSERT(sizeof (signed_word) == sizeof(word));
646 GC_ASSERT(sizeof (struct hblk) == HBLKSIZE);
647 # ifndef THREADS
648 # if defined(STACK_GROWS_UP) && defined(STACK_GROWS_DOWN)
649 ABORT(
650 "Only one of STACK_GROWS_UP and STACK_GROWS_DOWN should be defd\n");
651 # endif
652 # if !defined(STACK_GROWS_UP) && !defined(STACK_GROWS_DOWN)
653 ABORT(
654 "One of STACK_GROWS_UP and STACK_GROWS_DOWN should be defd\n");
655 # endif
656 # ifdef STACK_GROWS_DOWN
657 GC_ASSERT((word)(&dummy) <= (word)GC_stackbottom);
658 # else
659 GC_ASSERT((word)(&dummy) >= (word)GC_stackbottom);
660 # endif
661 # endif
662 # if !defined(_AUX_SOURCE) || defined(__GNUC__)
663 GC_ASSERT((word)(-1) > (word)0);
664 /* word should be unsigned */
665 # endif
666 GC_ASSERT((signed_word)(-1) < (signed_word)0);
667
668 /* Add initial guess of root sets. Do this first, since sbrk(0) */
669 /* might be used. */
670 if (GC_REGISTER_MAIN_STATIC_DATA()) GC_register_data_segments();
671 GC_init_headers();
672 GC_bl_init();
673 GC_mark_init();
674 {
675 char * sz_str = GETENV("GC_INITIAL_HEAP_SIZE");
676 if (sz_str != NULL) {
677 initial_heap_sz = atoi(sz_str);
678 if (initial_heap_sz <= MINHINCR * HBLKSIZE) {
679 WARN("Bad initial heap size %s - ignoring it.\n",
680 sz_str);
681 }
682 initial_heap_sz = divHBLKSZ(initial_heap_sz);
683 }
684 }
685 {
686 char * sz_str = GETENV("GC_MAXIMUM_HEAP_SIZE");
687 if (sz_str != NULL) {
688 word max_heap_sz = (word)atol(sz_str);
689 if (max_heap_sz < initial_heap_sz * HBLKSIZE) {
690 WARN("Bad maximum heap size %s - ignoring it.\n",
691 sz_str);
692 }
693 if (0 == GC_max_retries) GC_max_retries = 2;
694 GC_set_max_heap_size(max_heap_sz);
695 }
696 }
697 if (!GC_expand_hp_inner(initial_heap_sz)) {
698 GC_err_printf0("Can't start up: not enough memory\n");
699 EXIT();
700 }
701 /* Preallocate large object map. It's otherwise inconvenient to */
702 /* deal with failure. */
703 if (!GC_add_map_entry((word)0)) {
704 GC_err_printf0("Can't start up: not enough memory\n");
705 EXIT();
706 }
707 GC_register_displacement_inner(0L);
708 # ifdef MERGE_SIZES
709 GC_init_size_map();
710 # endif
711 # ifdef PCR
712 if (PCR_IL_Lock(PCR_Bool_false, PCR_allSigsBlocked, PCR_waitForever)
713 != PCR_ERes_okay) {
714 ABORT("Can't lock load state\n");
715 } else if (PCR_IL_Unlock() != PCR_ERes_okay) {
716 ABORT("Can't unlock load state\n");
717 }
718 PCR_IL_Unlock();
719 GC_pcr_install();
720 # endif
721 # if !defined(SMALL_CONFIG)
722 if (!GC_no_win32_dlls && 0 != GETENV("GC_ENABLE_INCREMENTAL")) {
723 GC_ASSERT(!GC_incremental);
724 GC_setpagesize();
725 # ifndef GC_SOLARIS_THREADS
726 GC_dirty_init();
727 # endif
728 GC_ASSERT(GC_words_allocd == 0)
729 GC_incremental = TRUE;
730 }
731 # endif /* !SMALL_CONFIG */
732 COND_DUMP;
733 /* Get black list set up and/or incrmental GC started */
734 if (!GC_dont_precollect || GC_incremental) GC_gcollect_inner();
735 GC_is_initialized = TRUE;
736 # ifdef STUBBORN_ALLOC
737 GC_stubborn_init();
738 # endif
739 /* Convince lint that some things are used */
740 # ifdef LINT
741 {
742 extern char * GC_copyright[];
743 extern int GC_read();
744 extern void GC_register_finalizer_no_order();
745
746 GC_noop(GC_copyright, GC_find_header,
747 GC_push_one, GC_call_with_alloc_lock, GC_read,
748 GC_dont_expand,
749 # ifndef NO_DEBUGGING
750 GC_dump,
751 # endif
752 GC_register_finalizer_no_order);
753 }
754 # endif
755 }
756
757 void GC_enable_incremental GC_PROTO(())
758 {
759 # if !defined(SMALL_CONFIG)
760 if (!GC_find_leak) {
761 DCL_LOCK_STATE;
762
763 DISABLE_SIGNALS();
764 LOCK();
765 if (GC_incremental) goto out;
766 GC_setpagesize();
767 if (GC_no_win32_dlls) goto out;
768 # ifndef GC_SOLARIS_THREADS
769 GC_dirty_init();
770 # endif
771 if (!GC_is_initialized) {
772 GC_init_inner();
773 }
774 if (GC_incremental) goto out;
775 if (GC_dont_gc) {
776 /* Can't easily do it. */
777 UNLOCK();
778 ENABLE_SIGNALS();
779 return;
780 }
781 if (GC_words_allocd > 0) {
782 /* There may be unmarked reachable objects */
783 GC_gcollect_inner();
784 } /* else we're OK in assuming everything's */
785 /* clean since nothing can point to an */
786 /* unmarked object. */
787 GC_read_dirty();
788 GC_incremental = TRUE;
789 out:
790 UNLOCK();
791 ENABLE_SIGNALS();
792 }
793 # endif
794 }
795
796
797 #if defined(MSWIN32) || defined(MSWINCE)
798 # define LOG_FILE _T("gc.log")
799
800 HANDLE GC_stdout = 0;
801
802 void GC_deinit()
803 {
804 if (GC_is_initialized) {
805 DeleteCriticalSection(&GC_write_cs);
806 }
807 }
808
809 int GC_write(buf, len)
810 GC_CONST char * buf;
811 size_t len;
812 {
813 BOOL tmp;
814 DWORD written;
815 if (len == 0)
816 return 0;
817 EnterCriticalSection(&GC_write_cs);
818 if (GC_stdout == INVALID_HANDLE_VALUE) {
819 return -1;
820 } else if (GC_stdout == 0) {
821 GC_stdout = CreateFile(LOG_FILE, GENERIC_WRITE,
822 FILE_SHARE_READ | FILE_SHARE_WRITE,
823 NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH,
824 NULL);
825 if (GC_stdout == INVALID_HANDLE_VALUE) ABORT("Open of log file failed");
826 }
827 tmp = WriteFile(GC_stdout, buf, len, &written, NULL);
828 if (!tmp)
829 DebugBreak();
830 LeaveCriticalSection(&GC_write_cs);
831 return tmp ? (int)written : -1;
832 }
833
834 #endif
835
836 #if defined(OS2) || defined(MACOS)
837 FILE * GC_stdout = NULL;
838 FILE * GC_stderr = NULL;
839 int GC_tmp; /* Should really be local ... */
840
841 void GC_set_files()
842 {
843 if (GC_stdout == NULL) {
844 GC_stdout = stdout;
845 }
846 if (GC_stderr == NULL) {
847 GC_stderr = stderr;
848 }
849 }
850 #endif
851
852 #if !defined(OS2) && !defined(MACOS) && !defined(MSWIN32) && !defined(MSWINCE)
853 int GC_stdout = 1;
854 int GC_stderr = 2;
855 # if !defined(AMIGA)
856 # include <unistd.h>
857 # endif
858 #endif
859
860 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(OS2) \
861 && !defined(MACOS) && !defined(ECOS) && !defined(NOSYS)
862 int GC_write(fd, buf, len)
863 int fd;
864 GC_CONST char *buf;
865 size_t len;
866 {
867 register int bytes_written = 0;
868 register int result;
869
870 while (bytes_written < len) {
871 # ifdef GC_SOLARIS_THREADS
872 result = syscall(SYS_write, fd, buf + bytes_written,
873 len - bytes_written);
874 # else
875 result = write(fd, buf + bytes_written, len - bytes_written);
876 # endif
877 if (-1 == result) return(result);
878 bytes_written += result;
879 }
880 return(bytes_written);
881 }
882 #endif /* UN*X */
883
884 #ifdef ECOS
885 int GC_write(fd, buf, len)
886 {
887 _Jv_diag_write (buf, len);
888 return len;
889 }
890 #endif
891
892 #ifdef NOSYS
893 int GC_write(fd, buf, len)
894 {
895 /* No writing. */
896 return len;
897 }
898 #endif
899
900
901 #if defined(MSWIN32) || defined(MSWINCE)
902 # define WRITE(f, buf, len) GC_write(buf, len)
903 #else
904 # if defined(OS2) || defined(MACOS)
905 # define WRITE(f, buf, len) (GC_set_files(), \
906 GC_tmp = fwrite((buf), 1, (len), (f)), \
907 fflush(f), GC_tmp)
908 # else
909 # define WRITE(f, buf, len) GC_write((f), (buf), (len))
910 # endif
911 #endif
912
913 /* A version of printf that is unlikely to call malloc, and is thus safer */
914 /* to call from the collector in case malloc has been bound to GC_malloc. */
915 /* Assumes that no more than 1023 characters are written at once. */
916 /* Assumes that all arguments have been converted to something of the */
917 /* same size as long, and that the format conversions expect something */
918 /* of that size. */
919 void GC_printf(format, a, b, c, d, e, f)
920 GC_CONST char * format;
921 long a, b, c, d, e, f;
922 {
923 char buf[1025];
924
925 if (GC_quiet) return;
926 buf[1024] = 0x15;
927 (void) sprintf(buf, format, a, b, c, d, e, f);
928 if (buf[1024] != 0x15) ABORT("GC_printf clobbered stack");
929 if (WRITE(GC_stdout, buf, strlen(buf)) < 0) ABORT("write to stdout failed");
930 }
931
932 void GC_err_printf(format, a, b, c, d, e, f)
933 GC_CONST char * format;
934 long a, b, c, d, e, f;
935 {
936 char buf[1025];
937
938 buf[1024] = 0x15;
939 (void) sprintf(buf, format, a, b, c, d, e, f);
940 if (buf[1024] != 0x15) ABORT("GC_err_printf clobbered stack");
941 if (WRITE(GC_stderr, buf, strlen(buf)) < 0) ABORT("write to stderr failed");
942 }
943
944 void GC_err_puts(s)
945 GC_CONST char *s;
946 {
947 if (WRITE(GC_stderr, s, strlen(s)) < 0) ABORT("write to stderr failed");
948 }
949
950 #if defined(LINUX) && !defined(SMALL_CONFIG)
951 void GC_err_write(buf, len)
952 GC_CONST char *buf;
953 size_t len;
954 {
955 if (WRITE(GC_stderr, buf, len) < 0) ABORT("write to stderr failed");
956 }
957 #endif
958
959 # if defined(__STDC__) || defined(__cplusplus)
960 void GC_default_warn_proc(char *msg, GC_word arg)
961 # else
962 void GC_default_warn_proc(msg, arg)
963 char *msg;
964 GC_word arg;
965 # endif
966 {
967 GC_err_printf1(msg, (unsigned long)arg);
968 }
969
970 GC_warn_proc GC_current_warn_proc = GC_default_warn_proc;
971
972 # if defined(__STDC__) || defined(__cplusplus)
973 GC_warn_proc GC_set_warn_proc(GC_warn_proc p)
974 # else
975 GC_warn_proc GC_set_warn_proc(p)
976 GC_warn_proc p;
977 # endif
978 {
979 GC_warn_proc result;
980
981 LOCK();
982 result = GC_current_warn_proc;
983 GC_current_warn_proc = p;
984 UNLOCK();
985 return(result);
986 }
987
988 # if defined(__STDC__) || defined(__cplusplus)
989 GC_word GC_set_free_space_divisor (GC_word value)
990 # else
991 GC_word GC_set_free_space_divisor (value)
992 GC_word value;
993 # endif
994 {
995 GC_word old = GC_free_space_divisor;
996 GC_free_space_divisor = value;
997 return old;
998 }
999
1000 #ifndef PCR
1001 void GC_abort(msg)
1002 GC_CONST char * msg;
1003 {
1004 # if defined(MSWIN32)
1005 (void) MessageBoxA(NULL, msg, "Fatal error in gc", MB_ICONERROR|MB_OK);
1006 DebugBreak();
1007 # else
1008 GC_err_printf1("%s\n", msg);
1009 # endif
1010 if (GETENV("GC_LOOP_ON_ABORT") != NULL) {
1011 /* In many cases it's easier to debug a running process. */
1012 /* It's arguably nicer to sleep, but that makes it harder */
1013 /* to look at the thread if the debugger doesn't know much */
1014 /* about threads. */
1015 for(;;) {}
1016 }
1017 # ifdef MSWIN32
1018 DebugBreak();
1019 # else
1020 (void) abort();
1021 # endif
1022 }
1023 #endif
1024
1025 void GC_enable()
1026 {
1027 LOCK();
1028 GC_dont_gc--;
1029 UNLOCK();
1030 }
1031
1032 void GC_disable()
1033 {
1034 LOCK();
1035 GC_dont_gc++;
1036 UNLOCK();
1037 }
1038
1039 #if !defined(NO_DEBUGGING)
1040
1041 void GC_dump()
1042 {
1043 GC_printf0("***Static roots:\n");
1044 GC_print_static_roots();
1045 GC_printf0("\n***Heap sections:\n");
1046 GC_print_heap_sects();
1047 GC_printf0("\n***Free blocks:\n");
1048 GC_print_hblkfreelist();
1049 GC_printf0("\n***Blocks in use:\n");
1050 GC_print_block_list();
1051 GC_printf0("\n***Finalization statistics:\n");
1052 GC_print_finalization_stats();
1053 }
1054
1055 #endif /* NO_DEBUGGING */