OSDN Git Service

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