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

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

E-mail: