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

1.2       paf         1: /** @file
                      2:        Parser: memory reference counting classes decls.
                      3: 
1.30      moko        4:        Copyright (c) 2001-2017 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.36    ! moko       12: #define IDENT_PA_MEMORY_H "$Id: pa_memory.h,v 1.35 2017/12/03 23:36:23 moko Exp $"
1.2       paf        13: 
                     14: // include
                     15: 
                     16: #include "pa_config_includes.h"
1.24      moko       17: #include <new>
1.2       paf        18: 
1.19      moko       19: // define destructors use for Array, Hash and VMethodFrame
1.17      misha      20: #define USE_DESTRUCTORS
1.35      moko       21: 
1.36    ! moko       22: // std::basic_stringstream used in ^table.csv-string[] is compatible with delete usage check only under Debian 9/10, FreeBSD 12
1.33      moko       23: // #define CHECK_DELETE_USAGE
1.17      misha      24: 
1.2       paf        25: // forwards
                     26: 
                     27: void *pa_fail_alloc(const char* what, size_t size);
                     28: 
                     29: // inlines
                     30: 
                     31: inline void *pa_malloc(size_t size) {
1.35      moko       32:        if(void *result=GC_MALLOC(size))
1.2       paf        33:                return result;
                     34: 
                     35:        return pa_fail_alloc("allocate", size);
                     36: }
                     37: 
                     38: inline void *pa_malloc_atomic(size_t size) {
1.35      moko       39:        if(void *result=GC_MALLOC_ATOMIC(size))
1.2       paf        40:                return result;
                     41: 
                     42:        return pa_fail_alloc("allocate clean", size);
                     43: }
1.20      moko       44: 
1.2       paf        45: /// @a length may be null, which mean "autocalc it"
                     46: inline char *pa_strdup(const char* auto_variable_never_null, size_t helper_length=0) {
1.20      moko       47:        size_t known_length= helper_length ? helper_length : strlen(auto_variable_never_null);
1.2       paf        48: 
1.10      paf        49:        size_t size=known_length+1;
1.35      moko       50:        if(char *result=static_cast<char*>(GC_MALLOC_ATOMIC(size))) {
1.9       paf        51:                memcpy(result, auto_variable_never_null, known_length);
1.2       paf        52:                result[known_length]=0;
                     53:                return result;
                     54:        }
                     55: 
                     56:        return static_cast<char*>(pa_fail_alloc("allocate clean", size));
                     57: }
                     58: 
                     59: inline void pa_free(void *ptr) {
1.35      moko       60:        GC_FREE(ptr);
1.2       paf        61: }
                     62: 
                     63: inline void *pa_realloc(void *ptr, size_t size) {
1.35      moko       64:        if(void *result=GC_REALLOC(ptr, size))
1.2       paf        65:                return result;
                     66: 
                     67:        return pa_fail_alloc("reallocate to", size);
                     68: }
                     69: 
1.32      moko       70: /// memory allocation/dallocation goes via pa_malloc/pa_free.
                     71: class PA_Allocated {
                     72: public:
                     73:        /// the sole: instances allocated using our functions
                     74:        static void *operator new(size_t size) {
                     75:                return pa_malloc(size);
                     76:        }
                     77:        static void operator delete(void *ptr) {
                     78:                pa_free(ptr);
                     79:        }
                     80:        static void *operator new[](size_t size) {
                     81:                return pa_malloc(size);
                     82:        }
                     83:        static void operator delete[](void *ptr) {
                     84:                pa_free(ptr);
                     85:        }
                     86: };
                     87: 
                     88: // new(PointerFreeGC)/new(PointerGC) should be used to allocate types not inherited from PA_Allocated
                     89: 
                     90: #define PointerFreeGC (true)
                     91: #define PointerGC (false)
                     92: 
                     93: inline void *operator new[] (size_t size, bool pointer_free) {
                     94:        return pointer_free ? pa_malloc_atomic(size) : pa_malloc(size);
                     95: }
                     96: 
                     97: inline void *operator new (size_t size, bool pointer_free) {
                     98:        return pointer_free ? pa_malloc_atomic(size) : pa_malloc(size);
                     99: }
                    100: 
                    101: /// Base for all Parser classes
                    102: typedef PA_Allocated PA_Object;
                    103: 
                    104: // defines
                    105: 
                    106: #define override
                    107: #define rethrow throw
                    108: 
1.31      moko      109: #if defined(_MSC_VER) || (__cplusplus>=201103L)
1.26      moko      110: #define PA_THROW(what)
                    111: #else
                    112: #define PA_THROW(what) throw(what)
                    113: #endif
                    114: 
1.32      moko      115: #ifndef _MSC_VER
                    116: // regular new/delete are disabled from accidental use
                    117: 
                    118: void *new_disabled();
                    119: void delete_disabled();
                    120: 
1.34      moko      121: inline void *operator new[] (std::size_t) PA_THROW(std::bad_alloc){ return new_disabled(); }
                    122: inline void operator delete[](void *) throw(){ delete_disabled(); }
1.2       paf       123: 
1.34      moko      124: inline void *operator new(std::size_t) PA_THROW(std::bad_alloc){ return new_disabled(); }
1.33      moko      125: #ifdef CHECK_DELETE_USAGE
1.34      moko      126: inline void operator delete(void *) throw(){ delete_disabled(); }
1.33      moko      127: #endif
1.2       paf       128: 
1.35      moko      129: #ifndef PA_DEBUG_DISABLE_GC
1.32      moko      130: // other regular allocators as disabled from accidental use as well
1.21      moko      131: 
                    132: void *calloc_disabled();
                    133: void *malloc_disabled();
                    134: void *realloc_disabled();
                    135: void free_disabled();
                    136: char *strdup_disabled();
                    137: 
1.24      moko      138: inline void *calloc(size_t) { return calloc_disabled(); }
1.25      moko      139: inline void *malloc(size_t) { return malloc_disabled(); }
                    140: inline void *realloc(void *, size_t) { return realloc_disabled(); }
                    141: inline void free(void *) { free_disabled(); }
1.21      moko      142: inline char *strdup(const char*, size_t){ return strdup_disabled(); }
1.35      moko      143: #endif // PA_DEBUG_DISABLE_GC
1.32      moko      144: 
1.35      moko      145: #endif // _MSC_VER
1.21      moko      146: 
1.2       paf       147: #endif

E-mail: