Annotation of parser3/src/include/pa_memory.h, revision 1.32
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.32 ! moko 12: #define IDENT_PA_MEMORY_H "$Id: pa_memory.h,v 1.31 2017/02/08 13:05:46 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.32 ! moko 73: /// memory allocation/dallocation goes via pa_malloc/pa_free.
! 74: class PA_Allocated {
! 75: public:
! 76: /// the sole: instances allocated using our functions
! 77: static void *operator new(size_t size) {
! 78: return pa_malloc(size);
! 79: }
! 80: static void operator delete(void *ptr) {
! 81: pa_free(ptr);
! 82: }
! 83: static void *operator new[](size_t size) {
! 84: return pa_malloc(size);
! 85: }
! 86: static void operator delete[](void *ptr) {
! 87: pa_free(ptr);
! 88: }
! 89: };
! 90:
! 91: // new(PointerFreeGC)/new(PointerGC) should be used to allocate types not inherited from PA_Allocated
! 92:
! 93: #define PointerFreeGC (true)
! 94: #define PointerGC (false)
! 95:
! 96: inline void *operator new[] (size_t size, bool pointer_free) {
! 97: return pointer_free ? pa_malloc_atomic(size) : pa_malloc(size);
! 98: }
! 99:
! 100: inline void *operator new (size_t size, bool pointer_free) {
! 101: return pointer_free ? pa_malloc_atomic(size) : pa_malloc(size);
! 102: }
! 103:
! 104: /// Base for all Parser classes
! 105: typedef PA_Allocated PA_Object;
! 106:
! 107: // defines
! 108:
! 109: #define override
! 110: #define rethrow throw
! 111:
1.31 moko 112: #if defined(_MSC_VER) || (__cplusplus>=201103L)
1.26 moko 113: #define PA_THROW(what)
114: #else
115: #define PA_THROW(what) throw(what)
116: #endif
117:
1.32 ! moko 118: #ifndef _MSC_VER
! 119:
! 120: // regular new/delete are disabled from accidental use
! 121:
! 122: void *new_disabled();
! 123: void delete_disabled();
! 124:
! 125: inline void *operator new[] (std::size_t size) PA_THROW(std::bad_alloc){ return new_disabled(); }
! 126: inline void operator delete[](void *ptr) throw(){ delete_disabled(); }
1.2 paf 127:
1.32 ! moko 128: inline void *operator new(std::size_t size) PA_THROW(std::bad_alloc){ return new_disabled(); }
! 129: inline void operator delete(void *ptr) throw(){ delete_disabled(); }
1.2 paf 130:
1.32 ! moko 131: // other regular allocators as disabled from accidental use as well
1.21 moko 132:
133: void *calloc_disabled();
134: void *malloc_disabled();
135: void *realloc_disabled();
136: void free_disabled();
137: char *strdup_disabled();
138:
1.24 moko 139: inline void *calloc(size_t) { return calloc_disabled(); }
1.25 moko 140: inline void *malloc(size_t) { return malloc_disabled(); }
141: inline void *realloc(void *, size_t) { return realloc_disabled(); }
142: inline void free(void *) { free_disabled(); }
1.21 moko 143: inline char *strdup(const char*, size_t){ return strdup_disabled(); }
1.32 ! moko 144:
1.22 moko 145: #endif
1.21 moko 146:
1.2 paf 147: #endif
E-mail: