CCAllocatorMacros.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. Author: Justin Graham (https://github.com/mannewalis)
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef CC_ALLOCATOR_MACROS_H
  22. #define CC_ALLOCATOR_MACROS_H
  23. /// @cond DO_NOT_SHOW
  24. #include "base/ccConfig.h"
  25. #include "platform/CCPlatformMacros.h"
  26. // namespace allocator {}
  27. #ifdef __cplusplus
  28. #define NS_CC_ALLOCATOR_BEGIN namespace allocator {
  29. #define NS_CC_ALLOCATOR_END }
  30. #define USING_NS_CC_ALLOCATOR using namespace cocos2d::allocator
  31. #define NS_CC_ALLOCATOR ::cocos2d::allocator
  32. #else
  33. #define NS_CC_ALLOCATOR_BEGIN
  34. #define NS_CC_ALLOCATOR_END
  35. #define USING_NS_CC_ALLOCATOR
  36. #define NS_CC_ALLOCATOR
  37. #endif
  38. #if COCOS2D_DEBUG
  39. // @brief CC_ALLOCATOR_INLINE
  40. // Turn off inlining of methods when debugging to make stack traces readable and stepping through code sane.
  41. #define CC_ALLOCATOR_INLINE
  42. // printf is safe to use
  43. #define LOG printf
  44. #else
  45. #define CC_ALLOCATOR_INLINE inline
  46. #define LOG
  47. #endif
  48. // allocator macros
  49. #if CC_ENABLE_ALLOCATOR
  50. // @brief macros for new/delete
  51. // we let global new/delete handle these as they are overridden.
  52. #define CC_NEW(klass, ...) new klass(__VAR_ARGS__)
  53. #define CC_DELETE(object) delete object;
  54. // @brief macros for malloc/free
  55. // these will use the global allocator
  56. #define CC_MALLOC(size) ccAllocatorGlobal.allocate(size)
  57. #define CC_FREE(address) ccAllocatorGlobal.deallocate(address)
  58. // alloc on the stack
  59. #define CC_ALLOCA(size) alloca(size)
  60. // @brief helper macro for overriding new/delete operators for a class.
  61. // This correctly passes the size in the deallocate method which is needed.
  62. #define CC_USE_ALLOCATOR_POOL(T, A) \
  63. CC_ALLOCATOR_INLINE void* operator new (size_t size) \
  64. { \
  65. return (void*)A.allocate(size); \
  66. } \
  67. CC_ALLOCATOR_INLINE void operator delete (void* object, size_t size) \
  68. { \
  69. A.deallocate((T*)object, size); \
  70. }
  71. #else
  72. // macros for new/delete
  73. // these will use a general thread safe allocator
  74. #define CC_NEW(klass, ...) new klass(__VAR_ARGS__)
  75. #define CC_DELETE(object) delete object
  76. // macros for malloc/free
  77. // these will use a general thread safe allocator
  78. #define CC_MALLOC(size) malloc(size)
  79. #define CC_FREE(address) free(address)
  80. // alloc on the stack
  81. #define CC_ALLOCA(size) alloca(size)
  82. // throw these away if not enabled
  83. #define CC_USE_ALLOCATOR_POOL(...)
  84. #define CC_OVERRIDE_GLOBAL_NEWDELETE_WITH_ALLOCATOR(...)
  85. #endif
  86. // @ brief Quick and dirty macro to dump an area of memory
  87. // useful for debugging blocks of memory from allocators.
  88. #define DUMP(a, l, C) \
  89. { \
  90. LOG("> %p len %zu\n", a, l); \
  91. uint8_t* p = (uint8_t*)a; \
  92. uint8_t* e = p + l; \
  93. while (p < e) \
  94. { \
  95. LOG("%p ", p); \
  96. for (int i = 0; i < C && &p[i] < e; ++i) \
  97. LOG("%02x ", p[i]); \
  98. for (int i = 0; i < C && &p[i] < e; ++i) \
  99. LOG("%c ", isalnum(p[i]) ? p[i] : '.'); \
  100. LOG("\n"); \
  101. p = p + C > e ? e : p + C; \
  102. } \
  103. }
  104. /// @endcond
  105. #endif//CC_ALLOCATOR_MACROS_H