OSDN Git Service

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