Allocator.cpp 4.1 KB

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