Allocator.h 3.6 KB

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