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.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
15 /* Boehm, July 31, 1995 5:02 pm PDT */
24 #define I_HIDE_POINTERS /* To make GC_call_with_alloc_lock visible */
25 #include "private/gc_pmark.h"
27 #ifdef GC_SOLARIS_THREADS
28 # include <sys/syscall.h>
30 #if defined(MSWIN32) || defined(MSWINCE)
31 # define WIN32_LEAN_AND_MEAN
39 # include "il/PCR_IL.h"
40 PCR_Th_ML GC_allocate_ml
;
43 /* Critical section counter is defined in the M3 runtime */
44 /* That's all we use. */
46 # ifdef GC_SOLARIS_THREADS
47 mutex_t GC_allocate_ml
; /* Implicitly initialized. */
49 # ifdef GC_WIN32_THREADS
50 # if !defined(GC_NOT_DLL) && (defined(_DLL) || defined(GC_DLL))
51 __declspec(dllexport
) CRITICAL_SECTION GC_allocate_ml
;
53 CRITICAL_SECTION GC_allocate_ml
;
56 # if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS)
57 # if defined(USE_SPIN_LOCK)
58 pthread_t GC_lock_holder
= NO_THREAD
;
60 pthread_mutex_t GC_allocate_ml
= PTHREAD_MUTEX_INITIALIZER
;
61 pthread_t GC_lock_holder
= NO_THREAD
;
62 /* Used only for assertions, and to prevent */
63 /* recursive reentry in the system call wrapper. */
66 --> declare allocator lock here
74 #if defined(NOSYS) || defined(ECOS)
78 GC_FAR
struct _GC_arrays GC_arrays
/* = { 0 } */;
81 GC_bool GC_debugging_started
= FALSE
;
82 /* defined here so we don't have to load debug_malloc.o */
84 void (*GC_check_heap
) GC_PROTO((void)) = (void (*) GC_PROTO((void)))0;
86 void (*GC_start_call_back
) GC_PROTO((void)) = (void (*) GC_PROTO((void)))0;
88 ptr_t GC_stackbottom
= 0;
91 ptr_t GC_register_stackbottom
= 0;
94 GC_bool GC_dont_gc
= 0;
96 GC_bool GC_dont_precollect
= 0;
100 GC_bool GC_print_stats
= 0;
102 GC_bool GC_print_back_height
= 0;
105 int GC_find_leak
= 1;
107 int GC_find_leak
= 0;
110 #ifdef ALL_INTERIOR_POINTERS
111 int GC_all_interior_pointers
= 1;
113 int GC_all_interior_pointers
= 0;
116 long GC_large_alloc_warn_interval
= 5;
117 /* Interval between unsuppressed warnings. */
119 long GC_large_alloc_warn_suppressed
= 0;
120 /* Number of warnings suppressed so far. */
123 GC_PTR GC_default_oom_fn
GC_PROTO((size_t bytes_requested
))
128 GC_PTR (*GC_oom_fn
) GC_PROTO((size_t bytes_requested
)) = GC_default_oom_fn
;
130 extern signed_word GC_mem_found
;
133 /* Set things up so that GC_size_map[i] >= words(i), */
134 /* but not too much bigger */
135 /* and so that size_map contains relatively few distinct entries */
136 /* This is stolen from Russ Atkinson's Cedar quantization */
137 /* alogrithm (but we precompute it). */
140 void GC_init_size_map()
144 /* Map size 0 to something bigger. */
145 /* This avoids problems at lower levels. */
146 /* One word objects don't have to be 2 word aligned, */
147 /* unless we're using mark bytes. */
148 for (i
= 0; i
< sizeof(word
); i
++) {
149 GC_size_map
[i
] = MIN_WORDS
;
152 GC_size_map
[sizeof(word
)] = MIN_WORDS
;
154 GC_size_map
[sizeof(word
)] = ROUNDED_UP_WORDS(sizeof(word
));
156 for (i
= sizeof(word
) + 1; i
<= 8 * sizeof(word
); i
++) {
157 GC_size_map
[i
] = ALIGNED_WORDS(i
);
159 for (i
= 8*sizeof(word
) + 1; i
<= 16 * sizeof(word
); i
++) {
160 GC_size_map
[i
] = (ROUNDED_UP_WORDS(i
) + 1) & (~1);
162 # ifdef GC_GCJ_SUPPORT
163 /* Make all sizes up to 32 words predictable, so that a */
164 /* compiler can statically perform the same computation, */
165 /* or at least a computation that results in similar size */
167 for (i
= 16*sizeof(word
) + 1; i
<= 32 * sizeof(word
); i
++) {
168 GC_size_map
[i
] = (ROUNDED_UP_WORDS(i
) + 3) & (~3);
171 /* We leave the rest of the array to be filled in on demand. */
174 /* Fill in additional entries in GC_size_map, including the ith one */
175 /* We assume the ith entry is currently 0. */
176 /* Note that a filled in section of the array ending at n always */
177 /* has length at least n/4. */
178 void GC_extend_size_map(i
)
181 word orig_word_sz
= ROUNDED_UP_WORDS(i
);
182 word word_sz
= orig_word_sz
;
183 register word byte_sz
= WORDS_TO_BYTES(word_sz
);
184 /* The size we try to preserve. */
185 /* Close to to i, unless this would */
186 /* introduce too many distinct sizes. */
187 word smaller_than_i
= byte_sz
- (byte_sz
>> 3);
188 word much_smaller_than_i
= byte_sz
- (byte_sz
>> 2);
189 register word low_limit
; /* The lowest indexed entry we */
193 if (GC_size_map
[smaller_than_i
] == 0) {
194 low_limit
= much_smaller_than_i
;
195 while (GC_size_map
[low_limit
] != 0) low_limit
++;
197 low_limit
= smaller_than_i
+ 1;
198 while (GC_size_map
[low_limit
] != 0) low_limit
++;
199 word_sz
= ROUNDED_UP_WORDS(low_limit
);
200 word_sz
+= word_sz
>> 3;
201 if (word_sz
< orig_word_sz
) word_sz
= orig_word_sz
;
207 if (word_sz
> MAXOBJSZ
) {
210 /* If we can fit the same number of larger objects in a block, */
213 size_t number_of_objs
= BODY_SZ
/word_sz
;
214 word_sz
= BODY_SZ
/number_of_objs
;
219 byte_sz
= WORDS_TO_BYTES(word_sz
);
220 if (GC_all_interior_pointers
) {
221 /* We need one extra byte; don't fill in GC_size_map[byte_sz] */
225 for (j
= low_limit
; j
<= byte_sz
; j
++) GC_size_map
[j
] = word_sz
;
231 * The following is a gross hack to deal with a problem that can occur
232 * on machines that are sloppy about stack frame sizes, notably SPARC.
233 * Bogus pointers may be written to the stack and not cleared for
234 * a LONG time, because they always fall into holes in stack frames
235 * that are not written. We partially address this by clearing
236 * sections of the stack whenever we get control.
238 word GC_stack_last_cleared
= 0; /* GC_no when we last did this */
240 # define BIG_CLEAR_SIZE 2048 /* Clear this much now and then. */
241 # define SMALL_CLEAR_SIZE 256 /* Clear this much every time. */
243 # define CLEAR_SIZE 213 /* Granularity for GC_clear_stack_inner */
244 # define DEGRADE_RATE 50
246 word GC_min_sp
; /* Coolest stack pointer value from which we've */
247 /* already cleared the stack. */
250 /* "hottest" stack pointer value we have seen */
251 /* recently. Degrades over time. */
253 word GC_words_allocd_at_reset
;
255 #if defined(ASM_CLEAR_CODE)
256 extern ptr_t
GC_clear_stack_inner();
258 /* Clear the stack up to about limit. Return arg. */
260 ptr_t
GC_clear_stack_inner(arg
, limit
)
264 word dummy
[CLEAR_SIZE
];
266 BZERO(dummy
, CLEAR_SIZE
*sizeof(word
));
267 if ((word
)(dummy
) COOLER_THAN limit
) {
268 (void) GC_clear_stack_inner(arg
, limit
);
270 /* Make sure the recursive call is not a tail call, and the bzero */
271 /* call is not recognized as dead code. */
272 GC_noop1((word
)dummy
);
277 /* Clear some of the inaccessible part of the stack. Returns its */
278 /* argument, so it can be used in a tail call position, hence clearing */
280 ptr_t
GC_clear_stack(arg
)
283 register word sp
= (word
)GC_approx_sp(); /* Hotter than actual sp */
285 word dummy
[SMALL_CLEAR_SIZE
];
286 static unsigned random_no
= 0;
287 /* Should be more random than it is ... */
288 /* Used to occasionally clear a bigger */
294 /* Extra bytes we clear every time. This clears our own */
295 /* activation record, and should cause more frequent */
296 /* clearing near the cold end of the stack, a good thing. */
297 # define GC_SLOP 4000
298 /* We make GC_high_water this much hotter than we really saw */
299 /* saw it, to cover for GC noise etc. above our current frame. */
300 # define CLEAR_THRESHOLD 100000
301 /* We restart the clearing process after this many bytes of */
302 /* allocation. Otherwise very heavily recursive programs */
303 /* with sparse stacks may result in heaps that grow almost */
304 /* without bounds. As the heap gets larger, collection */
305 /* frequency decreases, thus clearing frequency would decrease, */
306 /* thus more junk remains accessible, thus the heap gets */
309 if (++random_no
% 13 == 0) {
311 MAKE_HOTTER(limit
, BIG_CLEAR_SIZE
*sizeof(word
));
312 limit
&= ~0xf; /* Make it sufficiently aligned for assembly */
313 /* implementations of GC_clear_stack_inner. */
314 return GC_clear_stack_inner(arg
, limit
);
316 BZERO(dummy
, SMALL_CLEAR_SIZE
*sizeof(word
));
320 if (GC_gc_no
> GC_stack_last_cleared
) {
321 /* Start things over, so we clear the entire stack again */
322 if (GC_stack_last_cleared
== 0) GC_high_water
= (word
) GC_stackbottom
;
323 GC_min_sp
= GC_high_water
;
324 GC_stack_last_cleared
= GC_gc_no
;
325 GC_words_allocd_at_reset
= GC_words_allocd
;
327 /* Adjust GC_high_water */
328 MAKE_COOLER(GC_high_water
, WORDS_TO_BYTES(DEGRADE_RATE
) + GC_SLOP
);
329 if (sp HOTTER_THAN GC_high_water
) {
332 MAKE_HOTTER(GC_high_water
, GC_SLOP
);
334 MAKE_HOTTER(limit
, SLOP
);
335 if (sp COOLER_THAN limit
) {
336 limit
&= ~0xf; /* Make it sufficiently aligned for assembly */
337 /* implementations of GC_clear_stack_inner. */
339 return(GC_clear_stack_inner(arg
, limit
));
340 } else if (WORDS_TO_BYTES(GC_words_allocd
- GC_words_allocd_at_reset
)
342 /* Restart clearing process, but limit how much clearing we do. */
344 MAKE_HOTTER(GC_min_sp
, CLEAR_THRESHOLD
/4);
345 if (GC_min_sp HOTTER_THAN GC_high_water
) GC_min_sp
= GC_high_water
;
346 GC_words_allocd_at_reset
= GC_words_allocd
;
353 /* Return a pointer to the base address of p, given a pointer to a */
354 /* an address within an object. Return 0 o.w. */
356 GC_PTR
GC_base(GC_PTR p
)
363 register struct hblk
*h
;
364 register bottom_index
*bi
;
365 register hdr
*candidate_hdr
;
369 if (!GC_is_initialized
) return 0;
372 candidate_hdr
= HDR_FROM_BI(bi
, r
);
373 if (candidate_hdr
== 0) return(0);
374 /* If it's a pointer to the middle of a large object, move it */
375 /* to the beginning. */
376 while (IS_FORWARDING_ADDR_OR_NIL(candidate_hdr
)) {
377 h
= FORWARDED_ADDR(h
,candidate_hdr
);
379 candidate_hdr
= HDR(h
);
381 if (candidate_hdr
-> hb_map
== GC_invalid_map
) return(0);
382 /* Make sure r points to the beginning of the object */
383 r
&= ~(WORDS_TO_BYTES(1) - 1);
385 register int offset
= HBLKDISPL(r
);
386 register signed_word sz
= candidate_hdr
-> hb_sz
;
387 register signed_word map_entry
;
389 map_entry
= MAP_ENTRY((candidate_hdr
-> hb_map
), offset
);
390 if (map_entry
> CPP_MAX_OFFSET
) {
391 map_entry
= (signed_word
)(BYTES_TO_WORDS(offset
)) % sz
;
393 r
-= WORDS_TO_BYTES(map_entry
);
394 limit
= r
+ WORDS_TO_BYTES(sz
);
395 if (limit
> (word
)(h
+ 1)
396 && sz
<= BYTES_TO_WORDS(HBLKSIZE
)) {
399 if ((word
)p
>= limit
) return(0);
405 /* Return the size of an object, given a pointer to its base. */
406 /* (For small obects this also happens to work from interior pointers, */
407 /* but that shouldn't be relied upon.) */
409 size_t GC_size(GC_PTR p
)
416 register hdr
* hhdr
= HDR(p
);
418 sz
= WORDS_TO_BYTES(hhdr
-> hb_sz
);
422 size_t GC_get_heap_size
GC_PROTO(())
424 return ((size_t) GC_heapsize
);
427 size_t GC_get_free_bytes
GC_PROTO(())
429 return ((size_t) GC_large_free_bytes
);
432 size_t GC_get_bytes_since_gc
GC_PROTO(())
434 return ((size_t) WORDS_TO_BYTES(GC_words_allocd
));
437 size_t GC_get_total_bytes
GC_PROTO(())
439 return ((size_t) WORDS_TO_BYTES(GC_words_allocd
+GC_words_allocd_before_gc
));
442 GC_bool GC_is_initialized
= FALSE
;
451 if (!GC_is_initialized
) InitializeCriticalSection(&GC_allocate_ml
);
459 # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
460 /* Make sure marker threads and started and thread local */
461 /* allocation is initialized, in case we didn't get */
462 /* called from GC_init_parallel(); */
464 extern void GC_init_parallel(void);
467 # endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
470 #if defined(MSWIN32) || defined(MSWINCE)
471 CRITICAL_SECTION GC_write_cs
;
475 extern void GC_init_win32
GC_PROTO((void));
478 extern void GC_setpagesize();
482 extern void GC_set_and_save_fault_handler
GC_PROTO((void (*handler
)(int)));
484 static void looping_handler(sig
)
487 GC_err_printf1("Caught signal %d: looping in handler\n", sig
);
493 extern GC_bool GC_no_win32_dlls
;
495 # define GC_no_win32_dlls FALSE
500 # if !defined(THREADS) && defined(GC_ASSERTIONS)
503 word initial_heap_sz
= (word
)MINHINCR
;
505 if (GC_is_initialized
) return;
509 # if defined(MSWIN32) || defined(MSWINCE)
510 InitializeCriticalSection(&GC_write_cs
);
513 if (0 != GETENV("GC_PRINT_STATS")) {
516 if (0 != GETENV("GC_FIND_LEAK")) {
519 if (0 != GETENV("GC_ALL_INTERIOR_POINTERS")) {
520 GC_all_interior_pointers
= 1;
522 if (0 != GETENV("GC_DONT_GC")) {
525 if (0 != GETENV("GC_PRINT_BACK_HEIGHT")) {
526 GC_print_back_height
= 1;
528 if (0 != GETENV("GC_NO_BLACKLIST_WARNING")) {
529 GC_large_alloc_warn_interval
= LONG_MAX
;
532 char * time_limit_string
= GETENV("GC_PAUSE_TIME_TARGET");
533 if (0 != time_limit_string
) {
534 long time_limit
= atol(time_limit_string
);
535 if (time_limit
< 5) {
536 WARN("GC_PAUSE_TIME_TARGET environment variable value too small "
537 "or bad syntax: Ignoring\n", 0);
539 GC_time_limit
= time_limit
;
544 char * interval_string
= GETENV("GC_LARGE_ALLOC_WARN_INTERVAL");
545 if (0 != interval_string
) {
546 long interval
= atol(interval_string
);
548 WARN("GC_LARGE_ALLOC_WARN_INTERVAL environment variable has "
549 "bad value: Ignoring\n", 0);
551 GC_large_alloc_warn_interval
= interval
;
556 if (0 != GETENV("GC_LOOP_ON_ABORT")) {
557 GC_set_and_save_fault_handler(looping_handler
);
560 /* Adjust normal object descriptor for extra allocation. */
561 if (ALIGNMENT
> GC_DS_TAGS
&& EXTRA_BYTES
!= 0) {
562 GC_obj_kinds
[NORMAL
].ok_descriptor
= ((word
)(-ALIGNMENT
) | GC_DS_LENGTH
);
565 GC_exclude_static_roots(beginGC_arrays
, endGC_arrays
);
566 GC_exclude_static_roots(beginGC_obj_kinds
, endGC_obj_kinds
);
567 # ifdef SEPARATE_GLOBALS
568 GC_exclude_static_roots(beginGC_objfreelist
, endGC_objfreelist
);
569 GC_exclude_static_roots(beginGC_aobjfreelist
, endGC_aobjfreelist
);
574 # if defined(SEARCH_FOR_DATA_START)
575 GC_init_linux_data_start();
577 # if (defined(NETBSD) || defined(OPENBSD)) && defined(__ELF__)
578 GC_init_netbsd_elf();
580 # if defined(GC_PTHREADS) || defined(GC_SOLARIS_THREADS)
583 # ifdef GC_SOLARIS_THREADS
584 /* We need dirty bits in order to find live stack sections. */
587 # if !defined(THREADS) || defined(GC_PTHREADS) || defined(GC_WIN32_THREADS) \
588 || defined(GC_SOLARIS_THREADS)
589 if (GC_stackbottom
== 0) {
590 GC_stackbottom
= GC_get_stack_base();
591 # if defined(LINUX) && defined(IA64)
592 GC_register_stackbottom
= GC_get_register_stack_base();
596 GC_ASSERT(sizeof (ptr_t
) == sizeof(word
));
597 GC_ASSERT(sizeof (signed_word
) == sizeof(word
));
598 GC_ASSERT(sizeof (struct hblk
) == HBLKSIZE
);
600 # if defined(STACK_GROWS_UP) && defined(STACK_GROWS_DOWN)
602 "Only one of STACK_GROWS_UP and STACK_GROWS_DOWN should be defd\n");
604 # if !defined(STACK_GROWS_UP) && !defined(STACK_GROWS_DOWN)
606 "One of STACK_GROWS_UP and STACK_GROWS_DOWN should be defd\n");
608 # ifdef STACK_GROWS_DOWN
609 GC_ASSERT((word
)(&dummy
) <= (word
)GC_stackbottom
);
611 GC_ASSERT((word
)(&dummy
) >= (word
)GC_stackbottom
);
614 # if !defined(_AUX_SOURCE) || defined(__GNUC__)
615 GC_ASSERT((word
)(-1) > (word
)0);
616 /* word should be unsigned */
618 GC_ASSERT((signed_word
)(-1) < (signed_word
)0);
620 /* Add initial guess of root sets. Do this first, since sbrk(0) */
622 GC_register_data_segments();
627 char * sz_str
= GETENV("GC_INITIAL_HEAP_SIZE");
628 if (sz_str
!= NULL
) {
629 initial_heap_sz
= atoi(sz_str
);
630 if (initial_heap_sz
<= MINHINCR
* HBLKSIZE
) {
631 WARN("Bad initial heap size %s - ignoring it.\n",
634 initial_heap_sz
= divHBLKSZ(initial_heap_sz
);
637 if (!GC_expand_hp_inner(initial_heap_sz
)) {
638 GC_err_printf0("Can't start up: not enough memory\n");
641 /* Preallocate large object map. It's otherwise inconvenient to */
642 /* deal with failure. */
643 if (!GC_add_map_entry((word
)0)) {
644 GC_err_printf0("Can't start up: not enough memory\n");
647 GC_register_displacement_inner(0L);
652 if (PCR_IL_Lock(PCR_Bool_false
, PCR_allSigsBlocked
, PCR_waitForever
)
654 ABORT("Can't lock load state\n");
655 } else if (PCR_IL_Unlock() != PCR_ERes_okay
) {
656 ABORT("Can't unlock load state\n");
661 # if !defined(SMALL_CONFIG)
662 if (!GC_no_win32_dlls
&& 0 != GETENV("GC_ENABLE_INCREMENTAL")) {
663 GC_ASSERT(!GC_incremental
);
665 # ifndef GC_SOLARIS_THREADS
668 GC_ASSERT(GC_words_allocd
== 0)
669 GC_incremental
= TRUE
;
671 # endif /* !SMALL_CONFIG */
672 /* Get black list set up and/or incrmental GC started */
673 if (!GC_dont_precollect
|| GC_incremental
) GC_gcollect_inner();
674 GC_is_initialized
= TRUE
;
675 # ifdef STUBBORN_ALLOC
678 /* Convince lint that some things are used */
681 extern char * GC_copyright
[];
682 extern int GC_read();
683 extern void GC_register_finalizer_no_order();
685 GC_noop(GC_copyright
, GC_find_header
,
686 GC_push_one
, GC_call_with_alloc_lock
, GC_read
,
688 # ifndef NO_DEBUGGING
691 GC_register_finalizer_no_order
);
696 void GC_enable_incremental
GC_PROTO(())
698 # if !defined(SMALL_CONFIG)
704 if (GC_incremental
) goto out
;
706 if (GC_no_win32_dlls
) goto out
;
707 # ifndef GC_SOLARIS_THREADS
710 if (!GC_is_initialized
) {
713 if (GC_incremental
) goto out
;
715 /* Can't easily do it. */
720 if (GC_words_allocd
> 0) {
721 /* There may be unmarked reachable objects */
723 } /* else we're OK in assuming everything's */
724 /* clean since nothing can point to an */
725 /* unmarked object. */
727 GC_incremental
= TRUE
;
736 #if defined(MSWIN32) || defined(MSWINCE)
737 # define LOG_FILE _T("gc.log")
739 HANDLE GC_stdout
= 0;
743 if (GC_is_initialized
) {
744 DeleteCriticalSection(&GC_write_cs
);
748 int GC_write(buf
, len
)
756 EnterCriticalSection(&GC_write_cs
);
757 if (GC_stdout
== INVALID_HANDLE_VALUE
) {
759 } else if (GC_stdout
== 0) {
760 GC_stdout
= CreateFile(LOG_FILE
, GENERIC_WRITE
,
761 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
762 NULL
, CREATE_ALWAYS
, FILE_FLAG_WRITE_THROUGH
,
764 if (GC_stdout
== INVALID_HANDLE_VALUE
) ABORT("Open of log file failed");
766 tmp
= WriteFile(GC_stdout
, buf
, len
, &written
, NULL
);
769 LeaveCriticalSection(&GC_write_cs
);
770 return tmp
? (int)written
: -1;
775 #if defined(OS2) || defined(MACOS)
776 FILE * GC_stdout
= NULL
;
777 FILE * GC_stderr
= NULL
;
778 int GC_tmp
; /* Should really be local ... */
782 if (GC_stdout
== NULL
) {
785 if (GC_stderr
== NULL
) {
791 #if !defined(OS2) && !defined(MACOS) && !defined(MSWIN32) && !defined(MSWINCE)
799 #if !defined(MSWIN32) && !defined(MSWINCE) && !defined(OS2) \
800 && !defined(MACOS) && !defined(ECOS) && !defined(NOSYS)
801 int GC_write(fd
, buf
, len
)
806 register int bytes_written
= 0;
809 while (bytes_written
< len
) {
810 # ifdef GC_SOLARIS_THREADS
811 result
= syscall(SYS_write
, fd
, buf
+ bytes_written
,
812 len
- bytes_written
);
814 result
= write(fd
, buf
+ bytes_written
, len
- bytes_written
);
816 if (-1 == result
) return(result
);
817 bytes_written
+= result
;
819 return(bytes_written
);
824 int GC_write(fd
, buf
, len
)
826 _Jv_diag_write (buf
, len
);
832 int GC_write(fd
, buf
, len
)
840 #if defined(MSWIN32) || defined(MSWINCE)
841 # define WRITE(f, buf, len) GC_write(buf, len)
843 # if defined(OS2) || defined(MACOS)
844 # define WRITE(f, buf, len) (GC_set_files(), \
845 GC_tmp = fwrite((buf), 1, (len), (f)), \
848 # define WRITE(f, buf, len) GC_write((f), (buf), (len))
852 /* A version of printf that is unlikely to call malloc, and is thus safer */
853 /* to call from the collector in case malloc has been bound to GC_malloc. */
854 /* Assumes that no more than 1023 characters are written at once. */
855 /* Assumes that all arguments have been converted to something of the */
856 /* same size as long, and that the format conversions expect something */
858 void GC_printf(format
, a
, b
, c
, d
, e
, f
)
859 GC_CONST
char * format
;
860 long a
, b
, c
, d
, e
, f
;
864 if (GC_quiet
) return;
866 (void) sprintf(buf
, format
, a
, b
, c
, d
, e
, f
);
867 if (buf
[1024] != 0x15) ABORT("GC_printf clobbered stack");
868 if (WRITE(GC_stdout
, buf
, strlen(buf
)) < 0) ABORT("write to stdout failed");
871 void GC_err_printf(format
, a
, b
, c
, d
, e
, f
)
872 GC_CONST
char * format
;
873 long a
, b
, c
, d
, e
, f
;
878 (void) sprintf(buf
, format
, a
, b
, c
, d
, e
, f
);
879 if (buf
[1024] != 0x15) ABORT("GC_err_printf clobbered stack");
880 if (WRITE(GC_stderr
, buf
, strlen(buf
)) < 0) ABORT("write to stderr failed");
886 if (WRITE(GC_stderr
, s
, strlen(s
)) < 0) ABORT("write to stderr failed");
889 #if defined(LINUX) && !defined(SMALL_CONFIG)
890 void GC_err_write(buf
, len
)
894 if (WRITE(GC_stderr
, buf
, len
) < 0) ABORT("write to stderr failed");
898 # if defined(__STDC__) || defined(__cplusplus)
899 void GC_default_warn_proc(char *msg
, GC_word arg
)
901 void GC_default_warn_proc(msg
, arg
)
906 GC_err_printf1(msg
, (unsigned long)arg
);
909 GC_warn_proc GC_current_warn_proc
= GC_default_warn_proc
;
911 # if defined(__STDC__) || defined(__cplusplus)
912 GC_warn_proc
GC_set_warn_proc(GC_warn_proc p
)
914 GC_warn_proc
GC_set_warn_proc(p
)
921 result
= GC_current_warn_proc
;
922 GC_current_warn_proc
= p
;
932 # if defined(MSWIN32)
933 (void) MessageBoxA(NULL
, msg
, "Fatal error in gc", MB_ICONERROR
|MB_OK
);
936 GC_err_printf1("%s\n", msg
);
938 if (GETENV("GC_LOOP_ON_ABORT") != NULL
) {
939 /* In many cases it's easier to debug a running process. */
940 /* It's arguably nicer to sleep, but that makes it harder */
941 /* to look at the thread if the debugger doesn't know much */
955 #ifdef HAVE_BUILTIN_BACKTRACE
956 # include <execinfo.h>
962 void GC_print_callers (info
)
963 struct callinfo info
[NFRAMES
];
968 GC_err_printf0("\tCaller at allocation:\n");
970 GC_err_printf0("\tCall chain at allocation:\n");
972 for (i
= 0; i
< NFRAMES
; i
++) {
973 if (info
[i
].ci_pc
== 0) break;
978 GC_err_printf0("\t\targs: ");
979 for (j
= 0; j
< NARGS
; j
++) {
980 if (j
!= 0) GC_err_printf0(", ");
981 GC_err_printf2("%d (0x%X)", ~(info
[i
].ci_arg
[j
]),
982 ~(info
[i
].ci_arg
[j
]));
984 GC_err_printf0("\n");
987 # if defined(HAVE_BUILTIN_BACKTRACE) && !defined(REDIRECT_MALLOC)
988 /* Unfortunately backtrace_symbols calls malloc, which makes */
989 /* it dangersous if that has been redirected. */
992 backtrace_symbols((void **)(&(info
[i
].ci_pc
)), 1);
993 char *name
= sym_name
[0];
994 GC_bool found_it
= (strchr(name
, '(') != 0);
999 static char exe_name
[EXE_SZ
];
1001 char cmd_buf
[CMD_SZ
];
1002 # define RESULT_SZ 200
1003 static char result_buf
[RESULT_SZ
];
1005 static GC_bool found_exe_name
= FALSE
;
1006 static GC_bool will_fail
= FALSE
;
1008 /* Unfortunately, this is the common case for the */
1009 /* main executable. */
1010 /* Try to get it via a hairy and expensive scheme. */
1011 /* First we get the name of the executable: */
1012 if (will_fail
) goto out
;
1013 if (!found_exe_name
) {
1014 ret_code
= readlink("/proc/self/exe", exe_name
, EXE_SZ
);
1015 if (ret_code
< 0 || ret_code
>= EXE_SZ
|| exe_name
[0] != '/') {
1016 will_fail
= TRUE
; /* Dont try again. */
1019 exe_name
[ret_code
] = '\0';
1020 found_exe_name
= TRUE
;
1022 /* Then we use popen to start addr2line -e <exe> <addr> */
1023 /* There are faster ways to do this, but hopefully this */
1024 /* isn't time critical. */
1025 sprintf(cmd_buf
, "/usr/bin/addr2line -e %s 0x%lx", exe_name
,
1026 (unsigned long)info
[i
].ci_pc
);
1027 pipe
= popen(cmd_buf
, "r");
1028 if (pipe
< 0 || fgets(result_buf
, RESULT_SZ
, pipe
) == 0) {
1032 result_len
= strlen(result_buf
);
1033 if (result_buf
[result_len
- 1] == '\n') --result_len
;
1034 if (result_buf
[0] == '?'
1035 || result_buf
[result_len
-2] == ':'
1036 && result_buf
[result_len
-1] == '0')
1038 if (result_len
< RESULT_SZ
- 25) {
1039 /* Add in hex address */
1040 sprintf(result_buf
+ result_len
, " [0x%lx]",
1041 (unsigned long)info
[i
].ci_pc
);
1048 GC_err_printf1("\t\t%s\n", name
);
1052 GC_err_printf1("\t\t##PC##= 0x%lx\n", info
[i
].ci_pc
);
1057 #endif /* SAVE_CALL_CHAIN */
1059 /* Needed by SRC_M3, gcj, and should perhaps be the official interface */
1060 /* to GC_dont_gc. */
1071 #if !defined(NO_DEBUGGING)
1075 GC_printf0("***Static roots:\n");
1076 GC_print_static_roots();
1077 GC_printf0("\n***Heap sections:\n");
1078 GC_print_heap_sects();
1079 GC_printf0("\n***Free blocks:\n");
1080 GC_print_hblkfreelist();
1081 GC_printf0("\n***Blocks in use:\n");
1082 GC_print_block_list();
1085 #endif /* NO_DEBUGGING */