Annotation of parser3/src/include/pa_memory.h, revision 1.1.2.9.2.18

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

E-mail: