Back to index

plt-scheme  4.2.1
test.c
Go to the documentation of this file.
00001 /* 
00002  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
00003  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
00004  * Copyright (c) 1996 by Silicon Graphics.  All rights reserved.
00005  *
00006  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00007  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00008  *
00009  * Permission is hereby granted to use or copy this program
00010  * for any purpose,  provided the above notices are retained on all copies.
00011  * Permission to modify the code and to distribute modified code is granted,
00012  * provided the above notices are retained, and a notice that the code was
00013  * modified is included with the above copyright notice.
00014  */
00015 /* An incomplete test for the garbage collector.               */
00016 /* Some more obscure entry points are not tested at all.       */
00017 /* This must be compiled with the same flags used to build the        */
00018 /* GC.  It uses GC internals to allow more precise results     */
00019 /* checking for some of the tests.                      */
00020 
00021 # undef GC_BUILD
00022 
00023 #if defined(DBG_HDRS_ALL) || defined(MAKE_BACK_GRAPH)
00024 #  define GC_DEBUG
00025 #endif
00026 
00027 # if defined(mips) && defined(SYSTYPE_BSD43)
00028     /* MIPS RISCOS 4 */
00029 # else
00030 #   include <stdlib.h>
00031 # endif
00032 # include <stdio.h>
00033 # ifdef _WIN32_WCE
00034 #   include <winbase.h>
00035 #   define assert ASSERT
00036 # else
00037 #   include <assert.h>        /* Not normally used, but handy for debugging. */
00038 # endif
00039 # include <assert.h> /* Not normally used, but handy for debugging. */
00040 # include "gc.h"
00041 # include "gc_typed.h"
00042 # ifdef THREAD_LOCAL_ALLOC
00043 #   include "gc_local_alloc.h"
00044 # endif
00045 # include "private/gc_priv.h"      /* For output, locking, MIN_WORDS,        */
00046                             /* and some statistics.                   */
00047 # include "private/gcconfig.h"
00048 
00049 # if defined(MSWIN32) || defined(MSWINCE)
00050 #   include <windows.h>
00051 # endif
00052 
00053 # ifdef PCR
00054 #   include "th/PCR_ThCrSec.h"
00055 #   include "th/PCR_Th.h"
00056 #   undef GC_printf0
00057 #   define GC_printf0 printf
00058 #   undef GC_printf1
00059 #   define GC_printf1 printf
00060 # endif
00061 
00062 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
00063 #   include <thread.h>
00064 #   include <synch.h>
00065 # endif
00066 
00067 # if defined(GC_PTHREADS)
00068 #   include <pthread.h>
00069 # endif
00070 
00071 # if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
00072     static CRITICAL_SECTION incr_cs;
00073 # endif
00074 
00075 #ifdef __STDC__
00076 # include <stdarg.h>
00077 #endif
00078 
00079 
00080 /* Allocation Statistics */
00081 int stubborn_count = 0;
00082 int uncollectable_count = 0;
00083 int collectable_count = 0;
00084 int atomic_count = 0;
00085 int realloc_count = 0;
00086 
00087 #if defined(GC_AMIGA_FASTALLOC) && defined(AMIGA)
00088 
00089   extern void GC_amiga_free_all_mem(void);
00090   void Amiga_Fail(void){GC_amiga_free_all_mem();abort();}
00091 # define FAIL (void)Amiga_Fail()
00092   void *GC_amiga_gctest_malloc_explicitly_typed(size_t lb, GC_descr d){
00093     void *ret=GC_malloc_explicitly_typed(lb,d);
00094     if(ret==NULL){
00095               if(!GC_dont_gc){
00096              GC_gcollect();
00097              ret=GC_malloc_explicitly_typed(lb,d);
00098               }
00099       if(ret==NULL){
00100         GC_printf0("Out of memory, (typed allocations are not directly "
00101                  "supported with the GC_AMIGA_FASTALLOC option.)\n");
00102         FAIL;
00103       }
00104     }
00105     return ret;
00106   }
00107   void *GC_amiga_gctest_calloc_explicitly_typed(size_t a,size_t lb, GC_descr d){
00108     void *ret=GC_calloc_explicitly_typed(a,lb,d);
00109     if(ret==NULL){
00110               if(!GC_dont_gc){
00111              GC_gcollect();
00112              ret=GC_calloc_explicitly_typed(a,lb,d);
00113               }
00114       if(ret==NULL){
00115         GC_printf0("Out of memory, (typed allocations are not directly "
00116                  "supported with the GC_AMIGA_FASTALLOC option.)\n");
00117         FAIL;
00118       }
00119     }
00120     return ret;
00121   }
00122 # define GC_malloc_explicitly_typed(a,b) GC_amiga_gctest_malloc_explicitly_typed(a,b) 
00123 # define GC_calloc_explicitly_typed(a,b,c) GC_amiga_gctest_calloc_explicitly_typed(a,b,c) 
00124 
00125 #else /* !AMIGA_FASTALLOC */
00126 
00127 # ifdef PCR
00128 #   define FAIL (void)abort()
00129 # else
00130 #   ifdef MSWINCE
00131 #     define FAIL DebugBreak()
00132 #   else
00133 #     define FAIL GC_abort("Test failed");
00134 #   endif
00135 # endif
00136 
00137 #endif /* !AMIGA_FASTALLOC */
00138 
00139 /* AT_END may be defined to exercise the interior pointer test */
00140 /* if the collector is configured with ALL_INTERIOR_POINTERS.   */
00141 /* As it stands, this test should succeed with either          */
00142 /* configuration.  In the FIND_LEAK configuration, it should   */
00143 /* find lots of leaks, since we free almost nothing.           */
00144 
00145 struct SEXPR {
00146     struct SEXPR * sexpr_car;
00147     struct SEXPR * sexpr_cdr;
00148 };
00149 
00150 
00151 typedef struct SEXPR * sexpr;
00152 
00153 # define INT_TO_SEXPR(x) ((sexpr)(unsigned long)(x))
00154 
00155 # undef nil
00156 # define nil (INT_TO_SEXPR(0))
00157 # define car(x) ((x) -> sexpr_car)
00158 # define cdr(x) ((x) -> sexpr_cdr)
00159 # define is_nil(x) ((x) == nil)
00160 
00161 
00162 int extra_count = 0;        /* Amount of space wasted in cons node */
00163 
00164 /* Silly implementation of Lisp cons. Intentionally wastes lots of space */
00165 /* to test collector.                                                    */
00166 # ifdef VERY_SMALL_CONFIG
00167 #   define cons small_cons
00168 # else
00169 sexpr cons (x, y)
00170 sexpr x;
00171 sexpr y;
00172 {
00173     register sexpr r;
00174     register int *p;
00175     register int my_extra = extra_count;
00176     
00177     stubborn_count++;
00178     r = (sexpr) GC_MALLOC_STUBBORN(sizeof(struct SEXPR) + my_extra);
00179     if (r == 0) {
00180         (void)GC_printf0("Out of memory\n");
00181         exit(1);
00182     }
00183     for (p = (int *)r;
00184          ((char *)p) < ((char *)r) + my_extra + sizeof(struct SEXPR); p++) {
00185        if (*p) {
00186            (void)GC_printf1("Found nonzero at 0x%lx - allocator is broken\n",
00187                           (unsigned long)p);
00188            FAIL;
00189         }
00190         *p = 13;
00191     }
00192 #   ifdef AT_END
00193        r = (sexpr)((char *)r + (my_extra & ~7));
00194 #   endif
00195     r -> sexpr_car = x;
00196     r -> sexpr_cdr = y;
00197     my_extra++;
00198     if ( my_extra >= 5000 ) {
00199         extra_count = 0;
00200     } else {
00201         extra_count = my_extra;
00202     }
00203     GC_END_STUBBORN_CHANGE((char *)r);
00204     return(r);
00205 }
00206 # endif
00207 
00208 #ifdef GC_GCJ_SUPPORT
00209 
00210 #include "gc_mark.h"
00211 #include "gc_gcj.h"
00212 
00213 /* The following struct emulates the vtable in gcj.     */
00214 /* This assumes the default value of MARK_DESCR_OFFSET. */
00215 struct fake_vtable {
00216   void * dummy;             /* class pointer in real gcj.      */
00217   size_t descr;
00218 };
00219 
00220 struct fake_vtable gcj_class_struct1 = { 0, sizeof(struct SEXPR)
00221                                        + sizeof(struct fake_vtable *) };
00222                      /* length based descriptor. */
00223 struct fake_vtable gcj_class_struct2 =
00224                             { 0, (3l << (CPP_WORDSZ - 3)) | GC_DS_BITMAP};
00225                      /* Bitmap based descriptor. */
00226 
00227 struct GC_ms_entry * fake_gcj_mark_proc(word * addr,
00228                                     struct GC_ms_entry *mark_stack_ptr,
00229                                     struct GC_ms_entry *mark_stack_limit,
00230                                     word env   )
00231 {
00232     sexpr x;
00233     if (1 == env) {
00234        /* Object allocated with debug allocator. */
00235        addr = (word *)GC_USR_PTR_FROM_BASE(addr);
00236     }
00237     x = (sexpr)(addr + 1); /* Skip the vtable pointer. */
00238     mark_stack_ptr = GC_MARK_AND_PUSH(
00239                            (GC_PTR)(x -> sexpr_cdr), mark_stack_ptr,
00240                            mark_stack_limit, (GC_PTR *)&(x -> sexpr_cdr));
00241     mark_stack_ptr = GC_MARK_AND_PUSH(
00242                            (GC_PTR)(x -> sexpr_car), mark_stack_ptr,
00243                            mark_stack_limit, (GC_PTR *)&(x -> sexpr_car));
00244     return(mark_stack_ptr);
00245 }
00246 
00247 #endif /* GC_GCJ_SUPPORT */
00248 
00249 #ifdef THREAD_LOCAL_ALLOC
00250 
00251 #undef GC_REDIRECT_TO_LOCAL
00252 #include "gc_local_alloc.h"
00253 
00254 sexpr local_cons (x, y)
00255 sexpr x;
00256 sexpr y;
00257 {
00258     register sexpr r;
00259     register int *p;
00260     register int my_extra = extra_count;
00261     static int my_random = 0;
00262     
00263     collectable_count++;
00264     r = (sexpr) GC_LOCAL_MALLOC(sizeof(struct SEXPR) + my_extra);
00265 #   ifdef GC_GCJ_SUPPORT
00266       if (collectable_count % 2 == 0) {
00267         r = (sexpr) GC_LOCAL_GCJ_MALLOC(sizeof(struct SEXPR) + sizeof(GC_word) + my_extra,
00268                                    &gcj_class_struct1);
00269         r = (sexpr) ((GC_word *)r + 1);
00270       }
00271 #   endif
00272     if (r == 0) {
00273         (void)GC_printf0("Out of memory\n");
00274         exit(1);
00275     }
00276     for (p = (int *)r;
00277          ((char *)p) < ((char *)r) + my_extra + sizeof(struct SEXPR); p++) {
00278        if (*p) {
00279            (void)GC_printf1("Found nonzero at 0x%lx (local) - allocator is broken\n",
00280                           (unsigned long)p);
00281            FAIL;
00282         }
00283         *p = 13;
00284     }
00285     r -> sexpr_car = x;
00286     r -> sexpr_cdr = y;
00287     my_extra++;
00288     if ( my_extra >= 5000 || my_extra == 200 && ++my_random % 37 != 0) {
00289         extra_count = 0;
00290     } else {
00291         extra_count = my_extra;
00292     }
00293     return(r);
00294 }
00295 #endif /* THREAD_LOCAL_ALLOC */
00296 
00297 sexpr small_cons (x, y)
00298 sexpr x;
00299 sexpr y;
00300 {
00301     register sexpr r;
00302     
00303     collectable_count++;
00304     r = (sexpr) GC_MALLOC(sizeof(struct SEXPR));
00305     if (r == 0) {
00306         (void)GC_printf0("Out of memory\n");
00307         exit(1);
00308     }
00309     r -> sexpr_car = x;
00310     r -> sexpr_cdr = y;
00311     return(r);
00312 }
00313 
00314 sexpr small_cons_uncollectable (x, y)
00315 sexpr x;
00316 sexpr y;
00317 {
00318     register sexpr r;
00319     
00320     uncollectable_count++;
00321     r = (sexpr) GC_MALLOC_UNCOLLECTABLE(sizeof(struct SEXPR));
00322     if (r == 0) {
00323         (void)GC_printf0("Out of memory\n");
00324         exit(1);
00325     }
00326     r -> sexpr_car = x;
00327     r -> sexpr_cdr = (sexpr)(~(unsigned long)y);
00328     return(r);
00329 }
00330 
00331 #ifdef GC_GCJ_SUPPORT
00332 
00333 
00334 sexpr gcj_cons(x, y)
00335 sexpr x;
00336 sexpr y;
00337 {
00338     GC_word * r;
00339     sexpr result;
00340     static int count = 0;
00341     
00342     if (++count & 1) {
00343 #     ifdef USE_MARK_BYTES
00344         r = (GC_word *) GC_GCJ_FAST_MALLOC(4, &gcj_class_struct1);
00345 #     else
00346         r = (GC_word *) GC_GCJ_FAST_MALLOC(3, &gcj_class_struct1);
00347 #     endif
00348     } else {
00349         r = (GC_word *) GC_GCJ_MALLOC(sizeof(struct SEXPR)
00350                                   + sizeof(struct fake_vtable*),
00351                                   &gcj_class_struct2);
00352     }
00353     if (r == 0) {
00354         (void)GC_printf0("Out of memory\n");
00355         exit(1);
00356     }
00357     result = (sexpr)(r + 1);
00358     result -> sexpr_car = x;
00359     result -> sexpr_cdr = y;
00360     return(result);
00361 }
00362 #endif
00363 
00364 /* Return reverse(x) concatenated with y */
00365 sexpr reverse1(x, y)
00366 sexpr x, y;
00367 {
00368     if (is_nil(x)) {
00369         return(y);
00370     } else {
00371         return( reverse1(cdr(x), cons(car(x), y)) );
00372     }
00373 }
00374 
00375 sexpr reverse(x)
00376 sexpr x;
00377 {
00378 #   ifdef TEST_WITH_SYSTEM_MALLOC
00379       malloc(100000);
00380 #   endif
00381     return( reverse1(x, nil) );
00382 }
00383 
00384 sexpr ints(low, up)
00385 int low, up;
00386 {
00387     if (low > up) {
00388        return(nil);
00389     } else {
00390         return(small_cons(small_cons(INT_TO_SEXPR(low), nil), ints(low+1, up)));
00391     }
00392 }
00393 
00394 #ifdef GC_GCJ_SUPPORT
00395 /* Return reverse(x) concatenated with y */
00396 sexpr gcj_reverse1(x, y)
00397 sexpr x, y;
00398 {
00399     if (is_nil(x)) {
00400         return(y);
00401     } else {
00402         return( gcj_reverse1(cdr(x), gcj_cons(car(x), y)) );
00403     }
00404 }
00405 
00406 sexpr gcj_reverse(x)
00407 sexpr x;
00408 {
00409     return( gcj_reverse1(x, nil) );
00410 }
00411 
00412 sexpr gcj_ints(low, up)
00413 int low, up;
00414 {
00415     if (low > up) {
00416        return(nil);
00417     } else {
00418         return(gcj_cons(gcj_cons(INT_TO_SEXPR(low), nil), gcj_ints(low+1, up)));
00419     }
00420 }
00421 #endif /* GC_GCJ_SUPPORT */
00422 
00423 #ifdef THREAD_LOCAL_ALLOC
00424 /* Return reverse(x) concatenated with y */
00425 sexpr local_reverse1(x, y)
00426 sexpr x, y;
00427 {
00428     if (is_nil(x)) {
00429         return(y);
00430     } else {
00431         return( local_reverse1(cdr(x), local_cons(car(x), y)) );
00432     }
00433 }
00434 
00435 sexpr local_reverse(x)
00436 sexpr x;
00437 {
00438     return( local_reverse1(x, nil) );
00439 }
00440 
00441 sexpr local_ints(low, up)
00442 int low, up;
00443 {
00444     if (low > up) {
00445        return(nil);
00446     } else {
00447         return(local_cons(local_cons(INT_TO_SEXPR(low), nil), local_ints(low+1, up)));
00448     }
00449 }
00450 #endif /* THREAD_LOCAL_ALLOC */
00451 
00452 /* To check uncollectable allocation we build lists with disguised cdr       */
00453 /* pointers, and make sure they don't go away.                        */
00454 sexpr uncollectable_ints(low, up)
00455 int low, up;
00456 {
00457     if (low > up) {
00458        return(nil);
00459     } else {
00460         return(small_cons_uncollectable(small_cons(INT_TO_SEXPR(low), nil),
00461                uncollectable_ints(low+1, up)));
00462     }
00463 }
00464 
00465 void check_ints(list, low, up)
00466 sexpr list;
00467 int low, up;
00468 {
00469     if ((int)(GC_word)(car(car(list))) != low) {
00470         (void)GC_printf0(
00471            "List reversal produced incorrect list - collector is broken\n");
00472         FAIL;
00473     }
00474     if (low == up) {
00475         if (cdr(list) != nil) {
00476            (void)GC_printf0("List too long - collector is broken\n");
00477            FAIL;
00478         }
00479     } else {
00480         check_ints(cdr(list), low+1, up);
00481     }
00482 }
00483 
00484 # define UNCOLLECTABLE_CDR(x) (sexpr)(~(unsigned long)(cdr(x)))
00485 
00486 void check_uncollectable_ints(list, low, up)
00487 sexpr list;
00488 int low, up;
00489 {
00490     if ((int)(GC_word)(car(car(list))) != low) {
00491         (void)GC_printf0(
00492            "Uncollectable list corrupted - collector is broken\n");
00493         FAIL;
00494     }
00495     if (low == up) {
00496         if (UNCOLLECTABLE_CDR(list) != nil) {
00497            (void)GC_printf0("Uncollectable list too long - collector is broken\n");
00498            FAIL;
00499         }
00500     } else {
00501         check_uncollectable_ints(UNCOLLECTABLE_CDR(list), low+1, up);
00502     }
00503 }
00504 
00505 /* Not used, but useful for debugging: */
00506 void print_int_list(x)
00507 sexpr x;
00508 {
00509     if (is_nil(x)) {
00510         (void)GC_printf0("NIL\n");
00511     } else {
00512         (void)GC_printf1("(%ld)", (long)(car(car(x))));
00513         if (!is_nil(cdr(x))) {
00514             (void)GC_printf0(", ");
00515             (void)print_int_list(cdr(x));
00516         } else {
00517             (void)GC_printf0("\n");
00518         }
00519     }
00520 }
00521 
00522 /*
00523  * A tiny list reversal test to check thread creation.
00524  */
00525 #ifdef THREADS
00526 
00527 # if defined(GC_WIN32_THREADS) && !defined(CYGWIN32)
00528     DWORD  __stdcall tiny_reverse_test(void * arg)
00529 # else
00530     void * tiny_reverse_test(void * arg)
00531 # endif
00532 {
00533     int i;
00534     for (i = 0; i < 5; ++i) {
00535       check_ints(reverse(reverse(ints(1,10))), 1, 10);
00536 #     ifdef THREAD_LOCAL_ALLOC
00537         check_ints(local_reverse(local_reverse(local_ints(1,10))), 1, 10);
00538 #     endif
00539     }
00540     return 0;
00541 }
00542 
00543 # if defined(GC_PTHREADS)
00544     void fork_a_thread()
00545     {
00546       pthread_t t;
00547       int code;
00548       if ((code = pthread_create(&t, 0, tiny_reverse_test, 0)) != 0) {
00549        (void)GC_printf1("Small thread creation failed %lu\n",
00550                        (unsigned long)code);
00551        FAIL;
00552       }
00553       if ((code = pthread_join(t, 0)) != 0) {
00554         (void)GC_printf1("Small thread join failed %lu\n",
00555        (unsigned long)code);
00556         FAIL;
00557       }
00558     }
00559 
00560 # elif defined(GC_WIN32_THREADS)
00561     void fork_a_thread()
00562     {
00563        DWORD thread_id;
00564        HANDLE h;
00565        h = GC_CreateThread(NULL, 0, tiny_reverse_test, 0, 0, &thread_id);
00566         if (h == (HANDLE)NULL) {
00567             (void)GC_printf1("Small thread creation failed %lu\n",
00568                           (unsigned long)GetLastError());
00569            FAIL;
00570         }
00571        if (WaitForSingleObject(h, INFINITE) != WAIT_OBJECT_0) {
00572            (void)GC_printf1("Small thread wait failed %lu\n",
00573                           (unsigned long)GetLastError());
00574            FAIL;
00575        }
00576     }
00577 
00578 /* # elif defined(GC_SOLARIS_THREADS) */
00579 
00580 # else
00581 
00582 #   define fork_a_thread()
00583 
00584 # endif
00585 
00586 #else
00587 
00588 # define fork_a_thread()
00589 
00590 #endif 
00591 
00592 /* Try to force a to be strangely aligned */
00593 struct {
00594   char dummy;
00595   sexpr aa;
00596 } A;
00597 #define a A.aa
00598 
00599 /*
00600  * Repeatedly reverse lists built out of very different sized cons cells.
00601  * Check that we didn't lose anything.
00602  */
00603 void reverse_test()
00604 {
00605     int i;
00606     sexpr b;
00607     sexpr c;
00608     sexpr d;
00609     sexpr e;
00610     sexpr *f, *g, *h;
00611 #   if defined(MSWIN32) || defined(MACOS)
00612       /* Win32S only allows 128K stacks */
00613 #     define BIG 1000
00614 #   else
00615 #     if defined PCR
00616        /* PCR default stack is 100K.  Stack frames are up to 120 bytes. */
00617 #      define BIG 700
00618 #     else
00619 #      if defined MSWINCE
00620          /* WinCE only allows 64K stacks */
00621 #        define BIG 500
00622 #      else
00623 #        if defined(OSF1)
00624            /* OSF has limited stack space by default, and large frames. */
00625 #           define BIG 200
00626 #        else
00627 #           define BIG 4500
00628 #        endif
00629 #      endif
00630 #     endif
00631 #   endif
00632 
00633     A.dummy = 17;
00634     a = ints(1, 49);
00635     b = ints(1, 50);
00636     c = ints(1, BIG);
00637     d = uncollectable_ints(1, 100);
00638     e = uncollectable_ints(1, 1);
00639     /* Check that realloc updates object descriptors correctly */
00640     collectable_count++;
00641     f = (sexpr *)GC_MALLOC(4 * sizeof(sexpr));
00642     realloc_count++;
00643     f = (sexpr *)GC_REALLOC((GC_PTR)f, 6 * sizeof(sexpr));
00644     f[5] = ints(1,17);
00645     collectable_count++;
00646     g = (sexpr *)GC_MALLOC(513 * sizeof(sexpr));
00647     realloc_count++;
00648     g = (sexpr *)GC_REALLOC((GC_PTR)g, 800 * sizeof(sexpr));
00649     g[799] = ints(1,18);
00650     collectable_count++;
00651     h = (sexpr *)GC_MALLOC(1025 * sizeof(sexpr));
00652     realloc_count++;
00653     h = (sexpr *)GC_REALLOC((GC_PTR)h, 2000 * sizeof(sexpr));
00654 #   ifdef GC_GCJ_SUPPORT
00655       h[1999] = gcj_ints(1,200);
00656       for (i = 0; i < 51; ++i) 
00657         h[1999] = gcj_reverse(h[1999]);
00658       /* Leave it as the reveresed list for now. */
00659 #   else
00660       h[1999] = ints(1,200);
00661 #   endif
00662     /* Try to force some collections and reuse of small list elements */
00663       for (i = 0; i < 10; i++) {
00664         (void)ints(1, BIG);
00665       }
00666     /* Superficially test interior pointer recognition on stack */
00667       c = (sexpr)((char *)c + sizeof(char *));
00668       d = (sexpr)((char *)d + sizeof(char *));
00669 
00670 #   ifdef __STDC__
00671         GC_FREE((void *)e);
00672 #   else
00673         GC_FREE((char *)e);
00674 #   endif
00675     check_ints(b,1,50);
00676     check_ints(a,1,49);
00677     for (i = 0; i < 50; i++) {
00678         check_ints(b,1,50);
00679         b = reverse(reverse(b));
00680     }
00681     check_ints(b,1,50);
00682     check_ints(a,1,49);
00683     for (i = 0; i < 60; i++) {
00684        if (i % 10 == 0) fork_a_thread();
00685        /* This maintains the invariant that a always points to a list of */
00686        /* 49 integers.  Thus this is thread safe without locks,         */
00687        /* assuming atomic pointer assignments.                          */
00688         a = reverse(reverse(a));
00689 #       ifdef THREAD_LOCAL_ALLOC
00690          a = local_reverse(local_reverse(a));
00691 #      endif
00692 #      if !defined(AT_END) && !defined(THREADS)
00693          /* This is not thread safe, since realloc explicitly deallocates */
00694           if (i & 1) {
00695             a = (sexpr)GC_REALLOC((GC_PTR)a, 500);
00696           } else {
00697             a = (sexpr)GC_REALLOC((GC_PTR)a, 8200);
00698           }
00699 #      endif
00700     }
00701     check_ints(a,1,49);
00702     check_ints(b,1,50);
00703     c = (sexpr)((char *)c - sizeof(char *));
00704     d = (sexpr)((char *)d - sizeof(char *));
00705     check_ints(c,1,BIG);
00706     check_uncollectable_ints(d, 1, 100);
00707     check_ints(f[5], 1,17);
00708     check_ints(g[799], 1,18);
00709 #   ifdef GC_GCJ_SUPPORT
00710       h[1999] = gcj_reverse(h[1999]);
00711 #   endif
00712     check_ints(h[1999], 1,200);
00713 #   ifndef THREADS
00714        a = 0;
00715 #   endif  
00716     b = c = 0;
00717 }
00718 
00719 #undef a
00720 
00721 /*
00722  * The rest of this builds balanced binary trees, checks that they don't
00723  * disappear, and tests finalization.
00724  */
00725 typedef struct treenode {
00726     int level;
00727     struct treenode * lchild;
00728     struct treenode * rchild;
00729 } tn;
00730 
00731 int finalizable_count = 0;
00732 int finalized_count = 0;
00733 VOLATILE int dropped_something = 0;
00734 
00735 # ifdef __STDC__
00736   void finalizer(void * obj, void * client_data)
00737 # else
00738   void finalizer(obj, client_data)
00739   char * obj;
00740   char * client_data;
00741 # endif
00742 {
00743   tn * t = (tn *)obj;
00744 
00745 # ifdef PCR
00746      PCR_ThCrSec_EnterSys();
00747 # endif
00748 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
00749     static mutex_t incr_lock;
00750     mutex_lock(&incr_lock);
00751 # endif
00752 # if  defined(GC_PTHREADS)
00753     static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
00754     pthread_mutex_lock(&incr_lock);
00755 # else
00756 #   ifdef GC_WIN32_THREADS
00757       EnterCriticalSection(&incr_cs);
00758 #   endif
00759 # endif
00760   if ((int)(GC_word)client_data != t -> level) {
00761      (void)GC_printf0("Wrong finalization data - collector is broken\n");
00762      FAIL;
00763   }
00764   finalized_count++;
00765   t -> level = -1;   /* detect duplicate finalization immediately */
00766 # ifdef PCR
00767     PCR_ThCrSec_ExitSys();
00768 # endif
00769 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
00770     mutex_unlock(&incr_lock);
00771 # endif
00772 # if defined(GC_PTHREADS)
00773     pthread_mutex_unlock(&incr_lock);
00774 # else
00775 #   ifdef GC_WIN32_THREADS
00776       LeaveCriticalSection(&incr_cs);
00777 #   endif
00778 # endif
00779 }
00780 
00781 size_t counter = 0;
00782 
00783 # define MAX_FINALIZED 8000
00784 
00785 # if !defined(MACOS)
00786   GC_FAR GC_word live_indicators[MAX_FINALIZED] = {0};
00787 #else
00788   /* Too big for THINK_C. have to allocate it dynamically. */
00789   GC_word *live_indicators = 0;
00790 #endif
00791 
00792 int live_indicators_count = 0;
00793 
00794 tn * mktree(n)
00795 int n;
00796 {
00797 #   ifdef THREAD_LOCAL_ALLOC
00798       tn * result = (tn *)GC_LOCAL_MALLOC(sizeof(tn));
00799 #   else
00800       tn * result = (tn *)GC_MALLOC(sizeof(tn));
00801 #   endif
00802     
00803     collectable_count++;
00804 #   ifdef THREAD_LOCAL_ALLOC
00805        /* Minimally exercise thread local allocation */
00806        {
00807          char * result = (char *)GC_LOCAL_MALLOC_ATOMIC(17);
00808         memset(result, 'a', 17);
00809        }
00810 #   endif /* THREAD_LOCAL_ALLOC */
00811 #   if defined(MACOS)
00812        /* get around static data limitations. */
00813        if (!live_indicators)
00814               live_indicators =
00815                   (GC_word*)NewPtrClear(MAX_FINALIZED * sizeof(GC_word));
00816        if (!live_indicators) {
00817           (void)GC_printf0("Out of memory\n");
00818           exit(1);
00819         }
00820 #   endif
00821     if (n == 0) return(0);
00822     if (result == 0) {
00823         (void)GC_printf0("Out of memory\n");
00824         exit(1);
00825     }
00826     result -> level = n;
00827     result -> lchild = mktree(n-1);
00828     result -> rchild = mktree(n-1);
00829     if (counter++ % 17 == 0 && n >= 2) {
00830         tn * tmp = result -> lchild -> rchild;
00831         
00832         result -> lchild -> rchild = result -> rchild -> lchild;
00833         result -> rchild -> lchild = tmp;
00834     }
00835     if (counter++ % 119 == 0) {
00836         int my_index;
00837         
00838         {
00839 #        ifdef PCR
00840            PCR_ThCrSec_EnterSys();
00841 #        endif
00842 #        if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
00843            static mutex_t incr_lock;
00844            mutex_lock(&incr_lock);
00845 #        endif
00846 #         if defined(GC_PTHREADS)
00847             static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
00848             pthread_mutex_lock(&incr_lock);
00849 #         else
00850 #           ifdef GC_WIN32_THREADS
00851               EnterCriticalSection(&incr_cs);
00852 #           endif
00853 #         endif
00854               /* Losing a count here causes erroneous report of failure. */
00855           finalizable_count++;
00856           my_index = live_indicators_count++;
00857 #        ifdef PCR
00858            PCR_ThCrSec_ExitSys();
00859 #        endif
00860 #        if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
00861            mutex_unlock(&incr_lock);
00862 #        endif
00863 #        if defined(GC_PTHREADS)
00864            pthread_mutex_unlock(&incr_lock);
00865 #        else
00866 #           ifdef GC_WIN32_THREADS
00867               LeaveCriticalSection(&incr_cs);
00868 #           endif
00869 #         endif
00870        }
00871 
00872         GC_REGISTER_FINALIZER((GC_PTR)result, finalizer, (GC_PTR)(GC_word)n,
00873                            (GC_finalization_proc *)0, (GC_PTR *)0);
00874         if (my_index >= MAX_FINALIZED) {
00875               GC_printf0("live_indicators overflowed\n");
00876               FAIL;
00877        }
00878         live_indicators[my_index] = 13;
00879         if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
00880               (GC_PTR *)(&(live_indicators[my_index])),
00881               (GC_PTR)result) != 0) {
00882               GC_printf0("GC_general_register_disappearing_link failed\n");
00883               FAIL;
00884         }
00885         if (GC_unregister_disappearing_link(
00886               (GC_PTR *)
00887                  (&(live_indicators[my_index]))) == 0) {
00888               GC_printf0("GC_unregister_disappearing_link failed\n");
00889               FAIL;
00890         }
00891         if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
00892               (GC_PTR *)(&(live_indicators[my_index])),
00893               (GC_PTR)result) != 0) {
00894               GC_printf0("GC_general_register_disappearing_link failed 2\n");
00895               FAIL;
00896         }
00897     }
00898     return(result);
00899 }
00900 
00901 void chktree(t,n)
00902 tn *t;
00903 int n;
00904 {
00905     if (n == 0 && t != 0) {
00906         (void)GC_printf0("Clobbered a leaf - collector is broken\n");
00907         FAIL;
00908     }
00909     if (n == 0) return;
00910     if (t -> level != n) {
00911         (void)GC_printf1("Lost a node at level %lu - collector is broken\n",
00912                       (unsigned long)n);
00913         FAIL;
00914     }
00915     if (counter++ % 373 == 0) {
00916        collectable_count++;
00917        (void) GC_MALLOC(counter%5001);
00918     }
00919     chktree(t -> lchild, n-1);
00920     if (counter++ % 73 == 0) {
00921        collectable_count++;
00922        (void) GC_MALLOC(counter%373);
00923     }
00924     chktree(t -> rchild, n-1);
00925 }
00926 
00927 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
00928 thread_key_t fl_key;
00929 
00930 void * alloc8bytes()
00931 {
00932 # if defined(SMALL_CONFIG) || defined(GC_DEBUG)
00933     collectable_count++;
00934     return(GC_MALLOC(8));
00935 # else
00936     void ** my_free_list_ptr;
00937     void * my_free_list;
00938     
00939     if (thr_getspecific(fl_key, (void **)(&my_free_list_ptr)) != 0) {
00940        (void)GC_printf0("thr_getspecific failed\n");
00941        FAIL;
00942     }
00943     if (my_free_list_ptr == 0) {
00944         uncollectable_count++;
00945         my_free_list_ptr = GC_NEW_UNCOLLECTABLE(void *);
00946         if (thr_setspecific(fl_key, my_free_list_ptr) != 0) {
00947            (void)GC_printf0("thr_setspecific failed\n");
00948            FAIL;
00949         }
00950     }
00951     my_free_list = *my_free_list_ptr;
00952     if (my_free_list == 0) {
00953         collectable_count++;
00954         my_free_list = GC_malloc_many(8);
00955         if (my_free_list == 0) {
00956             (void)GC_printf0("alloc8bytes out of memory\n");
00957            FAIL;
00958         }
00959     }
00960     *my_free_list_ptr = GC_NEXT(my_free_list);
00961     GC_NEXT(my_free_list) = 0;
00962     return(my_free_list);
00963 # endif
00964 }
00965 
00966 #else
00967 
00968 # if defined(GC_PTHREADS)
00969 pthread_key_t fl_key;
00970 
00971 void * alloc8bytes()
00972 {
00973 # if defined(SMALL_CONFIG) || defined(GC_DEBUG)
00974     collectable_count++;
00975     return(GC_MALLOC(8));
00976 # else
00977     void ** my_free_list_ptr;
00978     void * my_free_list;
00979     
00980     my_free_list_ptr = (void **)pthread_getspecific(fl_key);
00981     if (my_free_list_ptr == 0) {
00982         uncollectable_count++;
00983         my_free_list_ptr = GC_NEW_UNCOLLECTABLE(void *);
00984         if (pthread_setspecific(fl_key, my_free_list_ptr) != 0) {
00985            (void)GC_printf0("pthread_setspecific failed\n");
00986            FAIL;
00987         }
00988     }
00989     my_free_list = *my_free_list_ptr;
00990     if (my_free_list == 0) {
00991         my_free_list = GC_malloc_many(8);
00992         if (my_free_list == 0) {
00993             (void)GC_printf0("alloc8bytes out of memory\n");
00994            FAIL;
00995         }
00996     }
00997     *my_free_list_ptr = GC_NEXT(my_free_list);
00998     GC_NEXT(my_free_list) = 0;
00999     collectable_count++;
01000     return(my_free_list);
01001 # endif
01002 }
01003 
01004 # else
01005 #   define alloc8bytes() GC_MALLOC_ATOMIC(8)
01006 # endif
01007 #endif
01008 
01009 void alloc_small(n)
01010 int n;
01011 {
01012     register int i;
01013     
01014     for (i = 0; i < n; i += 8) {
01015         atomic_count++;
01016         if (alloc8bytes() == 0) {
01017             (void)GC_printf0("Out of memory\n");
01018             FAIL;
01019         }
01020     }
01021 }
01022 
01023 # if defined(THREADS) && defined(GC_DEBUG)
01024 #   ifdef VERY_SMALL_CONFIG
01025 #     define TREE_HEIGHT 12
01026 #   else
01027 #     define TREE_HEIGHT 15
01028 #   endif
01029 # else
01030 #   ifdef VERY_SMALL_CONFIG
01031 #     define TREE_HEIGHT 13
01032 #   else
01033 #     define TREE_HEIGHT 16
01034 #   endif
01035 # endif
01036 void tree_test()
01037 {
01038     tn * root;
01039     register int i;
01040     
01041     root = mktree(TREE_HEIGHT);
01042 #   ifndef VERY_SMALL_CONFIG
01043       alloc_small(5000000);
01044 #   endif
01045     chktree(root, TREE_HEIGHT);
01046     if (finalized_count && ! dropped_something) {
01047         (void)GC_printf0("Premature finalization - collector is broken\n");
01048         FAIL;
01049     }
01050     dropped_something = 1;
01051     GC_noop(root);   /* Root needs to remain live until */
01052                      /* dropped_something is set.              */
01053     root = mktree(TREE_HEIGHT);
01054     chktree(root, TREE_HEIGHT);
01055     for (i = TREE_HEIGHT; i >= 0; i--) {
01056         root = mktree(i);
01057         chktree(root, i);
01058     }
01059 #   ifndef VERY_SMALL_CONFIG
01060       alloc_small(5000000);
01061 #   endif
01062 }
01063 
01064 unsigned n_tests = 0;
01065 
01066 GC_word bm_huge[10] = {
01067     0xffffffff,
01068     0xffffffff,
01069     0xffffffff,
01070     0xffffffff,
01071     0xffffffff,
01072     0xffffffff,
01073     0xffffffff,
01074     0xffffffff,
01075     0xffffffff,
01076     0x00ffffff,
01077 };
01078 
01079 /* A very simple test of explicitly typed allocation    */
01080 void typed_test()
01081 {
01082     GC_word * old, * new;
01083     GC_word bm3 = 0x3;
01084     GC_word bm2 = 0x2;
01085     GC_word bm_large = 0xf7ff7fff;
01086     GC_descr d1 = GC_make_descriptor(&bm3, 2);
01087     GC_descr d2 = GC_make_descriptor(&bm2, 2);
01088 #   ifndef LINT
01089       GC_descr dummy = GC_make_descriptor(&bm_large, 32);
01090 #   endif
01091     GC_descr d3 = GC_make_descriptor(&bm_large, 32);
01092     GC_descr d4 = GC_make_descriptor(bm_huge, 320);
01093     GC_word * x = (GC_word *)GC_malloc_explicitly_typed(2000, d4);
01094     register int i;
01095     
01096     collectable_count++;
01097     old = 0;
01098     for (i = 0; i < 4000; i++) {
01099        collectable_count++;
01100         new = (GC_word *) GC_malloc_explicitly_typed(4 * sizeof(GC_word), d1);
01101         if (0 != new[0] || 0 != new[1]) {
01102            GC_printf0("Bad initialization by GC_malloc_explicitly_typed\n");
01103            FAIL;
01104        }
01105         new[0] = 17;
01106         new[1] = (GC_word)old;
01107         old = new;
01108        collectable_count++;
01109         new = (GC_word *) GC_malloc_explicitly_typed(4 * sizeof(GC_word), d2);
01110         new[0] = 17;
01111         new[1] = (GC_word)old;
01112         old = new;
01113        collectable_count++;
01114         new = (GC_word *) GC_malloc_explicitly_typed(33 * sizeof(GC_word), d3);
01115         new[0] = 17;
01116         new[1] = (GC_word)old;
01117         old = new;
01118        collectable_count++;
01119         new = (GC_word *) GC_calloc_explicitly_typed(4, 2 * sizeof(GC_word),
01120                                                d1);
01121         new[0] = 17;
01122         new[1] = (GC_word)old;
01123         old = new;
01124        collectable_count++;
01125         if (i & 0xff) {
01126           new = (GC_word *) GC_calloc_explicitly_typed(7, 3 * sizeof(GC_word),
01127                                                d2);
01128         } else {
01129           new = (GC_word *) GC_calloc_explicitly_typed(1001,
01130                                                  3 * sizeof(GC_word),
01131                                                  d2);
01132           if (0 != new[0] || 0 != new[1]) {
01133            GC_printf0("Bad initialization by GC_malloc_explicitly_typed\n");
01134            FAIL;
01135          }
01136         }
01137         new[0] = 17;
01138         new[1] = (GC_word)old;
01139         old = new;
01140     }
01141     for (i = 0; i < 20000; i++) {
01142         if (new[0] != 17) {
01143             (void)GC_printf1("typed alloc failed at %lu\n",
01144                           (unsigned long)i);
01145             FAIL;
01146         }
01147         new[0] = 0;
01148         old = new;
01149         new = (GC_word *)(old[1]);
01150     }
01151     GC_gcollect();
01152     GC_noop(x);
01153 }
01154 
01155 int fail_count = 0;
01156 
01157 #ifndef __STDC__
01158 /*ARGSUSED*/
01159 void fail_proc1(x)
01160 GC_PTR x;
01161 {
01162     fail_count++;
01163 }
01164 
01165 #else
01166 
01167 /*ARGSUSED*/
01168 void fail_proc1(GC_PTR x)
01169 {
01170     fail_count++;
01171 }   
01172 
01173 static void uniq(void *p, ...) {
01174   va_list a;
01175   void *q[100];
01176   int n = 0, i, j;
01177   q[n++] = p;
01178   va_start(a,p);
01179   for (;(q[n] = va_arg(a,void *));n++) ;
01180   va_end(a);
01181   for (i=0; i<n; i++)
01182     for (j=0; j<i; j++)
01183       if (q[i] == q[j]) {
01184         GC_printf0(
01185               "Apparently failed to mark form some function arguments.\n"
01186               "Perhaps GC_push_regs was configured incorrectly?\n"
01187         );
01188        FAIL;
01189       }
01190 }
01191 
01192 #endif /* __STDC__ */
01193 
01194 #ifdef THREADS
01195 #   define TEST_FAIL_COUNT(n) 1
01196 #else 
01197 #   define TEST_FAIL_COUNT(n) (fail_count >= (n))
01198 #endif
01199 
01200 void run_one_test()
01201 {
01202     char *x;
01203 #   ifdef LINT
01204        char *y = 0;
01205 #   else
01206        char *y = (char *)(size_t)fail_proc1;
01207 #   endif
01208     DCL_LOCK_STATE;
01209     
01210 #   ifdef FIND_LEAK
01211        (void)GC_printf0(
01212               "This test program is not designed for leak detection mode\n");
01213        (void)GC_printf0("Expect lots of problems.\n");
01214 #   endif
01215     GC_FREE(0);
01216 #   ifndef DBG_HDRS_ALL
01217       collectable_count += 3;
01218       if (GC_size(GC_malloc(7)) != 8 &&
01219          GC_size(GC_malloc(7)) != MIN_WORDS * sizeof(GC_word)
01220        || GC_size(GC_malloc(15)) != 16) {
01221            (void)GC_printf0("GC_size produced unexpected results\n");
01222            FAIL;
01223       }
01224       collectable_count += 1;
01225       if (GC_size(GC_malloc(0)) != MIN_WORDS * sizeof(GC_word)) {
01226        (void)GC_printf1("GC_malloc(0) failed: GC_size returns %ld\n",
01227                       GC_size(GC_malloc(0)));
01228            FAIL;
01229       }
01230       collectable_count += 1;
01231       if (GC_size(GC_malloc_uncollectable(0)) != MIN_WORDS * sizeof(GC_word)) {
01232        (void)GC_printf0("GC_malloc_uncollectable(0) failed\n");
01233            FAIL;
01234       }
01235       GC_is_valid_displacement_print_proc = fail_proc1;
01236       GC_is_visible_print_proc = fail_proc1;
01237       collectable_count += 1;
01238       x = GC_malloc(16);
01239       if (GC_base(x + 13) != x) {
01240        (void)GC_printf0("GC_base(heap ptr) produced incorrect result\n");
01241        FAIL;
01242       }
01243 #     ifndef PCR
01244         if (GC_base(y) != 0) {
01245          (void)GC_printf0("GC_base(fn_ptr) produced incorrect result\n");
01246          FAIL;
01247         }
01248 #     endif
01249       if (GC_same_obj(x+5, x) != x + 5) {
01250        (void)GC_printf0("GC_same_obj produced incorrect result\n");
01251        FAIL;
01252       }
01253       if (GC_is_visible(y) != y || GC_is_visible(x) != x) {
01254        (void)GC_printf0("GC_is_visible produced incorrect result\n");
01255        FAIL;
01256       }
01257       if (!TEST_FAIL_COUNT(1)) {
01258 #      if!(defined(RS6000) || defined(POWERPC) || defined(IA64)) || defined(M68K)
01259          /* ON RS6000s function pointers point to a descriptor in the */
01260          /* data segment, so there should have been no failures.      */
01261          /* The same applies to IA64.  Something similar seems to     */
01262          /* be going on with NetBSD/M68K.                      */
01263          (void)GC_printf0("GC_is_visible produced wrong failure indication\n");
01264          FAIL;
01265 #      endif
01266       }
01267       if (GC_is_valid_displacement(y) != y
01268         || GC_is_valid_displacement(x) != x
01269         || GC_is_valid_displacement(x + 3) != x + 3) {
01270        (void)GC_printf0(
01271               "GC_is_valid_displacement produced incorrect result\n");
01272        FAIL;
01273       }
01274 #     if defined(__STDC__) && !defined(MSWIN32) && !defined(MSWINCE)
01275         /* Harder to test under Windows without a gc.h declaration.  */
01276         {
01277          size_t i;
01278          extern void *GC_memalign();
01279 
01280          GC_malloc(17);
01281          for (i = sizeof(GC_word); i < 512; i *= 2) {
01282            GC_word result = (GC_word) GC_memalign(i, 17);
01283            if (result % i != 0 || result == 0 || *(int *)result != 0) FAIL;
01284          } 
01285        }
01286 #     endif
01287 #     ifndef ALL_INTERIOR_POINTERS
01288 #      if defined(RS6000) || defined(POWERPC)
01289         if (!TEST_FAIL_COUNT(1)) {
01290 #      else
01291         if (GC_all_interior_pointers && !TEST_FAIL_COUNT(1)
01292            || !GC_all_interior_pointers && !TEST_FAIL_COUNT(2)) {
01293 #      endif
01294          (void)GC_printf0("GC_is_valid_displacement produced wrong failure indication\n");
01295          FAIL;
01296         }
01297 #     endif
01298 #   endif /* DBG_HDRS_ALL */
01299     /* Test floating point alignment */
01300    collectable_count += 2;
01301        *(double *)GC_MALLOC(sizeof(double)) = 1.0;
01302        *(double *)GC_MALLOC(sizeof(double)) = 1.0;
01303 #   ifdef GC_GCJ_SUPPORT
01304       GC_REGISTER_DISPLACEMENT(sizeof(struct fake_vtable *));
01305       GC_init_gcj_malloc(0, (void *)fake_gcj_mark_proc);
01306 #   endif
01307     /* Make sure that fn arguments are visible to the collector.      */
01308 #   ifdef __STDC__
01309       uniq(
01310         GC_malloc(12), GC_malloc(12), GC_malloc(12),
01311         (GC_gcollect(),GC_malloc(12)),
01312         GC_malloc(12), GC_malloc(12), GC_malloc(12),
01313        (GC_gcollect(),GC_malloc(12)),
01314         GC_malloc(12), GC_malloc(12), GC_malloc(12),
01315        (GC_gcollect(),GC_malloc(12)),
01316         GC_malloc(12), GC_malloc(12), GC_malloc(12),
01317        (GC_gcollect(),GC_malloc(12)),
01318         GC_malloc(12), GC_malloc(12), GC_malloc(12),
01319        (GC_gcollect(),GC_malloc(12)),
01320         (void *)0);
01321 #   endif
01322     /* Repeated list reversal test. */
01323        reverse_test();
01324 #   ifdef PRINTSTATS
01325        GC_printf0("-------------Finished reverse_test\n");
01326 #   endif
01327 #   ifndef DBG_HDRS_ALL
01328       typed_test();
01329 #     ifdef PRINTSTATS
01330        GC_printf0("-------------Finished typed_test\n");
01331 #     endif
01332 #   endif /* DBG_HDRS_ALL */
01333     tree_test();
01334     LOCK();
01335     n_tests++;
01336     UNLOCK();
01337 #   if defined(THREADS) && defined(HANDLE_FORK)
01338       if (fork() == 0) {
01339        GC_gcollect();
01340        tiny_reverse_test(0);
01341        GC_gcollect();
01342        GC_printf0("Finished a child process\n");
01343        exit(0);
01344       }
01345 #   endif
01346     /* GC_printf1("Finished %x\n", pthread_self()); */
01347 }
01348 
01349 void check_heap_stats()
01350 {
01351     unsigned long max_heap_sz;
01352     register int i;
01353     int still_live;
01354     int late_finalize_count = 0;
01355     
01356 #   ifdef VERY_SMALL_CONFIG
01357     /* these are something of a guess */
01358     if (sizeof(char *) > 4) {
01359         max_heap_sz = 4500000;
01360     } else {
01361        max_heap_sz = 2800000;
01362     }
01363 #   else
01364     if (sizeof(char *) > 4) {
01365         max_heap_sz = 19000000;
01366     } else {
01367        max_heap_sz = 11000000;
01368     }
01369 #   endif
01370 #   ifndef ALIGN_DOUBLE
01371         /* We end up needing more small object pages. */
01372         max_heap_sz += 2000000;
01373 #   endif
01374 #   ifdef GC_DEBUG
01375        max_heap_sz *= 2;
01376 #       ifdef SAVE_CALL_CHAIN
01377            max_heap_sz *= 3;
01378 #           ifdef SAVE_CALL_COUNT
01379               max_heap_sz += max_heap_sz * SAVE_CALL_COUNT/4;
01380 #          endif
01381 #       endif
01382 #   endif
01383     /* Garbage collect repeatedly so that all inaccessible objects    */
01384     /* can be finalized.                                       */
01385       while (GC_collect_a_little()) { }
01386       for (i = 0; i < 16; i++) {
01387         GC_gcollect();
01388         late_finalize_count += GC_invoke_finalizers();
01389       }
01390     (void)GC_printf1("Completed %lu tests\n", (unsigned long)n_tests);
01391     (void)GC_printf1("Allocated %lu collectable objects\n", (unsigned long)collectable_count);
01392     (void)GC_printf1("Allocated %lu uncollectable objects\n", (unsigned long)uncollectable_count);
01393     (void)GC_printf1("Allocated %lu atomic objects\n", (unsigned long)atomic_count);
01394     (void)GC_printf1("Allocated %lu stubborn objects\n", (unsigned long)stubborn_count);
01395     (void)GC_printf2("Finalized %lu/%lu objects - ",
01396                    (unsigned long)finalized_count,
01397                    (unsigned long)finalizable_count);
01398 #   ifdef FINALIZE_ON_DEMAND
01399        if (finalized_count != late_finalize_count) {
01400             (void)GC_printf0("Demand finalization error\n");
01401            FAIL;
01402        }
01403 #   endif
01404     if (finalized_count > finalizable_count
01405         || finalized_count < finalizable_count/2) {
01406         (void)GC_printf0("finalization is probably broken\n");
01407         FAIL;
01408     } else {
01409         (void)GC_printf0("finalization is probably ok\n");
01410     }
01411     still_live = 0;
01412     for (i = 0; i < MAX_FINALIZED; i++) {
01413        if (live_indicators[i] != 0) {
01414            still_live++;
01415        }
01416     }
01417     i = finalizable_count - finalized_count - still_live;
01418     if (0 != i) {
01419         (void)GC_printf2
01420             ("%lu disappearing links remain and %ld more objects were not finalized\n",
01421              (unsigned long) still_live, (long)i);
01422         if (i > 10) {
01423            GC_printf0("\tVery suspicious!\n");
01424        } else {
01425            GC_printf0("\tSlightly suspicious, but probably OK.\n");
01426        }
01427     }
01428     (void)GC_printf1("Total number of bytes allocated is %lu\n",
01429               (unsigned long)
01430                   WORDS_TO_BYTES(GC_words_allocd + GC_words_allocd_before_gc));
01431     (void)GC_printf1("Final heap size is %lu bytes\n",
01432                    (unsigned long)GC_get_heap_size());
01433     if (WORDS_TO_BYTES(GC_words_allocd + GC_words_allocd_before_gc)
01434 #   ifdef VERY_SMALL_CONFIG
01435         < 2700000*n_tests) {
01436 #   else
01437         < 33500000*n_tests) {
01438 #   endif
01439         (void)GC_printf0("Incorrect execution - missed some allocations\n");
01440         FAIL;
01441     }
01442     if (GC_get_heap_size() > max_heap_sz*n_tests) {
01443         (void)GC_printf0("Unexpected heap growth - collector may be broken\n");
01444         FAIL;
01445     }
01446     (void)GC_printf0("Collector appears to work\n");
01447 }
01448 
01449 #if defined(MACOS)
01450 void SetMinimumStack(long minSize)
01451 {
01452        long newApplLimit;
01453 
01454        if (minSize > LMGetDefltStack())
01455        {
01456               newApplLimit = (long) GetApplLimit()
01457                             - (minSize - LMGetDefltStack());
01458               SetApplLimit((Ptr) newApplLimit);
01459               MaxApplZone();
01460        }
01461 }
01462 
01463 #define cMinStackSpace (512L * 1024L)
01464 
01465 #endif
01466 
01467 #ifdef __STDC__
01468     void warn_proc(char *msg, GC_word p)
01469 #else
01470     void warn_proc(msg, p)
01471     char *msg;
01472     GC_word p;
01473 #endif
01474 {
01475     GC_printf1(msg, (unsigned long)p);
01476     /*FAIL;*/
01477 }
01478 
01479 
01480 #if !defined(PCR) && !defined(GC_SOLARIS_THREADS) \
01481     && !defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS) \
01482     || defined(LINT)
01483 #if defined(MSWIN32) && !defined(__MINGW32__)
01484   int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, LPTSTR cmd, int n)
01485 #else
01486   int main()
01487 #endif
01488 {
01489 #   if defined(DJGPP)
01490        int dummy;
01491 #   endif
01492     n_tests = 0;
01493     
01494 #   if defined(DJGPP)
01495        /* No good way to determine stack base from library; do it */
01496        /* manually on this platform.                              */
01497        GC_stackbottom = (GC_PTR)(&dummy);
01498 #   endif
01499 #   if defined(MACOS)
01500        /* Make sure we have lots and lots of stack space.      */
01501        SetMinimumStack(cMinStackSpace);
01502        /* Cheat and let stdio initialize toolbox for us.       */
01503        printf("Testing GC Macintosh port.\n");
01504 #   endif
01505     GC_INIT();       /* Only needed on a few platforms. */
01506     (void) GC_set_warn_proc(warn_proc);
01507 #   if (defined(MPROTECT_VDB) || defined(PROC_VDB)) \
01508           && !defined(MAKE_BACK_GRAPH)
01509       GC_enable_incremental();
01510       (void) GC_printf0("Switched to incremental mode\n");
01511 #     if defined(MPROTECT_VDB)
01512        (void)GC_printf0("Emulating dirty bits with mprotect/signals\n");
01513 #     else
01514 #       ifdef PROC_VDB
01515        (void)GC_printf0("Reading dirty bits from /proc\n");
01516 #       else
01517     (void)GC_printf0("Using DEFAULT_VDB dirty bit implementation\n");
01518 #       endif
01519 #      endif
01520 #   endif
01521     run_one_test();
01522     check_heap_stats();
01523 #   ifndef MSWINCE
01524     (void)fflush(stdout);
01525 #   endif
01526 #   ifdef LINT
01527        /* Entry points we should be testing, but aren't.                 */
01528        /* Some can be tested by defining GC_DEBUG at the top of this file */
01529        /* This is a bit SunOS4 specific.                          */                
01530        GC_noop(GC_expand_hp, GC_add_roots, GC_clear_roots,
01531                GC_register_disappearing_link,
01532                GC_register_finalizer_ignore_self,
01533               GC_debug_register_displacement,
01534                GC_print_obj, GC_debug_change_stubborn,
01535                GC_debug_end_stubborn_change, GC_debug_malloc_uncollectable,
01536                GC_debug_free, GC_debug_realloc, GC_generic_malloc_words_small,
01537                GC_init, GC_make_closure, GC_debug_invoke_finalizer,
01538                GC_page_was_ever_dirty, GC_is_fresh,
01539               GC_malloc_ignore_off_page, GC_malloc_atomic_ignore_off_page,
01540               GC_set_max_heap_size, GC_get_bytes_since_gc,
01541               GC_get_total_bytes, GC_pre_incr, GC_post_incr);
01542 #   endif
01543 #   ifdef MSWIN32
01544       GC_win32_free_heap();
01545 #   endif
01546     return(0);
01547 }
01548 # endif
01549 
01550 #if defined(GC_WIN32_THREADS) && !defined(CYGWIN32)
01551 
01552 DWORD __stdcall thr_run_one_test(void *arg)
01553 {
01554   run_one_test();
01555   return 0;
01556 }
01557 
01558 #ifdef MSWINCE
01559 HANDLE win_created_h;
01560 HWND win_handle;
01561 
01562 LRESULT CALLBACK window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
01563 {
01564   LRESULT ret = 0;
01565   switch (uMsg) {
01566     case WM_HIBERNATE:
01567       GC_printf0("Received WM_HIBERNATE, calling GC_gcollect\n");
01568       GC_gcollect();
01569       break;
01570     case WM_CLOSE:
01571       GC_printf0("Received WM_CLOSE, closing window\n");
01572       DestroyWindow(hwnd);
01573       break;
01574     case WM_DESTROY:
01575       PostQuitMessage(0);
01576       break;
01577     default:
01578       ret = DefWindowProc(hwnd, uMsg, wParam, lParam);
01579       break;
01580   }
01581   return ret;
01582 }
01583 
01584 DWORD __stdcall thr_window(void *arg)
01585 {
01586   WNDCLASS win_class = {
01587     CS_NOCLOSE,
01588     window_proc,
01589     0,
01590     0,
01591     GetModuleHandle(NULL),
01592     NULL,
01593     NULL,
01594     (HBRUSH)(COLOR_APPWORKSPACE+1),
01595     NULL,
01596     L"GCtestWindow"
01597   };
01598   MSG msg;
01599 
01600   if (!RegisterClass(&win_class))
01601     FAIL;
01602 
01603   win_handle = CreateWindowEx(
01604     0,
01605     L"GCtestWindow",
01606     L"GCtest",
01607     0,
01608     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
01609     NULL,
01610     NULL,
01611     GetModuleHandle(NULL),
01612     NULL);
01613 
01614   if (win_handle == NULL)
01615     FAIL;
01616 
01617   SetEvent(win_created_h);
01618 
01619   ShowWindow(win_handle, SW_SHOW);
01620   UpdateWindow(win_handle);
01621 
01622   while (GetMessage(&msg, NULL, 0, 0)) {
01623     TranslateMessage(&msg);
01624     DispatchMessage(&msg);
01625   }
01626 
01627   return 0;
01628 }
01629 #endif
01630 
01631 #define NTEST 2
01632 
01633 # ifdef MSWINCE
01634 int APIENTRY GC_WinMain(HINSTANCE instance, HINSTANCE prev, LPWSTR cmd, int n)
01635 #   else
01636 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, LPSTR cmd, int n)
01637 # endif
01638 {
01639 # if NTEST > 0
01640    HANDLE h[NTEST];
01641    int i;
01642 # endif
01643 # ifdef MSWINCE
01644     HANDLE win_thr_h;
01645 # endif
01646   DWORD thread_id;
01647 # if 0
01648     GC_enable_incremental();
01649 # endif
01650   GC_init();
01651   InitializeCriticalSection(&incr_cs);
01652   (void) GC_set_warn_proc(warn_proc);
01653 # ifdef MSWINCE
01654     win_created_h = CreateEvent(NULL, FALSE, FALSE, NULL);
01655     if (win_created_h == (HANDLE)NULL) {
01656       (void)GC_printf1("Event creation failed %lu\n", (unsigned long)GetLastError());
01657       FAIL;
01658     }
01659     win_thr_h = GC_CreateThread(NULL, 0, thr_window, 0, 0, &thread_id);
01660     if (win_thr_h == (HANDLE)NULL) {
01661       (void)GC_printf1("Thread creation failed %lu\n", (unsigned long)GetLastError());
01662       FAIL;
01663     }
01664     if (WaitForSingleObject(win_created_h, INFINITE) != WAIT_OBJECT_0)
01665       FAIL;
01666     CloseHandle(win_created_h);
01667 # endif
01668 # if NTEST > 0
01669    for (i = 0; i < NTEST; i++) {
01670     h[i] = GC_CreateThread(NULL, 0, thr_run_one_test, 0, 0, &thread_id);
01671     if (h[i] == (HANDLE)NULL) {
01672       (void)GC_printf1("Thread creation failed %lu\n", (unsigned long)GetLastError());
01673       FAIL;
01674     }
01675    }
01676 # endif /* NTEST > 0 */
01677   run_one_test();
01678 # if NTEST > 0
01679    for (i = 0; i < NTEST; i++) {
01680     if (WaitForSingleObject(h[i], INFINITE) != WAIT_OBJECT_0) {
01681       (void)GC_printf1("Thread wait failed %lu\n", (unsigned long)GetLastError());
01682       FAIL;
01683     }
01684    }
01685 # endif /* NTEST > 0 */
01686 # ifdef MSWINCE
01687     PostMessage(win_handle, WM_CLOSE, 0, 0);
01688     if (WaitForSingleObject(win_thr_h, INFINITE) != WAIT_OBJECT_0)
01689       FAIL;
01690 # endif
01691   check_heap_stats();
01692   return(0);
01693 }
01694 
01695 #endif /* GC_WIN32_THREADS */
01696 
01697 
01698 #ifdef PCR
01699 test()
01700 {
01701     PCR_Th_T * th1;
01702     PCR_Th_T * th2;
01703     int code;
01704 
01705     n_tests = 0;
01706     /* GC_enable_incremental(); */
01707     (void) GC_set_warn_proc(warn_proc);
01708     th1 = PCR_Th_Fork(run_one_test, 0);
01709     th2 = PCR_Th_Fork(run_one_test, 0);
01710     run_one_test();
01711     if (PCR_Th_T_Join(th1, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
01712         != PCR_ERes_okay || code != 0) {
01713         (void)GC_printf0("Thread 1 failed\n");
01714     }
01715     if (PCR_Th_T_Join(th2, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
01716         != PCR_ERes_okay || code != 0) {
01717         (void)GC_printf0("Thread 2 failed\n");
01718     }
01719     check_heap_stats();
01720     return(0);
01721 }
01722 #endif
01723 
01724 #if defined(GC_SOLARIS_THREADS) || defined(GC_PTHREADS)
01725 void * thr_run_one_test(void * arg)
01726 {
01727     run_one_test();
01728     return(0);
01729 }
01730 
01731 #ifdef GC_DEBUG
01732 #  define GC_free GC_debug_free
01733 #endif
01734 
01735 #if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
01736 main()
01737 {
01738     thread_t th1;
01739     thread_t th2;
01740     int code;
01741 
01742     n_tests = 0;
01743     GC_INIT();       /* Only needed if gc is dynamic library.  */
01744 #   ifndef MAKE_BACK_GRAPH
01745       GC_enable_incremental();
01746 #   endif
01747     (void) GC_set_warn_proc(warn_proc);
01748     if (thr_keycreate(&fl_key, GC_free) != 0) {
01749         (void)GC_printf1("Key creation failed %lu\n", (unsigned long)code);
01750        FAIL;
01751     }
01752     if ((code = thr_create(0, 1024*1024, thr_run_one_test, 0, 0, &th1)) != 0) {
01753        (void)GC_printf1("Thread 1 creation failed %lu\n", (unsigned long)code);
01754        FAIL;
01755     }
01756     if ((code = thr_create(0, 1024*1024, thr_run_one_test, 0, THR_NEW_LWP, &th2)) != 0) {
01757        (void)GC_printf1("Thread 2 creation failed %lu\n", (unsigned long)code);
01758        FAIL;
01759     }
01760     run_one_test();
01761     if ((code = thr_join(th1, 0, 0)) != 0) {
01762         (void)GC_printf1("Thread 1 failed %lu\n", (unsigned long)code);
01763         FAIL;
01764     }
01765     if (thr_join(th2, 0, 0) != 0) {
01766         (void)GC_printf1("Thread 2 failed %lu\n", (unsigned long)code);
01767         FAIL;
01768     }
01769     check_heap_stats();
01770     (void)fflush(stdout);
01771     return(0);
01772 }
01773 #else /* pthreads */
01774 
01775 #ifndef GC_PTHREADS
01776   --> bad news
01777 #endif
01778 
01779 main()
01780 {
01781     pthread_t th1;
01782     pthread_t th2;
01783     pthread_attr_t attr;
01784     int code;
01785 
01786 #   ifdef GC_IRIX_THREADS
01787        /* Force a larger stack to be preallocated      */
01788        /* Since the initial cant always grow later.     */
01789        *((volatile char *)&code - 1024*1024) = 0;      /* Require 1 Mb */
01790 #   endif /* GC_IRIX_THREADS */
01791 #   if defined(GC_HPUX_THREADS)
01792        /* Default stack size is too small, especially with the 64 bit ABI */
01793        /* Increase it.                                                   */
01794        if (pthread_default_stacksize_np(1024*1024, 0) != 0) {
01795           (void)GC_printf0("pthread_default_stacksize_np failed.\n");
01796        }
01797 #   endif     /* GC_HPUX_THREADS */
01798     GC_INIT();
01799 
01800     pthread_attr_init(&attr);
01801 #   if defined(GC_IRIX_THREADS) || defined(GC_FREEBSD_THREADS) \
01802        || defined(GC_DARWIN_THREADS) || defined(GC_AIX_THREADS)
01803        pthread_attr_setstacksize(&attr, 1000000);
01804 #   endif
01805     n_tests = 0;
01806 #   if (defined(MPROTECT_VDB)) \
01807             && !defined(PARALLEL_MARK) &&!defined(REDIRECT_MALLOC) \
01808             && !defined(MAKE_BACK_GRAPH)
01809        GC_enable_incremental();
01810         (void) GC_printf0("Switched to incremental mode\n");
01811 #     if defined(MPROTECT_VDB)
01812         (void)GC_printf0("Emulating dirty bits with mprotect/signals\n");
01813 #     else
01814 #       ifdef PROC_VDB
01815             (void)GC_printf0("Reading dirty bits from /proc\n");
01816 #       else
01817             (void)GC_printf0("Using DEFAULT_VDB dirty bit implementation\n");
01818 #       endif
01819 #     endif
01820 #   endif
01821     (void) GC_set_warn_proc(warn_proc);
01822     if ((code = pthread_key_create(&fl_key, 0)) != 0) {
01823         (void)GC_printf1("Key creation failed %lu\n", (unsigned long)code);
01824        FAIL;
01825     }
01826     if ((code = pthread_create(&th1, &attr, thr_run_one_test, 0)) != 0) {
01827        (void)GC_printf1("Thread 1 creation failed %lu\n", (unsigned long)code);
01828        FAIL;
01829     }
01830     if ((code = pthread_create(&th2, &attr, thr_run_one_test, 0)) != 0) {
01831        (void)GC_printf1("Thread 2 creation failed %lu\n", (unsigned long)code);
01832        FAIL;
01833     }
01834     run_one_test();
01835     if ((code = pthread_join(th1, 0)) != 0) {
01836         (void)GC_printf1("Thread 1 failed %lu\n", (unsigned long)code);
01837         FAIL;
01838     }
01839     if (pthread_join(th2, 0) != 0) {
01840         (void)GC_printf1("Thread 2 failed %lu\n", (unsigned long)code);
01841         FAIL;
01842     }
01843     check_heap_stats();
01844     (void)fflush(stdout);
01845     pthread_attr_destroy(&attr);
01846     GC_printf1("Completed %d collections\n", GC_gc_no);
01847     return(0);
01848 }
01849 #endif /* GC_PTHREADS */
01850 #endif /* GC_SOLARIS_THREADS || GC_PTHREADS */