1 /* 2 AngelCode Scripting Library 3 Copyright (c) 2003-2014 Andreas Jonsson 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any 7 damages arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any 10 purpose, including commercial applications, and to alter it and 11 redistribute it freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you 14 must not claim that you wrote the original software. If you use 15 this software in a product, an acknowledgment in the product 16 documentation would be appreciated but is not required. 17 18 2. Altered source versions must be plainly marked as such, and 19 must not be misrepresented as being the original software. 20 21 3. This notice may not be removed or altered from any source 22 distribution. 23 24 The original version of this library can be located at: 25 http://www.angelcode.com/angelscript/ 26 27 Andreas Jonsson 28 andreas@angelcode.com 29 */ 30 31 32 // 33 // as_memory.h 34 // 35 // Overload the default memory management functions so that we 36 // can let the application decide how to do it. 37 // 38 39 40 41 #ifndef AS_MEMORY_H 42 #define AS_MEMORY_H 43 44 #include "as_config.h" 45 46 BEGIN_AS_NAMESPACE 47 48 extern asALLOCFUNC_t userAlloc; 49 extern asFREEFUNC_t userFree; 50 51 #ifdef WIP_16BYTE_ALIGN 52 53 // TODO: This declaration should be in angelscript.h 54 // when the application can register it's own 55 // aligned memory routines 56 typedef void *(*asALLOCALIGNEDFUNC_t)(size_t, size_t); 57 typedef void (*asFREEALIGNEDFUNC_t)(void *); 58 extern asALLOCALIGNEDFUNC_t userAllocAligned; 59 extern asFREEALIGNEDFUNC_t userFreeAligned; 60 typedef void *(*asALLOCALIGNEDFUNCDEBUG_t)(size_t, size_t, const char *, unsigned int); 61 62 // The maximum type alignment supported. 63 const int MAX_TYPE_ALIGNMENT = 16; 64 65 // Utility function used for assertions. 66 bool isAligned(const void* const pointer, asUINT alignment); 67 68 #endif // WIP_16BYTE_ALIGN 69 70 // We don't overload the new operator as that would affect the application as well 71 72 #ifndef AS_DEBUG 73 74 #define asNEW(x) new(userAlloc(sizeof(x))) x 75 #define asDELETE(ptr,x) {void *tmp = ptr; (ptr)->~x(); userFree(tmp);} 76 77 #define asNEWARRAY(x,cnt) (x*)userAlloc(sizeof(x)*cnt) 78 #define asDELETEARRAY(ptr) userFree(ptr) 79 80 #ifdef WIP_16BYTE_ALIGN 81 #define asNEWARRAYALIGNED(x,cnt, alignment) (x*)userAllocAligned(sizeof(x)*cnt, alignment) 82 #define asDELETEARRAYALIGNED(ptr) userFreeAligned(ptr) 83 #endif 84 85 #else 86 87 typedef void *(*asALLOCFUNCDEBUG_t)(size_t, const char *, unsigned int); 88 89 #define asNEW(x) new(((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(x), __FILE__, __LINE__)) x 90 #define asDELETE(ptr,x) {void *tmp = ptr; (ptr)->~x(); userFree(tmp);} 91 92 #define asNEWARRAY(x,cnt) (x*)((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(x)*cnt, __FILE__, __LINE__) 93 #define asDELETEARRAY(ptr) userFree(ptr) 94 95 #ifdef WIP_16BYTE_ALIGN 96 //TODO: Equivalent of debug allocation function with alignment? 97 #define asNEWARRAYALIGNED(x,cnt, alignment) (x*)userAllocAligned(sizeof(x)*cnt, alignment) 98 #define asDELETEARRAYALIGNED(ptr) userFreeAligned(ptr) 99 #endif 100 101 #endif 102 103 END_AS_NAMESPACE 104 105 #include <new> 106 #include "as_criticalsection.h" 107 #include "as_array.h" 108 109 BEGIN_AS_NAMESPACE 110 111 class asCMemoryMgr 112 { 113 public: 114 asCMemoryMgr(); 115 ~asCMemoryMgr(); 116 117 void FreeUnusedMemory(); 118 119 void *AllocScriptNode(); 120 void FreeScriptNode(void *ptr); 121 122 #ifndef AS_NO_COMPILER 123 void *AllocByteInstruction(); 124 void FreeByteInstruction(void *ptr); 125 #endif 126 127 protected: 128 DECLARECRITICALSECTION(cs) 129 asCArray<void *> scriptNodePool; 130 asCArray<void *> byteInstructionPool; 131 }; 132 133 END_AS_NAMESPACE 134 135 #endif 136