Allocator.h 3.7 KB

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