Annotation of parser3/src/include/pa_memory.h, revision 1.1.2.1
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:
! 12: static const char* IDENT_MEMORY_H="$Date: 2003/02/19 16:19:00 $";
! 13:
! 14: // include
! 15:
! 16: #include "pa_config_includes.h"
! 17:
! 18: #ifdef XML
! 19: # include "gdome.h"
! 20: // for xmlChar
! 21: # include "libxml/tree.h"
! 22: #endif
! 23:
! 24: // forwards
! 25:
! 26: void *pa_malloc(size_t size);
! 27: void *pa_calloc(size_t size);
! 28: void pa_free(void *ptr);
! 29: void *pa_realloc(void *ptr, size_t size);
! 30:
! 31: class Exception;
! 32: class String;
! 33: class Charset;
! 34: class GdomeDOMString_auto_ptr;
! 35:
! 36: template<typename T> class object_ptr {
! 37: T *ptr;
! 38: public:
! 39: typedef T element_type;
! 40:
! 41: explicit object_ptr(T *ptr = 0) {
! 42: this->ptr=ptr;
! 43: if(ptr)
! 44: ptr->ref();
! 45: }
! 46: template<class B> object_ptr(const object_ptr<B>& src) {
! 47: this->ptr=src.get();
! 48: if(ptr)
! 49: ptr->ref();
! 50: }
! 51: object_ptr(const object_ptr<T>& src) {
! 52: ptr=src.get();
! 53: if(ptr)
! 54: ptr->ref();
! 55: }
! 56: object_ptr(T *ptr, int) {
! 57: this->ptr=ptr;
! 58: }
! 59: object_ptr<T>& operator=(const object_ptr<T>& src) {
! 60: if(this!=&src)
! 61: if(ptr!=src.get()) {
! 62: if(ptr)
! 63: ptr->unref();
! 64: ptr=src.get();
! 65: if(ptr)
! 66: ptr->ref();
! 67: }
! 68: return *this;
! 69: }
! 70: ~object_ptr() {
! 71: if(ptr)
! 72: ptr->unref();
! 73: }
! 74: T& operator*() const {
! 75: return *get();
! 76: }
! 77: T *operator->() const {
! 78: return get();
! 79: }
! 80: T *get() const {
! 81: return ptr;
! 82: }
! 83: operator bool() const {
! 84: return get()!=0;
! 85: }
! 86: bool operator !() const {
! 87: return get()==0;
! 88: }
! 89: };
! 90:
! 91: #define DECLARE_OBJECT_PTR(name) \
! 92: typedef object_ptr<name> name##Ptr
! 93:
! 94: /// TEMPLATE CLASS smart_ptr, stolen from stl:auto_ptr & renamed field/params [not compiled on gcc]
! 95: template<class T>
! 96: class smart_ptr {
! 97: bool owns;
! 98: T *ptr;
! 99: public:
! 100: typedef T element_type;
! 101: explicit smart_ptr(T *aptr = 0)
! 102: : owns(aptr != 0), ptr(aptr) {}
! 103: smart_ptr(const smart_ptr<T>& src)
! 104: : owns(src.owns), ptr(src.release()) {}
! 105: smart_ptr<T>& operator=(const smart_ptr<T>& src)
! 106: {if (this != &src)
! 107: {if (ptr != src.get())
! 108: {if (owns)
! 109: delete ptr;
! 110: owns = src.owns; }
! 111: else if (src.owns)
! 112: owns = true;
! 113: ptr = src.release(); }
! 114: return (*this); }
! 115: ~smart_ptr()
! 116: {if (owns)
! 117: delete ptr; }
! 118: T& operator*() const
! 119: {return (*get()); }
! 120: T *get() const
! 121: {return ptr; }
! 122: T *release() const
! 123: {((smart_ptr<T> *)this)->owns = false;
! 124: return (ptr); }
! 125: operator bool() const {
! 126: return get()!=0;
! 127: }
! 128: operator T*() const {
! 129: return ptr;
! 130: }
! 131: };
! 132:
! 133: // defines
! 134:
! 135: #define override
! 136: #define rethrow throw
! 137:
! 138: // memory
! 139:
! 140: inline void *operator new[] (size_t size) {
! 141: return pa_malloc(size);
! 142: }
! 143: inline void operator delete[] (void *ptr) {
! 144: pa_free(ptr);
! 145: }
! 146: inline void operator delete (void *ptr) {
! 147: pa_free(ptr);
! 148: }
! 149:
! 150: class PA_Allocated {
! 151: public:
! 152: /// the sole: instances allocated using our functions
! 153: static void *operator new(size_t size) {
! 154: return pa_malloc(size);
! 155: }
! 156: static void operator delete(void *ptr) {
! 157: pa_free(ptr);
! 158: }
! 159: static void *malloc(size_t size) {
! 160: return pa_malloc(size);
! 161: }
! 162: static void *calloc(size_t size) {
! 163: return pa_calloc(size);
! 164: }
! 165: static void free(void *ptr) {
! 166: pa_free(ptr);
! 167: }
! 168: static void *realloc(void *ptr, size_t size) {
! 169: return pa_realloc(ptr, size);
! 170: }
! 171:
! 172: };
! 173:
! 174: /**
! 175: Base for all Parser classes, memory allocation/dallocation goes via pa_malloc/pa_free.
! 176: */
! 177: class PA_Object: public PA_Allocated {
! 178: mutable unsigned long freferences;
! 179: public:
! 180: PA_Object(): freferences(0) {}
! 181: virtual ~PA_Object() {}
! 182:
! 183: void ref() const {
! 184: freferences++;
! 185: }
! 186: void unref() const {
! 187: if(freferences) {
! 188: if(--freferences==0)
! 189: delete this;
! 190: }
! 191: }
! 192: };
! 193: DECLARE_OBJECT_PTR(PA_Object);
! 194:
! 195: // convinient types
! 196:
! 197: typedef smart_ptr<char> CharPtr;
! 198:
! 199: #endif
E-mail: