|
|
| version 1.14, 2005/12/16 10:15:12 | version 1.26, 2015/10/12 14:36:56 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: memory reference counting classes decls. | Parser: memory reference counting classes decls. |
| Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 9 | Line 9 |
| #ifndef PA_MEMORY_H | #ifndef PA_MEMORY_H |
| #define PA_MEMORY_H | #define PA_MEMORY_H |
| static const char * const IDENT_MEMORY_H="$Date$"; | #define IDENT_PA_MEMORY_H "$Id$" |
| // include | // include |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| #include "gc.h" | #include "gc.h" |
| #include <new> | |
| // defines | // define destructors use for Array, Hash and VMethodFrame |
| #define USE_DESTRUCTORS | |
| //#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); | |
| #else | |
| inline void* pa_gc_malloc(size_t size) { return GC_MALLOC(size); } | 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_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_realloc(void* ptr, size_t size) { return GC_REALLOC(ptr, size); } |
| inline void pa_gc_free(void* ptr) { GC_FREE(ptr); } | inline void pa_gc_free(void* ptr) { GC_FREE(ptr); } |
| #endif | |
| // forwards | // forwards |
| Line 52 inline void *pa_malloc_atomic(size_t siz | Line 44 inline void *pa_malloc_atomic(size_t siz |
| 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 helper_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 known_length= helper_length ? helper_length : strlen(auto_variable_never_null); |
| size_t size=known_length+1; | size_t size=known_length+1; |
| if(char *result=static_cast<char*>(pa_gc_malloc_atomic(size))) { | if(char *result=static_cast<char*>(pa_gc_malloc_atomic(size))) { |
| Line 77 inline void *pa_realloc(void *ptr, size_ | Line 70 inline void *pa_realloc(void *ptr, size_ |
| 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) | #ifdef _MSC_VER |
| inline void *operator new(size_t) { abort(); } // disabled | #define PA_THROW(what) |
| inline void *operator new[] (size_t) { abort(); } // disabled | #else |
| //}@ | #define PA_THROW(what) throw(what) |
| #endif | |
| #define UseGC ((int)1) | |
| #define PointerFreeGC (true) | #define PointerFreeGC (true) |
| //{@ Array-oriented | //{@ Array-oriented |
| inline void *operator new[] (size_t size, int) { // UseGC | |
| return pa_malloc(size); | |
| } | |
| inline void *operator new[] (size_t size, bool) { // PointerFreeGC | inline void *operator new[] (size_t size, bool) { // PointerFreeGC |
| return pa_malloc_atomic(size); | return pa_malloc_atomic(size); |
| } | } |
| inline void operator delete[] (void *ptr) { | inline void *operator new[] (std::size_t size) PA_THROW(std::bad_alloc) { |
| return pa_malloc(size); | |
| } | |
| inline void operator delete[] (void *ptr) PA_THROW() { | |
| pa_free(ptr); | pa_free(ptr); |
| } | } |
| //}@ | //}@ |
| //{@ Structure-oriented | //{@ Structure-oriented |
| inline void *operator new (size_t size, int) { // UseGC | |
| return pa_malloc(size); | |
| } | |
| inline void *operator new (size_t size, bool) { // PointerFreeGC | inline void *operator new (size_t size, bool) { // PointerFreeGC |
| return pa_malloc_atomic(size); | return pa_malloc_atomic(size); |
| } | } |
| inline void operator delete(void *ptr) { | inline void *operator new(std::size_t size) PA_THROW(std::bad_alloc) { |
| return pa_malloc(size); | |
| } | |
| inline void operator delete(void *ptr) PA_THROW() { | |
| pa_free(ptr); | pa_free(ptr); |
| } | } |
| //}@ | //}@ |
| #ifndef _MSC_VER | |
| // disabled from accidental use | |
| 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 | |
| /// memory allocation/dallocation goes via pa_malloc/pa_free. | /// memory allocation/dallocation goes via pa_malloc/pa_free. |
| class PA_Allocated { | class PA_Allocated { |
| public: | public: |
| Line 119 public: | Line 128 public: |
| static void operator delete(void *ptr) { | static void operator delete(void *ptr) { |
| pa_free(ptr); | pa_free(ptr); |
| } | } |
| static void *malloc(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) { | |
| 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(); | |
| } | |
| 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 ); | |
| } | |
| } | |
| } | |
| virtual ~PA_Cleaned() { | |
| GC_REGISTER_FINALIZER_IGNORE_SELF( GC_base(this), 0, 0, 0, 0 ); | |
| } | |
| #endif | |
| }; | }; |
| /// Base for all Parser classes | /// Base for all Parser classes |
| Line 179 typedef PA_Allocated PA_Object; | Line 138 typedef PA_Allocated PA_Object; |
| #define override | #define override |
| #define rethrow throw | #define rethrow throw |
| #endif | #endif |