as_memory.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.h
  25. //
  26. // Overload the default memory management functions so that we
  27. // can let the application decide how to do it.
  28. //
  29. #ifndef AS_MEMORY_H
  30. #define AS_MEMORY_H
  31. #include "as_config.h"
  32. BEGIN_AS_NAMESPACE
  33. extern asALLOCFUNC_t userAlloc;
  34. extern asFREEFUNC_t userFree;
  35. // We don't overload the new operator as that would affect the application as well
  36. #ifndef AS_DEBUG
  37. #define asNEW(x) new(userAlloc(sizeof(x))) x
  38. #define asDELETE(ptr,x) {void *tmp = ptr; (ptr)->~x(); userFree(tmp);}
  39. #define asNEWARRAY(x,cnt) (x*)userAlloc(sizeof(x)*cnt)
  40. #define asDELETEARRAY(ptr) userFree(ptr)
  41. #else
  42. typedef void *(*asALLOCFUNCDEBUG_t)(size_t, const char *, unsigned int);
  43. #define asNEW(x) new(((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(x), __FILE__, __LINE__)) x
  44. #define asDELETE(ptr,x) {void *tmp = ptr; (ptr)->~x(); userFree(tmp);}
  45. #define asNEWARRAY(x,cnt) (x*)((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(x)*cnt, __FILE__, __LINE__)
  46. #define asDELETEARRAY(ptr) userFree(ptr)
  47. #endif
  48. END_AS_NAMESPACE
  49. #include <new>
  50. #include "as_criticalsection.h"
  51. #include "as_array.h"
  52. BEGIN_AS_NAMESPACE
  53. class asCMemoryMgr
  54. {
  55. public:
  56. asCMemoryMgr();
  57. ~asCMemoryMgr();
  58. void FreeUnusedMemory();
  59. void *AllocScriptNode();
  60. void FreeScriptNode(void *ptr);
  61. #ifndef AS_NO_COMPILER
  62. void *AllocByteInstruction();
  63. void FreeByteInstruction(void *ptr);
  64. #endif
  65. protected:
  66. DECLARECRITICALSECTION(cs)
  67. asCArray<void *> scriptNodePool;
  68. asCArray<void *> byteInstructionPool;
  69. };
  70. END_AS_NAMESPACE
  71. #endif