Annotation of parser3/src/include/pa_memory.h, revision 1.31
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.31 ! moko 12: #define IDENT_PA_MEMORY_H "$Id: pa_memory.h,v 1.30 2017/02/07 22:00:34 moko Exp $"
1.2 paf 13:
14: // include
15:
16: #include "pa_config_includes.h"
17: #include "gc.h"
1.24 moko 18: #include <new>
1.2 paf 19:
1.19 moko 20: // define destructors use for Array, Hash and VMethodFrame
1.17 misha 21: #define USE_DESTRUCTORS
22:
1.2 paf 23: inline void* pa_gc_malloc(size_t size) { return GC_MALLOC(size); }
24: inline void* pa_gc_malloc_atomic(size_t size) { return GC_MALLOC_ATOMIC(size); }
25: inline void* pa_gc_realloc(void* ptr, size_t size) { return GC_REALLOC(ptr, size); }
26: inline void pa_gc_free(void* ptr) { GC_FREE(ptr); }
27:
28: // forwards
29:
30: void *pa_fail_alloc(const char* what, size_t size);
31:
32: // inlines
33:
34: inline void *pa_malloc(size_t size) {
1.4 paf 35: if(void *result=pa_gc_malloc(size))
1.2 paf 36: return result;
37:
38: return pa_fail_alloc("allocate", size);
39: }
40:
41: inline void *pa_malloc_atomic(size_t size) {
1.4 paf 42: if(void *result=pa_gc_malloc_atomic(size))
1.2 paf 43: return result;
44:
45: return pa_fail_alloc("allocate clean", size);
46: }
1.20 moko 47:
1.2 paf 48: /// @a length may be null, which mean "autocalc it"
49: inline char *pa_strdup(const char* auto_variable_never_null, size_t helper_length=0) {
1.20 moko 50: size_t known_length= helper_length ? helper_length : strlen(auto_variable_never_null);
1.2 paf 51:
1.10 paf 52: size_t size=known_length+1;
53: if(char *result=static_cast<char*>(pa_gc_malloc_atomic(size))) {
1.9 paf 54: memcpy(result, auto_variable_never_null, known_length);
1.2 paf 55: result[known_length]=0;
56: return result;
57: }
58:
59: return static_cast<char*>(pa_fail_alloc("allocate clean", size));
60: }
61:
62: inline void pa_free(void *ptr) {
1.4 paf 63: pa_gc_free(ptr);
1.2 paf 64: }
65:
66: inline void *pa_realloc(void *ptr, size_t size) {
1.4 paf 67: if(void *result=pa_gc_realloc(ptr, size))
1.2 paf 68: return result;
69:
70: return pa_fail_alloc("reallocate to", size);
71: }
72:
1.31 ! moko 73: #if defined(_MSC_VER) || (__cplusplus>=201103L)
1.26 moko 74: #define PA_THROW(what)
75: #else
76: #define PA_THROW(what) throw(what)
77: #endif
78:
1.2 paf 79: #define PointerFreeGC (true)
80:
81: //{@ Array-oriented
1.29 moko 82: void *operator new[] (size_t size, bool); // PointerFreeGC
83: void *operator new[] (std::size_t size) PA_THROW(std::bad_alloc);
84: void operator delete[] (void *ptr) throw();
1.2 paf 85: //}@
86:
87: //{@ Structure-oriented
1.29 moko 88: void *operator new (size_t size, bool); // PointerFreeGC
89: void *operator new(std::size_t size) PA_THROW(std::bad_alloc);
90: void operator delete(void *ptr) throw();
1.2 paf 91: //}@
92:
1.23 moko 93: #ifndef _MSC_VER
1.21 moko 94: // disabled from accidental use
95:
96: void *calloc_disabled();
97: void *malloc_disabled();
98: void *realloc_disabled();
99: void free_disabled();
100: char *strdup_disabled();
101:
1.24 moko 102: inline void *calloc(size_t) { return calloc_disabled(); }
1.25 moko 103: inline void *malloc(size_t) { return malloc_disabled(); }
104: inline void *realloc(void *, size_t) { return realloc_disabled(); }
105: inline void free(void *) { free_disabled(); }
1.21 moko 106: inline char *strdup(const char*, size_t){ return strdup_disabled(); }
1.22 moko 107: #endif
1.21 moko 108:
1.2 paf 109: /// memory allocation/dallocation goes via pa_malloc/pa_free.
110: class PA_Allocated {
111: public:
112: /// the sole: instances allocated using our functions
113: static void *operator new(size_t size) {
114: return pa_malloc(size);
115: }
116: static void operator delete(void *ptr) {
117: pa_free(ptr);
118: }
119: };
120:
121: /// Base for all Parser classes
122: typedef PA_Allocated PA_Object;
123:
124: // defines
125:
126: #define override
127: #define rethrow throw
128:
129: #endif
E-mail: