Annotation of parser3/src/include/pa_memory.h, revision 1.9
1.2 paf 1: /** @file
2: Parser: memory reference counting classes decls.
3:
4: Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
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.9 ! paf 12: static const char * const IDENT_MEMORY_H="$Date: 2003/11/20 16:34:25 $";
1.2 paf 13:
14: // include
15:
16: #include "pa_config_includes.h"
17:
1.5 paf 18: /// to debug backtrace pointers switch this on:
1.7 paf 19: #ifdef _DEBUG
20: #define GC_DEBUG
21: #endif
1.2 paf 22: #include "gc.h"
23:
24: // defines
25:
26: //#define PA_DEBUG_GC_MEMORY
27:
28: #ifdef PA_DEBUG_GC_MEMORY
29: void* pa_gc_malloc(size_t size);
30: void* pa_gc_malloc_atomic(size_t size);
31: void* pa_gc_realloc(void* ptr, size_t size);
32: void pa_gc_free(void* ptr);
33: #else
34: inline void* pa_gc_malloc(size_t size) { return GC_MALLOC(size); }
35: inline void* pa_gc_malloc_atomic(size_t size) { return GC_MALLOC_ATOMIC(size); }
36: inline void* pa_gc_realloc(void* ptr, size_t size) { return GC_REALLOC(ptr, size); }
37: inline void pa_gc_free(void* ptr) { GC_FREE(ptr); }
38: #endif
39:
40:
41: // forwards
42:
43: void *pa_fail_alloc(const char* what, size_t size);
44:
45: // inlines
46:
47: inline void *pa_malloc(size_t size) {
1.4 paf 48: if(void *result=pa_gc_malloc(size))
1.2 paf 49: return result;
50:
51: return pa_fail_alloc("allocate", size);
52: }
53:
54: inline void *pa_malloc_atomic(size_t size) {
1.4 paf 55: if(void *result=pa_gc_malloc_atomic(size))
1.2 paf 56: return result;
57:
58: return pa_fail_alloc("allocate clean", size);
59: }
60: /// @a length may be null, which mean "autocalc it"
61: inline char *pa_strdup(const char* auto_variable_never_null, size_t helper_length=0) {
62: size_t known_length=(helper_length?helper_length:strlen(auto_variable_never_null));
63:
1.9 ! paf 64: if(char *result=static_cast<char*>(pa_gc_malloc_atomic(known_length+1))) {
! 65: memcpy(result, auto_variable_never_null, known_length);
1.2 paf 66: result[known_length]=0;
67: return result;
68: }
69:
70: return static_cast<char*>(pa_fail_alloc("allocate clean", size));
71: }
72:
73: inline void pa_free(void *ptr) {
1.4 paf 74: pa_gc_free(ptr);
1.2 paf 75: }
76:
77: inline void *pa_realloc(void *ptr, size_t size) {
1.4 paf 78: if(void *result=pa_gc_realloc(ptr, size))
1.2 paf 79: return result;
80:
81: return pa_fail_alloc("reallocate to", size);
82: }
83:
84: //{@ these operators are disabled, one should explicitely specify either new(UseGC) or new(PointerFreeGC)
1.3 paf 85: inline void *operator new(size_t) { abort(); } // disabled
86: inline void *operator new[] (size_t) { abort(); } // disabled
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: