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

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

E-mail: