Annotation of parser3/src/include/pa_memory.h, revision 1.41
1.2 paf 1: /** @file
2: Parser: memory reference counting classes decls.
3:
1.41 ! moko 4: Copyright (c) 2001-2020 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.41 ! moko 12: #define IDENT_PA_MEMORY_H "$Id: pa_memory.h,v 1.40 2020/12/15 13:33:26 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.38 moko 115: #if !defined(_MSC_VER) && !defined(FREEBSD1X)
1.32 moko 116: // regular new/delete are disabled from accidental use
1.38 moko 117: // no checks for FreeBSD1X.X due to https://bugs.llvm.org/show_bug.cgi?id=40161 bug
1.32 moko 118:
119: void *new_disabled();
120: void delete_disabled();
121:
1.34 moko 122: inline void *operator new[] (std::size_t) PA_THROW(std::bad_alloc){ return new_disabled(); }
123: inline void operator delete[](void *) throw(){ delete_disabled(); }
1.37 moko 124: inline void operator delete[](void *, std::size_t) throw(){ delete_disabled(); }
1.2 paf 125:
1.34 moko 126: inline void *operator new(std::size_t) PA_THROW(std::bad_alloc){ return new_disabled(); }
1.33 moko 127: #ifdef CHECK_DELETE_USAGE
1.34 moko 128: inline void operator delete(void *) throw(){ delete_disabled(); }
1.33 moko 129: #endif
1.2 paf 130:
1.35 moko 131: #ifndef PA_DEBUG_DISABLE_GC
1.32 moko 132: // other regular allocators as disabled from accidental use as well
1.21 moko 133:
134: void *calloc_disabled();
135: void *malloc_disabled();
136: void *realloc_disabled();
137: void free_disabled();
138: char *strdup_disabled();
139:
1.24 moko 140: inline void *calloc(size_t) { return calloc_disabled(); }
1.25 moko 141: inline void *malloc(size_t) { return malloc_disabled(); }
142: inline void *realloc(void *, size_t) { return realloc_disabled(); }
143: inline void free(void *) { free_disabled(); }
1.21 moko 144: inline char *strdup(const char*, size_t){ return strdup_disabled(); }
1.35 moko 145: #endif // PA_DEBUG_DISABLE_GC
1.32 moko 146:
1.35 moko 147: #endif // _MSC_VER
1.21 moko 148:
1.39 moko 149: #ifdef PA_DEBUG_DISABLE_GC
1.40 moko 150: #define PA_GC_GCOLLECT
1.39 moko 151: #else
1.40 moko 152: #define PA_GC_GCOLLECT { GC_enable(); GC_gcollect(); GC_disable(); }
1.39 moko 153: #endif
154:
1.2 paf 155: #endif
E-mail: