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

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.10! paf        12: static const char* IDENT_MEMORY_H="$Date: 2003/03/24 09:39:55 $";
1.1.2.9   paf        13: 
                     14: #define PA_DEBUG_REFERENCES
1.1.2.1   paf        15: 
                     16: // include
                     17: 
                     18: #include "pa_config_includes.h"
1.1.2.9.2.1  paf        19: #include "gc.h"
                     20: 
1.1.2.1   paf        21: 
                     22: #ifdef XML
                     23: #      include "gdome.h"
                     24: // for xmlChar
                     25: #      include "libxml/tree.h"
                     26: #endif
                     27: 
                     28: // forwards
                     29: 
1.1.2.9.2.5  paf        30: void *pa_fail_alloc(const char* what, size_t size);
1.1.2.2   paf        31: 
                     32: // inlines
                     33: 
                     34: inline void *pa_malloc(size_t size) {
1.1.2.9.2.1  paf        35:        if(void *result=GC_MALLOC(size))
1.1.2.2   paf        36:                return result;
                     37: 
                     38:        return pa_fail_alloc("allocate", size);
                     39: }
                     40: 
1.1.2.9.2.1  paf        41: inline void *pa_malloc_atomic(size_t size) {
                     42:        if(void *result=GC_MALLOC_ATOMIC(size))
1.1.2.2   paf        43:                return result;
                     44: 
                     45:        return pa_fail_alloc("allocate clean", size);
                     46: }
1.1.2.9.2.4  paf        47: /// @a length may be null, which mean "autocalc it"
1.1.2.9.2.5  paf        48: inline char *pa_strdup(const char* auto_variable_never_null, size_t length=0) {
1.1.2.9.2.4  paf        49:        if(!length)
                     50:                length=strlen(auto_variable_never_null);
1.1.2.9.2.7  paf        51:        if(!length)
                     52:                abort();
                     53: 
                     54:        size_t size=length+1;
1.1.2.9.2.4  paf        55:        if(char *result=static_cast<char*>(GC_MALLOC_ATOMIC(size))) {
1.1.2.9.2.7  paf        56:                memcpy(result, auto_variable_never_null, length);
                     57:                result[length]=0;
1.1.2.9.2.4  paf        58:                return result;
                     59:        }
                     60: 
                     61:        return static_cast<char*>(pa_fail_alloc("allocate clean", size));
                     62: }
1.1.2.9.2.10! paf        63: char* pa_format_integer(int value);
        !            64: 
1.1.2.2   paf        65: inline void pa_free(void *ptr) {
1.1.2.9.2.1  paf        66:        GC_FREE(ptr);
1.1.2.2   paf        67: }
                     68: 
                     69: inline void *pa_realloc(void *ptr, size_t size) {
1.1.2.9.2.1  paf        70:        if(void *result=GC_REALLOC(ptr, size))
1.1.2.2   paf        71:                return result;
                     72: 
                     73:        return pa_fail_alloc("reallocate to", size);
                     74: }
1.1.2.1   paf        75: 
1.1.2.9.2.4  paf        76: //{@ these operators are disabled, one should explicitely specify either new(UseGC) or new(PointerFreeGC)
1.1.2.9.2.6  paf        77: inline void *operator new(size_t size) { abort(); } // disabled
                     78: inline void operator delete (void *ptr) { abort(); } // disabled
                     79: inline void *operator new[] (size_t size) { abort(); } // disabled
1.1.2.9.2.4  paf        80: //}@
1.1.2.9.2.3  paf        81: 
                     82: #define UseGC ((int)1)
                     83: #define PointerFreeGC (true)
                     84: 
                     85: inline void *operator new[] (size_t size, int) { // UseGC
                     86:        return pa_malloc(size);
                     87: }
                     88: inline void *operator new[] (size_t size, bool) { // PointerFreeGC
1.1.2.9.2.1  paf        89:        return pa_malloc_atomic(size);
1.1.2.1   paf        90: }
                     91: inline void operator delete[] (void *ptr) {
                     92:        pa_free(ptr);
                     93: }
                     94: 
1.1.2.9.2.1  paf        95: /// memory allocation/dallocation goes via pa_malloc/pa_free.
1.1.2.1   paf        96: class PA_Allocated {
                     97: public:
                     98:        /// the sole: instances allocated using our functions
                     99:        static void *operator new(size_t size) { 
                    100:                return pa_malloc(size);
                    101:        }
                    102:        static void operator delete(void *ptr) {
                    103:                pa_free(ptr);
                    104:        }
                    105:        static void *malloc(size_t size) {
                    106:                return pa_malloc(size);
                    107:        }
1.1.2.9.2.1  paf       108:        static void *malloc_atomic(size_t size) {
                    109:                return pa_malloc_atomic(size);
1.1.2.9.2.8  paf       110:        }
                    111:        static char *strdup(const char* auto_variable_never_null, size_t length=0) {
                    112:                return pa_strdup(auto_variable_never_null, length);
1.1.2.1   paf       113:        }
                    114:        static void free(void *ptr) {
                    115:                pa_free(ptr);
                    116:        }
                    117:        static void *realloc(void *ptr, size_t size) {
                    118:                return pa_realloc(ptr, size);
                    119:        }
1.1.2.9.2.2  paf       120: 
                    121: private: // disabled from accidental use
                    122: 
1.1.2.9.2.9  paf       123:        /// use malloc/malloc_atomic instead [GC clears result of those]
1.1.2.9.2.2  paf       124:        static void *calloc(size_t size);
1.1.2.1   paf       125: 
                    126: };
                    127: 
1.1.2.9.2.1  paf       128: /// Base for all Parser classes
                    129: typedef PA_Allocated PA_Object;
1.1.2.1   paf       130: 
1.1.2.9.2.6  paf       131: // defines
                    132: 
                    133: #define override
                    134: #define rethrow throw
1.1.2.9.2.10! paf       135: 
1.1.2.1   paf       136: 
                    137: #endif

E-mail: