as_memory.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #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. int asSetGlobalMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc)
  56. {
  57. userAlloc = allocFunc;
  58. userFree = freeFunc;
  59. return 0;
  60. }
  61. int asResetGlobalMemoryFunctions()
  62. {
  63. asThreadCleanup();
  64. userAlloc = malloc;
  65. userFree = free;
  66. return 0;
  67. }
  68. } // extern "C"
  69. asCMemoryMgr::asCMemoryMgr()
  70. {
  71. }
  72. asCMemoryMgr::~asCMemoryMgr()
  73. {
  74. FreeUnusedMemory();
  75. }
  76. void asCMemoryMgr::FreeUnusedMemory()
  77. {
  78. // It's necessary to protect the scriptNodePool from multiple
  79. // simultaneous accesses, as the parser is used by several methods
  80. // that can be executed simultaneously.
  81. ENTERCRITICALSECTION(cs);
  82. int n;
  83. for( n = 0; n < (signed)scriptNodePool.GetLength(); n++ )
  84. userFree(scriptNodePool[n]);
  85. scriptNodePool.Allocate(0, false);
  86. LEAVECRITICALSECTION(cs);
  87. // The engine already protects against multiple threads
  88. // compiling scripts simultaneously so this pool doesn't have
  89. // to be protected again.
  90. for( n = 0; n < (signed)byteInstructionPool.GetLength(); n++ )
  91. userFree(byteInstructionPool[n]);
  92. byteInstructionPool.Allocate(0, false);
  93. }
  94. void *asCMemoryMgr::AllocScriptNode()
  95. {
  96. ENTERCRITICALSECTION(cs);
  97. if( scriptNodePool.GetLength() )
  98. {
  99. void *tRet = scriptNodePool.PopLast();
  100. LEAVECRITICALSECTION(cs);
  101. return tRet;
  102. }
  103. LEAVECRITICALSECTION(cs);
  104. #if defined(AS_DEBUG)
  105. return ((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(asCScriptNode), __FILE__, __LINE__);
  106. #else
  107. return userAlloc(sizeof(asCScriptNode));
  108. #endif
  109. }
  110. void asCMemoryMgr::FreeScriptNode(void *ptr)
  111. {
  112. ENTERCRITICALSECTION(cs);
  113. // Pre allocate memory for the array to avoid slow growth
  114. if( scriptNodePool.GetLength() == 0 )
  115. scriptNodePool.Allocate(100, 0);
  116. scriptNodePool.PushLast(ptr);
  117. LEAVECRITICALSECTION(cs);
  118. }
  119. #ifndef AS_NO_COMPILER
  120. void *asCMemoryMgr::AllocByteInstruction()
  121. {
  122. if( byteInstructionPool.GetLength() )
  123. return byteInstructionPool.PopLast();
  124. #if defined(AS_DEBUG)
  125. return ((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(asCByteInstruction), __FILE__, __LINE__);
  126. #else
  127. return userAlloc(sizeof(asCByteInstruction));
  128. #endif
  129. }
  130. void asCMemoryMgr::FreeByteInstruction(void *ptr)
  131. {
  132. // Pre allocate memory for the array to avoid slow growth
  133. if( byteInstructionPool.GetLength() == 0 )
  134. byteInstructionPool.Allocate(100, 0);
  135. byteInstructionPool.PushLast(ptr);
  136. }
  137. #endif // AS_NO_COMPILER
  138. END_AS_NAMESPACE