Annotation of parser3/src/include/pa_memory.h, revision 1.1.2.9.2.4
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.4! paf 12: static const char* IDENT_MEMORY_H="$Date: 2003/03/19 16:28:08 $";
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.1 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"
! 48: inline char *pa_strdup(const char *auto_variable_never_null, size_t length=0) {
! 49: if(!length)
! 50: length=strlen(auto_variable_never_null);
! 51: size_t size=length+1; // terminating zero
! 52: if(char *result=static_cast<char*>(GC_MALLOC_ATOMIC(size))) {
! 53: memcpy(result, auto_variable_never_null, size);
! 54: return result;
! 55: }
! 56:
! 57: return static_cast<char*>(pa_fail_alloc("allocate clean", size));
! 58: }
1.1.2.2 paf 59: inline void pa_free(void *ptr) {
1.1.2.9.2.1 paf 60: GC_FREE(ptr);
1.1.2.2 paf 61: }
62:
63: inline void *pa_realloc(void *ptr, size_t size) {
1.1.2.9.2.1 paf 64: if(void *result=GC_REALLOC(ptr, size))
1.1.2.2 paf 65: return result;
66:
67: return pa_fail_alloc("reallocate to", size);
68: }
1.1.2.1 paf 69:
70: #define DECLARE_OBJECT_PTR(name) \
1.1.2.9.2.1 paf 71: typedef name* name##Ptr
1.1.2.6 paf 72:
73: #define DECLARE_SMART_PTR(name) \
1.1.2.9.2.1 paf 74: typedef name* name##Ptr
1.1.2.1 paf 75:
76: // defines
77:
78: #define override
79: #define rethrow throw
80:
81: // memory
82:
1.1.2.9.2.4! paf 83: //{@ these operators are disabled, one should explicitely specify either new(UseGC) or new(PointerFreeGC)
1.1.2.9.2.3 paf 84: void *operator new(size_t size); // disabled, should cause link error!
85: void operator delete (void *ptr); // disabled, should cause link error!
86: void *operator new[] (size_t size); // disabled, should cause link error!
1.1.2.9.2.4! paf 87: //}@
1.1.2.9.2.3 paf 88:
89: #define UseGC ((int)1)
90: #define PointerFreeGC (true)
91:
92: inline void *operator new[] (size_t size, int) { // UseGC
93: return pa_malloc(size);
94: }
95: inline void *operator new[] (size_t size, bool) { // PointerFreeGC
1.1.2.9.2.1 paf 96: return pa_malloc_atomic(size);
1.1.2.1 paf 97: }
98: inline void operator delete[] (void *ptr) {
99: pa_free(ptr);
100: }
101:
1.1.2.9.2.1 paf 102: /// memory allocation/dallocation goes via pa_malloc/pa_free.
1.1.2.1 paf 103: class PA_Allocated {
104: public:
105: /// the sole: instances allocated using our functions
106: static void *operator new(size_t size) {
107: return pa_malloc(size);
108: }
109: static void operator delete(void *ptr) {
110: pa_free(ptr);
111: }
112: static void *malloc(size_t size) {
113: return pa_malloc(size);
114: }
1.1.2.9.2.1 paf 115: static void *malloc_atomic(size_t size) {
116: return pa_malloc_atomic(size);
1.1.2.1 paf 117: }
118: static void free(void *ptr) {
119: pa_free(ptr);
120: }
121: static void *realloc(void *ptr, size_t size) {
122: return pa_realloc(ptr, size);
123: }
1.1.2.9.2.2 paf 124:
125: private: // disabled from accidental use
126:
127: /// use malloc instead [GC clears malloc result]
128: static void *calloc(size_t size);
1.1.2.1 paf 129:
130: };
131:
1.1.2.9.2.1 paf 132: /// Base for all Parser classes
133: typedef PA_Allocated PA_Object;
1.1.2.1 paf 134:
135: // convinient types
136:
137: #endif
E-mail: