CCAllocatorBase.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_BASE_H
  22. #define CC_ALLOCATOR_BASE_H
  23. /// @cond DO_NOT_SHOW
  24. #include <string>
  25. #include "platform/CCPlatformMacros.h"
  26. #include "base/allocator/CCAllocatorMacros.h"
  27. NS_CC_BEGIN
  28. NS_CC_ALLOCATOR_BEGIN
  29. // @brief
  30. // AllocatorBase
  31. // Provides a base that contains a few methods and definitions that are helpful to all allocation strategies.
  32. // Also provides a common base that can be used to point to all allocators generically.
  33. class AllocatorBase
  34. {
  35. public:
  36. typedef void* pointer;
  37. enum { kDefaultAlignment = 16 };
  38. // this must be true for SSE instructions to be 16 byte aligned
  39. // we can now use kDefault alignment as our smallest alloc size
  40. static_assert(sizeof(uintptr_t) <= kDefaultAlignment, "pointer size must be smaller than default alignment");
  41. #if CC_ENABLE_ALLOCATOR_DIAGNOSTICS
  42. AllocatorBase()
  43. : _next_allocator(nullptr)
  44. {}
  45. #endif
  46. virtual ~AllocatorBase()
  47. {}
  48. // @brief
  49. // Given an address and alignment in bytes, returns an address aligned to the number of bytes
  50. // For example, if the alignment is 4 which is standard, then the address is divisible evenly by 4.
  51. CC_ALLOCATOR_INLINE pointer aligned(const pointer address, const size_t alignment = kDefaultAlignment) const
  52. {
  53. return (pointer) (((intptr_t)address + (alignment - 1)) & ~(alignment - 1));
  54. }
  55. // @brief Calculate the next power of two for a given size.
  56. // Most blocks requested are already a power of two. For small block alloc
  57. // this means we cannot add overhead, hence the slightly less performance
  58. // searching of fixed block pages to determine size if none is specified.
  59. CC_ALLOCATOR_INLINE size_t nextPow2BlockSize(size_t size) const
  60. {
  61. --size;
  62. size |= size >> 1;
  63. size |= size >> 2;
  64. size |= size >> 4;
  65. size |= size >> 8;
  66. size |= size >> 16;
  67. return ++size;
  68. }
  69. #if CC_ENABLE_ALLOCATOR_DIAGNOSTICS
  70. void setTag(const char* tag)
  71. {
  72. strncpy(_tag, tag, sizeof(_tag)-1);
  73. }
  74. const char* tag() const
  75. {
  76. return _tag;
  77. }
  78. // cannot use std::string as it allocates memory.
  79. // for some allocator this tag is set before the new/delete allocator has finished initializing.
  80. char _tag[1000];
  81. // @brief return any diagnostic info for this allocator
  82. virtual std::string diagnostics() const
  83. {
  84. return "";
  85. }
  86. AllocatorBase* _next_allocator;
  87. #endif//CC_ENABLE_ALLOCATOR_DIAGNOSTICS
  88. };
  89. NS_CC_ALLOCATOR_END
  90. NS_CC_END
  91. /// @endcond
  92. #endif//CC_ALLOCATOR_BASE_H