--- parser3/src/include/pa_memory.h 2003/02/21 10:58:57 1.1.2.2 +++ parser3/src/include/pa_memory.h 2003/03/18 11:54:56 1.1.2.9 @@ -9,7 +9,9 @@ #ifndef PA_MEMORY_H #define PA_MEMORY_H -static const char* IDENT_MEMORY_H="$Date: 2003/02/21 10:58:57 $"; +static const char* IDENT_MEMORY_H="$Date: 2003/03/18 11:54:56 $"; + +#define PA_DEBUG_REFERENCES // include @@ -51,13 +53,6 @@ inline void *pa_realloc(void *ptr, size_ return pa_fail_alloc("reallocate to", size); } -// forwards - -class Exception; -class String; -class Charset; -class GdomeDOMString_auto_ptr; - template class object_ptr { T *ptr; public: @@ -131,7 +126,7 @@ public: {if (this != &src) {if (ptr != src.get()) {if (owns) - delete ptr; + delete[] ptr; owns = src.owns; } else if (src.owns) owns = true; @@ -139,7 +134,7 @@ public: return (*this); } ~smart_ptr() {if (owns) - delete ptr; } + delete[] ptr; } T& operator*() const {return (*get()); } T *get() const @@ -150,11 +145,17 @@ public: operator bool() const { return get()!=0; } + bool operator !() const { + return get()==0; + } operator T*() const { return ptr; } }; +#define DECLARE_SMART_PTR(name) \ + typedef smart_ptr name##Ptr + // defines #define override @@ -165,6 +166,9 @@ public: inline void *operator new[] (size_t size) { return pa_malloc(size); } +inline void *operator new[] (size_t size, int /*zero, just flag to use calloc instead of malloc*/) { + return pa_calloc(size); +} inline void operator delete[] (void *ptr) { pa_free(ptr); } @@ -196,6 +200,11 @@ public: }; +#ifdef PA_DEBUG_REFERENCES +extern const void *refdbg_this; +void pa_stack_dump(const char *msg); +#endif + /** Base for all Parser classes, memory allocation/dallocation goes via pa_malloc/pa_free. */ @@ -206,14 +215,29 @@ public: virtual ~PA_Object() {} void ref() const { +#ifdef PA_DEBUG_REFERENCES + if(this==refdbg_this) { + char buf[0x400]; + _snprintf(buf, sizeof(buf), "[0x%p#%d++]\n", this, freferences); + pa_stack_dump(buf); + } +#endif freferences++; } void unref() const { if(freferences) { +#ifdef PA_DEBUG_REFERENCES + if(this==refdbg_this) { + char buf[0x400]; + _snprintf(buf, sizeof(buf), "[0x%p#%d--]\n", this, freferences); + pa_stack_dump(buf); + } +#endif if(--freferences==0) delete this; } } + unsigned long references() { return freferences; } }; DECLARE_OBJECT_PTR(PA_Object);