Allocator.cpp 4.1 KB

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