Allocator.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Copyright (c) 2008-2014 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 <new>
  24. namespace Atomic
  25. {
  26. struct AllocatorBlock;
  27. struct AllocatorNode;
  28. /// %Allocator memory block.
  29. struct AllocatorBlock
  30. {
  31. /// Size of a node.
  32. unsigned nodeSize_;
  33. /// Number of nodes in this block.
  34. unsigned capacity_;
  35. /// First free node.
  36. AllocatorNode* free_;
  37. /// Next allocator block.
  38. AllocatorBlock* next_;
  39. /// Nodes follow.
  40. };
  41. /// %Allocator node.
  42. struct AllocatorNode
  43. {
  44. /// Next free node.
  45. AllocatorNode* next_;
  46. /// Data follows.
  47. };
  48. /// Initialize a fixed-size allocator with the node size and initial capacity.
  49. ATOMIC_API AllocatorBlock* AllocatorInitialize(unsigned nodeSize, unsigned initialCapacity = 1);
  50. /// Uninitialize a fixed-size allocator. Frees all blocks in the chain.
  51. ATOMIC_API void AllocatorUninitialize(AllocatorBlock* allocator);
  52. /// Reserve a node. Creates a new block if necessary.
  53. ATOMIC_API void* AllocatorReserve(AllocatorBlock* allocator);
  54. /// Free a node. Does not free any blocks.
  55. ATOMIC_API void AllocatorFree(AllocatorBlock* allocator, void* ptr);
  56. /// %Allocator template class. Allocates objects of a specific class.
  57. template <class T> class Allocator
  58. {
  59. public:
  60. /// Construct.
  61. Allocator(unsigned initialCapacity = 0) :
  62. allocator_(0)
  63. {
  64. if (initialCapacity)
  65. allocator_ = AllocatorInitialize(sizeof(T), initialCapacity);
  66. }
  67. /// Destruct.
  68. ~Allocator()
  69. {
  70. AllocatorUninitialize(allocator_);
  71. }
  72. /// Reserve and default-construct an object.
  73. T* Reserve()
  74. {
  75. if (!allocator_)
  76. allocator_ = AllocatorInitialize(sizeof(T));
  77. T* newObject = static_cast<T*>(AllocatorReserve(allocator_));
  78. new(newObject) T();
  79. return newObject;
  80. }
  81. /// Reserve and copy-construct an object.
  82. T* Reserve(const T& object)
  83. {
  84. if (!allocator_)
  85. allocator_ = AllocatorInitialize(sizeof(T));
  86. T* newObject = static_cast<T*>(AllocatorReserve(allocator_));
  87. new(newObject) T(object);
  88. return newObject;
  89. }
  90. /// Destruct and free an object.
  91. void Free(T* object)
  92. {
  93. (object)->~T();
  94. AllocatorFree(allocator_, object);
  95. }
  96. private:
  97. /// Prevent copy construction.
  98. Allocator(const Allocator<T>& rhs);
  99. /// Prevent assignment.
  100. Allocator<T>& operator = (const Allocator<T>& rhs);
  101. /// Allocator block.
  102. AllocatorBlock* allocator_;
  103. };
  104. }