Allocator.cpp 4.2 KB

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