/xbmc/visualizations/Vortex/angelscript/angelscript/source/as_memory.cpp

http://github.com/xbmc/xbmc · C++ · 163 lines · 84 code · 35 blank · 44 comment · 8 complexity · 77a8a3a978e2c462f3983dd6f07152bc MD5 · raw file

  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2009 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. andreas@angelcode.com
  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. asALLOCFUNC_t userAlloc = malloc;
  37. asFREEFUNC_t userFree = free;
  38. extern "C"
  39. {
  40. int asSetGlobalMemoryFunctions(asALLOCFUNC_t allocFunc, asFREEFUNC_t freeFunc)
  41. {
  42. userAlloc = allocFunc;
  43. userFree = freeFunc;
  44. return 0;
  45. }
  46. int asResetGlobalMemoryFunctions()
  47. {
  48. asThreadCleanup();
  49. userAlloc = malloc;
  50. userFree = free;
  51. return 0;
  52. }
  53. } // extern "C"
  54. asCMemoryMgr::asCMemoryMgr()
  55. {
  56. }
  57. asCMemoryMgr::~asCMemoryMgr()
  58. {
  59. FreeUnusedMemory();
  60. }
  61. void asCMemoryMgr::FreeUnusedMemory()
  62. {
  63. // It's necessary to protect the scriptNodePool from multiple
  64. // simultaneous accesses, as the parser is used by several methods
  65. // that can be executed simultaneously.
  66. ENTERCRITICALSECTION(cs);
  67. int n;
  68. for( n = 0; n < (signed)scriptNodePool.GetLength(); n++ )
  69. userFree(scriptNodePool[n]);
  70. scriptNodePool.Allocate(0, false);
  71. LEAVECRITICALSECTION(cs);
  72. // The engine already protects against multiple threads
  73. // compiling scripts simultaneously so this pool doesn't have
  74. // to be protected again.
  75. for( n = 0; n < (signed)byteInstructionPool.GetLength(); n++ )
  76. userFree(byteInstructionPool[n]);
  77. byteInstructionPool.Allocate(0, false);
  78. }
  79. void *asCMemoryMgr::AllocScriptNode()
  80. {
  81. ENTERCRITICALSECTION(cs);
  82. if( scriptNodePool.GetLength() )
  83. {
  84. void *tRet = scriptNodePool.PopLast();
  85. LEAVECRITICALSECTION(cs);
  86. return tRet;
  87. }
  88. LEAVECRITICALSECTION(cs);
  89. #if defined(AS_DEBUG)
  90. return ((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(asCScriptNode), __FILE__, __LINE__);
  91. #else
  92. return userAlloc(sizeof(asCScriptNode));
  93. #endif
  94. }
  95. void asCMemoryMgr::FreeScriptNode(void *ptr)
  96. {
  97. ENTERCRITICALSECTION(cs);
  98. // Pre allocate memory for the array to avoid slow growth
  99. if( scriptNodePool.GetLength() == 0 )
  100. scriptNodePool.Allocate(100, 0);
  101. scriptNodePool.PushLast(ptr);
  102. LEAVECRITICALSECTION(cs);
  103. }
  104. void *asCMemoryMgr::AllocByteInstruction()
  105. {
  106. if( byteInstructionPool.GetLength() )
  107. return byteInstructionPool.PopLast();
  108. #if defined(AS_DEBUG)
  109. return ((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(cByteInstruction), __FILE__, __LINE__);
  110. #else
  111. return userAlloc(sizeof(cByteInstruction));
  112. #endif
  113. }
  114. void asCMemoryMgr::FreeByteInstruction(void *ptr)
  115. {
  116. // Pre allocate memory for the array to avoid slow growth
  117. if( byteInstructionPool.GetLength() == 0 )
  118. byteInstructionPool.Allocate(100, 0);
  119. byteInstructionPool.PushLast(ptr);
  120. }
  121. END_AS_NAMESPACE