--- parser3/src/include/pa_memory.h 2003/02/21 10:58:57 1.1.2.2 +++ parser3/src/include/pa_memory.h 2020/12/15 17:10:31 1.41 @@ -1,7 +1,7 @@ /** @file Parser: memory reference counting classes decls. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ @@ -9,216 +9,147 @@ #ifndef PA_MEMORY_H #define PA_MEMORY_H -static const char* IDENT_MEMORY_H="$Date: 2003/02/21 10:58:57 $"; +#define IDENT_PA_MEMORY_H "$Id: pa_memory.h,v 1.41 2020/12/15 17:10:31 moko Exp $" // include #include "pa_config_includes.h" +#include -#ifdef XML -# include "gdome.h" -// for xmlChar -# include "libxml/tree.h" -#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 -void *pa_fail_alloc(char *what, size_t size); +void *pa_fail_alloc(const char* what, size_t size); // inlines inline void *pa_malloc(size_t size) { - if(void *result=malloc(size)) + if(void *result=GC_MALLOC(size)) return result; return pa_fail_alloc("allocate", size); } -inline void *pa_calloc(size_t size) { - if(void *result=calloc(1, size)) +inline void *pa_malloc_atomic(size_t 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(GC_MALLOC_ATOMIC(size))) { + memcpy(result, auto_variable_never_null, known_length); + result[known_length]=0; + return result; + } + + return static_cast(pa_fail_alloc("allocate clean", size)); +} + inline void pa_free(void *ptr) { - free(ptr); + GC_FREE(ptr); } inline void *pa_realloc(void *ptr, size_t size) { - if(void *result=realloc(ptr, size)) + if(void *result=GC_REALLOC(ptr, size)) return result; return pa_fail_alloc("reallocate to", size); } -// forwards - -class Exception; -class String; -class Charset; -class GdomeDOMString_auto_ptr; - -template class object_ptr { - T *ptr; +/// memory allocation/dallocation goes via pa_malloc/pa_free. +class PA_Allocated { public: - typedef T element_type; - - explicit object_ptr(T *ptr = 0) { - this->ptr=ptr; - if(ptr) - ptr->ref(); - } - template object_ptr(const object_ptr& src) { - this->ptr=src.get(); - if(ptr) - ptr->ref(); - } - object_ptr(const object_ptr& src) { - ptr=src.get(); - if(ptr) - ptr->ref(); - } - object_ptr(T *ptr, int) { - this->ptr=ptr; - } - object_ptr& operator=(const object_ptr& src) { - if(this!=&src) - if(ptr!=src.get()) { - if(ptr) - ptr->unref(); - ptr=src.get(); - if(ptr) - ptr->ref(); - } - return *this; - } - ~object_ptr() { - if(ptr) - ptr->unref(); - } - T& operator*() const { - return *get(); - } - T *operator->() const { - return get(); + /// the sole: instances allocated using our functions + static void *operator new(size_t size) { + return pa_malloc(size); } - T *get() const { - return ptr; + static void operator delete(void *ptr) { + pa_free(ptr); } - operator bool() const { - return get()!=0; + static void *operator new[](size_t size) { + return pa_malloc(size); } - bool operator !() const { - return get()==0; + static void operator delete[](void *ptr) { + pa_free(ptr); } }; -#define DECLARE_OBJECT_PTR(name) \ - typedef object_ptr name##Ptr +// new(PointerFreeGC)/new(PointerGC) should be used to allocate types not inherited from PA_Allocated -/// TEMPLATE CLASS smart_ptr, stolen from stl:auto_ptr & renamed field/params [not compiled on gcc] -template - class smart_ptr { - bool owns; - T *ptr; -public: - typedef T element_type; - explicit smart_ptr(T *aptr = 0) - : owns(aptr != 0), ptr(aptr) {} - smart_ptr(const smart_ptr& src) - : owns(src.owns), ptr(src.release()) {} - smart_ptr& operator=(const smart_ptr& src) - {if (this != &src) - {if (ptr != src.get()) - {if (owns) - delete ptr; - owns = src.owns; } - else if (src.owns) - owns = true; - ptr = src.release(); } - return (*this); } - ~smart_ptr() - {if (owns) - delete ptr; } - T& operator*() const - {return (*get()); } - T *get() const - {return ptr; } - T *release() const - {((smart_ptr *)this)->owns = false; - return (ptr); } - operator bool() const { - return get()!=0; - } - operator T*() const { - return ptr; - } - }; +#define PointerFreeGC (true) +#define PointerGC (false) -// defines +inline void *operator new[] (size_t size, bool pointer_free) { + return pointer_free ? pa_malloc_atomic(size) : pa_malloc(size); +} -#define override -#define rethrow throw +inline void *operator new (size_t size, bool pointer_free) { + return pointer_free ? pa_malloc_atomic(size) : pa_malloc(size); +} -// memory +/// Base for all Parser classes +typedef PA_Allocated PA_Object; -inline void *operator new[] (size_t size) { - return pa_malloc(size); -} -inline void operator delete[] (void *ptr) { - pa_free(ptr); -} -inline void operator delete (void *ptr) { - pa_free(ptr); -} +// defines -class PA_Allocated { -public: - /// the sole: instances allocated using our functions - 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) { - return pa_malloc(size); - } - static void *calloc(size_t size) { - return pa_calloc(size); - } - static void free(void *ptr) { - pa_free(ptr); - } - static void *realloc(void *ptr, size_t size) { - return pa_realloc(ptr, size); - } +#define override +#define rethrow throw -}; +#if defined(_MSC_VER) || (__cplusplus>=201103L) +#define PA_THROW(what) +#else +#define PA_THROW(what) throw(what) +#endif -/** - Base for all Parser classes, memory allocation/dallocation goes via pa_malloc/pa_free. -*/ -class PA_Object: public PA_Allocated { - mutable unsigned long freferences; -public: - PA_Object(): freferences(0) {} - virtual ~PA_Object() {} - - void ref() const { - freferences++; - } - void unref() const { - if(freferences) { - if(--freferences==0) - delete this; - } - } -}; -DECLARE_OBJECT_PTR(PA_Object); +#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 -// convinient types +#ifndef PA_DEBUG_DISABLE_GC +// other regular allocators as disabled from accidental use as well -typedef smart_ptr CharPtr; +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 + +#ifdef PA_DEBUG_DISABLE_GC +#define PA_GC_GCOLLECT +#else +#define PA_GC_GCOLLECT { GC_enable(); GC_gcollect(); GC_disable(); } +#endif #endif