--- parser3/src/include/pa_memory.h 2003/03/24 09:39:55 1.1.2.9.2.9 +++ parser3/src/include/pa_memory.h 2003/03/25 10:25:25 1.1.2.9.2.14 @@ -9,7 +9,7 @@ #ifndef PA_MEMORY_H #define PA_MEMORY_H -static const char* IDENT_MEMORY_H="$Date: 2003/03/24 09:39:55 $"; +static const char* IDENT_MEMORY_H="$Date: 2003/03/25 10:25:25 $"; #define PA_DEBUG_REFERENCES @@ -48,8 +48,6 @@ inline void *pa_malloc_atomic(size_t siz inline char *pa_strdup(const char* auto_variable_never_null, size_t length=0) { if(!length) length=strlen(auto_variable_never_null); - if(!length) - abort(); size_t size=length+1; if(char *result=static_cast(GC_MALLOC_ATOMIC(size))) { @@ -60,6 +58,7 @@ inline char *pa_strdup(const char* auto_ return static_cast(pa_fail_alloc("allocate clean", size)); } + inline void pa_free(void *ptr) { GC_FREE(ptr); } @@ -73,13 +72,13 @@ inline void *pa_realloc(void *ptr, size_ //{@ these operators are disabled, one should explicitely specify either new(UseGC) or new(PointerFreeGC) inline void *operator new(size_t size) { abort(); } // disabled -inline void operator delete (void *ptr) { abort(); } // disabled inline void *operator new[] (size_t size) { abort(); } // disabled //}@ #define UseGC ((int)1) #define PointerFreeGC (true) +//{@ Array-oriented inline void *operator new[] (size_t size, int) { // UseGC return pa_malloc(size); } @@ -89,6 +88,19 @@ inline void *operator new[] (size_t size inline void operator delete[] (void *ptr) { pa_free(ptr); } +//}@ + +//{@ Structure-oriented +inline void *operator new (size_t size, int) { // UseGC + return pa_malloc(size); +} +inline void *operator new (size_t size, bool) { // PointerFreeGC + return pa_malloc_atomic(size); +} +inline void operator delete(void *ptr) { + pa_free(ptr); +} +//}@ /// memory allocation/dallocation goes via pa_malloc/pa_free. class PA_Allocated { @@ -123,6 +135,36 @@ private: // disabled from accidental use }; +/// Those who want their destructor called during finalization, must derive from this class [also] +class PA_Cleaned { + + static void cleanup( void* obj, void* displ ) { + ((PA_Cleaned*) ((char*) obj + (ptrdiff_t) displ))->~PA_Cleaned(); + } + +public: + + PA_Cleaned() { + GC_finalization_proc oldProc; + void* oldData; + void* base = GC_base( (void *) this ); + if (0 != base) { + // Don't call the debug version, since this is a real base address. + GC_register_finalizer_ignore_self( + base, (GC_finalization_proc)cleanup, (void*) ((char*) this - (char*) base), + &oldProc, &oldData ); + if (0 != oldProc) { + GC_register_finalizer_ignore_self( base, oldProc, oldData, 0, 0 ); + } + } + } + + virtual ~PA_Cleaned() { + GC_REGISTER_FINALIZER_IGNORE_SELF( GC_base(this), 0, 0, 0, 0 ); + } + +}; + /// Base for all Parser classes typedef PA_Allocated PA_Object; @@ -131,4 +173,5 @@ typedef PA_Allocated PA_Object; #define override #define rethrow throw + #endif