Allocator.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Atomic/Atomic.h"
  24. #include <stddef.h>
  25. namespace Atomic
  26. {
  27. struct AllocatorBlock;
  28. struct AllocatorNode;
  29. /// %Allocator memory block.
  30. struct AllocatorBlock
  31. {
  32. /// Size of a node.
  33. unsigned nodeSize_;
  34. /// Number of nodes in this block.
  35. unsigned capacity_;
  36. /// First free node.
  37. AllocatorNode* free_;
  38. /// Next allocator block.
  39. AllocatorBlock* next_;
  40. /// Nodes follow.
  41. };
  42. /// %Allocator node.
  43. struct AllocatorNode
  44. {
  45. /// Next free node.
  46. AllocatorNode* next_;
  47. /// Data follows.
  48. };
  49. /// Initialize a fixed-size allocator with the node size and initial capacity.
  50. ATOMIC_API AllocatorBlock* AllocatorInitialize(unsigned nodeSize, unsigned initialCapacity = 1);
  51. /// Uninitialize a fixed-size allocator. Frees all blocks in the chain.
  52. ATOMIC_API void AllocatorUninitialize(AllocatorBlock* allocator);
  53. /// Reserve a node. Creates a new block if necessary.
  54. ATOMIC_API void* AllocatorReserve(AllocatorBlock* allocator);
  55. /// Free a node. Does not free any blocks.
  56. ATOMIC_API void AllocatorFree(AllocatorBlock* allocator, void* ptr);
  57. /// %Allocator template class. Allocates objects of a specific class.
  58. template <class T> class Allocator
  59. {
  60. public:
  61. /// Construct.
  62. Allocator(unsigned initialCapacity = 0) :
  63. allocator_(0)
  64. {
  65. if (initialCapacity)
  66. allocator_ = AllocatorInitialize((unsigned)sizeof(T), initialCapacity);
  67. }
  68. /// Destruct.
  69. ~Allocator()
  70. {
  71. AllocatorUninitialize(allocator_);
  72. }
  73. /// Reserve and default-construct an object.
  74. T* Reserve()
  75. {
  76. if (!allocator_)
  77. allocator_ = AllocatorInitialize((unsigned)sizeof(T));
  78. T* newObject = static_cast<T*>(AllocatorReserve(allocator_));
  79. new(newObject) T();
  80. return newObject;
  81. }
  82. /// Reserve and copy-construct an object.
  83. T* Reserve(const T& object)
  84. {
  85. if (!allocator_)
  86. allocator_ = AllocatorInitialize((unsigned)sizeof(T));
  87. T* newObject = static_cast<T*>(AllocatorReserve(allocator_));
  88. new(newObject) T(object);
  89. return newObject;
  90. }
  91. /// Destruct and free an object.
  92. void Free(T* object)
  93. {
  94. (object)->~T();
  95. AllocatorFree(allocator_, object);
  96. }
  97. private:
  98. /// Prevent copy construction.
  99. Allocator(const Allocator<T>& rhs);
  100. /// Prevent assignment.
  101. Allocator<T>& operator =(const Allocator<T>& rhs);
  102. /// Allocator block.
  103. AllocatorBlock* allocator_;
  104. };
  105. }