as_memory.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2012 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. #include "as_config.h"
  31. #include "as_memory.h"
  32. #include "as_scriptnode.h"
  33. #include "as_bytecode.h"
  34. BEGIN_AS_NAMESPACE
  35. // By default we'll use the standard memory management functions
  36. // Make sure these globals are initialized first. Otherwise the
  37. // library may crash in case the application initializes the engine
  38. // as a global variable.
  39. #ifdef _MSC_VER
  40. // MSVC let's us choose between a couple of different initialization orders.
  41. #pragma warning(disable: 4073)
  42. #pragma init_seg(lib)
  43. asALLOCFUNC_t userAlloc = malloc;
  44. asFREEFUNC_t userFree = free;
  45. #else
  46. // Other compilers will just have to rely on luck.
  47. asALLOCFUNC_t userAlloc = malloc;
  48. asFREEFUNC_t userFree = free;
  49. #endif
  50. extern "C"
  51. {
  52. int asSetGlobalMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc)
  53. {
  54. userAlloc = allocFunc;
  55. userFree = freeFunc;
  56. return 0;
  57. }
  58. int asResetGlobalMemoryFunctions()
  59. {
  60. asThreadCleanup();
  61. userAlloc = malloc;
  62. userFree = free;
  63. return 0;
  64. }
  65. } // extern "C"
  66. asCMemoryMgr::asCMemoryMgr()
  67. {
  68. }
  69. asCMemoryMgr::~asCMemoryMgr()
  70. {
  71. FreeUnusedMemory();
  72. }
  73. void asCMemoryMgr::FreeUnusedMemory()
  74. {
  75. // It's necessary to protect the scriptNodePool from multiple
  76. // simultaneous accesses, as the parser is used by several methods
  77. // that can be executed simultaneously.
  78. ENTERCRITICALSECTION(cs);
  79. int n;
  80. for( n = 0; n < (signed)scriptNodePool.GetLength(); n++ )
  81. userFree(scriptNodePool[n]);
  82. scriptNodePool.Allocate(0, false);
  83. LEAVECRITICALSECTION(cs);
  84. // The engine already protects against multiple threads
  85. // compiling scripts simultaneously so this pool doesn't have
  86. // to be protected again.
  87. for( n = 0; n < (signed)byteInstructionPool.GetLength(); n++ )
  88. userFree(byteInstructionPool[n]);
  89. byteInstructionPool.Allocate(0, false);
  90. }
  91. void *asCMemoryMgr::AllocScriptNode()
  92. {
  93. ENTERCRITICALSECTION(cs);
  94. if( scriptNodePool.GetLength() )
  95. {
  96. void *tRet = scriptNodePool.PopLast();
  97. LEAVECRITICALSECTION(cs);
  98. return tRet;
  99. }
  100. LEAVECRITICALSECTION(cs);
  101. #if defined(AS_DEBUG)
  102. return ((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(asCScriptNode), __FILE__, __LINE__);
  103. #else
  104. return userAlloc(sizeof(asCScriptNode));
  105. #endif
  106. }
  107. void asCMemoryMgr::FreeScriptNode(void *ptr)
  108. {
  109. ENTERCRITICALSECTION(cs);
  110. // Pre allocate memory for the array to avoid slow growth
  111. if( scriptNodePool.GetLength() == 0 )
  112. scriptNodePool.Allocate(100, 0);
  113. scriptNodePool.PushLast(ptr);
  114. LEAVECRITICALSECTION(cs);
  115. }
  116. #ifndef AS_NO_COMPILER
  117. void *asCMemoryMgr::AllocByteInstruction()
  118. {
  119. if( byteInstructionPool.GetLength() )
  120. return byteInstructionPool.PopLast();
  121. #if defined(AS_DEBUG)
  122. return ((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(cByteInstruction), __FILE__, __LINE__);
  123. #else
  124. return userAlloc(sizeof(cByteInstruction));
  125. #endif
  126. }
  127. void asCMemoryMgr::FreeByteInstruction(void *ptr)
  128. {
  129. // Pre allocate memory for the array to avoid slow growth
  130. if( byteInstructionPool.GetLength() == 0 )
  131. byteInstructionPool.Allocate(100, 0);
  132. byteInstructionPool.PushLast(ptr);
  133. }
  134. #endif // AS_NO_COMPILER
  135. END_AS_NAMESPACE