--- parser3/src/include/pa_memory.h 2003/07/24 11:31:21 1.2 +++ parser3/src/include/pa_memory.h 2023/09/26 20:49:07 1.44 @@ -1,55 +1,26 @@ /** @file Parser: memory reference counting classes decls. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #ifndef PA_MEMORY_H #define PA_MEMORY_H -static const char* IDENT_MEMORY_H="$Date: 2003/07/24 11:31:21 $"; +#define IDENT_PA_MEMORY_H "$Id: pa_memory.h,v 1.44 2023/09/26 20:49:07 moko Exp $" // include #include "pa_config_includes.h" +#include -#include "gc.h" - - -#ifdef XML -# include "gdome.h" -// for xmlChar -# 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 +// define destructors use for Array, Hash and VMethodFrame +#define USE_DESTRUCTORS +// std::basic_stringstream used in ^table.csv-string[] is compatible with delete usage check only under Debian 9/10, FreeBSD 12 +// #define CHECK_DELETE_USAGE // forwards @@ -58,136 +29,75 @@ void *pa_fail_alloc(const char* what, si // inlines inline void *pa_malloc(size_t size) { - if(void *result=PA_GC_MALLOC(size)) + if(void *result=GC_MALLOC(size)) return result; return pa_fail_alloc("allocate", size); } inline void *pa_malloc_atomic(size_t size) { - if(void *result=PA_GC_MALLOC_ATOMIC(size)) + if(void *result=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 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); + +/// length may be zero, and this is normal +inline char *pa_strdup(const char* auto_variable_never_null, size_t known_length) { + if(char *result=static_cast(GC_MALLOC_ATOMIC(known_length+1))) { + memcpy(result, auto_variable_never_null, known_length); result[known_length]=0; return result; } - return static_cast(pa_fail_alloc("allocate clean", size)); + return static_cast(pa_fail_alloc("allocate clean", known_length+1)); +} + +inline char *pa_strdup(const char* auto_variable_never_null) { + return pa_strdup(auto_variable_never_null, strlen(auto_variable_never_null)); } inline void pa_free(void *ptr) { - PA_GC_FREE(ptr); + GC_FREE(ptr); } inline void *pa_realloc(void *ptr, size_t size) { - if(void *result=PA_GC_REALLOC(ptr, size)) + if(void *result=GC_REALLOC(ptr, size)) return result; return pa_fail_alloc("reallocate to", 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 new[] (size_t size) { abort(); } // disabled -//}@ - -#define UseGC ((int)1) -#define PointerFreeGC (true) - -//{@ Array-oriented -inline void *operator new[] (size_t size, int mode) { // 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); -} -//}@ - -//{@ 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 { public: /// the sole: instances allocated using our functions - static void *operator new(size_t size) { + static void *operator new(size_t size) { return pa_malloc(size); } static void operator delete(void *ptr) { pa_free(ptr); } - static void *malloc(size_t size) { + static void *operator new[](size_t size) { return pa_malloc(size); } - static void *malloc_atomic(size_t size) { - return pa_malloc_atomic(size); - } - 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) { + static void operator delete[](void *ptr) { pa_free(ptr); } - static void *realloc(void *ptr, size_t size) { - return pa_realloc(ptr, size); - } - -private: // disabled from accidental use - - /// use malloc/malloc_atomic instead [GC clears result of those] - static void *calloc(size_t size); - }; -/// Those who want their destructor called during finalization, must derive from this class [also] -class PA_Cleaned { -#ifndef PA_DEBUG_DISABLE_GC - static void cleanup( void* obj, void* displ ) { - ((PA_Cleaned*) ((char*) obj + (ptrdiff_t) displ))->~PA_Cleaned(); - } +// new(PointerFreeGC)/new(PointerGC) should be used to allocate types not inherited from PA_Allocated -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 ); - } - } - } +#define PointerFreeGC (true) +#define PointerGC (false) - virtual ~PA_Cleaned() { - GC_REGISTER_FINALIZER_IGNORE_SELF( GC_base(this), 0, 0, 0, 0 ); - } -#endif -}; +inline void *operator new[] (size_t size, bool pointer_free) { + return pointer_free ? pa_malloc_atomic(size) : pa_malloc(size); +} + +inline void *operator new (size_t size, bool pointer_free) { + return pointer_free ? pa_malloc_atomic(size) : pa_malloc(size); +} /// Base for all Parser classes typedef PA_Allocated PA_Object; @@ -197,5 +107,47 @@ typedef PA_Allocated PA_Object; #define override #define rethrow throw +#if defined(_MSC_VER) || (__cplusplus>=201103L) +#define PA_THROW(what) +#else +#define PA_THROW(what) throw(what) +#endif + +#if !defined(_MSC_VER) && !defined(FREEBSD1X) +// regular new/delete are disabled from accidental use +// no checks for FreeBSD1X.X due to https://bugs.llvm.org/show_bug.cgi?id=40161 bug + +void *new_disabled(); +void delete_disabled(); + +inline void *operator new[] (std::size_t) PA_THROW(std::bad_alloc){ return new_disabled(); } +inline void operator delete[](void *) throw(){ delete_disabled(); } +inline void operator delete[](void *, std::size_t) throw(){ delete_disabled(); } + +inline void *operator new(std::size_t) PA_THROW(std::bad_alloc){ return new_disabled(); } +#ifdef CHECK_DELETE_USAGE +inline void operator delete(void *) throw(){ delete_disabled(); } +#endif + +#ifndef PA_DEBUG_DISABLE_GC +// other regular allocators as disabled from accidental use as well + +void *calloc_disabled(); +void *malloc_disabled(); +void *realloc_disabled(); +void free_disabled(); +char *strdup_disabled(); + +inline void *calloc(size_t) { return calloc_disabled(); } +inline void *malloc(size_t) { return malloc_disabled(); } +inline void *realloc(void *, size_t) { return realloc_disabled(); } +inline void free(void *) { free_disabled(); } +inline char *strdup(const char*, size_t){ return strdup_disabled(); } +#endif // PA_DEBUG_DISABLE_GC + +#endif // _MSC_VER + +void pa_gc_collect(bool forced=false); +void pa_gc_set_free_space_divisor(int); #endif