as_memory.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2014 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_memory.cpp
  25. //
  26. // Overload the default memory management functions so that we
  27. // can let the application decide how to do it.
  28. //
  29. #include <stdlib.h>
  30. #if !defined(__APPLE__) && !defined( __SNC__ ) && !defined( __ghs__ ) && !defined(__FreeBSD__)
  31. #include <malloc.h>
  32. #endif
  33. #include "as_config.h"
  34. #include "as_memory.h"
  35. #include "as_scriptnode.h"
  36. #include "as_bytecode.h"
  37. BEGIN_AS_NAMESPACE
  38. // By default we'll use the standard memory management functions
  39. // Make sure these globals are initialized first. Otherwise the
  40. // library may crash in case the application initializes the engine
  41. // as a global variable.
  42. #ifdef _MSC_VER
  43. // MSVC let's us choose between a couple of different initialization orders.
  44. #pragma warning(disable: 4073)
  45. #pragma init_seg(lib)
  46. asALLOCFUNC_t userAlloc = malloc;
  47. asFREEFUNC_t userFree = free;
  48. #else
  49. // Other compilers will just have to rely on luck.
  50. asALLOCFUNC_t userAlloc = malloc;
  51. asFREEFUNC_t userFree = free;
  52. #endif
  53. extern "C"
  54. {
  55. // interface
  56. int asSetGlobalMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc)
  57. {
  58. userAlloc = allocFunc;
  59. userFree = freeFunc;
  60. return 0;
  61. }
  62. // interface
  63. int asResetGlobalMemoryFunctions()
  64. {
  65. asThreadCleanup();
  66. userAlloc = malloc;
  67. userFree = free;
  68. return 0;
  69. }
  70. // interface
  71. void *asAllocMem(size_t size)
  72. {
  73. return asNEWARRAY(asBYTE, size);
  74. }
  75. // interface
  76. void asFreeMem(void *mem)
  77. {
  78. asDELETEARRAY(mem);
  79. }
  80. } // extern "C"
  81. asCMemoryMgr::asCMemoryMgr()
  82. {
  83. }
  84. asCMemoryMgr::~asCMemoryMgr()
  85. {
  86. FreeUnusedMemory();
  87. }
  88. void asCMemoryMgr::FreeUnusedMemory()
  89. {
  90. // It's necessary to protect the scriptNodePool from multiple
  91. // simultaneous accesses, as the parser is used by several methods
  92. // that can be executed simultaneously.
  93. ENTERCRITICALSECTION(cs);
  94. int n;
  95. for( n = 0; n < (signed)scriptNodePool.GetLength(); n++ )
  96. userFree(scriptNodePool[n]);
  97. scriptNodePool.Allocate(0, false);
  98. LEAVECRITICALSECTION(cs);
  99. // The engine already protects against multiple threads
  100. // compiling scripts simultaneously so this pool doesn't have
  101. // to be protected again.
  102. for( n = 0; n < (signed)byteInstructionPool.GetLength(); n++ )
  103. userFree(byteInstructionPool[n]);
  104. byteInstructionPool.Allocate(0, false);
  105. }
  106. void *asCMemoryMgr::AllocScriptNode()
  107. {
  108. ENTERCRITICALSECTION(cs);
  109. if( scriptNodePool.GetLength() )
  110. {
  111. void *tRet = scriptNodePool.PopLast();
  112. LEAVECRITICALSECTION(cs);
  113. return tRet;
  114. }
  115. LEAVECRITICALSECTION(cs);
  116. #if defined(AS_DEBUG)
  117. return ((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(asCScriptNode), __FILE__, __LINE__);
  118. #else
  119. return userAlloc(sizeof(asCScriptNode));
  120. #endif
  121. }
  122. void asCMemoryMgr::FreeScriptNode(void *ptr)
  123. {
  124. ENTERCRITICALSECTION(cs);
  125. // Pre allocate memory for the array to avoid slow growth
  126. if( scriptNodePool.GetLength() == 0 )
  127. scriptNodePool.Allocate(100, 0);
  128. scriptNodePool.PushLast(ptr);
  129. LEAVECRITICALSECTION(cs);
  130. }
  131. #ifndef AS_NO_COMPILER
  132. void *asCMemoryMgr::AllocByteInstruction()
  133. {
  134. if( byteInstructionPool.GetLength() )
  135. return byteInstructionPool.PopLast();
  136. #if defined(AS_DEBUG)
  137. return ((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(asCByteInstruction), __FILE__, __LINE__);
  138. #else
  139. return userAlloc(sizeof(asCByteInstruction));
  140. #endif
  141. }
  142. void asCMemoryMgr::FreeByteInstruction(void *ptr)
  143. {
  144. // Pre allocate memory for the array to avoid slow growth
  145. if( byteInstructionPool.GetLength() == 0 )
  146. byteInstructionPool.Allocate(100, 0);
  147. byteInstructionPool.PushLast(ptr);
  148. }
  149. #endif // AS_NO_COMPILER
  150. END_AS_NAMESPACE