OSDN Git Service

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