Back to index

plt-scheme  4.2.1
wxGC.h
Go to the documentation of this file.
00001 #ifndef WXGC_CPP_H
00002 #define WXGC_CPP_H
00003 
00004 /****************************************************************************
00005 
00006 MrEd interface to various garbage collectors, including the Boehm
00007  collector, SenoraGC, and MzScheme's precise collector.
00008 
00009 Copyright (c) 2004-2009 PLT Scheme Inc.
00010 
00011 ****************************************************************************/
00012 
00013 /****************************************************************************
00014 Based On:
00015 
00016 C++ Interface to the Boehm Collector
00017 
00018     John R. Ellis and Jesse Hull 
00019     Last modified on Wed Jan  4 16:30:20 PST 1995 by ellis
00020 
00021 
00022 Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
00023  
00024 THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00025 OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00026  
00027 Permission is hereby granted to use or copy this program for any
00028 purpose, provided the above notices are retained on all copies.
00029 Permission to modify the code and to distribute modified code is
00030 granted, provided the above notices are retained, and a notice that
00031 the code was modified is included with the above copyright notice.
00032 ****************************************************************************/
00033 
00034 enum GCPlacement {UseGC, AtomicGC};
00035 
00036 #ifdef MZ_PRECISE_GC
00037 # define WXGC_ATOMIC /* empty */
00038 # define WXGC_PTRS /* empty */
00039 #else
00040 # define WXGC_ATOMIC (AtomicGC)
00041 # define WXGC_PTRS (UseGC)
00042 #endif
00043 
00044 typedef void (*GCCleanUpFunc)(void* obj, void* clientData);
00045 
00046 extern "C" {
00047   void objscheme_mark_external_invalid(void *);
00048 }
00049 void GC_cleanup(void *obj, void *ignored);
00050 
00051 #include "gc.h"
00052 
00053 /******************** Special kinds of GC *********************/
00054 
00055 #if SGC_STD_DEBUGGING
00056 # ifndef USE_SENORA_GC
00057 #  define USE_SENORA_GC
00058 # endif
00059 # define USE_MEMORY_TRACING 
00060 #endif
00061 
00062 #ifdef USE_SENORA_GC
00063 extern void *GC_cpp_malloc(size_t);
00064 #endif
00065 
00066 #define WX_REGISTER_GLOBAL_MEMORY
00067 
00068 #ifdef WX_REGISTER_GLOBAL_MEMORY
00069 extern "C" {
00070 # ifdef wx_msw
00071        __declspec(dllimport)
00072 # endif
00073        void scheme_register_static(void *p, long size);
00074 }
00075 # define wxREGGLOB(x) scheme_register_static((void *)&x, sizeof(x))
00076 #else
00077 # define wxREGGLOB(x) /* empty */
00078 #endif
00079 
00080 #ifdef MZ_PRECISE_GC
00081 extern void *GC_cpp_malloc(size_t);
00082 extern void GC_cpp_delete(class gc *);
00083 # define GC_register_finalizer_ignore_self(self, a, b, c, d) GC_set_finalizer(self, 0, 3, a, b, c, d)
00084 extern void *GC_weak_box_val(void *);
00085 #endif
00086 
00087 
00088 #define gcOBJ_TO_PTR(x) x
00089 #define gcPTR_TO_OBJ(x) x
00090 
00091 extern void *GC_malloc_accounting_shadow(long a);
00092 extern void GC_free_accounting_shadow(void *a);
00093 
00094 #ifndef START_XFORM_SKIP
00095 # ifndef MZ_PRECISE_GC
00096 #  define START_XFORM_SKIP 
00097 #  define END_XFORM_SKIP 
00098 #  define GC_CAN_IGNORE 
00099 #  define GC_MAYBE_IGNORE_INTERIOR 
00100 #  define XFORM_OK_PLUS +
00101 #  define XFORM_OK_MINUS -
00102 # else
00103 #  ifdef GC_INTERIORABLES_NEVER_MOVE
00104 #   define GC_MAYBE_IGNORE_INTERIOR GC_CAN_IGNORE
00105 #  else
00106 #   define GC_MAYBE_IGNORE_INTERIOR 
00107 #  endif
00108 # endif
00109 #endif
00110 
00111 /**** The `gc' and `gc_cleanup' class ************/
00112 
00113 class gc
00114 {
00115 public:
00116   inline virtual ~gc();
00117 
00118   inline void *operator new(size_t size);
00119   inline void *operator new(size_t size, GCPlacement gcp);
00120   inline void operator delete(void *obj);
00121 #ifdef _MSC_VER
00122   inline void operator delete(void *obj, GCPlacement gcp);
00123 #endif
00124 #ifdef OPERATOR_NEW_ARRAY
00125   inline void *operator new[](size_t size);
00126   inline void *operator new[](size_t size, GCPlacement gcp);
00127   inline void operator delete[](void *obj);
00128 # ifdef _MSC_VER
00129   inline void operator delete[](void *obj, GCPlacement gcp);
00130 # endif
00131 #endif
00132 
00133 #ifdef MZ_PRECISE_GC
00134   /* Overridden in each subclass: */
00135   virtual inline void gcMark();
00136   virtual inline void gcFixup();
00137 # ifdef COMPACT_BACKTRACE_GC
00138   virtual char *gcGetName();
00139 # endif
00140 #endif
00141 };
00142 
00143 class gc_cleanup : public gc
00144 {
00145 public:
00146   void *__gc_external;
00147 
00148   inline gc_cleanup();
00149   inline gc_cleanup(int cleanup);
00150   inline virtual ~gc_cleanup();
00151   void install_cleanup();  
00152 
00153 #ifdef MZ_PRECISE_GC
00154   inline void gcMark();
00155   inline void gcFixup();
00156 #endif
00157 };
00158 
00159 #ifdef MZ_PRECISE_GC
00160 inline void gc::gcMark()
00161 {
00162 }
00163 
00164 inline void gc::gcFixup()
00165 {
00166 }
00167 
00168 inline void gc_cleanup::gcMark()
00169 {
00170   gcMARK(__gc_external);
00171 }
00172 
00173 inline void gc_cleanup::gcFixup()
00174 {
00175   gcFIXUP(__gc_external);
00176 }
00177 #endif
00178 
00179 /***** Constructors and Destructors: ******/
00180 
00181 inline gc_cleanup::gc_cleanup(void)
00182 {
00183   __gc_external = NULL;
00184   install_cleanup();
00185 }
00186 
00187 inline gc_cleanup::gc_cleanup(int cleanup)
00188 {
00189   __gc_external = NULL;
00190   if (cleanup)
00191     install_cleanup();
00192 }
00193 
00194 inline gc_cleanup::~gc_cleanup(void)
00195 {
00196   if (__gc_external)
00197     objscheme_mark_external_invalid(__gc_external);
00198   GC_register_finalizer_ignore_self(gcOBJ_TO_PTR(this), 0, 0, 0, 0);
00199 }
00200 
00201 inline gc::~gc(void)
00202 {
00203 }
00204 
00205 /***** Allocators: ******/
00206 
00207 inline void *gc::operator new(size_t size)
00208 {
00209 #if defined(USE_SENORA_GC) || defined(MZ_PRECISE_GC)
00210   return GC_cpp_malloc(size);
00211 #else
00212   return GC_malloc(size);
00213 #endif
00214 }
00215 
00216 inline void *gc::operator new(size_t size, GCPlacement gcp)
00217 {
00218   if (gcp == AtomicGC) 
00219     return GC_malloc_atomic(size);
00220 
00221 #if defined(USE_SENORA_GC) || defined(MZ_PRECISE_GC)
00222   return GC_cpp_malloc(size);
00223 #else
00224   return GC_malloc(size);
00225 #endif
00226 }
00227 
00228 inline void gc::operator delete(void * /*obj*/) 
00229 {
00230 }
00231 
00232 #ifdef _MSC_VER
00233 inline void gc::operator delete(void *, GCPlacement gcp)
00234 {
00235 }
00236 #endif
00237 
00238 
00239 #ifdef OPERATOR_NEW_ARRAY
00240 inline void *gc::operator new[](size_t size) {
00241 #if defined(USE_SENORA_GC) || defined(MZ_PRECISE_GC)
00242   return ::operator new(size);
00243 #else
00244   return gc::operator new(size);
00245 #endif
00246 }
00247     
00248 inline void *gc::operator new[](size_t size, GCPlacement gcp) {
00249   return gc::operator new(size, gcp);
00250 }
00251 
00252 inline void gc::operator delete[](void *obj) {
00253   gc::operator delete(obj);
00254 }
00255 
00256 # ifdef _MSC_VER
00257 inline void gc::operator delete[](void *obj, GCPlacement gcp) {
00258   gc::operator delete(obj, gcp);
00259 }
00260 # endif
00261 #endif
00262 
00263 
00264 /*************** For objects not derived from `gc' ***********************/
00265 
00266 inline void *operator new(size_t size, GCPlacement gcp)
00267 {
00268   void *obj;
00269   
00270   if (gcp == AtomicGC)
00271     obj = GC_malloc_atomic(size);
00272   else
00273     obj = GC_malloc(size);
00274 
00275   return obj;
00276 }
00277         
00278 #ifdef OPERATOR_NEW_ARRAY
00279 inline void *operator new[](size_t size, GCPlacement gcp)
00280 {
00281   return ::operator new(size, gcp);
00282 }
00283 #endif
00284 
00285 #ifdef _MSC_VER
00286 inline void operator delete(void *, GCPlacement)
00287 {
00288 }
00289 
00290 inline void operator delete[](void *, GCPlacement)
00291 {
00292 }
00293 #endif
00294 
00295 #endif /* WXGC_CPP_H */