Annotation of parser3/src/include/pa_memory.h, revision 1.42
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.42 ! moko 12: #define IDENT_PA_MEMORY_H "$Id: pa_memory.h,v 1.41 2020/12/15 17:10:31 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.42 ! moko 45: /// length may be zero, and this is normal
! 46: inline char *pa_strdup(const char* auto_variable_never_null, size_t known_length) {
! 47: if(char *result=static_cast<char*>(GC_MALLOC_ATOMIC(known_length+1))) {
1.9 paf 48: memcpy(result, auto_variable_never_null, known_length);
1.2 paf 49: result[known_length]=0;
50: return result;
51: }
52:
1.42 ! moko 53: return static_cast<char*>(pa_fail_alloc("allocate clean", known_length+1));
! 54: }
! 55:
! 56: inline char *pa_strdup(const char* auto_variable_never_null) {
! 57: return pa_strdup(auto_variable_never_null, strlen(auto_variable_never_null));
1.2 paf 58: }
59:
60: inline void pa_free(void *ptr) {
1.35 moko 61: GC_FREE(ptr);
1.2 paf 62: }
63:
64: inline void *pa_realloc(void *ptr, size_t size) {
1.35 moko 65: if(void *result=GC_REALLOC(ptr, size))
1.2 paf 66: return result;
67:
68: return pa_fail_alloc("reallocate to", size);
69: }
70:
1.32 moko 71: /// memory allocation/dallocation goes via pa_malloc/pa_free.
72: class PA_Allocated {
73: public:
74: /// the sole: instances allocated using our functions
75: static void *operator new(size_t size) {
76: return pa_malloc(size);
77: }
78: static void operator delete(void *ptr) {
79: pa_free(ptr);
80: }
81: static void *operator new[](size_t size) {
82: return pa_malloc(size);
83: }
84: static void operator delete[](void *ptr) {
85: pa_free(ptr);
86: }
87: };
88:
89: // new(PointerFreeGC)/new(PointerGC) should be used to allocate types not inherited from PA_Allocated
90:
91: #define PointerFreeGC (true)
92: #define PointerGC (false)
93:
94: inline void *operator new[] (size_t size, bool pointer_free) {
95: return pointer_free ? pa_malloc_atomic(size) : pa_malloc(size);
96: }
97:
98: inline void *operator new (size_t size, bool pointer_free) {
99: return pointer_free ? pa_malloc_atomic(size) : pa_malloc(size);
100: }
101:
102: /// Base for all Parser classes
103: typedef PA_Allocated PA_Object;
104:
105: // defines
106:
107: #define override
108: #define rethrow throw
109:
1.31 moko 110: #if defined(_MSC_VER) || (__cplusplus>=201103L)
1.26 moko 111: #define PA_THROW(what)
112: #else
113: #define PA_THROW(what) throw(what)
114: #endif
115:
1.38 moko 116: #if !defined(_MSC_VER) && !defined(FREEBSD1X)
1.32 moko 117: // regular new/delete are disabled from accidental use
1.38 moko 118: // no checks for FreeBSD1X.X due to https://bugs.llvm.org/show_bug.cgi?id=40161 bug
1.32 moko 119:
120: void *new_disabled();
121: void delete_disabled();
122:
1.34 moko 123: inline void *operator new[] (std::size_t) PA_THROW(std::bad_alloc){ return new_disabled(); }
124: inline void operator delete[](void *) throw(){ delete_disabled(); }
1.37 moko 125: inline void operator delete[](void *, std::size_t) throw(){ delete_disabled(); }
1.2 paf 126:
1.34 moko 127: inline void *operator new(std::size_t) PA_THROW(std::bad_alloc){ return new_disabled(); }
1.33 moko 128: #ifdef CHECK_DELETE_USAGE
1.34 moko 129: inline void operator delete(void *) throw(){ delete_disabled(); }
1.33 moko 130: #endif
1.2 paf 131:
1.35 moko 132: #ifndef PA_DEBUG_DISABLE_GC
1.32 moko 133: // other regular allocators as disabled from accidental use as well
1.21 moko 134:
135: void *calloc_disabled();
136: void *malloc_disabled();
137: void *realloc_disabled();
138: void free_disabled();
139: char *strdup_disabled();
140:
1.24 moko 141: inline void *calloc(size_t) { return calloc_disabled(); }
1.25 moko 142: inline void *malloc(size_t) { return malloc_disabled(); }
143: inline void *realloc(void *, size_t) { return realloc_disabled(); }
144: inline void free(void *) { free_disabled(); }
1.21 moko 145: inline char *strdup(const char*, size_t){ return strdup_disabled(); }
1.35 moko 146: #endif // PA_DEBUG_DISABLE_GC
1.32 moko 147:
1.35 moko 148: #endif // _MSC_VER
1.21 moko 149:
1.39 moko 150: #ifdef PA_DEBUG_DISABLE_GC
1.40 moko 151: #define PA_GC_GCOLLECT
1.39 moko 152: #else
1.40 moko 153: #define PA_GC_GCOLLECT { GC_enable(); GC_gcollect(); GC_disable(); }
1.39 moko 154: #endif
155:
1.2 paf 156: #endif
E-mail: