Allocator.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include "../Precompiled.h"
  23. #include "../DebugNew.h"
  24. namespace Atomic
  25. {
  26. AllocatorBlock* AllocatorReserveBlock(AllocatorBlock* allocator, unsigned nodeSize, unsigned capacity)
  27. {
  28. if (!capacity)
  29. capacity = 1;
  30. unsigned char* blockPtr = new unsigned char[sizeof(AllocatorBlock) + capacity * (sizeof(AllocatorNode) + nodeSize)];
  31. AllocatorBlock* newBlock = reinterpret_cast<AllocatorBlock*>(blockPtr);
  32. newBlock->nodeSize_ = nodeSize;
  33. newBlock->capacity_ = capacity;
  34. newBlock->free_ = 0;
  35. newBlock->next_ = 0;
  36. if (!allocator)
  37. allocator = newBlock;
  38. else
  39. {
  40. newBlock->next_ = allocator->next_;
  41. allocator->next_ = newBlock;
  42. }
  43. // Initialize the nodes. Free nodes are always chained to the first (parent) allocator
  44. unsigned char* nodePtr = blockPtr + sizeof(AllocatorBlock);
  45. AllocatorNode* firstNewNode = reinterpret_cast<AllocatorNode*>(nodePtr);
  46. for (unsigned i = 0; i < capacity - 1; ++i)
  47. {
  48. AllocatorNode* newNode = reinterpret_cast<AllocatorNode*>(nodePtr);
  49. newNode->next_ = reinterpret_cast<AllocatorNode*>(nodePtr + sizeof(AllocatorNode) + nodeSize);
  50. nodePtr += sizeof(AllocatorNode) + nodeSize;
  51. }
  52. // i == capacity - 1
  53. {
  54. AllocatorNode* newNode = reinterpret_cast<AllocatorNode*>(nodePtr);
  55. newNode->next_ = 0;
  56. }
  57. allocator->free_ = firstNewNode;
  58. return newBlock;
  59. }
  60. AllocatorBlock* AllocatorInitialize(unsigned nodeSize, unsigned initialCapacity)
  61. {
  62. AllocatorBlock* block = AllocatorReserveBlock(0, nodeSize, initialCapacity);
  63. return block;
  64. }
  65. void AllocatorUninitialize(AllocatorBlock* allocator)
  66. {
  67. while (allocator)
  68. {
  69. AllocatorBlock* next = allocator->next_;
  70. delete[] reinterpret_cast<unsigned char*>(allocator);
  71. allocator = next;
  72. }
  73. }
  74. void* AllocatorReserve(AllocatorBlock* allocator)
  75. {
  76. if (!allocator)
  77. return 0;
  78. if (!allocator->free_)
  79. {
  80. // Free nodes have been exhausted. Allocate a new larger block
  81. unsigned newCapacity = (allocator->capacity_ + 1) >> 1;
  82. AllocatorReserveBlock(allocator, allocator->nodeSize_, newCapacity);
  83. allocator->capacity_ += newCapacity;
  84. }
  85. // We should have new free node(s) chained
  86. AllocatorNode* freeNode = allocator->free_;
  87. void* ptr = (reinterpret_cast<unsigned char*>(freeNode)) + sizeof(AllocatorNode);
  88. allocator->free_ = freeNode->next_;
  89. freeNode->next_ = 0;
  90. return ptr;
  91. }
  92. void AllocatorFree(AllocatorBlock* allocator, void* ptr)
  93. {
  94. if (!allocator || !ptr)
  95. return;
  96. unsigned char* dataPtr = static_cast<unsigned char*>(ptr);
  97. AllocatorNode* node = reinterpret_cast<AllocatorNode*>(dataPtr - sizeof(AllocatorNode));
  98. // Chain the node back to free nodes
  99. node->next_ = allocator->free_;
  100. allocator->free_ = node;
  101. }
  102. }