* c-decl.c (finish_decl): Don't set DECL_C_HARD_REGISTER for
[official-gcc.git] / boehm-gc / misc.c
blob9edb9453bb4169ef3bc8fd89e514676b4be3ac93
1 /*
2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 * Permission is hereby granted to use or copy this program
9 * for any purpose, provided the above notices are retained on all copies.
10 * Permission to modify the code and to distribute modified code is granted,
11 * provided the above notices are retained, and a notice that the code was
12 * modified is included with the above copyright notice.
14 /* Boehm, July 31, 1995 5:02 pm PDT */
17 #include <stdio.h>
18 #include <signal.h>
20 #define I_HIDE_POINTERS /* To make GC_call_with_alloc_lock visible */
21 #include "gc_priv.h"
23 #ifdef SOLARIS_THREADS
24 # include <sys/syscall.h>
25 #endif
26 #ifdef MSWIN32
27 # include <windows.h>
28 #endif
30 # ifdef THREADS
31 # ifdef PCR
32 # include "il/PCR_IL.h"
33 PCR_Th_ML GC_allocate_ml;
34 # else
35 # ifdef SRC_M3
36 /* Critical section counter is defined in the M3 runtime */
37 /* That's all we use. */
38 # else
39 # ifdef SOLARIS_THREADS
40 mutex_t GC_allocate_ml; /* Implicitly initialized. */
41 # else
42 # ifdef WIN32_THREADS
43 GC_API CRITICAL_SECTION GC_allocate_ml;
44 # else
45 # if defined(IRIX_THREADS) || defined(IRIX_JDK_THREADS) \
46 || (defined(LINUX_THREADS) && defined(USE_SPIN_LOCK))
47 pthread_t GC_lock_holder = NO_THREAD;
48 # else
49 # if defined(HPUX_THREADS) \
50 || defined(LINUX_THREADS) && !defined(USE_SPIN_LOCK)
51 pthread_mutex_t GC_allocate_ml = PTHREAD_MUTEX_INITIALIZER;
52 # else
53 --> declare allocator lock here
54 # endif
55 # endif
56 # endif
57 # endif
58 # endif
59 # endif
60 # endif
62 #ifdef ECOS
63 #undef STACKBASE
64 #endif
66 GC_FAR struct _GC_arrays GC_arrays /* = { 0 } */;
69 GC_bool GC_debugging_started = FALSE;
70 /* defined here so we don't have to load debug_malloc.o */
72 void (*GC_check_heap)() = (void (*)())0;
74 void (*GC_start_call_back)() = (void (*)())0;
76 ptr_t GC_stackbottom = 0;
78 GC_bool GC_dont_gc = 0;
80 GC_bool GC_quiet = 0;
82 #ifdef FIND_LEAK
83 int GC_find_leak = 1;
84 #else
85 int GC_find_leak = 0;
86 #endif
88 /*ARGSUSED*/
89 GC_PTR GC_default_oom_fn GC_PROTO((size_t bytes_requested))
91 return(0);
94 GC_PTR (*GC_oom_fn) GC_PROTO((size_t bytes_requested)) = GC_default_oom_fn;
96 extern signed_word GC_mem_found;
98 # ifdef MERGE_SIZES
99 /* Set things up so that GC_size_map[i] >= words(i), */
100 /* but not too much bigger */
101 /* and so that size_map contains relatively few distinct entries */
102 /* This is stolen from Russ Atkinson's Cedar quantization */
103 /* alogrithm (but we precompute it). */
106 void GC_init_size_map()
108 register unsigned i;
110 /* Map size 0 to something bigger. */
111 /* This avoids problems at lower levels. */
112 /* One word objects don't have to be 2 word aligned. */
113 for (i = 0; i < sizeof(word); i++) {
114 GC_size_map[i] = MIN_WORDS;
116 # if MIN_WORDS > 1
117 GC_size_map[sizeof(word)] = MIN_WORDS;
118 # else
119 GC_size_map[sizeof(word)] = ROUNDED_UP_WORDS(sizeof(word));
120 # endif
121 for (i = sizeof(word) + 1; i <= 8 * sizeof(word); i++) {
122 # ifdef ALIGN_DOUBLE
123 GC_size_map[i] = (ROUNDED_UP_WORDS(i) + 1) & (~1);
124 # else
125 GC_size_map[i] = ROUNDED_UP_WORDS(i);
126 # endif
128 for (i = 8*sizeof(word) + 1; i <= 16 * sizeof(word); i++) {
129 GC_size_map[i] = (ROUNDED_UP_WORDS(i) + 1) & (~1);
131 # ifdef GC_GCJ_SUPPORT
132 /* Make all sizes up to 32 words predictable, so that a */
133 /* compiler can statically perform the same computation, */
134 /* or at least a computation that results in similar size */
135 /* classes. */
136 for (i = 16*sizeof(word) + 1; i <= 32 * sizeof(word); i++) {
137 GC_size_map[i] = (ROUNDED_UP_WORDS(i) + 3) & (~3);
139 # endif
140 /* We leave the rest of the array to be filled in on demand. */
143 /* Fill in additional entries in GC_size_map, including the ith one */
144 /* We assume the ith entry is currently 0. */
145 /* Note that a filled in section of the array ending at n always */
146 /* has length at least n/4. */
147 void GC_extend_size_map(i)
148 word i;
150 word orig_word_sz = ROUNDED_UP_WORDS(i);
151 word word_sz = orig_word_sz;
152 register word byte_sz = WORDS_TO_BYTES(word_sz);
153 /* The size we try to preserve. */
154 /* Close to to i, unless this would */
155 /* introduce too many distinct sizes. */
156 word smaller_than_i = byte_sz - (byte_sz >> 3);
157 word much_smaller_than_i = byte_sz - (byte_sz >> 2);
158 register word low_limit; /* The lowest indexed entry we */
159 /* initialize. */
160 register word j;
162 if (GC_size_map[smaller_than_i] == 0) {
163 low_limit = much_smaller_than_i;
164 while (GC_size_map[low_limit] != 0) low_limit++;
165 } else {
166 low_limit = smaller_than_i + 1;
167 while (GC_size_map[low_limit] != 0) low_limit++;
168 word_sz = ROUNDED_UP_WORDS(low_limit);
169 word_sz += word_sz >> 3;
170 if (word_sz < orig_word_sz) word_sz = orig_word_sz;
172 # ifdef ALIGN_DOUBLE
173 word_sz += 1;
174 word_sz &= ~1;
175 # endif
176 if (word_sz > MAXOBJSZ) {
177 word_sz = MAXOBJSZ;
179 /* If we can fit the same number of larger objects in a block, */
180 /* do so. */
182 size_t number_of_objs = BODY_SZ/word_sz;
183 word_sz = BODY_SZ/number_of_objs;
184 # ifdef ALIGN_DOUBLE
185 word_sz &= ~1;
186 # endif
188 byte_sz = WORDS_TO_BYTES(word_sz);
189 # ifdef ADD_BYTE_AT_END
190 /* We need one extra byte; don't fill in GC_size_map[byte_sz] */
191 byte_sz--;
192 # endif
194 for (j = low_limit; j <= byte_sz; j++) GC_size_map[j] = word_sz;
196 # endif
200 * The following is a gross hack to deal with a problem that can occur
201 * on machines that are sloppy about stack frame sizes, notably SPARC.
202 * Bogus pointers may be written to the stack and not cleared for
203 * a LONG time, because they always fall into holes in stack frames
204 * that are not written. We partially address this by clearing
205 * sections of the stack whenever we get control.
207 word GC_stack_last_cleared = 0; /* GC_no when we last did this */
208 # ifdef THREADS
209 # define CLEAR_SIZE 512
210 # else
211 # define CLEAR_SIZE 213
212 # endif
213 # define DEGRADE_RATE 50
215 word GC_min_sp; /* Coolest stack pointer value from which we've */
216 /* already cleared the stack. */
218 # ifdef STACK_GROWS_DOWN
219 # define COOLER_THAN >
220 # define HOTTER_THAN <
221 # define MAKE_COOLER(x,y) if ((word)(x)+(y) > (word)(x)) {(x) += (y);} \
222 else {(x) = (word)ONES;}
223 # define MAKE_HOTTER(x,y) (x) -= (y)
224 # else
225 # define COOLER_THAN <
226 # define HOTTER_THAN >
227 # define MAKE_COOLER(x,y) if ((word)(x)-(y) < (word)(x)) {(x) -= (y);} else {(x) = 0;}
228 # define MAKE_HOTTER(x,y) (x) += (y)
229 # endif
231 word GC_high_water;
232 /* "hottest" stack pointer value we have seen */
233 /* recently. Degrades over time. */
235 word GC_words_allocd_at_reset;
237 #if defined(ASM_CLEAR_CODE) && !defined(THREADS)
238 extern ptr_t GC_clear_stack_inner();
239 #endif
241 #if !defined(ASM_CLEAR_CODE) && !defined(THREADS)
242 /* Clear the stack up to about limit. Return arg. */
243 /*ARGSUSED*/
244 ptr_t GC_clear_stack_inner(arg, limit)
245 ptr_t arg;
246 word limit;
248 word dummy[CLEAR_SIZE];
250 BZERO(dummy, CLEAR_SIZE*sizeof(word));
251 if ((word)(dummy) COOLER_THAN limit) {
252 (void) GC_clear_stack_inner(arg, limit);
254 /* Make sure the recursive call is not a tail call, and the bzero */
255 /* call is not recognized as dead code. */
256 GC_noop1((word)dummy);
257 return(arg);
259 #endif
261 /* Clear some of the inaccessible part of the stack. Returns its */
262 /* argument, so it can be used in a tail call position, hence clearing */
263 /* another frame. */
264 ptr_t GC_clear_stack(arg)
265 ptr_t arg;
267 register word sp = (word)GC_approx_sp(); /* Hotter than actual sp */
268 # ifdef THREADS
269 word dummy[CLEAR_SIZE];
270 # else
271 register word limit;
272 # endif
274 # define SLOP 400
275 /* Extra bytes we clear every time. This clears our own */
276 /* activation record, and should cause more frequent */
277 /* clearing near the cold end of the stack, a good thing. */
278 # define GC_SLOP 4000
279 /* We make GC_high_water this much hotter than we really saw */
280 /* saw it, to cover for GC noise etc. above our current frame. */
281 # define CLEAR_THRESHOLD 100000
282 /* We restart the clearing process after this many bytes of */
283 /* allocation. Otherwise very heavily recursive programs */
284 /* with sparse stacks may result in heaps that grow almost */
285 /* without bounds. As the heap gets larger, collection */
286 /* frequency decreases, thus clearing frequency would decrease, */
287 /* thus more junk remains accessible, thus the heap gets */
288 /* larger ... */
289 # ifdef THREADS
290 BZERO(dummy, CLEAR_SIZE*sizeof(word));
291 # else
292 if (GC_gc_no > GC_stack_last_cleared) {
293 /* Start things over, so we clear the entire stack again */
294 if (GC_stack_last_cleared == 0) GC_high_water = (word) GC_stackbottom;
295 GC_min_sp = GC_high_water;
296 GC_stack_last_cleared = GC_gc_no;
297 GC_words_allocd_at_reset = GC_words_allocd;
299 /* Adjust GC_high_water */
300 MAKE_COOLER(GC_high_water, WORDS_TO_BYTES(DEGRADE_RATE) + GC_SLOP);
301 if (sp HOTTER_THAN GC_high_water) {
302 GC_high_water = sp;
304 MAKE_HOTTER(GC_high_water, GC_SLOP);
305 limit = GC_min_sp;
306 MAKE_HOTTER(limit, SLOP);
307 if (sp COOLER_THAN limit) {
308 limit &= ~0xf; /* Make it sufficiently aligned for assembly */
309 /* implementations of GC_clear_stack_inner. */
310 GC_min_sp = sp;
311 return(GC_clear_stack_inner(arg, limit));
312 } else if (WORDS_TO_BYTES(GC_words_allocd - GC_words_allocd_at_reset)
313 > CLEAR_THRESHOLD) {
314 /* Restart clearing process, but limit how much clearing we do. */
315 GC_min_sp = sp;
316 MAKE_HOTTER(GC_min_sp, CLEAR_THRESHOLD/4);
317 if (GC_min_sp HOTTER_THAN GC_high_water) GC_min_sp = GC_high_water;
318 GC_words_allocd_at_reset = GC_words_allocd;
320 # endif
321 return(arg);
325 /* Return a pointer to the base address of p, given a pointer to a */
326 /* an address within an object. Return 0 o.w. */
327 # ifdef __STDC__
328 GC_PTR GC_base(GC_PTR p)
329 # else
330 GC_PTR GC_base(p)
331 GC_PTR p;
332 # endif
334 register word r;
335 register struct hblk *h;
336 register bottom_index *bi;
337 register hdr *candidate_hdr;
338 register word limit;
340 r = (word)p;
341 if (!GC_is_initialized) return 0;
342 h = HBLKPTR(r);
343 GET_BI(r, bi);
344 candidate_hdr = HDR_FROM_BI(bi, r);
345 if (candidate_hdr == 0) return(0);
346 /* If it's a pointer to the middle of a large object, move it */
347 /* to the beginning. */
348 while (IS_FORWARDING_ADDR_OR_NIL(candidate_hdr)) {
349 h = FORWARDED_ADDR(h,candidate_hdr);
350 r = (word)h + HDR_BYTES;
351 candidate_hdr = HDR(h);
353 if (candidate_hdr -> hb_map == GC_invalid_map) return(0);
354 /* Make sure r points to the beginning of the object */
355 r &= ~(WORDS_TO_BYTES(1) - 1);
357 register int offset = (char *)r - (char *)(HBLKPTR(r));
358 register signed_word sz = candidate_hdr -> hb_sz;
360 # ifdef ALL_INTERIOR_POINTERS
361 register map_entry_type map_entry;
363 map_entry = MAP_ENTRY((candidate_hdr -> hb_map), offset);
364 if (map_entry == OBJ_INVALID) {
365 return(0);
367 r -= WORDS_TO_BYTES(map_entry);
368 limit = r + WORDS_TO_BYTES(sz);
369 # else
370 register int correction;
372 offset = BYTES_TO_WORDS(offset - HDR_BYTES);
373 correction = offset % sz;
374 r -= (WORDS_TO_BYTES(correction));
375 limit = r + WORDS_TO_BYTES(sz);
376 if (limit > (word)(h + 1)
377 && sz <= BYTES_TO_WORDS(HBLKSIZE) - HDR_WORDS) {
378 return(0);
380 # endif
381 if ((word)p >= limit) return(0);
383 return((GC_PTR)r);
387 /* Return the size of an object, given a pointer to its base. */
388 /* (For small obects this also happens to work from interior pointers, */
389 /* but that shouldn't be relied upon.) */
390 # ifdef __STDC__
391 size_t GC_size(GC_PTR p)
392 # else
393 size_t GC_size(p)
394 GC_PTR p;
395 # endif
397 register int sz;
398 register hdr * hhdr = HDR(p);
400 sz = WORDS_TO_BYTES(hhdr -> hb_sz);
401 if (sz < 0) {
402 return(-sz);
403 } else {
404 return(sz);
408 size_t GC_get_heap_size GC_PROTO(())
410 return ((size_t) GC_heapsize);
413 size_t GC_get_free_bytes GC_PROTO(())
415 return ((size_t) GC_large_free_bytes);
418 size_t GC_get_bytes_since_gc GC_PROTO(())
420 return ((size_t) WORDS_TO_BYTES(GC_words_allocd));
423 GC_bool GC_is_initialized = FALSE;
425 void GC_init()
427 DCL_LOCK_STATE;
429 DISABLE_SIGNALS();
430 LOCK();
431 GC_init_inner();
432 UNLOCK();
433 ENABLE_SIGNALS();
437 #ifdef MSWIN32
438 extern void GC_init_win32();
439 #endif
441 extern void GC_setpagesize();
443 void GC_init_inner()
445 # ifndef THREADS
446 word dummy;
447 # endif
449 if (GC_is_initialized) return;
450 GC_setpagesize();
451 GC_exclude_static_roots(beginGC_arrays, end_gc_area);
452 # ifdef PRINTSTATS
453 if ((ptr_t)endGC_arrays != (ptr_t)(&GC_obj_kinds)) {
454 GC_printf0("Reordering linker, didn't exclude obj_kinds\n");
456 # endif
457 # ifdef MSWIN32
458 GC_init_win32();
459 # endif
460 # if defined(SEARCH_FOR_DATA_START)
461 /* This doesn't really work if the collector is in a shared library. */
462 GC_init_linux_data_start();
463 # endif
464 # ifdef SOLARIS_THREADS
465 GC_thr_init();
466 /* We need dirty bits in order to find live stack sections. */
467 GC_dirty_init();
468 # endif
469 # if defined(IRIX_THREADS) || defined(LINUX_THREADS) \
470 || defined(IRIX_JDK_THREADS) || defined(HPUX_THREADS)
471 GC_thr_init();
472 # endif
473 # if !defined(THREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) \
474 || defined(IRIX_THREADS) || defined(LINUX_THREADS) \
475 || defined(HPUX_THREADS)
476 if (GC_stackbottom == 0) {
477 GC_stackbottom = GC_get_stack_base();
479 # endif
480 if (sizeof (ptr_t) != sizeof(word)) {
481 ABORT("sizeof (ptr_t) != sizeof(word)\n");
483 if (sizeof (signed_word) != sizeof(word)) {
484 ABORT("sizeof (signed_word) != sizeof(word)\n");
486 if (sizeof (struct hblk) != HBLKSIZE) {
487 ABORT("sizeof (struct hblk) != HBLKSIZE\n");
489 # ifndef THREADS
490 # if defined(STACK_GROWS_UP) && defined(STACK_GROWS_DOWN)
491 ABORT(
492 "Only one of STACK_GROWS_UP and STACK_GROWS_DOWN should be defd\n");
493 # endif
494 # if !defined(STACK_GROWS_UP) && !defined(STACK_GROWS_DOWN)
495 ABORT(
496 "One of STACK_GROWS_UP and STACK_GROWS_DOWN should be defd\n");
497 # endif
498 # ifdef STACK_GROWS_DOWN
499 if ((word)(&dummy) > (word)GC_stackbottom) {
500 GC_err_printf0(
501 "STACK_GROWS_DOWN is defd, but stack appears to grow up\n");
502 # ifndef UTS4 /* Compiler bug workaround */
503 GC_err_printf2("sp = 0x%lx, GC_stackbottom = 0x%lx\n",
504 (unsigned long) (&dummy),
505 (unsigned long) GC_stackbottom);
506 # endif
507 ABORT("stack direction 3\n");
509 # else
510 if ((word)(&dummy) < (word)GC_stackbottom) {
511 GC_err_printf0(
512 "STACK_GROWS_UP is defd, but stack appears to grow down\n");
513 GC_err_printf2("sp = 0x%lx, GC_stackbottom = 0x%lx\n",
514 (unsigned long) (&dummy),
515 (unsigned long) GC_stackbottom);
516 ABORT("stack direction 4");
518 # endif
519 # endif
520 # if !defined(_AUX_SOURCE) || defined(__GNUC__)
521 if ((word)(-1) < (word)0) {
522 GC_err_printf0("The type word should be an unsigned integer type\n");
523 GC_err_printf0("It appears to be signed\n");
524 ABORT("word");
526 # endif
527 if ((signed_word)(-1) >= (signed_word)0) {
528 GC_err_printf0(
529 "The type signed_word should be a signed integer type\n");
530 GC_err_printf0("It appears to be unsigned\n");
531 ABORT("signed_word");
534 /* Add initial guess of root sets. Do this first, since sbrk(0) */
535 /* might be used. */
536 GC_register_data_segments();
537 GC_init_headers();
538 GC_bl_init();
539 GC_mark_init();
540 if (!GC_expand_hp_inner((word)MINHINCR)) {
541 GC_err_printf0("Can't start up: not enough memory\n");
542 EXIT();
544 /* Preallocate large object map. It's otherwise inconvenient to */
545 /* deal with failure. */
546 if (!GC_add_map_entry((word)0)) {
547 GC_err_printf0("Can't start up: not enough memory\n");
548 EXIT();
550 GC_register_displacement_inner(0L);
551 # ifdef MERGE_SIZES
552 GC_init_size_map();
553 # endif
554 # ifdef PCR
555 if (PCR_IL_Lock(PCR_Bool_false, PCR_allSigsBlocked, PCR_waitForever)
556 != PCR_ERes_okay) {
557 ABORT("Can't lock load state\n");
558 } else if (PCR_IL_Unlock() != PCR_ERes_okay) {
559 ABORT("Can't unlock load state\n");
561 PCR_IL_Unlock();
562 GC_pcr_install();
563 # endif
564 /* Get black list set up */
565 GC_gcollect_inner();
566 # ifdef STUBBORN_ALLOC
567 GC_stubborn_init();
568 # endif
569 GC_is_initialized = TRUE;
570 /* Convince lint that some things are used */
571 # ifdef LINT
573 extern char * GC_copyright[];
574 extern int GC_read();
575 extern void GC_register_finalizer_no_order();
577 GC_noop(GC_copyright, GC_find_header,
578 GC_push_one, GC_call_with_alloc_lock, GC_read,
579 GC_dont_expand,
580 # ifndef NO_DEBUGGING
581 GC_dump,
582 # endif
583 GC_register_finalizer_no_order);
585 # endif
588 void GC_enable_incremental GC_PROTO(())
590 # if !defined(SMALL_CONFIG)
591 if (!GC_find_leak) {
592 DCL_LOCK_STATE;
594 DISABLE_SIGNALS();
595 LOCK();
596 if (GC_incremental) goto out;
597 GC_setpagesize();
598 # ifdef MSWIN32
600 extern GC_bool GC_is_win32s();
602 /* VirtualProtect is not functional under win32s. */
603 if (GC_is_win32s()) goto out;
605 # endif /* MSWIN32 */
606 # ifndef SOLARIS_THREADS
607 GC_dirty_init();
608 # endif
609 if (!GC_is_initialized) {
610 GC_init_inner();
612 if (GC_dont_gc) {
613 /* Can't easily do it. */
614 UNLOCK();
615 ENABLE_SIGNALS();
616 return;
618 if (GC_words_allocd > 0) {
619 /* There may be unmarked reachable objects */
620 GC_gcollect_inner();
621 } /* else we're OK in assuming everything's */
622 /* clean since nothing can point to an */
623 /* unmarked object. */
624 GC_read_dirty();
625 GC_incremental = TRUE;
626 out:
627 UNLOCK();
628 ENABLE_SIGNALS();
630 # endif
634 #ifdef MSWIN32
635 # define LOG_FILE "gc.log"
637 HANDLE GC_stdout = 0, GC_stderr;
638 int GC_tmp;
639 DWORD GC_junk;
641 void GC_set_files()
643 if (!GC_stdout) {
644 GC_stdout = CreateFile(LOG_FILE, GENERIC_WRITE,
645 FILE_SHARE_READ | FILE_SHARE_WRITE,
646 NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH,
647 NULL);
648 if (INVALID_HANDLE_VALUE == GC_stdout) ABORT("Open of log file failed");
650 if (GC_stderr == 0) {
651 GC_stderr = GC_stdout;
655 #endif
657 #if defined(OS2) || defined(MACOS)
658 FILE * GC_stdout = NULL;
659 FILE * GC_stderr = NULL;
660 int GC_tmp; /* Should really be local ... */
662 void GC_set_files()
664 if (GC_stdout == NULL) {
665 GC_stdout = stdout;
667 if (GC_stderr == NULL) {
668 GC_stderr = stderr;
671 #endif
673 #if !defined(OS2) && !defined(MACOS) && !defined(MSWIN32)
674 int GC_stdout = 1;
675 int GC_stderr = 2;
676 # if !defined(AMIGA)
677 # include <unistd.h>
678 # endif
679 #endif
681 #if !defined(MSWIN32) && !defined(OS2) && !defined(MACOS) && !defined(ECOS)
682 int GC_write(fd, buf, len)
683 int fd;
684 char *buf;
685 size_t len;
687 register int bytes_written = 0;
688 register int result;
690 while (bytes_written < len) {
691 # ifdef SOLARIS_THREADS
692 result = syscall(SYS_write, fd, buf + bytes_written,
693 len - bytes_written);
694 # else
695 result = write(fd, buf + bytes_written, len - bytes_written);
696 # endif
697 if (-1 == result) return(result);
698 bytes_written += result;
700 return(bytes_written);
702 #endif /* UN*X */
704 #if defined(ECOS)
705 int GC_write(fd, buf, len)
707 _Jv_diag_write (buf, len);
708 return len;
710 #endif
713 #ifdef MSWIN32
714 # define WRITE(f, buf, len) (GC_set_files(), \
715 GC_tmp = WriteFile((f), (buf), \
716 (len), &GC_junk, NULL),\
717 (GC_tmp? 1 : -1))
718 #else
719 # if defined(OS2) || defined(MACOS)
720 # define WRITE(f, buf, len) (GC_set_files(), \
721 GC_tmp = fwrite((buf), 1, (len), (f)), \
722 fflush(f), GC_tmp)
723 # else
724 # define WRITE(f, buf, len) GC_write((f), (buf), (len))
725 # endif
726 #endif
728 /* A version of printf that is unlikely to call malloc, and is thus safer */
729 /* to call from the collector in case malloc has been bound to GC_malloc. */
730 /* Assumes that no more than 1023 characters are written at once. */
731 /* Assumes that all arguments have been converted to something of the */
732 /* same size as long, and that the format conversions expect something */
733 /* of that size. */
734 void GC_printf(format, a, b, c, d, e, f)
735 char * format;
736 long a, b, c, d, e, f;
738 char buf[1025];
740 if (GC_quiet) return;
741 buf[1024] = 0x15;
742 (void) sprintf(buf, format, a, b, c, d, e, f);
743 if (buf[1024] != 0x15) ABORT("GC_printf clobbered stack");
744 if (WRITE(GC_stdout, buf, strlen(buf)) < 0) ABORT("write to stdout failed");
747 void GC_err_printf(format, a, b, c, d, e, f)
748 char * format;
749 long a, b, c, d, e, f;
751 char buf[1025];
753 buf[1024] = 0x15;
754 (void) sprintf(buf, format, a, b, c, d, e, f);
755 if (buf[1024] != 0x15) ABORT("GC_err_printf clobbered stack");
756 if (WRITE(GC_stderr, buf, strlen(buf)) < 0) ABORT("write to stderr failed");
759 void GC_err_puts(s)
760 char *s;
762 if (WRITE(GC_stderr, s, strlen(s)) < 0) ABORT("write to stderr failed");
765 # if defined(__STDC__) || defined(__cplusplus)
766 void GC_default_warn_proc(char *msg, GC_word arg)
767 # else
768 void GC_default_warn_proc(msg, arg)
769 char *msg;
770 GC_word arg;
771 # endif
773 GC_err_printf1(msg, (unsigned long)arg);
776 GC_warn_proc GC_current_warn_proc = GC_default_warn_proc;
778 # if defined(__STDC__) || defined(__cplusplus)
779 GC_warn_proc GC_set_warn_proc(GC_warn_proc p)
780 # else
781 GC_warn_proc GC_set_warn_proc(p)
782 GC_warn_proc p;
783 # endif
785 GC_warn_proc result;
787 LOCK();
788 result = GC_current_warn_proc;
789 GC_current_warn_proc = p;
790 UNLOCK();
791 return(result);
795 #ifndef PCR
796 void GC_abort(msg)
797 char * msg;
799 GC_err_printf1("%s\n", msg);
800 (void) abort();
802 #endif
804 #ifdef NEED_CALLINFO
806 void GC_print_callers (info)
807 struct callinfo info[NFRAMES];
809 register int i;
811 # if NFRAMES == 1
812 GC_err_printf0("\tCaller at allocation:\n");
813 # else
814 GC_err_printf0("\tCall chain at allocation:\n");
815 # endif
816 for (i = 0; i < NFRAMES; i++) {
817 if (info[i].ci_pc == 0) break;
818 # if NARGS > 0
820 int j;
822 GC_err_printf0("\t\targs: ");
823 for (j = 0; j < NARGS; j++) {
824 if (j != 0) GC_err_printf0(", ");
825 GC_err_printf2("%d (0x%X)", ~(info[i].ci_arg[j]),
826 ~(info[i].ci_arg[j]));
828 GC_err_printf0("\n");
830 # endif
831 GC_err_printf1("\t\t##PC##= 0x%X\n", info[i].ci_pc);
835 #endif /* SAVE_CALL_CHAIN */
837 /* Needed by SRC_M3, gcj, and should perhaps be the official interface */
838 /* to GC_dont_gc. */
839 void GC_enable()
841 GC_dont_gc--;
844 void GC_disable()
846 GC_dont_gc++;
849 #if !defined(NO_DEBUGGING)
851 void GC_dump()
853 GC_printf0("***Static roots:\n");
854 GC_print_static_roots();
855 GC_printf0("\n***Heap sections:\n");
856 GC_print_heap_sects();
857 GC_printf0("\n***Free blocks:\n");
858 GC_print_hblkfreelist();
859 GC_printf0("\n***Blocks in use:\n");
860 GC_print_block_list();
863 # endif /* NO_DEBUGGING */