Back to index

plt-scheme  4.2.1
pcr_interface.c
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
00003  *
00004  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00005  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00006  *
00007  * Permission is hereby granted to use or copy this program
00008  * for any purpose,  provided the above notices are retained on all copies.
00009  * Permission to modify the code and to distribute modified code is granted,
00010  * provided the above notices are retained, and a notice that the code was
00011  * modified is included with the above copyright notice.
00012  */
00013 # include "private/gc_priv.h"
00014 
00015 # ifdef PCR
00016 /*
00017  * Note that POSIX PCR requires an ANSI C compiler.  Hence we are allowed
00018  * to make the same assumption here.
00019  * We wrap all of the allocator functions to avoid questions of
00020  * compatibility between the prototyped and nonprototyped versions of the f
00021  */
00022 # include "config/PCR_StdTypes.h"
00023 # include "mm/PCR_MM.h"
00024 # include <errno.h>
00025 
00026 # define MY_MAGIC 17L
00027 # define MY_DEBUGMAGIC 42L
00028 
00029 void * GC_AllocProc(size_t size, PCR_Bool ptrFree, PCR_Bool clear )
00030 {
00031     if (ptrFree) {
00032         void * result = (void *)GC_malloc_atomic(size);
00033         if (clear && result != 0) BZERO(result, size);
00034         return(result);
00035     } else {
00036         return((void *)GC_malloc(size));
00037     }
00038 }
00039 
00040 void * GC_DebugAllocProc(size_t size, PCR_Bool ptrFree, PCR_Bool clear )
00041 {
00042     if (ptrFree) {
00043         void * result = (void *)GC_debug_malloc_atomic(size, __FILE__,
00044                                                       __LINE__);
00045         if (clear && result != 0) BZERO(result, size);
00046         return(result);
00047     } else {
00048         return((void *)GC_debug_malloc(size, __FILE__, __LINE__));
00049     }
00050 }
00051 
00052 # define GC_ReallocProc GC_realloc
00053 void * GC_DebugReallocProc(void * old_object, size_t new_size_in_bytes)
00054 {
00055     return(GC_debug_realloc(old_object, new_size_in_bytes, __FILE__, __LINE__));
00056 }
00057 
00058 # define GC_FreeProc GC_free
00059 # define GC_DebugFreeProc GC_debug_free
00060 
00061 typedef struct {
00062   PCR_ERes (*ed_proc)(void *p, size_t size, PCR_Any data);
00063   GC_bool ed_pointerfree;
00064   PCR_ERes ed_fail_code;
00065   PCR_Any ed_client_data;
00066 } enumerate_data;
00067 
00068 void GC_enumerate_block(h, ed)
00069 register struct hblk *h;
00070 enumerate_data * ed;
00071 {
00072     register hdr * hhdr;
00073     register int sz;
00074     word *p;
00075     word * lim;
00076     
00077     hhdr = HDR(h);
00078     sz = hhdr -> hb_sz;
00079     if (sz >= 0 && ed -> ed_pointerfree
00080        || sz <= 0 && !(ed -> ed_pointerfree)) return;
00081     if (sz < 0) sz = -sz;
00082     lim = (word *)(h+1) - sz;
00083     p = (word *)h;
00084     do {
00085         if (PCR_ERes_IsErr(ed -> ed_fail_code)) return;
00086         ed -> ed_fail_code =
00087             (*(ed -> ed_proc))(p, WORDS_TO_BYTES(sz), ed -> ed_client_data);
00088         p+= sz;
00089     } while (p <= lim);
00090 }
00091 
00092 struct PCR_MM_ProcsRep * GC_old_allocator = 0;
00093 
00094 PCR_ERes GC_EnumerateProc(
00095     PCR_Bool ptrFree,
00096     PCR_ERes (*proc)(void *p, size_t size, PCR_Any data),
00097     PCR_Any data
00098 )
00099 {
00100     enumerate_data ed;
00101     
00102     ed.ed_proc = proc;
00103     ed.ed_pointerfree = ptrFree;
00104     ed.ed_fail_code = PCR_ERes_okay;
00105     ed.ed_client_data = data;
00106     GC_apply_to_all_blocks(GC_enumerate_block, &ed);
00107     if (ed.ed_fail_code != PCR_ERes_okay) {
00108         return(ed.ed_fail_code);
00109     } else {
00110        /* Also enumerate objects allocated by my predecessors */
00111        return((*(GC_old_allocator->mmp_enumerate))(ptrFree, proc, data));
00112     }
00113 }
00114 
00115 void GC_DummyFreeProc(void *p) {}
00116 
00117 void GC_DummyShutdownProc(void) {}
00118 
00119 struct PCR_MM_ProcsRep GC_Rep = {
00120        MY_MAGIC,
00121        GC_AllocProc,
00122        GC_ReallocProc,
00123        GC_DummyFreeProc,    /* mmp_free */
00124        GC_FreeProc,                /* mmp_unsafeFree */
00125        GC_EnumerateProc,
00126        GC_DummyShutdownProc /* mmp_shutdown */
00127 };
00128 
00129 struct PCR_MM_ProcsRep GC_DebugRep = {
00130        MY_DEBUGMAGIC,
00131        GC_DebugAllocProc,
00132        GC_DebugReallocProc,
00133        GC_DummyFreeProc,    /* mmp_free */
00134        GC_DebugFreeProc,           /* mmp_unsafeFree */
00135        GC_EnumerateProc,
00136        GC_DummyShutdownProc /* mmp_shutdown */
00137 };
00138 
00139 GC_bool GC_use_debug = 0;
00140 
00141 void GC_pcr_install()
00142 {
00143     PCR_MM_Install((GC_use_debug? &GC_DebugRep : &GC_Rep), &GC_old_allocator);
00144 }
00145 
00146 PCR_ERes
00147 PCR_GC_Setup(void)
00148 {
00149     return PCR_ERes_okay;
00150 }
00151 
00152 PCR_ERes
00153 PCR_GC_Run(void)
00154 {
00155 
00156     if( !PCR_Base_TestPCRArg("-nogc") ) {
00157         GC_quiet = ( PCR_Base_TestPCRArg("-gctrace") ? 0 : 1 );
00158         GC_use_debug = (GC_bool)PCR_Base_TestPCRArg("-debug_alloc");
00159         GC_init();
00160         if( !PCR_Base_TestPCRArg("-nogc_incremental") ) {
00161             /*
00162              * awful hack to test whether VD is implemented ...
00163              */
00164             if( PCR_VD_Start( 0, NIL, 0) != PCR_ERes_FromErr(ENOSYS) ) {
00165                GC_enable_incremental();
00166            }
00167        }
00168     }
00169     return PCR_ERes_okay;
00170 }
00171 
00172 void GC_push_thread_structures(void)
00173 {
00174     /* PCR doesn't work unless static roots are pushed.  Can't get here. */
00175     ABORT("In GC_push_thread_structures()");
00176 }
00177 
00178 # endif