as_memory.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. andreas@angelcode.com
  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. #ifdef WIP_16BYTE_ALIGN
  36. // TODO: This declaration should be in angelscript.h
  37. // when the application can register it's own
  38. // aligned memory routines
  39. typedef void *(*asALLOCALIGNEDFUNC_t)(size_t, size_t);
  40. typedef void (*asFREEALIGNEDFUNC_t)(void *);
  41. extern asALLOCALIGNEDFUNC_t userAllocAligned;
  42. extern asFREEALIGNEDFUNC_t userFreeAligned;
  43. typedef void *(*asALLOCALIGNEDFUNCDEBUG_t)(size_t, size_t, const char *, unsigned int);
  44. // The maximum type alignment supported.
  45. const int MAX_TYPE_ALIGNMENT = 16;
  46. // Utility function used for assertions.
  47. bool isAligned(const void* const pointer, asUINT alignment);
  48. #endif // WIP_16BYTE_ALIGN
  49. // We don't overload the new operator as that would affect the application as well
  50. #ifndef AS_DEBUG
  51. #define asNEW(x) new(userAlloc(sizeof(x))) x
  52. #define asDELETE(ptr,x) {void *tmp = ptr; (ptr)->~x(); userFree(tmp);}
  53. #define asNEWARRAY(x,cnt) (x*)userAlloc(sizeof(x)*cnt)
  54. #define asDELETEARRAY(ptr) userFree(ptr)
  55. #ifdef WIP_16BYTE_ALIGN
  56. #define asNEWARRAYALIGNED(x,cnt, alignment) (x*)userAllocAligned(sizeof(x)*cnt, alignment)
  57. #define asDELETEARRAYALIGNED(ptr) userFreeAligned(ptr)
  58. #endif
  59. #else
  60. typedef void *(*asALLOCFUNCDEBUG_t)(size_t, const char *, unsigned int);
  61. #define asNEW(x) new(((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(x), __FILE__, __LINE__)) x
  62. #define asDELETE(ptr,x) {void *tmp = ptr; (ptr)->~x(); userFree(tmp);}
  63. #define asNEWARRAY(x,cnt) (x*)((asALLOCFUNCDEBUG_t)(userAlloc))(sizeof(x)*cnt, __FILE__, __LINE__)
  64. #define asDELETEARRAY(ptr) userFree(ptr)
  65. #ifdef WIP_16BYTE_ALIGN
  66. //TODO: Equivalent of debug allocation function with alignment?
  67. #define asNEWARRAYALIGNED(x,cnt, alignment) (x*)userAllocAligned(sizeof(x)*cnt, alignment)
  68. #define asDELETEARRAYALIGNED(ptr) userFreeAligned(ptr)
  69. #endif
  70. #endif
  71. END_AS_NAMESPACE
  72. #include <new>
  73. #include "as_criticalsection.h"
  74. #include "as_array.h"
  75. BEGIN_AS_NAMESPACE
  76. class asCMemoryMgr
  77. {
  78. public:
  79. asCMemoryMgr();
  80. ~asCMemoryMgr();
  81. void FreeUnusedMemory();
  82. void *AllocScriptNode();
  83. void FreeScriptNode(void *ptr);
  84. #ifndef AS_NO_COMPILER
  85. void *AllocByteInstruction();
  86. void FreeByteInstruction(void *ptr);
  87. #endif
  88. protected:
  89. DECLARECRITICALSECTION(cs)
  90. asCArray<void *> scriptNodePool;
  91. asCArray<void *> byteInstructionPool;
  92. };
  93. END_AS_NAMESPACE
  94. #endif