[BACK]Return to test.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / gc / tests

Annotation of OpenXM_contrib2/asir2000/gc/tests/test.c, Revision 1.2

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>