--- parser3/src/include/pa_memory.h 2003/03/27 14:05:40 1.1.2.9.2.15 +++ parser3/src/include/pa_memory.h 2003/07/24 11:31:21 1.2 @@ -9,7 +9,7 @@ #ifndef PA_MEMORY_H #define PA_MEMORY_H -static const char* IDENT_MEMORY_H="$Date: 2003/03/27 14:05:40 $"; +static const char* IDENT_MEMORY_H="$Date: 2003/07/24 11:31:21 $"; // include @@ -24,6 +24,33 @@ static const char* IDENT_MEMORY_H="$Date # include "libxml/tree.h" #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 void *pa_fail_alloc(const char* what, size_t size); @@ -31,27 +58,26 @@ void *pa_fail_alloc(const char* what, si // inlines inline void *pa_malloc(size_t size) { - if(void *result=GC_MALLOC(size)) + if(void *result=PA_GC_MALLOC(size)) return result; return pa_fail_alloc("allocate", 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 pa_fail_alloc("allocate clean", size); } /// @a length may be null, which mean "autocalc it" -inline char *pa_strdup(const char* auto_variable_never_null, size_t length=0) { - if(!length) - length=strlen(auto_variable_never_null); - - size_t size=length+1; - if(char *result=static_cast(GC_MALLOC_ATOMIC(size))) { - memcpy(result, auto_variable_never_null, length); - result[length]=0; +inline char *pa_strdup(const char* auto_variable_never_null, size_t helper_length=0) { + size_t known_length=(helper_length?helper_length:strlen(auto_variable_never_null)); + + size_t size=known_length+1; + if(char *result=static_cast(PA_GC_MALLOC_ATOMIC(size))) { + memcpy(result, auto_variable_never_null, size); + result[known_length]=0; return result; } @@ -59,11 +85,11 @@ inline char *pa_strdup(const char* auto_ } inline void pa_free(void *ptr) { - GC_FREE(ptr); + PA_GC_FREE(ptr); } 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 pa_fail_alloc("reallocate to", size); @@ -117,8 +143,8 @@ public: static void *malloc_atomic(size_t size) { return pa_malloc_atomic(size); } - static char *strdup(const char* auto_variable_never_null, size_t length=0) { - return pa_strdup(auto_variable_never_null, length); + static char *strdup(const char* auto_variable_never_null, size_t helper_length=0) { + return pa_strdup(auto_variable_never_null, helper_length); } static void free(void *ptr) { pa_free(ptr);