Annotation of parser3/src/include/pa_memory.h, revision 1.16
1.2 paf 1: /** @file
2: Parser: memory reference counting classes decls.
3:
1.12 paf 4: Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)
1.2 paf 5:
6: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
7: */
8:
9: #ifndef PA_MEMORY_H
10: #define PA_MEMORY_H
11:
1.16 ! misha 12: static const char * const IDENT_MEMORY_H="$Date: 2006/04/09 13:38:47 $";
1.2 paf 13:
14: // include
15:
16: #include "pa_config_includes.h"
17: #include "gc.h"
18:
19: // defines
20:
21: //#define PA_DEBUG_GC_MEMORY
22:
23: #ifdef PA_DEBUG_GC_MEMORY
24: void* pa_gc_malloc(size_t size);
25: void* pa_gc_malloc_atomic(size_t size);
26: void* pa_gc_realloc(void* ptr, size_t size);
27: void pa_gc_free(void* ptr);
28: #else
29: inline void* pa_gc_malloc(size_t size) { return GC_MALLOC(size); }
30: inline void* pa_gc_malloc_atomic(size_t size) { return GC_MALLOC_ATOMIC(size); }
31: inline void* pa_gc_realloc(void* ptr, size_t size) { return GC_REALLOC(ptr, size); }
32: inline void pa_gc_free(void* ptr) { GC_FREE(ptr); }
33: #endif
34:
35:
36: // forwards
37:
38: void *pa_fail_alloc(const char* what, size_t size);
39:
40: // inlines
41:
42: inline void *pa_malloc(size_t size) {
1.4 paf 43: if(void *result=pa_gc_malloc(size))
1.2 paf 44: return result;
45:
46: return pa_fail_alloc("allocate", size);
47: }
48:
49: inline void *pa_malloc_atomic(size_t size) {
1.4 paf 50: if(void *result=pa_gc_malloc_atomic(size))
1.2 paf 51: return result;
52:
53: return pa_fail_alloc("allocate clean", size);
54: }
55: /// @a length may be null, which mean "autocalc it"
56: inline char *pa_strdup(const char* auto_variable_never_null, size_t helper_length=0) {
57: size_t known_length=(helper_length?helper_length:strlen(auto_variable_never_null));
58:
1.10 paf 59: size_t size=known_length+1;
60: if(char *result=static_cast<char*>(pa_gc_malloc_atomic(size))) {
1.9 paf 61: memcpy(result, auto_variable_never_null, known_length);
1.2 paf 62: result[known_length]=0;
63: return result;
64: }
65:
66: return static_cast<char*>(pa_fail_alloc("allocate clean", size));
67: }
68:
69: inline void pa_free(void *ptr) {
1.4 paf 70: pa_gc_free(ptr);
1.2 paf 71: }
72:
73: inline void *pa_realloc(void *ptr, size_t size) {
1.4 paf 74: if(void *result=pa_gc_realloc(ptr, size))
1.2 paf 75: return result;
76:
77: return pa_fail_alloc("reallocate to", size);
78: }
79:
1.16 ! misha 80: //{@ these operators can be used from stl. to be on a safe side, assume that data may contain pointers
! 81: inline void *operator new[] (size_t size) {
! 82: return pa_malloc(size);
! 83: }
! 84: inline void *operator new(size_t size) {
! 85: return pa_malloc(size);
! 86: }
1.2 paf 87: //}@
88:
89: #define UseGC ((int)1)
90: #define PointerFreeGC (true)
91:
92: //{@ Array-oriented
1.3 paf 93: inline void *operator new[] (size_t size, int) { // UseGC
1.2 paf 94: return pa_malloc(size);
95: }
96: inline void *operator new[] (size_t size, bool) { // PointerFreeGC
97: return pa_malloc_atomic(size);
98: }
99: inline void operator delete[] (void *ptr) {
100: pa_free(ptr);
101: }
102: //}@
103:
104: //{@ Structure-oriented
105: inline void *operator new (size_t size, int) { // UseGC
106: return pa_malloc(size);
107: }
108: inline void *operator new (size_t size, bool) { // PointerFreeGC
109: return pa_malloc_atomic(size);
110: }
111: inline void operator delete(void *ptr) {
112: pa_free(ptr);
113: }
114: //}@
115:
116: /// memory allocation/dallocation goes via pa_malloc/pa_free.
117: class PA_Allocated {
118: public:
119: /// the sole: instances allocated using our functions
120: static void *operator new(size_t size) {
121: return pa_malloc(size);
122: }
123: static void operator delete(void *ptr) {
124: pa_free(ptr);
125: }
126: static void *malloc(size_t size) {
127: return pa_malloc(size);
128: }
129: static void *malloc_atomic(size_t size) {
130: return pa_malloc_atomic(size);
131: }
132: static char *strdup(const char* auto_variable_never_null, size_t helper_length=0) {
133: return pa_strdup(auto_variable_never_null, helper_length);
134: }
135: static void free(void *ptr) {
136: pa_free(ptr);
137: }
138: static void *realloc(void *ptr, size_t size) {
139: return pa_realloc(ptr, size);
140: }
141:
142: private: // disabled from accidental use
143:
144: /// use malloc/malloc_atomic instead [GC clears result of those]
145: static void *calloc(size_t size);
146:
147: };
148:
149: /// Those who want their destructor called during finalization, must derive from this class [also]
150: class PA_Cleaned {
151: #ifndef PA_DEBUG_DISABLE_GC
152: static void cleanup( void* obj, void* displ ) {
153: ((PA_Cleaned*) ((char*) obj + (ptrdiff_t) displ))->~PA_Cleaned();
154: }
155:
156: public:
157: PA_Cleaned() {
158: GC_finalization_proc oldProc;
159: void* oldData;
160: void* base = GC_base( (void *) this );
161: if (0 != base) {
162: // Don't call the debug version, since this is a real base address.
163: GC_register_finalizer_ignore_self(
164: base, (GC_finalization_proc)cleanup, (void*) ((char*) this - (char*) base),
165: &oldProc, &oldData );
166: if (0 != oldProc) {
167: GC_register_finalizer_ignore_self( base, oldProc, oldData, 0, 0 );
168: }
169: }
170: }
171:
172: virtual ~PA_Cleaned() {
173: GC_REGISTER_FINALIZER_IGNORE_SELF( GC_base(this), 0, 0, 0, 0 );
174: }
175: #endif
176: };
177:
178: /// Base for all Parser classes
179: typedef PA_Allocated PA_Object;
180:
181: // defines
182:
183: #define override
184: #define rethrow throw
185:
186:
187: #endif
E-mail: