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

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

E-mail: