|
|
| version 1.1.2.9.2.2, 2003/03/19 13:16:56 | version 1.22, 2015/04/21 23:23:34 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: memory reference counting classes decls. | Parser: memory reference counting classes decls. |
| Copyright (c) 2001-2003 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* IDENT_MEMORY_H="$Date$"; | #define IDENT_PA_MEMORY_H "$Id$" |
| #define PA_DEBUG_REFERENCES | |
| // include | // include |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| #include "gc.h" | #include "gc.h" |
| // define destructors use for Array, Hash and VMethodFrame | |
| #define USE_DESTRUCTORS | |
| #ifdef XML | inline void* pa_gc_malloc(size_t size) { return GC_MALLOC(size); } |
| # include "gdome.h" | inline void* pa_gc_malloc_atomic(size_t size) { return GC_MALLOC_ATOMIC(size); } |
| // for xmlChar | inline void* pa_gc_realloc(void* ptr, size_t size) { return GC_REALLOC(ptr, size); } |
| # include "libxml/tree.h" | inline void pa_gc_free(void* ptr) { GC_FREE(ptr); } |
| #endif | |
| // forwards | // forwards |
| void *pa_fail_alloc(const char *what, size_t size); | void *pa_fail_alloc(const char* what, size_t size); |
| // inlines | // inlines |
| inline void *pa_malloc(size_t size) { | inline void *pa_malloc(size_t size) { |
| if(void *result=GC_MALLOC(size)) | if(void *result=pa_gc_malloc(size)) |
| return result; | return result; |
| return pa_fail_alloc("allocate", size); | return pa_fail_alloc("allocate", size); |
| } | } |
| inline void *pa_malloc_atomic(size_t 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 result; |
| return pa_fail_alloc("allocate clean", size); | 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<char*>(pa_gc_malloc_atomic(size))) { | |
| memcpy(result, auto_variable_never_null, known_length); | |
| result[known_length]=0; | |
| return result; | |
| } | |
| return static_cast<char*>(pa_fail_alloc("allocate clean", size)); | |
| } | |
| inline void pa_free(void *ptr) { | inline void pa_free(void *ptr) { |
| GC_FREE(ptr); | pa_gc_free(ptr); |
| } | } |
| inline void *pa_realloc(void *ptr, size_t size) { | 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 result; |
| return pa_fail_alloc("reallocate to", size); | return pa_fail_alloc("reallocate to", size); |
| } | } |
| #define DECLARE_OBJECT_PTR(name) \ | #define PointerFreeGC (true) |
| typedef name* name##Ptr | |
| #define DECLARE_SMART_PTR(name) \ | |
| typedef name* name##Ptr | |
| // defines | |
| #define override | |
| #define rethrow throw | |
| // memory | |
| //{@ Array-oriented | |
| inline void *operator new[] (size_t size) { | inline void *operator new[] (size_t size) { |
| return pa_malloc(size); | |
| } | |
| 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 delete[] (void *ptr) { |
| pa_free(ptr); | pa_free(ptr); |
| } | } |
| inline void operator delete (void *ptr) { | //}@ |
| abort(); | |
| //{@ Structure-oriented | |
| inline void *operator new(size_t size) { | |
| 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); | |
| } | |
| //}@ | |
| #ifndef WIN32 | |
| // 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 { |
| Line 88 public: | Line 121 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 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 instead [GC clears malloc result] | |
| static void *calloc(size_t size); | |
| }; | }; |
| /// Base for all Parser classes | /// Base for all Parser classes |
| typedef PA_Allocated PA_Object; | typedef PA_Allocated PA_Object; |
| // convinient types | // defines |
| #define override | |
| #define rethrow throw | |
| #endif | #endif |