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

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.19! paf        12: static const char* IDENT_MEMORY_H="$Date: 2003/04/08 12:47:30 $";
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));
1.1.2.9.2.19! paf        76:        if(!known_length)
        !            77:                return "";
        !            78: 
1.1.2.9.2.18  paf        79:        size_t size=known_length+1;
1.1.2.9.2.16  paf        80:        if(char *result=static_cast<char*>(PA_GC_MALLOC_ATOMIC(size))) {
1.1.2.9.2.18  paf        81:                memcpy(result, auto_variable_never_null, size);
                     82:                result[known_length]=0;
1.1.2.9.2.4  paf        83:                return result;
                     84:        }
                     85: 
                     86:        return static_cast<char*>(pa_fail_alloc("allocate clean", size));
                     87: }
1.1.2.9.2.10  paf        88: 
1.1.2.2   paf        89: inline void pa_free(void *ptr) {
1.1.2.9.2.16  paf        90:        PA_GC_FREE(ptr);
1.1.2.2   paf        91: }
                     92: 
                     93: inline void *pa_realloc(void *ptr, size_t size) {
1.1.2.9.2.16  paf        94:        if(void *result=PA_GC_REALLOC(ptr, size))
1.1.2.2   paf        95:                return result;
                     96: 
                     97:        return pa_fail_alloc("reallocate to", size);
                     98: }
1.1.2.1   paf        99: 
1.1.2.9.2.4  paf       100: //{@ these operators are disabled, one should explicitely specify either new(UseGC) or new(PointerFreeGC)
1.1.2.9.2.6  paf       101: inline void *operator new(size_t size) { abort(); } // disabled
                    102: inline void *operator new[] (size_t size) { abort(); } // disabled
1.1.2.9.2.4  paf       103: //}@
1.1.2.9.2.3  paf       104: 
                    105: #define UseGC ((int)1)
                    106: #define PointerFreeGC (true)
                    107: 
1.1.2.9.2.14  paf       108: //{@ Array-oriented
1.1.2.9.2.15  paf       109: inline void *operator new[] (size_t size, int mode) { // UseGC
1.1.2.9.2.3  paf       110:        return pa_malloc(size);
                    111: }
                    112: inline void *operator new[] (size_t size, bool) { // PointerFreeGC
1.1.2.9.2.1  paf       113:        return pa_malloc_atomic(size);
1.1.2.1   paf       114: }
                    115: inline void operator delete[] (void *ptr) {
                    116:        pa_free(ptr);
                    117: }
1.1.2.9.2.14  paf       118: //}@
                    119: 
                    120: //{@ Structure-oriented
                    121: inline void *operator new (size_t size, int) { // UseGC
                    122:        return pa_malloc(size);
                    123: }
                    124: inline void *operator new (size_t size, bool) { // PointerFreeGC
                    125:        return pa_malloc_atomic(size);
                    126: }
                    127: inline void operator delete(void *ptr) {
                    128:        pa_free(ptr);
                    129: }
                    130: //}@
1.1.2.1   paf       131: 
1.1.2.9.2.1  paf       132: /// memory allocation/dallocation goes via pa_malloc/pa_free.
1.1.2.1   paf       133: class PA_Allocated {
                    134: public:
                    135:        /// the sole: instances allocated using our functions
                    136:        static void *operator new(size_t size) { 
                    137:                return pa_malloc(size);
                    138:        }
                    139:        static void operator delete(void *ptr) {
                    140:                pa_free(ptr);
                    141:        }
                    142:        static void *malloc(size_t size) {
                    143:                return pa_malloc(size);
                    144:        }
1.1.2.9.2.1  paf       145:        static void *malloc_atomic(size_t size) {
                    146:                return pa_malloc_atomic(size);
1.1.2.9.2.8  paf       147:        }
1.1.2.9.2.18  paf       148:        static char *strdup(const char* auto_variable_never_null, size_t helper_length=0) {
                    149:                return pa_strdup(auto_variable_never_null, helper_length);
1.1.2.1   paf       150:        }
                    151:        static void free(void *ptr) {
                    152:                pa_free(ptr);
                    153:        }
                    154:        static void *realloc(void *ptr, size_t size) {
                    155:                return pa_realloc(ptr, size);
                    156:        }
1.1.2.9.2.2  paf       157: 
                    158: private: // disabled from accidental use
                    159: 
1.1.2.9.2.9  paf       160:        /// use malloc/malloc_atomic instead [GC clears result of those]
1.1.2.9.2.2  paf       161:        static void *calloc(size_t size);
1.1.2.9.2.13  paf       162: 
                    163: };
                    164: 
                    165: /// Those who want their destructor called during finalization, must derive from this class [also]
                    166: class PA_Cleaned {
1.1.2.9.2.15  paf       167: #ifndef PA_DEBUG_DISABLE_GC
1.1.2.9.2.13  paf       168:        static void cleanup( void* obj, void* displ ) {
                    169:            ((PA_Cleaned*) ((char*) obj + (ptrdiff_t) displ))->~PA_Cleaned();
                    170:        }
                    171: 
                    172: public:
                    173:        PA_Cleaned() {
                    174:                GC_finalization_proc oldProc;
                    175:                void* oldData;
                    176:                void* base = GC_base( (void *) this );
                    177:                if (0 != base)  {
                    178:                        // Don't call the debug version, since this is a real base address.
                    179:                        GC_register_finalizer_ignore_self( 
                    180:                                base, (GC_finalization_proc)cleanup, (void*) ((char*) this - (char*) base), 
                    181:                                &oldProc, &oldData );
                    182:                        if (0 != oldProc) {
                    183:                                GC_register_finalizer_ignore_self( base, oldProc, oldData, 0, 0 );
                    184:                        }
                    185:                }
                    186:        }
                    187: 
                    188:        virtual ~PA_Cleaned() {
                    189:            GC_REGISTER_FINALIZER_IGNORE_SELF( GC_base(this), 0, 0, 0, 0 );
                    190:        }
1.1.2.9.2.15  paf       191: #endif
1.1.2.1   paf       192: };
                    193: 
1.1.2.9.2.1  paf       194: /// Base for all Parser classes
                    195: typedef PA_Allocated PA_Object;
1.1.2.1   paf       196: 
1.1.2.9.2.6  paf       197: // defines
                    198: 
                    199: #define override
                    200: #define rethrow throw
1.1.2.9.2.10  paf       201: 
1.1.2.1   paf       202: 
                    203: #endif

E-mail: