OSDN Git Service

1bd98d64e0f5abb76b501fc7bf1353252ac7353c
[pf3gnuchains/gcc-fork.git] / boehm-gc / tests / test.c
1 /* 
2  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
4  * Copyright (c) 1996 by Silicon Graphics.  All rights reserved.
5  *
6  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
8  *
9  * Permission is hereby granted to use or copy this program
10  * for any purpose,  provided the above notices are retained on all copies.
11  * Permission to modify the code and to distribute modified code is granted,
12  * provided the above notices are retained, and a notice that the code was
13  * modified is included with the above copyright notice.
14  */
15 /* An incomplete test for the garbage collector.                */
16 /* Some more obscure entry points are not tested at all.        */
17 /* This must be compiled with the same flags used to build the  */
18 /* GC.  It uses GC internals to allow more precise results      */
19 /* checking for some of the tests.                              */
20
21 # undef GC_BUILD
22
23 #ifdef DBG_HDRS_ALL
24 #  define GC_DEBUG
25 #endif
26
27 # if defined(mips) && defined(SYSTYPE_BSD43)
28     /* MIPS RISCOS 4 */
29 # else
30 #   include <stdlib.h>
31 # endif
32 # include <stdio.h>
33 # ifdef _WIN32_WCE
34 #   include <winbase.h>
35 #   define assert ASSERT
36 # else
37 #   include <assert.h>        /* Not normally used, but handy for debugging. */
38 # endif
39 # include <assert.h>    /* Not normally used, but handy for debugging. */
40 # include "gc.h"
41 # include "gc_typed.h"
42 # ifdef THREAD_LOCAL_ALLOC
43 #   include "gc_local_alloc.h"
44 # endif
45 # include "private/gc_priv.h"   /* For output, locking, MIN_WORDS,      */
46                         /* and some statistics.                 */
47 # include "private/gcconfig.h"
48
49 # if defined(MSWIN32) || defined(MSWINCE)
50 #   include <windows.h>
51 # endif
52
53 # ifdef PCR
54 #   include "th/PCR_ThCrSec.h"
55 #   include "th/PCR_Th.h"
56 #   undef GC_printf0
57 #   define GC_printf0 printf
58 #   undef GC_printf1
59 #   define GC_printf1 printf
60 # endif
61
62 # ifdef SOLARIS_THREADS
63 #   include <thread.h>
64 #   include <synch.h>
65 # endif
66
67 # if defined(IRIX_THREADS) || defined(LINUX_THREADS) || defined(HPUX_THREADS)
68 #   include <pthread.h>
69 # endif
70
71 # ifdef WIN32_THREADS
72 #   ifndef MSWINCE
73 #     include <process.h>
74 #     define GC_CreateThread(a,b,c,d,e,f) ((HANDLE) _beginthreadex(a,b,c,d,e,f))
75 #   endif
76     static CRITICAL_SECTION incr_cs;
77 # endif
78
79
80 /* Allocation Statistics */
81 int stubborn_count = 0;
82 int uncollectable_count = 0;
83 int collectable_count = 0;
84 int atomic_count = 0;
85 int realloc_count = 0;
86
87 #if defined(GC_AMIGA_FASTALLOC) && defined(AMIGA)
88
89   extern void GC_amiga_free_all_mem(void);
90   void Amiga_Fail(void){GC_amiga_free_all_mem();abort();}
91 # define FAIL (void)Amiga_Fail()
92   void *GC_amiga_gctest_malloc_explicitly_typed(size_t lb, GC_descr d){
93     void *ret=GC_malloc_explicitly_typed(lb,d);
94     if(ret==NULL){
95                 if(!GC_dont_gc){
96               GC_gcollect();
97               ret=GC_malloc_explicitly_typed(lb,d);
98                 }
99       if(ret==NULL){
100         GC_printf0("Out of memory, (typed allocations are not directly "
101                    "supported with the GC_AMIGA_FASTALLOC option.)\n");
102         FAIL;
103       }
104     }
105     return ret;
106   }
107   void *GC_amiga_gctest_calloc_explicitly_typed(size_t a,size_t lb, GC_descr d){
108     void *ret=GC_calloc_explicitly_typed(a,lb,d);
109     if(ret==NULL){
110                 if(!GC_dont_gc){
111               GC_gcollect();
112               ret=GC_calloc_explicitly_typed(a,lb,d);
113                 }
114       if(ret==NULL){
115         GC_printf0("Out of memory, (typed allocations are not directly "
116                    "supported with the GC_AMIGA_FASTALLOC option.)\n");
117         FAIL;
118       }
119     }
120     return ret;
121   }
122 # define GC_malloc_explicitly_typed(a,b) GC_amiga_gctest_malloc_explicitly_typed(a,b) 
123 # define GC_calloc_explicitly_typed(a,b,c) GC_amiga_gctest_calloc_explicitly_typed(a,b,c) 
124
125 #else /* !AMIGA_FASTALLOC */
126
127 # ifdef PCR
128 #   define FAIL (void)abort()
129 # else
130 #   ifdef MSWINCE
131 #     define FAIL DebugBreak()
132 #   else
133 #     define FAIL GC_abort("Test failed");
134 #   endif
135 # endif
136
137 #endif /* !AMIGA_FASTALLOC */
138
139 /* AT_END may be defined to exercise the interior pointer test  */
140 /* if the collector is configured with ALL_INTERIOR_POINTERS.   */
141 /* As it stands, this test should succeed with either           */
142 /* configuration.  In the FIND_LEAK configuration, it should    */
143 /* find lots of leaks, since we free almost nothing.            */
144
145 struct SEXPR {
146     struct SEXPR * sexpr_car;
147     struct SEXPR * sexpr_cdr;
148 };
149
150
151 typedef struct SEXPR * sexpr;
152
153 # define INT_TO_SEXPR(x) ((sexpr)(unsigned long)(x))
154
155 # undef nil
156 # define nil (INT_TO_SEXPR(0))
157 # define car(x) ((x) -> sexpr_car)
158 # define cdr(x) ((x) -> sexpr_cdr)
159 # define is_nil(x) ((x) == nil)
160
161
162 int extra_count = 0;        /* Amount of space wasted in cons node */
163
164 /* Silly implementation of Lisp cons. Intentionally wastes lots of space */
165 /* to test collector.                                                    */
166 # ifdef VERY_SMALL_CONFIG
167 #   define cons small_cons
168 # else
169 sexpr cons (x, y)
170 sexpr x;
171 sexpr y;
172 {
173     register sexpr r;
174     register int *p;
175     register int my_extra = extra_count;
176     
177     stubborn_count++;
178     r = (sexpr) GC_MALLOC_STUBBORN(sizeof(struct SEXPR) + my_extra);
179     if (r == 0) {
180         (void)GC_printf0("Out of memory\n");
181         exit(1);
182     }
183     for (p = (int *)r;
184          ((char *)p) < ((char *)r) + my_extra + sizeof(struct SEXPR); p++) {
185         if (*p) {
186             (void)GC_printf1("Found nonzero at 0x%lx - allocator is broken\n",
187                              (unsigned long)p);
188             FAIL;
189         }
190         *p = 13;
191     }
192 #   ifdef AT_END
193         r = (sexpr)((char *)r + (my_extra & ~7));
194 #   endif
195     r -> sexpr_car = x;
196     r -> sexpr_cdr = y;
197     my_extra++;
198     if ( my_extra >= 5000 ) {
199         extra_count = 0;
200     } else {
201         extra_count = my_extra;
202     }
203     GC_END_STUBBORN_CHANGE((char *)r);
204     return(r);
205 }
206 # endif
207
208 sexpr small_cons (x, y)
209 sexpr x;
210 sexpr y;
211 {
212     register sexpr r;
213     
214     collectable_count++;
215     r = (sexpr) GC_MALLOC(sizeof(struct SEXPR));
216     if (r == 0) {
217         (void)GC_printf0("Out of memory\n");
218         exit(1);
219     }
220     r -> sexpr_car = x;
221     r -> sexpr_cdr = y;
222     return(r);
223 }
224
225 sexpr small_cons_uncollectable (x, y)
226 sexpr x;
227 sexpr y;
228 {
229     register sexpr r;
230     
231     uncollectable_count++;
232     r = (sexpr) GC_MALLOC_UNCOLLECTABLE(sizeof(struct SEXPR));
233     if (r == 0) {
234         (void)GC_printf0("Out of memory\n");
235         exit(1);
236     }
237     r -> sexpr_car = x;
238     r -> sexpr_cdr = (sexpr)(~(unsigned long)y);
239     return(r);
240 }
241
242 #ifdef GC_GCJ_SUPPORT
243
244 #include "gc_mark.h"
245 #include "private/dbg_mlc.h"  /* For USR_PTR_FROM_BASE */
246 #include "gc_gcj.h"
247
248 /* The following struct emulates the vtable in gcj.     */
249 /* This assumes the default value of MARK_DESCR_OFFSET. */
250 struct fake_vtable {
251   void * dummy;         /* class pointer in real gcj.   */
252   size_t descr;
253 };
254
255 struct fake_vtable gcj_class_struct1 = { 0, sizeof(struct SEXPR)
256                                             + sizeof(struct fake_vtable *) };
257                         /* length based descriptor.     */
258 struct fake_vtable gcj_class_struct2 =
259                                 { 0, (3l << (CPP_WORDSZ - 3)) | GC_DS_BITMAP};
260                         /* Bitmap based descriptor.     */
261
262 struct GC_ms_entry * fake_gcj_mark_proc(word * addr,
263                                         struct GC_ms_entry *mark_stack_ptr,
264                                         struct GC_ms_entry *mark_stack_limit,
265                                         word env   )
266 {
267     sexpr x;
268     if (1 == env) {
269         /* Object allocated with debug allocator.       */
270         addr = (word *)USR_PTR_FROM_BASE(addr);
271     }
272     x = (sexpr)(addr + 1); /* Skip the vtable pointer. */
273     mark_stack_ptr = GC_MARK_AND_PUSH(
274                               (GC_PTR)(x -> sexpr_cdr), mark_stack_ptr,
275                               mark_stack_limit, (GC_PTR *)&(x -> sexpr_cdr));
276     mark_stack_ptr = GC_MARK_AND_PUSH(
277                               (GC_PTR)(x -> sexpr_car), mark_stack_ptr,
278                               mark_stack_limit, (GC_PTR *)&(x -> sexpr_car));
279     return(mark_stack_ptr);
280 }
281
282 sexpr gcj_cons(x, y)
283 sexpr x;
284 sexpr y;
285 {
286     GC_word * r;
287     sexpr result;
288     static int count = 0;
289     
290     if (++count & 1) {
291 #     ifdef USE_MARK_BYTES
292         r = (GC_word *) GC_GCJ_FAST_MALLOC(4, &gcj_class_struct1);
293 #     else
294         r = (GC_word *) GC_GCJ_FAST_MALLOC(3, &gcj_class_struct1);
295 #     endif
296     } else {
297         r = (GC_word *) GC_GCJ_MALLOC(sizeof(struct SEXPR)
298                                       + sizeof(struct fake_vtable*),
299                                       &gcj_class_struct2);
300     }
301     if (r == 0) {
302         (void)GC_printf0("Out of memory\n");
303         exit(1);
304     }
305     result = (sexpr)(r + 1);
306     result -> sexpr_car = x;
307     result -> sexpr_cdr = y;
308     return(result);
309 }
310 #endif
311
312 /* Return reverse(x) concatenated with y */
313 sexpr reverse1(x, y)
314 sexpr x, y;
315 {
316     if (is_nil(x)) {
317         return(y);
318     } else {
319         return( reverse1(cdr(x), cons(car(x), y)) );
320     }
321 }
322
323 sexpr reverse(x)
324 sexpr x;
325 {
326     return( reverse1(x, nil) );
327 }
328
329 sexpr ints(low, up)
330 int low, up;
331 {
332     if (low > up) {
333         return(nil);
334     } else {
335         return(small_cons(small_cons(INT_TO_SEXPR(low), nil), ints(low+1, up)));
336     }
337 }
338
339 #ifdef GC_GCJ_SUPPORT
340 /* Return reverse(x) concatenated with y */
341 sexpr gcj_reverse1(x, y)
342 sexpr x, y;
343 {
344     if (is_nil(x)) {
345         return(y);
346     } else {
347         return( gcj_reverse1(cdr(x), gcj_cons(car(x), y)) );
348     }
349 }
350
351 sexpr gcj_reverse(x)
352 sexpr x;
353 {
354     return( gcj_reverse1(x, nil) );
355 }
356
357 sexpr gcj_ints(low, up)
358 int low, up;
359 {
360     if (low > up) {
361         return(nil);
362     } else {
363         return(gcj_cons(gcj_cons(INT_TO_SEXPR(low), nil), gcj_ints(low+1, up)));
364     }
365 }
366 #endif /* GC_GCJ_SUPPORT */
367
368 /* To check uncollectable allocation we build lists with disguised cdr  */
369 /* pointers, and make sure they don't go away.                          */
370 sexpr uncollectable_ints(low, up)
371 int low, up;
372 {
373     if (low > up) {
374         return(nil);
375     } else {
376         return(small_cons_uncollectable(small_cons(INT_TO_SEXPR(low), nil),
377                uncollectable_ints(low+1, up)));
378     }
379 }
380
381 void check_ints(list, low, up)
382 sexpr list;
383 int low, up;
384 {
385     if ((int)(GC_word)(car(car(list))) != low) {
386         (void)GC_printf0(
387            "List reversal produced incorrect list - collector is broken\n");
388         FAIL;
389     }
390     if (low == up) {
391         if (cdr(list) != nil) {
392            (void)GC_printf0("List too long - collector is broken\n");
393            FAIL;
394         }
395     } else {
396         check_ints(cdr(list), low+1, up);
397     }
398 }
399
400 # define UNCOLLECTABLE_CDR(x) (sexpr)(~(unsigned long)(cdr(x)))
401
402 void check_uncollectable_ints(list, low, up)
403 sexpr list;
404 int low, up;
405 {
406     if ((int)(GC_word)(car(car(list))) != low) {
407         (void)GC_printf0(
408            "Uncollectable list corrupted - collector is broken\n");
409         FAIL;
410     }
411     if (low == up) {
412         if (UNCOLLECTABLE_CDR(list) != nil) {
413            (void)GC_printf0("Uncollectable list too long - collector is broken\n");
414            FAIL;
415         }
416     } else {
417         check_uncollectable_ints(UNCOLLECTABLE_CDR(list), low+1, up);
418     }
419 }
420
421 /* Not used, but useful for debugging: */
422 void print_int_list(x)
423 sexpr x;
424 {
425     if (is_nil(x)) {
426         (void)GC_printf0("NIL\n");
427     } else {
428         (void)GC_printf1("(%ld)", (long)(car(car(x))));
429         if (!is_nil(cdr(x))) {
430             (void)GC_printf0(", ");
431             (void)print_int_list(cdr(x));
432         } else {
433             (void)GC_printf0("\n");
434         }
435     }
436 }
437
438 /* Try to force a to be strangely aligned */
439 struct {
440   char dummy;
441   sexpr aa;
442 } A;
443 #define a A.aa
444
445 /*
446  * A tiny list reversal test to check thread creation.
447  */
448 #ifdef THREADS
449
450 # ifdef WIN32_THREADS
451     unsigned __stdcall tiny_reverse_test(void * arg)
452 # else
453     void * tiny_reverse_test(void * arg)
454 # endif
455 {
456     check_ints(reverse(reverse(ints(1,10))), 1, 10);
457     return 0;
458 }
459
460 # if defined(IRIX_THREADS) || defined(LINUX_THREADS) \
461      || defined(SOLARIS_PTHREADS) || defined(HPUX_THREADS)
462     void fork_a_thread()
463     {
464       pthread_t t;
465       int code;
466       if ((code = pthread_create(&t, 0, tiny_reverse_test, 0)) != 0) {
467         (void)GC_printf1("Small thread creation failed %lu\n",
468                          (unsigned long)code);
469         FAIL;
470       }
471       if ((code = pthread_join(t, 0)) != 0) {
472         (void)GC_printf1("Small thread join failed %lu\n",
473         (unsigned long)code);
474         FAIL;
475       }
476     }
477
478 # elif defined(WIN32_THREADS)
479     void fork_a_thread()
480     {
481         unsigned thread_id;
482         HANDLE h;
483         h = GC_CreateThread(NULL, 0, tiny_reverse_test, 0, 0, &thread_id);
484         if (h == (HANDLE)NULL) {
485             (void)GC_printf1("Small thread creation failed %lu\n",
486                              (unsigned long)GetLastError());
487             FAIL;
488         }
489         if (WaitForSingleObject(h, INFINITE) != WAIT_OBJECT_0) {
490             (void)GC_printf1("Small thread wait failed %lu\n",
491                              (unsigned long)GetLastError());
492             FAIL;
493         }
494     }
495
496 /* # elif defined(SOLARIS_THREADS) */
497
498 # else
499
500 #   define fork_a_thread()
501
502 # endif
503
504 #else
505
506 # define fork_a_thread()
507
508 #endif 
509
510 /*
511  * Repeatedly reverse lists built out of very different sized cons cells.
512  * Check that we didn't lose anything.
513  */
514 void reverse_test()
515 {
516     int i;
517     sexpr b;
518     sexpr c;
519     sexpr d;
520     sexpr e;
521     sexpr *f, *g, *h;
522 #   if defined(MSWIN32) || defined(MACOS)
523       /* Win32S only allows 128K stacks */
524 #     define BIG 1000
525 #   else
526 #     if defined PCR
527         /* PCR default stack is 100K.  Stack frames are up to 120 bytes. */
528 #       define BIG 700
529 #     else
530 #       if defined MSWINCE
531           /* WinCE only allows 64K stacks */
532 #         define BIG 500
533 #       else
534 #         if defined(OSF1)
535             /* OSF has limited stack space by default, and large frames. */
536 #           define BIG 200
537 #         else
538 #           define BIG 4500
539 #         endif
540 #       endif
541 #     endif
542 #   endif
543
544     A.dummy = 17;
545     a = ints(1, 49);
546     b = ints(1, 50);
547     c = ints(1, BIG);
548     d = uncollectable_ints(1, 100);
549     e = uncollectable_ints(1, 1);
550     /* Check that realloc updates object descriptors correctly */
551     collectable_count++;
552     f = (sexpr *)GC_MALLOC(4 * sizeof(sexpr));
553     realloc_count++;
554     f = (sexpr *)GC_REALLOC((GC_PTR)f, 6 * sizeof(sexpr));
555     f[5] = ints(1,17);
556     collectable_count++;
557     g = (sexpr *)GC_MALLOC(513 * sizeof(sexpr));
558     realloc_count++;
559     g = (sexpr *)GC_REALLOC((GC_PTR)g, 800 * sizeof(sexpr));
560     g[799] = ints(1,18);
561     collectable_count++;
562     h = (sexpr *)GC_MALLOC(1025 * sizeof(sexpr));
563     realloc_count++;
564     h = (sexpr *)GC_REALLOC((GC_PTR)h, 2000 * sizeof(sexpr));
565 #   ifdef GC_GCJ_SUPPORT
566       h[1999] = gcj_ints(1,200);
567       h[1999] = gcj_reverse(h[1999]);
568 #   else
569       h[1999] = ints(1,200);
570 #   endif
571     /* Try to force some collections and reuse of small list elements */
572       for (i = 0; i < 10; i++) {
573         (void)ints(1, BIG);
574       }
575     /* Superficially test interior pointer recognition on stack */
576       c = (sexpr)((char *)c + sizeof(char *));
577       d = (sexpr)((char *)d + sizeof(char *));
578
579 #   ifdef __STDC__
580         GC_FREE((void *)e);
581 #   else
582         GC_FREE((char *)e);
583 #   endif
584     check_ints(b,1,50);
585     check_ints(a,1,49);
586     for (i = 0; i < 50; i++) {
587         check_ints(b,1,50);
588         b = reverse(reverse(b));
589     }
590     check_ints(b,1,50);
591     check_ints(a,1,49);
592     for (i = 0; i < 60; i++) {
593         if (i % 10 == 0) fork_a_thread();
594         /* This maintains the invariant that a always points to a list of */
595         /* 49 integers.  Thus this is thread safe without locks,          */
596         /* assuming atomic pointer assignments.                           */
597         a = reverse(reverse(a));
598 #       if !defined(AT_END) && !defined(THREADS)
599           /* This is not thread safe, since realloc explicitly deallocates */
600           if (i & 1) {
601             a = (sexpr)GC_REALLOC((GC_PTR)a, 500);
602           } else {
603             a = (sexpr)GC_REALLOC((GC_PTR)a, 8200);
604           }
605 #       endif
606     }
607     check_ints(a,1,49);
608     check_ints(b,1,50);
609     c = (sexpr)((char *)c - sizeof(char *));
610     d = (sexpr)((char *)d - sizeof(char *));
611     check_ints(c,1,BIG);
612     check_uncollectable_ints(d, 1, 100);
613     check_ints(f[5], 1,17);
614     check_ints(g[799], 1,18);
615 #   ifdef GC_GCJ_SUPPORT
616       h[1999] = gcj_reverse(h[1999]);
617 #   endif
618     check_ints(h[1999], 1,200);
619 #   ifndef THREADS
620         a = 0;
621 #   endif  
622     b = c = 0;
623 }
624
625 /*
626  * The rest of this builds balanced binary trees, checks that they don't
627  * disappear, and tests finalization.
628  */
629 typedef struct treenode {
630     int level;
631     struct treenode * lchild;
632     struct treenode * rchild;
633 } tn;
634
635 int finalizable_count = 0;
636 int finalized_count = 0;
637 VOLATILE int dropped_something = 0;
638
639 # ifdef __STDC__
640   void finalizer(void * obj, void * client_data)
641 # else
642   void finalizer(obj, client_data)
643   char * obj;
644   char * client_data;
645 # endif
646 {
647   tn * t = (tn *)obj;
648
649 # ifdef PCR
650      PCR_ThCrSec_EnterSys();
651 # endif
652 # ifdef SOLARIS_THREADS
653     static mutex_t incr_lock;
654     mutex_lock(&incr_lock);
655 # endif
656 # if  defined(IRIX_THREADS) || defined(LINUX_THREADS) || defined(HPUX_THREADS)
657     static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
658     pthread_mutex_lock(&incr_lock);
659 # endif
660 # ifdef WIN32_THREADS
661     EnterCriticalSection(&incr_cs);
662 # endif
663   if ((int)(GC_word)client_data != t -> level) {
664      (void)GC_printf0("Wrong finalization data - collector is broken\n");
665      FAIL;
666   }
667   finalized_count++;
668 # ifdef PCR
669     PCR_ThCrSec_ExitSys();
670 # endif
671 # ifdef SOLARIS_THREADS
672     mutex_unlock(&incr_lock);
673 # endif
674 # if defined(IRIX_THREADS) || defined(LINUX_THREADS) || defined(HPUX_THREADS)
675     pthread_mutex_unlock(&incr_lock);
676 # endif
677 # ifdef WIN32_THREADS
678     LeaveCriticalSection(&incr_cs);
679 # endif
680 }
681
682 size_t counter = 0;
683
684 # define MAX_FINALIZED 8000
685
686 # if !defined(MACOS)
687   GC_FAR GC_word live_indicators[MAX_FINALIZED] = {0};
688 #else
689   /* Too big for THINK_C. have to allocate it dynamically. */
690   GC_word *live_indicators = 0;
691 #endif
692
693 int live_indicators_count = 0;
694
695 tn * mktree(n)
696 int n;
697 {
698 #   ifdef THREAD_LOCAL_ALLOC
699       tn * result = (tn *)GC_LOCAL_MALLOC(sizeof(tn));
700 #   else
701       tn * result = (tn *)GC_MALLOC(sizeof(tn));
702 #   endif
703     
704     collectable_count++;
705 #   ifdef THREAD_LOCAL_ALLOC
706        /* Minimally exercise thread local allocation */
707        {
708          char * result = (char *)GC_LOCAL_MALLOC_ATOMIC(17);
709          memset(result, 'a', 17);
710        }
711 #   endif /* THREAD_LOCAL_ALLOC */
712 #   if defined(MACOS)
713         /* get around static data limitations. */
714         if (!live_indicators)
715                 live_indicators =
716                     (GC_word*)NewPtrClear(MAX_FINALIZED * sizeof(GC_word));
717         if (!live_indicators) {
718           (void)GC_printf0("Out of memory\n");
719           exit(1);
720         }
721 #   endif
722     if (n == 0) return(0);
723     if (result == 0) {
724         (void)GC_printf0("Out of memory\n");
725         exit(1);
726     }
727     result -> level = n;
728     result -> lchild = mktree(n-1);
729     result -> rchild = mktree(n-1);
730     if (counter++ % 17 == 0 && n >= 2) {
731         tn * tmp = result -> lchild -> rchild;
732         
733         result -> lchild -> rchild = result -> rchild -> lchild;
734         result -> rchild -> lchild = tmp;
735     }
736     if (counter++ % 119 == 0) {
737         int my_index;
738         
739         {
740 #         ifdef PCR
741             PCR_ThCrSec_EnterSys();
742 #         endif
743 #         ifdef SOLARIS_THREADS
744             static mutex_t incr_lock;
745             mutex_lock(&incr_lock);
746 #         endif
747 #         if defined(IRIX_THREADS) || defined(LINUX_THREADS) \
748              || defined(HPUX_THREADS)
749             static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
750             pthread_mutex_lock(&incr_lock);
751 #         endif
752 #         ifdef WIN32_THREADS
753             EnterCriticalSection(&incr_cs);
754 #         endif
755                 /* Losing a count here causes erroneous report of failure. */
756           finalizable_count++;
757           my_index = live_indicators_count++;
758 #         ifdef PCR
759             PCR_ThCrSec_ExitSys();
760 #         endif
761 #         ifdef SOLARIS_THREADS
762             mutex_unlock(&incr_lock);
763 #         endif
764 #         if defined(IRIX_THREADS) || defined(LINUX_THREADS) \
765              || defined(HPUX_THREADS)
766             pthread_mutex_unlock(&incr_lock);
767 #         endif
768 #         ifdef WIN32_THREADS
769             LeaveCriticalSection(&incr_cs);
770 #         endif
771         }
772
773         GC_REGISTER_FINALIZER((GC_PTR)result, finalizer, (GC_PTR)(GC_word)n,
774                               (GC_finalization_proc *)0, (GC_PTR *)0);
775         if (my_index >= MAX_FINALIZED) {
776                 GC_printf0("live_indicators overflowed\n");
777                 FAIL;
778         }
779         live_indicators[my_index] = 13;
780         if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
781                 (GC_PTR *)(&(live_indicators[my_index])),
782                 (GC_PTR)result) != 0) {
783                 GC_printf0("GC_general_register_disappearing_link failed\n");
784                 FAIL;
785         }
786         if (GC_unregister_disappearing_link(
787                 (GC_PTR *)
788                    (&(live_indicators[my_index]))) == 0) {
789                 GC_printf0("GC_unregister_disappearing_link failed\n");
790                 FAIL;
791         }
792         if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
793                 (GC_PTR *)(&(live_indicators[my_index])),
794                 (GC_PTR)result) != 0) {
795                 GC_printf0("GC_general_register_disappearing_link failed 2\n");
796                 FAIL;
797         }
798     }
799     return(result);
800 }
801
802 void chktree(t,n)
803 tn *t;
804 int n;
805 {
806     if (n == 0 && t != 0) {
807         (void)GC_printf0("Clobbered a leaf - collector is broken\n");
808         FAIL;
809     }
810     if (n == 0) return;
811     if (t -> level != n) {
812         (void)GC_printf1("Lost a node at level %lu - collector is broken\n",
813                          (unsigned long)n);
814         FAIL;
815     }
816     if (counter++ % 373 == 0) {
817         collectable_count++;
818         (void) GC_MALLOC(counter%5001);
819     }
820     chktree(t -> lchild, n-1);
821     if (counter++ % 73 == 0) {
822         collectable_count++;
823         (void) GC_MALLOC(counter%373);
824     }
825     chktree(t -> rchild, n-1);
826 }
827
828 # if defined(SOLARIS_THREADS) && !defined(_SOLARIS_PTHREADS)
829 thread_key_t fl_key;
830
831 void * alloc8bytes()
832 {
833 # if defined(SMALL_CONFIG) || defined(GC_DEBUG)
834     collectable_count++;
835     return(GC_MALLOC(8));
836 # else
837     void ** my_free_list_ptr;
838     void * my_free_list;
839     
840     if (thr_getspecific(fl_key, (void **)(&my_free_list_ptr)) != 0) {
841         (void)GC_printf0("thr_getspecific failed\n");
842         FAIL;
843     }
844     if (my_free_list_ptr == 0) {
845         uncollectable_count++;
846         my_free_list_ptr = GC_NEW_UNCOLLECTABLE(void *);
847         if (thr_setspecific(fl_key, my_free_list_ptr) != 0) {
848             (void)GC_printf0("thr_setspecific failed\n");
849             FAIL;
850         }
851     }
852     my_free_list = *my_free_list_ptr;
853     if (my_free_list == 0) {
854         collectable_count++;
855         my_free_list = GC_malloc_many(8);
856         if (my_free_list == 0) {
857             (void)GC_printf0("alloc8bytes out of memory\n");
858             FAIL;
859         }
860     }
861     *my_free_list_ptr = GC_NEXT(my_free_list);
862     GC_NEXT(my_free_list) = 0;
863     return(my_free_list);
864 # endif
865 }
866
867 #else
868
869 # if defined(GC_SOLARIS_PTHREADS) || defined(GC_IRIX_THREADS) \
870      || defined(GC_LINUX_THREADS) || defined(GC_HPUX_THREADS) \
871      || defined(GC_SOLARIS_THREADS)
872 pthread_key_t fl_key;
873
874 void * alloc8bytes()
875 {
876 # if defined(SMALL_CONFIG) || defined(GC_DEBUG)
877     collectable_count++;
878     return(GC_MALLOC(8));
879 # else
880     void ** my_free_list_ptr;
881     void * my_free_list;
882     
883     my_free_list_ptr = (void **)pthread_getspecific(fl_key);
884     if (my_free_list_ptr == 0) {
885         uncollectable_count++;
886         my_free_list_ptr = GC_NEW_UNCOLLECTABLE(void *);
887         if (pthread_setspecific(fl_key, my_free_list_ptr) != 0) {
888             (void)GC_printf0("pthread_setspecific failed\n");
889             FAIL;
890         }
891     }
892     my_free_list = *my_free_list_ptr;
893     if (my_free_list == 0) {
894         my_free_list = GC_malloc_many(8);
895         if (my_free_list == 0) {
896             (void)GC_printf0("alloc8bytes out of memory\n");
897             FAIL;
898         }
899     }
900     *my_free_list_ptr = GC_NEXT(my_free_list);
901     GC_NEXT(my_free_list) = 0;
902     collectable_count++;
903     return(my_free_list);
904 # endif
905 }
906
907 # else
908 #   define alloc8bytes() GC_MALLOC_ATOMIC(8)
909 # endif
910 #endif
911
912 void alloc_small(n)
913 int n;
914 {
915     register int i;
916     
917     for (i = 0; i < n; i += 8) {
918         atomic_count++;
919         if (alloc8bytes() == 0) {
920             (void)GC_printf0("Out of memory\n");
921             FAIL;
922         }
923     }
924 }
925
926 # if defined(THREADS) && defined(GC_DEBUG)
927 #   ifdef VERY_SMALL_CONFIG
928 #     define TREE_HEIGHT 12
929 #   else
930 #     define TREE_HEIGHT 15
931 #   endif
932 # else
933 #   ifdef VERY_SMALL_CONFIG
934 #     define TREE_HEIGHT 13
935 #   else
936 #     define TREE_HEIGHT 16
937 #   endif
938 # endif
939 void tree_test()
940 {
941     tn * root;
942     register int i;
943     
944     root = mktree(TREE_HEIGHT);
945 #   ifndef VERY_SMALL_CONFIG
946       alloc_small(5000000);
947 #   endif
948     chktree(root, TREE_HEIGHT);
949     if (finalized_count && ! dropped_something) {
950         (void)GC_printf0("Premature finalization - collector is broken\n");
951         FAIL;
952     }
953     dropped_something = 1;
954     GC_noop(root);      /* Root needs to remain live until      */
955                         /* dropped_something is set.            */
956     root = mktree(TREE_HEIGHT);
957     chktree(root, TREE_HEIGHT);
958     for (i = TREE_HEIGHT; i >= 0; i--) {
959         root = mktree(i);
960         chktree(root, i);
961     }
962 #   ifndef VERY_SMALL_CONFIG
963       alloc_small(5000000);
964 #   endif
965 }
966
967 unsigned n_tests = 0;
968
969 GC_word bm_huge[10] = {
970     0xffffffff,
971     0xffffffff,
972     0xffffffff,
973     0xffffffff,
974     0xffffffff,
975     0xffffffff,
976     0xffffffff,
977     0xffffffff,
978     0xffffffff,
979     0x00ffffff,
980 };
981
982 /* A very simple test of explicitly typed allocation    */
983 void typed_test()
984 {
985     GC_word * old, * new;
986     GC_word bm3 = 0x3;
987     GC_word bm2 = 0x2;
988     GC_word bm_large = 0xf7ff7fff;
989     GC_descr d1 = GC_make_descriptor(&bm3, 2);
990     GC_descr d2 = GC_make_descriptor(&bm2, 2);
991 #   ifndef LINT
992       GC_descr dummy = GC_make_descriptor(&bm_large, 32);
993 #   endif
994     GC_descr d3 = GC_make_descriptor(&bm_large, 32);
995     GC_descr d4 = GC_make_descriptor(bm_huge, 320);
996     GC_word * x = (GC_word *)GC_malloc_explicitly_typed(2000, d4);
997     register int i;
998     
999     collectable_count++;
1000     old = 0;
1001     for (i = 0; i < 4000; i++) {
1002         collectable_count++;
1003         new = (GC_word *) GC_malloc_explicitly_typed(4 * sizeof(GC_word), d1);
1004         if (0 != new[0] || 0 != new[1]) {
1005             GC_printf0("Bad initialization by GC_malloc_explicitly_typed\n");
1006             FAIL;
1007         }
1008         new[0] = 17;
1009         new[1] = (GC_word)old;
1010         old = new;
1011         collectable_count++;
1012         new = (GC_word *) GC_malloc_explicitly_typed(4 * sizeof(GC_word), d2);
1013         new[0] = 17;
1014         new[1] = (GC_word)old;
1015         old = new;
1016         collectable_count++;
1017         new = (GC_word *) GC_malloc_explicitly_typed(33 * sizeof(GC_word), d3);
1018         new[0] = 17;
1019         new[1] = (GC_word)old;
1020         old = new;
1021         collectable_count++;
1022         new = (GC_word *) GC_calloc_explicitly_typed(4, 2 * sizeof(GC_word),
1023                                                      d1);
1024         new[0] = 17;
1025         new[1] = (GC_word)old;
1026         old = new;
1027         collectable_count++;
1028         if (i & 0xff) {
1029           new = (GC_word *) GC_calloc_explicitly_typed(7, 3 * sizeof(GC_word),
1030                                                      d2);
1031         } else {
1032           new = (GC_word *) GC_calloc_explicitly_typed(1001,
1033                                                        3 * sizeof(GC_word),
1034                                                        d2);
1035           if (0 != new[0] || 0 != new[1]) {
1036             GC_printf0("Bad initialization by GC_malloc_explicitly_typed\n");
1037             FAIL;
1038           }
1039         }
1040         new[0] = 17;
1041         new[1] = (GC_word)old;
1042         old = new;
1043     }
1044     for (i = 0; i < 20000; i++) {
1045         if (new[0] != 17) {
1046             (void)GC_printf1("typed alloc failed at %lu\n",
1047                              (unsigned long)i);
1048             FAIL;
1049         }
1050         new[0] = 0;
1051         old = new;
1052         new = (GC_word *)(old[1]);
1053     }
1054     GC_gcollect();
1055     GC_noop(x);
1056 }
1057
1058 int fail_count = 0;
1059
1060 #ifndef __STDC__
1061 /*ARGSUSED*/
1062 void fail_proc1(x)
1063 GC_PTR x;
1064 {
1065     fail_count++;
1066 }
1067
1068 #else
1069
1070 /*ARGSUSED*/
1071 void fail_proc1(GC_PTR x)
1072 {
1073     fail_count++;
1074 }   
1075
1076 #endif /* __STDC__ */
1077
1078 #ifdef THREADS
1079 #   define TEST_FAIL_COUNT(n) 1
1080 #else 
1081 #   define TEST_FAIL_COUNT(n) (fail_count >= (n))
1082 #endif
1083
1084 void run_one_test()
1085 {
1086     char *x;
1087 #   ifdef LINT
1088         char *y = 0;
1089 #   else
1090         char *y = (char *)(size_t)fail_proc1;
1091 #   endif
1092     DCL_LOCK_STATE;
1093     
1094 #   ifdef FIND_LEAK
1095         (void)GC_printf0(
1096                 "This test program is not designed for leak detection mode\n");
1097         (void)GC_printf0("Expect lots of problems.\n");
1098 #   endif
1099     GC_FREE(0);
1100 #   ifndef DBG_HDRS_ALL
1101       collectable_count += 3;
1102       if (GC_size(GC_malloc(7)) != 8 &&
1103           GC_size(GC_malloc(7)) != MIN_WORDS * sizeof(GC_word)
1104         || GC_size(GC_malloc(15)) != 16) {
1105             (void)GC_printf0("GC_size produced unexpected results\n");
1106             FAIL;
1107       }
1108       collectable_count += 1;
1109       if (GC_size(GC_malloc(0)) != MIN_WORDS * sizeof(GC_word)) {
1110         (void)GC_printf1("GC_malloc(0) failed: GC_size returns %ld\n",
1111                          GC_size(GC_malloc(0)));
1112             FAIL;
1113       }
1114       collectable_count += 1;
1115       if (GC_size(GC_malloc_uncollectable(0)) != MIN_WORDS * sizeof(GC_word)) {
1116         (void)GC_printf0("GC_malloc_uncollectable(0) failed\n");
1117             FAIL;
1118       }
1119       GC_is_valid_displacement_print_proc = fail_proc1;
1120       GC_is_visible_print_proc = fail_proc1;
1121       collectable_count += 1;
1122       x = GC_malloc(16);
1123       if (GC_base(x + 13) != x) {
1124         (void)GC_printf0("GC_base(heap ptr) produced incorrect result\n");
1125         FAIL;
1126       }
1127 #     ifndef PCR
1128         if (GC_base(y) != 0) {
1129           (void)GC_printf0("GC_base(fn_ptr) produced incorrect result\n");
1130           FAIL;
1131         }
1132 #     endif
1133       if (GC_same_obj(x+5, x) != x + 5) {
1134         (void)GC_printf0("GC_same_obj produced incorrect result\n");
1135         FAIL;
1136       }
1137       if (GC_is_visible(y) != y || GC_is_visible(x) != x) {
1138         (void)GC_printf0("GC_is_visible produced incorrect result\n");
1139         FAIL;
1140       }
1141       if (!TEST_FAIL_COUNT(1)) {
1142 #       if!(defined(RS6000) || defined(POWERPC) || defined(IA64))
1143           /* ON RS6000s function pointers point to a descriptor in the  */
1144           /* data segment, so there should have been no failures.       */
1145           (void)GC_printf0("GC_is_visible produced wrong failure indication\n");
1146           FAIL;
1147 #       endif
1148       }
1149       if (GC_is_valid_displacement(y) != y
1150         || GC_is_valid_displacement(x) != x
1151         || GC_is_valid_displacement(x + 3) != x + 3) {
1152         (void)GC_printf0(
1153                 "GC_is_valid_displacement produced incorrect result\n");
1154         FAIL;
1155       }
1156 #     ifndef ALL_INTERIOR_POINTERS
1157 #      if defined(RS6000) || defined(POWERPC)
1158         if (!TEST_FAIL_COUNT(1)) {
1159 #      else
1160         if (GC_all_interior_pointers && !TEST_FAIL_COUNT(1)
1161             || !GC_all_interior_pointers && !TEST_FAIL_COUNT(2)) {
1162 #      endif
1163           (void)GC_printf0("GC_is_valid_displacement produced wrong failure indication\n");
1164           FAIL;
1165         }
1166 #     endif
1167 #   endif /* DBG_HDRS_ALL */
1168     /* Test floating point alignment */
1169    collectable_count += 2;
1170         *(double *)GC_MALLOC(sizeof(double)) = 1.0;
1171         *(double *)GC_MALLOC(sizeof(double)) = 1.0;
1172 #   ifdef GC_GCJ_SUPPORT
1173       GC_REGISTER_DISPLACEMENT(sizeof(struct fake_vtable *));
1174       GC_init_gcj_malloc(0, (void *)fake_gcj_mark_proc);
1175 #   endif
1176     /* Repeated list reversal test. */
1177         reverse_test();
1178 #   ifdef PRINTSTATS
1179         GC_printf0("-------------Finished reverse_test\n");
1180 #   endif
1181 #   ifndef DBG_HDRS_ALL
1182       typed_test();
1183 #     ifdef PRINTSTATS
1184         GC_printf0("-------------Finished typed_test\n");
1185 #     endif
1186 #   endif /* DBG_HDRS_ALL */
1187     tree_test();
1188     LOCK();
1189     n_tests++;
1190     UNLOCK();
1191     /* GC_printf1("Finished %x\n", pthread_self()); */
1192 }
1193
1194 void check_heap_stats()
1195 {
1196     unsigned long max_heap_sz;
1197     register int i;
1198     int still_live;
1199     int late_finalize_count = 0;
1200     
1201 #   ifdef VERY_SMALL_CONFIG
1202     /* these are something of a guess */
1203     if (sizeof(char *) > 4) {
1204         max_heap_sz = 4500000;
1205     } else {
1206         max_heap_sz = 2800000;
1207     }
1208 #   else
1209     if (sizeof(char *) > 4) {
1210         max_heap_sz = 15000000;
1211     } else {
1212         max_heap_sz = 11000000;
1213     }
1214 #   endif
1215 #   ifdef GC_DEBUG
1216         max_heap_sz *= 2;
1217 #       ifdef SAVE_CALL_CHAIN
1218             max_heap_sz *= 3;
1219 #           ifdef SAVE_CALL_COUNT
1220                 max_heap_sz *= SAVE_CALL_COUNT/4;
1221 #           endif
1222 #       endif
1223 #   endif
1224     /* Garbage collect repeatedly so that all inaccessible objects      */
1225     /* can be finalized.                                                */
1226       while (GC_collect_a_little()) { }
1227       for (i = 0; i < 16; i++) {
1228         GC_gcollect();
1229         late_finalize_count += GC_invoke_finalizers();
1230       }
1231     (void)GC_printf1("Completed %lu tests\n", (unsigned long)n_tests);
1232     (void)GC_printf1("Allocated %lu collectable objects\n", (unsigned long)collectable_count);
1233     (void)GC_printf1("Allocated %lu uncollectable objects\n", (unsigned long)uncollectable_count);
1234     (void)GC_printf1("Allocated %lu atomic objects\n", (unsigned long)atomic_count);
1235     (void)GC_printf1("Allocated %lu stubborn objects\n", (unsigned long)stubborn_count);
1236     (void)GC_printf2("Finalized %lu/%lu objects - ",
1237                      (unsigned long)finalized_count,
1238                      (unsigned long)finalizable_count);
1239 #   ifdef FINALIZE_ON_DEMAND
1240         if (finalized_count != late_finalize_count) {
1241             (void)GC_printf0("Demand finalization error\n");
1242             FAIL;
1243         }
1244 #   endif
1245     if (finalized_count > finalizable_count
1246         || finalized_count < finalizable_count/2) {
1247         (void)GC_printf0("finalization is probably broken\n");
1248         FAIL;
1249     } else {
1250         (void)GC_printf0("finalization is probably ok\n");
1251     }
1252     still_live = 0;
1253     for (i = 0; i < MAX_FINALIZED; i++) {
1254         if (live_indicators[i] != 0) {
1255             still_live++;
1256         }
1257     }
1258     i = finalizable_count - finalized_count - still_live;
1259     if (0 != i) {
1260         (void)GC_printf2
1261             ("%lu disappearing links remain and %ld more objects were not finalized\n",
1262              (unsigned long) still_live, (long)i);
1263         if (i > 10) {
1264             GC_printf0("\tVery suspicious!\n");
1265         } else {
1266             GC_printf0("\tSlightly suspicious, but probably OK.\n");
1267         }
1268     }
1269     (void)GC_printf1("Total number of bytes allocated is %lu\n",
1270                 (unsigned long)
1271                    WORDS_TO_BYTES(GC_words_allocd + GC_words_allocd_before_gc));
1272     (void)GC_printf1("Final heap size is %lu bytes\n",
1273                      (unsigned long)GC_get_heap_size());
1274     if (WORDS_TO_BYTES(GC_words_allocd + GC_words_allocd_before_gc)
1275 #   ifdef VERY_SMALL_CONFIG
1276         < 2700000*n_tests) {
1277 #   else
1278         < 33500000*n_tests) {
1279 #   endif
1280         (void)GC_printf0("Incorrect execution - missed some allocations\n");
1281         FAIL;
1282     }
1283     if (GC_get_heap_size() > max_heap_sz*n_tests) {
1284         (void)GC_printf0("Unexpected heap growth - collector may be broken\n");
1285         FAIL;
1286     }
1287     (void)GC_printf0("Collector appears to work\n");
1288 }
1289
1290 #if defined(MACOS)
1291 void SetMinimumStack(long minSize)
1292 {
1293         long newApplLimit;
1294
1295         if (minSize > LMGetDefltStack())
1296         {
1297                 newApplLimit = (long) GetApplLimit()
1298                                 - (minSize - LMGetDefltStack());
1299                 SetApplLimit((Ptr) newApplLimit);
1300                 MaxApplZone();
1301         }
1302 }
1303
1304 #define cMinStackSpace (512L * 1024L)
1305
1306 #endif
1307
1308 #ifdef __STDC__
1309     void warn_proc(char *msg, GC_word p)
1310 #else
1311     void warn_proc(msg, p)
1312     char *msg;
1313     GC_word p;
1314 #endif
1315 {
1316     GC_printf1(msg, (unsigned long)p);
1317     /*FAIL;*/
1318 }
1319
1320
1321 #if !defined(PCR) && !defined(GC_SOLARIS_THREADS) \
1322     && !defined(GC_WIN32_THREADS) \
1323     && !defined(GC_IRIX_THREADS) && !defined(GC_LINUX_THREADS) \
1324     && !defined(GC_HPUX_THREADS) || defined(LINT)
1325 #if defined(MSWIN32) && !defined(__MINGW32__)
1326   int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, LPTSTR cmd, int n)
1327 #else
1328   int main()
1329 #endif
1330 {
1331 #   if defined(DJGPP)
1332         int dummy;
1333 #   endif
1334     n_tests = 0;
1335     
1336 #   if defined(DJGPP)
1337         /* No good way to determine stack base from library; do it */
1338         /* manually on this platform.                              */
1339         GC_stackbottom = (GC_PTR)(&dummy);
1340 #   endif
1341 #   if defined(MACOS)
1342         /* Make sure we have lots and lots of stack space.      */
1343         SetMinimumStack(cMinStackSpace);
1344         /* Cheat and let stdio initialize toolbox for us.       */
1345         printf("Testing GC Macintosh port.\n");
1346 #   endif
1347     GC_INIT();  /* Only needed if gc is dynamic library.        */
1348     (void) GC_set_warn_proc(warn_proc);
1349 #   if defined(MPROTECT_VDB) || defined(PROC_VDB)
1350       GC_enable_incremental();
1351       (void) GC_printf0("Switched to incremental mode\n");
1352 #     if defined(MPROTECT_VDB)
1353         (void)GC_printf0("Emulating dirty bits with mprotect/signals\n");
1354 #     else
1355         (void)GC_printf0("Reading dirty bits from /proc\n");
1356 #      endif
1357 #   endif
1358     run_one_test();
1359     check_heap_stats();
1360 #   ifndef MSWINCE
1361     (void)fflush(stdout);
1362 #   endif
1363 #   ifdef LINT
1364         /* Entry points we should be testing, but aren't.                  */
1365         /* Some can be tested by defining GC_DEBUG at the top of this file */
1366         /* This is a bit SunOS4 specific.                                  */                   
1367         GC_noop(GC_expand_hp, GC_add_roots, GC_clear_roots,
1368                 GC_register_disappearing_link,
1369                 GC_register_finalizer_ignore_self,
1370                 GC_debug_register_displacement,
1371                 GC_print_obj, GC_debug_change_stubborn,
1372                 GC_debug_end_stubborn_change, GC_debug_malloc_uncollectable,
1373                 GC_debug_free, GC_debug_realloc, GC_generic_malloc_words_small,
1374                 GC_init, GC_make_closure, GC_debug_invoke_finalizer,
1375                 GC_page_was_ever_dirty, GC_is_fresh,
1376                 GC_malloc_ignore_off_page, GC_malloc_atomic_ignore_off_page,
1377                 GC_set_max_heap_size, GC_get_bytes_since_gc,
1378                 GC_get_total_bytes, GC_pre_incr, GC_post_incr);
1379 #   endif
1380 #   ifdef MSWIN32
1381       GC_win32_free_heap();
1382 #   endif
1383     return(0);
1384 }
1385 # endif
1386
1387 #ifdef GC_WIN32_THREADS
1388
1389 unsigned __stdcall thr_run_one_test(void *arg)
1390 {
1391   run_one_test();
1392   return 0;
1393 }
1394
1395 #ifdef MSWINCE
1396 HANDLE win_created_h;
1397 HWND win_handle;
1398
1399 LRESULT CALLBACK window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1400 {
1401   LRESULT ret = 0;
1402   switch (uMsg) {
1403     case WM_HIBERNATE:
1404       GC_printf0("Received WM_HIBERNATE, calling GC_gcollect\n");
1405       GC_gcollect();
1406       break;
1407     case WM_CLOSE:
1408       GC_printf0("Received WM_CLOSE, closing window\n");
1409       DestroyWindow(hwnd);
1410       break;
1411     case WM_DESTROY:
1412       PostQuitMessage(0);
1413       break;
1414     default:
1415       ret = DefWindowProc(hwnd, uMsg, wParam, lParam);
1416       break;
1417   }
1418   return ret;
1419 }
1420
1421 unsigned __stdcall thr_window(void *arg)
1422 {
1423   WNDCLASS win_class = {
1424     CS_NOCLOSE,
1425     window_proc,
1426     0,
1427     0,
1428     GetModuleHandle(NULL),
1429     NULL,
1430     NULL,
1431     (HBRUSH)(COLOR_APPWORKSPACE+1),
1432     NULL,
1433     L"GCtestWindow"
1434   };
1435   MSG msg;
1436
1437   if (!RegisterClass(&win_class))
1438     FAIL;
1439
1440   win_handle = CreateWindowEx(
1441     0,
1442     L"GCtestWindow",
1443     L"GCtest",
1444     0,
1445     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1446     NULL,
1447     NULL,
1448     GetModuleHandle(NULL),
1449     NULL);
1450
1451   if (win_handle == NULL)
1452     FAIL;
1453
1454   SetEvent(win_created_h);
1455
1456   ShowWindow(win_handle, SW_SHOW);
1457   UpdateWindow(win_handle);
1458
1459   while (GetMessage(&msg, NULL, 0, 0)) {
1460     TranslateMessage(&msg);
1461     DispatchMessage(&msg);
1462   }
1463
1464   return 0;
1465 }
1466 #endif
1467
1468 #define NTEST 2
1469
1470 # ifdef MSWINCE
1471 int APIENTRY GC_WinMain(HINSTANCE instance, HINSTANCE prev, LPWSTR cmd, int n)
1472 #   else
1473 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, LPSTR cmd, int n)
1474 # endif
1475 {
1476 # if NTEST > 0
1477    HANDLE h[NTEST];
1478    int i;
1479 # endif
1480 # ifdef MSWINCE
1481     HANDLE win_thr_h;
1482 # endif
1483   unsigned thread_id;
1484 # if 0
1485     GC_enable_incremental();
1486 # endif
1487   InitializeCriticalSection(&incr_cs);
1488   (void) GC_set_warn_proc(warn_proc);
1489 # ifdef MSWINCE
1490     win_created_h = CreateEvent(NULL, FALSE, FALSE, NULL);
1491     if (win_created_h == (HANDLE)NULL) {
1492       (void)GC_printf1("Event creation failed %lu\n", (unsigned long)GetLastError());
1493       FAIL;
1494     }
1495     win_thr_h = GC_CreateThread(NULL, 0, thr_window, 0, 0, &thread_id);
1496     if (win_thr_h == (HANDLE)NULL) {
1497       (void)GC_printf1("Thread creation failed %lu\n", (unsigned long)GetLastError());
1498       FAIL;
1499     }
1500     if (WaitForSingleObject(win_created_h, INFINITE) != WAIT_OBJECT_0)
1501       FAIL;
1502     CloseHandle(win_created_h);
1503 # endif
1504 # if NTEST > 0
1505    for (i = 0; i < NTEST; i++) {
1506     h[i] = GC_CreateThread(NULL, 0, thr_run_one_test, 0, 0, &thread_id);
1507     if (h[i] == (HANDLE)NULL) {
1508       (void)GC_printf1("Thread creation failed %lu\n", (unsigned long)GetLastError());
1509       FAIL;
1510     }
1511    }
1512 # endif /* NTEST > 0 */
1513   run_one_test();
1514 # if NTEST > 0
1515    for (i = 0; i < NTEST; i++) {
1516     if (WaitForSingleObject(h[i], INFINITE) != WAIT_OBJECT_0) {
1517       (void)GC_printf1("Thread wait failed %lu\n", (unsigned long)GetLastError());
1518       FAIL;
1519     }
1520    }
1521 # endif /* NTEST > 0 */
1522 # ifdef MSWINCE
1523     PostMessage(win_handle, WM_CLOSE, 0, 0);
1524     if (WaitForSingleObject(win_thr_h, INFINITE) != WAIT_OBJECT_0)
1525       FAIL;
1526 # endif
1527   check_heap_stats();
1528   return(0);
1529 }
1530
1531 #endif /* GC_WIN32_THREADS */
1532
1533
1534 #ifdef PCR
1535 test()
1536 {
1537     PCR_Th_T * th1;
1538     PCR_Th_T * th2;
1539     int code;
1540
1541     n_tests = 0;
1542     /* GC_enable_incremental(); */
1543     (void) GC_set_warn_proc(warn_proc);
1544     th1 = PCR_Th_Fork(run_one_test, 0);
1545     th2 = PCR_Th_Fork(run_one_test, 0);
1546     run_one_test();
1547     if (PCR_Th_T_Join(th1, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
1548         != PCR_ERes_okay || code != 0) {
1549         (void)GC_printf0("Thread 1 failed\n");
1550     }
1551     if (PCR_Th_T_Join(th2, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
1552         != PCR_ERes_okay || code != 0) {
1553         (void)GC_printf0("Thread 2 failed\n");
1554     }
1555     check_heap_stats();
1556     return(0);
1557 }
1558 #endif
1559
1560 #if defined(GC_SOLARIS_THREADS) || defined(GC_IRIX_THREADS) \
1561  || defined(GC_HPUX_THREADS) || defined(GC_LINUX_THREADS)
1562 void * thr_run_one_test(void * arg)
1563 {
1564     run_one_test();
1565     return(0);
1566 }
1567
1568 #ifdef GC_DEBUG
1569 #  define GC_free GC_debug_free
1570 #endif
1571
1572 #ifdef GC_SOLARIS_THREADS
1573 main()
1574 {
1575     thread_t th1;
1576     thread_t th2;
1577     int code;
1578
1579     n_tests = 0;
1580     GC_INIT();  /* Only needed if gc is dynamic library.        */
1581     GC_enable_incremental();
1582     (void) GC_set_warn_proc(warn_proc);
1583     if (thr_keycreate(&fl_key, GC_free) != 0) {
1584         (void)GC_printf1("Key creation failed %lu\n", (unsigned long)code);
1585         FAIL;
1586     }
1587     if ((code = thr_create(0, 1024*1024, thr_run_one_test, 0, 0, &th1)) != 0) {
1588         (void)GC_printf1("Thread 1 creation failed %lu\n", (unsigned long)code);
1589         FAIL;
1590     }
1591     if ((code = thr_create(0, 1024*1024, thr_run_one_test, 0, THR_NEW_LWP, &th2)) != 0) {
1592         (void)GC_printf1("Thread 2 creation failed %lu\n", (unsigned long)code);
1593         FAIL;
1594     }
1595     run_one_test();
1596     if ((code = thr_join(th1, 0, 0)) != 0) {
1597         (void)GC_printf1("Thread 1 failed %lu\n", (unsigned long)code);
1598         FAIL;
1599     }
1600     if (thr_join(th2, 0, 0) != 0) {
1601         (void)GC_printf1("Thread 2 failed %lu\n", (unsigned long)code);
1602         FAIL;
1603     }
1604     check_heap_stats();
1605     (void)fflush(stdout);
1606     return(0);
1607 }
1608 #else /* pthreads */
1609 main()
1610 {
1611     pthread_t th1;
1612     pthread_t th2;
1613     pthread_attr_t attr;
1614     int code;
1615
1616 #   ifdef GC_IRIX_THREADS
1617         /* Force a larger stack to be preallocated      */
1618         /* Since the initial cant always grow later.    */
1619         *((volatile char *)&code - 1024*1024) = 0;      /* Require 1 Mb */
1620 #   endif /* GC_IRIX_THREADS */
1621     pthread_attr_init(&attr);
1622 #   if defined(GC_IRIX_THREADS) || defined(GC_HPUX_THREADS)
1623         pthread_attr_setstacksize(&attr, 1000000);
1624 #   endif
1625     n_tests = 0;
1626 #   if  defined(MPROTECT_VDB) && !defined(PARALLEL_MARK) &&!defined(REDIRECT_MALLOC)
1627         GC_enable_incremental();
1628         (void) GC_printf0("Switched to incremental mode\n");
1629         (void) GC_printf0("Emulating dirty bits with mprotect/signals\n");
1630 #   endif
1631     (void) GC_set_warn_proc(warn_proc);
1632     if ((code = pthread_key_create(&fl_key, 0)) != 0) {
1633         (void)GC_printf1("Key creation failed %lu\n", (unsigned long)code);
1634         FAIL;
1635     }
1636     if ((code = pthread_create(&th1, &attr, thr_run_one_test, 0)) != 0) {
1637         (void)GC_printf1("Thread 1 creation failed %lu\n", (unsigned long)code);
1638         FAIL;
1639     }
1640     if ((code = pthread_create(&th2, &attr, thr_run_one_test, 0)) != 0) {
1641         (void)GC_printf1("Thread 2 creation failed %lu\n", (unsigned long)code);
1642         FAIL;
1643     }
1644     run_one_test();
1645     if ((code = pthread_join(th1, 0)) != 0) {
1646         (void)GC_printf1("Thread 1 failed %lu\n", (unsigned long)code);
1647         FAIL;
1648     }
1649     if (pthread_join(th2, 0) != 0) {
1650         (void)GC_printf1("Thread 2 failed %lu\n", (unsigned long)code);
1651         FAIL;
1652     }
1653     check_heap_stats();
1654     (void)fflush(stdout);
1655     pthread_attr_destroy(&attr);
1656     GC_printf1("Completed %d collections\n", GC_gc_no);
1657     return(0);
1658 }
1659 #endif /* pthreads */
1660 #endif /* SOLARIS_THREADS || IRIX_THREADS || LINUX_THREADS || HPUX_THREADS */