--- parser3/src/include/pa_memory.h 2015/10/26 01:21:56 1.28 +++ parser3/src/include/pa_memory.h 2017/12/03 23:36:23 1.35 @@ -1,7 +1,7 @@ /** @file Parser: memory reference counting classes decls. - Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ @@ -9,21 +9,18 @@ #ifndef PA_MEMORY_H #define PA_MEMORY_H -#define IDENT_PA_MEMORY_H "$Id: pa_memory.h,v 1.28 2015/10/26 01:21:56 moko Exp $" +#define IDENT_PA_MEMORY_H "$Id: pa_memory.h,v 1.35 2017/12/03 23:36:23 moko Exp $" // include #include "pa_config_includes.h" -#include "gc.h" #include // define destructors use for Array, Hash and VMethodFrame #define USE_DESTRUCTORS -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); } +// std::basic_stringstream used in ^table.csv-string[] is compatible with delete usage check only under Debian 9 +// #define CHECK_DELETE_USAGE // forwards @@ -32,14 +29,14 @@ 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); @@ -50,7 +47,7 @@ inline char *pa_strdup(const char* auto_ 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))) { + if(char *result=static_cast(GC_MALLOC_ATOMIC(size))) { memcpy(result, auto_variable_never_null, known_length); result[known_length]=0; return result; @@ -60,50 +57,77 @@ inline char *pa_strdup(const char* auto_ } 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); } -#ifdef _MSC_VER -#define PA_THROW(what) -#else -#define PA_THROW(what) throw(what) -#endif +/// 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) { + return pa_malloc(size); + } + static void operator delete(void *ptr) { + pa_free(ptr); + } + static void *operator new[](size_t size) { + return pa_malloc(size); + } + static void operator delete[](void *ptr) { + pa_free(ptr); + } +}; + +// new(PointerFreeGC)/new(PointerGC) should be used to allocate types not inherited from PA_Allocated #define PointerFreeGC (true) +#define PointerGC (false) -//{@ Array-oriented -inline void *operator new[] (size_t size, bool) { // PointerFreeGC - return pa_malloc_atomic(size); +inline void *operator new[] (size_t size, bool pointer_free) { + return pointer_free ? pa_malloc_atomic(size) : pa_malloc(size); } -inline void *operator new[] (std::size_t size) PA_THROW(std::bad_alloc) { - return pa_malloc(size); -} -inline void operator delete[] (void *ptr) throw() { - pa_free(ptr); -} -//}@ -//{@ Structure-oriented -inline void *operator new (size_t size, bool) { // PointerFreeGC - return pa_malloc_atomic(size); -} -inline void *operator new(std::size_t size) PA_THROW(std::bad_alloc) { - return pa_malloc(size); -} -inline void operator delete(void *ptr) throw() { - pa_free(ptr); +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; + +// defines + +#define override +#define rethrow throw + +#if defined(_MSC_VER) || (__cplusplus>=201103L) +#define PA_THROW(what) +#else +#define PA_THROW(what) throw(what) +#endif #ifndef _MSC_VER -// disabled from accidental use +// regular new/delete are disabled from accidental use + +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 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(); @@ -116,26 +140,8 @@ inline void *malloc(size_t) { return mal 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 - -/// 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) { - return pa_malloc(size); - } - static void operator delete(void *ptr) { - pa_free(ptr); - } -}; +#endif // PA_DEBUG_DISABLE_GC -/// Base for all Parser classes -typedef PA_Allocated PA_Object; - -// defines - -#define override -#define rethrow throw +#endif // _MSC_VER #endif