OSDN Git Service

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