|
|
| version 1.1.2.9.2.13, 2003/03/25 10:07:00 | version 1.3, 2003/08/19 09:12:04 |
|---|---|
| Line 11 | Line 11 |
| static const char* IDENT_MEMORY_H="$Date$"; | static const char* IDENT_MEMORY_H="$Date$"; |
| #define PA_DEBUG_REFERENCES | |
| // include | // include |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| #include "gc.h" | #include "gc.h" |
| Line 25 static const char* IDENT_MEMORY_H="$Date | Line 24 static const char* IDENT_MEMORY_H="$Date |
| # include "libxml/tree.h" | # include "libxml/tree.h" |
| #endif | #endif |
| // defines | |
| //#define PA_DEBUG_GC_MEMORY | |
| #ifdef PA_DEBUG_GC_MEMORY | |
| void* pa_gc_malloc(size_t size); | |
| void* pa_gc_malloc_atomic(size_t size); | |
| void* pa_gc_realloc(void* ptr, size_t size); | |
| void pa_gc_free(void* ptr); | |
| # define PA_GC_MALLOC(size) pa_gc_malloc(size) | |
| # define PA_GC_MALLOC_ATOMIC(size) pa_gc_malloc_atomic(size) | |
| # define PA_GC_REALLOC(ptr,size) pa_gc_realloc(ptr,size) | |
| # define PA_GC_FREE(ptr) pa_gc_free(ptr) | |
| #else | |
| inline void* pa_gc_malloc(size_t size) { return GC_MALLOC(size); } | |
| inline void* pa_gc_malloc_atomic(size_t size) { return GC_MALLOC_ATOMIC(size); } | |
| inline void* pa_gc_realloc(void* ptr, size_t size) { return GC_REALLOC(ptr, size); } | |
| inline void pa_gc_free(void* ptr) { GC_FREE(ptr); } | |
| # define PA_GC_MALLOC(size) pa_gc_malloc(size) | |
| # define PA_GC_MALLOC_ATOMIC(size) pa_gc_malloc_atomic(size) | |
| # define PA_GC_REALLOC(ptr,size) pa_gc_realloc(ptr,size) | |
| # define PA_GC_FREE(ptr) pa_gc_free(ptr) | |
| #endif | |
| // forwards | // forwards |
| void *pa_fail_alloc(const char* what, size_t size); | void *pa_fail_alloc(const char* what, size_t size); |
| Line 32 void *pa_fail_alloc(const char* what, si | Line 58 void *pa_fail_alloc(const char* what, si |
| // inlines | // inlines |
| inline void *pa_malloc(size_t size) { | inline void *pa_malloc(size_t size) { |
| if(void *result=GC_MALLOC(size)) | if(void *result=PA_GC_MALLOC(size)) |
| return result; | return result; |
| return pa_fail_alloc("allocate", size); | return pa_fail_alloc("allocate", size); |
| } | } |
| inline void *pa_malloc_atomic(size_t size) { | inline void *pa_malloc_atomic(size_t size) { |
| if(void *result=GC_MALLOC_ATOMIC(size)) | if(void *result=PA_GC_MALLOC_ATOMIC(size)) |
| return result; | return result; |
| return pa_fail_alloc("allocate clean", size); | return pa_fail_alloc("allocate clean", size); |
| } | } |
| /// @a length may be null, which mean "autocalc it" | /// @a length may be null, which mean "autocalc it" |
| inline char *pa_strdup(const char* auto_variable_never_null, size_t length=0) { | inline char *pa_strdup(const char* auto_variable_never_null, size_t helper_length=0) { |
| if(!length) | size_t known_length=(helper_length?helper_length:strlen(auto_variable_never_null)); |
| length=strlen(auto_variable_never_null); | |
| size_t size=known_length+1; | |
| size_t size=length+1; | if(char *result=static_cast<char*>(PA_GC_MALLOC_ATOMIC(size))) { |
| if(char *result=static_cast<char*>(GC_MALLOC_ATOMIC(size))) { | memcpy(result, auto_variable_never_null, size); |
| memcpy(result, auto_variable_never_null, length); | result[known_length]=0; |
| result[length]=0; | |
| return result; | return result; |
| } | } |
| Line 60 inline char *pa_strdup(const char* auto_ | Line 85 inline char *pa_strdup(const char* auto_ |
| } | } |
| inline void pa_free(void *ptr) { | inline void pa_free(void *ptr) { |
| GC_FREE(ptr); | PA_GC_FREE(ptr); |
| } | } |
| inline void *pa_realloc(void *ptr, size_t size) { | inline void *pa_realloc(void *ptr, size_t size) { |
| if(void *result=GC_REALLOC(ptr, size)) | if(void *result=PA_GC_REALLOC(ptr, size)) |
| return result; | return result; |
| return pa_fail_alloc("reallocate to", size); | return pa_fail_alloc("reallocate to", size); |
| } | } |
| //{@ these operators are disabled, one should explicitely specify either new(UseGC) or new(PointerFreeGC) | //{@ 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 new(size_t) { abort(); } // disabled |
| inline void operator delete (void *ptr) { abort(); } // disabled | inline void *operator new[] (size_t) { abort(); } // disabled |
| inline void *operator new[] (size_t size) { abort(); } // disabled | |
| //}@ | //}@ |
| #define UseGC ((int)1) | #define UseGC ((int)1) |
| #define PointerFreeGC (true) | #define PointerFreeGC (true) |
| //{@ Array-oriented | |
| inline void *operator new[] (size_t size, int) { // UseGC | inline void *operator new[] (size_t size, int) { // UseGC |
| return pa_malloc(size); | return pa_malloc(size); |
| } | } |
| Line 88 inline void *operator new[] (size_t size | Line 113 inline void *operator new[] (size_t size |
| inline void operator delete[] (void *ptr) { | inline void operator delete[] (void *ptr) { |
| pa_free(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. | /// memory allocation/dallocation goes via pa_malloc/pa_free. |
| class PA_Allocated { | class PA_Allocated { |
| Line 105 public: | Line 143 public: |
| static void *malloc_atomic(size_t size) { | static void *malloc_atomic(size_t size) { |
| return pa_malloc_atomic(size); | return pa_malloc_atomic(size); |
| } | } |
| static char *strdup(const char* auto_variable_never_null, size_t length=0) { | static char *strdup(const char* auto_variable_never_null, size_t helper_length=0) { |
| return pa_strdup(auto_variable_never_null, length); | return pa_strdup(auto_variable_never_null, helper_length); |
| } | } |
| static void free(void *ptr) { | static void free(void *ptr) { |
| pa_free(ptr); | pa_free(ptr); |
| Line 124 private: // disabled from accidental use | Line 162 private: // disabled from accidental use |
| /// Those who want their destructor called during finalization, must derive from this class [also] | /// Those who want their destructor called during finalization, must derive from this class [also] |
| class PA_Cleaned { | class PA_Cleaned { |
| #ifndef PA_DEBUG_DISABLE_GC | |
| static void cleanup( void* obj, void* displ ) { | static void cleanup( void* obj, void* displ ) { |
| ((PA_Cleaned*) ((char*) obj + (ptrdiff_t) displ))->~PA_Cleaned(); | ((PA_Cleaned*) ((char*) obj + (ptrdiff_t) displ))->~PA_Cleaned(); |
| } | } |
| public: | public: |
| PA_Cleaned() { | PA_Cleaned() { |
| GC_finalization_proc oldProc; | GC_finalization_proc oldProc; |
| void* oldData; | void* oldData; |
| Line 149 public: | Line 186 public: |
| virtual ~PA_Cleaned() { | virtual ~PA_Cleaned() { |
| GC_REGISTER_FINALIZER_IGNORE_SELF( GC_base(this), 0, 0, 0, 0 ); | GC_REGISTER_FINALIZER_IGNORE_SELF( GC_base(this), 0, 0, 0, 0 ); |
| } | } |
| #endif | |
| }; | }; |
| /// Base for all Parser classes | /// Base for all Parser classes |