Annotation of parser3/src/include/pa_memory.h, revision 1.1.2.9.2.14
1.1.2.1 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.1.2.9.2.14! paf 12: static const char* IDENT_MEMORY_H="$Date: 2003/03/25 10:07:00 $";
1.1.2.9 paf 13:
14: #define PA_DEBUG_REFERENCES
1.1.2.1 paf 15:
16: // include
17:
18: #include "pa_config_includes.h"
1.1.2.9.2.1 paf 19: #include "gc.h"
20:
1.1.2.1 paf 21:
22: #ifdef XML
23: # include "gdome.h"
24: // for xmlChar
25: # include "libxml/tree.h"
26: #endif
27:
28: // forwards
29:
1.1.2.9.2.5 paf 30: void *pa_fail_alloc(const char* what, size_t size);
1.1.2.2 paf 31:
32: // inlines
33:
34: inline void *pa_malloc(size_t size) {
1.1.2.9.2.1 paf 35: if(void *result=GC_MALLOC(size))
1.1.2.2 paf 36: return result;
37:
38: return pa_fail_alloc("allocate", size);
39: }
40:
1.1.2.9.2.1 paf 41: inline void *pa_malloc_atomic(size_t size) {
42: if(void *result=GC_MALLOC_ATOMIC(size))
1.1.2.2 paf 43: return result;
44:
45: return pa_fail_alloc("allocate clean", size);
46: }
1.1.2.9.2.4 paf 47: /// @a length may be null, which mean "autocalc it"
1.1.2.9.2.5 paf 48: inline char *pa_strdup(const char* auto_variable_never_null, size_t length=0) {
1.1.2.9.2.4 paf 49: if(!length)
50: length=strlen(auto_variable_never_null);
1.1.2.9.2.7 paf 51:
52: size_t size=length+1;
1.1.2.9.2.4 paf 53: if(char *result=static_cast<char*>(GC_MALLOC_ATOMIC(size))) {
1.1.2.9.2.7 paf 54: memcpy(result, auto_variable_never_null, length);
55: result[length]=0;
1.1.2.9.2.4 paf 56: return result;
57: }
58:
59: return static_cast<char*>(pa_fail_alloc("allocate clean", size));
60: }
1.1.2.9.2.10 paf 61:
1.1.2.2 paf 62: inline void pa_free(void *ptr) {
1.1.2.9.2.1 paf 63: GC_FREE(ptr);
1.1.2.2 paf 64: }
65:
66: inline void *pa_realloc(void *ptr, size_t size) {
1.1.2.9.2.1 paf 67: if(void *result=GC_REALLOC(ptr, size))
1.1.2.2 paf 68: return result;
69:
70: return pa_fail_alloc("reallocate to", size);
71: }
1.1.2.1 paf 72:
1.1.2.9.2.4 paf 73: //{@ these operators are disabled, one should explicitely specify either new(UseGC) or new(PointerFreeGC)
1.1.2.9.2.6 paf 74: inline void *operator new(size_t size) { abort(); } // disabled
75: inline void *operator new[] (size_t size) { abort(); } // disabled
1.1.2.9.2.4 paf 76: //}@
1.1.2.9.2.3 paf 77:
78: #define UseGC ((int)1)
79: #define PointerFreeGC (true)
80:
1.1.2.9.2.14! paf 81: //{@ Array-oriented
1.1.2.9.2.3 paf 82: inline void *operator new[] (size_t size, int) { // UseGC
83: return pa_malloc(size);
84: }
85: inline void *operator new[] (size_t size, bool) { // PointerFreeGC
1.1.2.9.2.1 paf 86: return pa_malloc_atomic(size);
1.1.2.1 paf 87: }
88: inline void operator delete[] (void *ptr) {
89: pa_free(ptr);
90: }
1.1.2.9.2.14! paf 91: //}@
! 92:
! 93: //{@ Structure-oriented
! 94: inline void *operator new (size_t size, int) { // UseGC
! 95: return pa_malloc(size);
! 96: }
! 97: inline void *operator new (size_t size, bool) { // PointerFreeGC
! 98: return pa_malloc_atomic(size);
! 99: }
! 100: inline void operator delete(void *ptr) {
! 101: pa_free(ptr);
! 102: }
! 103: //}@
1.1.2.1 paf 104:
1.1.2.9.2.1 paf 105: /// memory allocation/dallocation goes via pa_malloc/pa_free.
1.1.2.1 paf 106: class PA_Allocated {
107: public:
108: /// the sole: instances allocated using our functions
109: static void *operator new(size_t size) {
110: return pa_malloc(size);
111: }
112: static void operator delete(void *ptr) {
113: pa_free(ptr);
114: }
115: static void *malloc(size_t size) {
116: return pa_malloc(size);
117: }
1.1.2.9.2.1 paf 118: static void *malloc_atomic(size_t size) {
119: return pa_malloc_atomic(size);
1.1.2.9.2.8 paf 120: }
121: static char *strdup(const char* auto_variable_never_null, size_t length=0) {
122: return pa_strdup(auto_variable_never_null, length);
1.1.2.1 paf 123: }
124: static void free(void *ptr) {
125: pa_free(ptr);
126: }
127: static void *realloc(void *ptr, size_t size) {
128: return pa_realloc(ptr, size);
129: }
1.1.2.9.2.2 paf 130:
131: private: // disabled from accidental use
132:
1.1.2.9.2.9 paf 133: /// use malloc/malloc_atomic instead [GC clears result of those]
1.1.2.9.2.2 paf 134: static void *calloc(size_t size);
1.1.2.9.2.13 paf 135:
136: };
137:
138: /// Those who want their destructor called during finalization, must derive from this class [also]
139: class PA_Cleaned {
140:
141: static void cleanup( void* obj, void* displ ) {
142: ((PA_Cleaned*) ((char*) obj + (ptrdiff_t) displ))->~PA_Cleaned();
143: }
144:
145: public:
146:
147: PA_Cleaned() {
148: GC_finalization_proc oldProc;
149: void* oldData;
150: void* base = GC_base( (void *) this );
151: if (0 != base) {
152: // Don't call the debug version, since this is a real base address.
153: GC_register_finalizer_ignore_self(
154: base, (GC_finalization_proc)cleanup, (void*) ((char*) this - (char*) base),
155: &oldProc, &oldData );
156: if (0 != oldProc) {
157: GC_register_finalizer_ignore_self( base, oldProc, oldData, 0, 0 );
158: }
159: }
160: }
161:
162: virtual ~PA_Cleaned() {
163: GC_REGISTER_FINALIZER_IGNORE_SELF( GC_base(this), 0, 0, 0, 0 );
164: }
1.1.2.1 paf 165:
166: };
167:
1.1.2.9.2.1 paf 168: /// Base for all Parser classes
169: typedef PA_Allocated PA_Object;
1.1.2.1 paf 170:
1.1.2.9.2.6 paf 171: // defines
172:
173: #define override
174: #define rethrow throw
1.1.2.9.2.10 paf 175:
1.1.2.1 paf 176:
177: #endif
E-mail: