|
@@ -8,20 +8,21 @@
|
|
|
namespace Urho3D
|
|
namespace Urho3D
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
-AllocatorBlock* AllocatorReserveBlock(AllocatorBlock* allocator, unsigned nodeSize, unsigned capacity)
|
|
|
|
|
|
|
+static AllocatorBlock* AllocatorReserveBlock(AllocatorBlock* allocator, i32 nodeSize, i32 capacity)
|
|
|
{
|
|
{
|
|
|
- if (!capacity)
|
|
|
|
|
- capacity = 1;
|
|
|
|
|
|
|
+ assert(nodeSize > 0 && capacity > 0);
|
|
|
|
|
|
|
|
- auto* blockPtr = new unsigned char[sizeof(AllocatorBlock) + capacity * (sizeof(AllocatorNode) + nodeSize)];
|
|
|
|
|
- auto* newBlock = reinterpret_cast<AllocatorBlock*>(blockPtr);
|
|
|
|
|
|
|
+ u8* blockPtr = new u8[sizeof(AllocatorBlock) + capacity * (sizeof(AllocatorNode) + nodeSize)];
|
|
|
|
|
+ AllocatorBlock* newBlock = reinterpret_cast<AllocatorBlock*>(blockPtr);
|
|
|
newBlock->nodeSize_ = nodeSize;
|
|
newBlock->nodeSize_ = nodeSize;
|
|
|
newBlock->capacity_ = capacity;
|
|
newBlock->capacity_ = capacity;
|
|
|
newBlock->free_ = nullptr;
|
|
newBlock->free_ = nullptr;
|
|
|
newBlock->next_ = nullptr;
|
|
newBlock->next_ = nullptr;
|
|
|
|
|
|
|
|
if (!allocator)
|
|
if (!allocator)
|
|
|
|
|
+ {
|
|
|
allocator = newBlock;
|
|
allocator = newBlock;
|
|
|
|
|
+ }
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
newBlock->next_ = allocator->next_;
|
|
newBlock->next_ = allocator->next_;
|
|
@@ -29,18 +30,19 @@ AllocatorBlock* AllocatorReserveBlock(AllocatorBlock* allocator, unsigned nodeSi
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Initialize the nodes. Free nodes are always chained to the first (parent) allocator
|
|
// Initialize the nodes. Free nodes are always chained to the first (parent) allocator
|
|
|
- unsigned char* nodePtr = blockPtr + sizeof(AllocatorBlock);
|
|
|
|
|
- auto* firstNewNode = reinterpret_cast<AllocatorNode*>(nodePtr);
|
|
|
|
|
|
|
+ u8* nodePtr = blockPtr + sizeof(AllocatorBlock);
|
|
|
|
|
+ AllocatorNode* firstNewNode = reinterpret_cast<AllocatorNode*>(nodePtr);
|
|
|
|
|
|
|
|
- for (unsigned i = 0; i < capacity - 1; ++i)
|
|
|
|
|
|
|
+ for (i32 i = 0; i < capacity - 1; ++i)
|
|
|
{
|
|
{
|
|
|
- auto* newNode = reinterpret_cast<AllocatorNode*>(nodePtr);
|
|
|
|
|
|
|
+ AllocatorNode* newNode = reinterpret_cast<AllocatorNode*>(nodePtr);
|
|
|
newNode->next_ = reinterpret_cast<AllocatorNode*>(nodePtr + sizeof(AllocatorNode) + nodeSize);
|
|
newNode->next_ = reinterpret_cast<AllocatorNode*>(nodePtr + sizeof(AllocatorNode) + nodeSize);
|
|
|
nodePtr += sizeof(AllocatorNode) + nodeSize;
|
|
nodePtr += sizeof(AllocatorNode) + nodeSize;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
// i == capacity - 1
|
|
// i == capacity - 1
|
|
|
{
|
|
{
|
|
|
- auto* newNode = reinterpret_cast<AllocatorNode*>(nodePtr);
|
|
|
|
|
|
|
+ AllocatorNode* newNode = reinterpret_cast<AllocatorNode*>(nodePtr);
|
|
|
newNode->next_ = nullptr;
|
|
newNode->next_ = nullptr;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -49,10 +51,9 @@ AllocatorBlock* AllocatorReserveBlock(AllocatorBlock* allocator, unsigned nodeSi
|
|
|
return newBlock;
|
|
return newBlock;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-AllocatorBlock* AllocatorInitialize(unsigned nodeSize, unsigned initialCapacity)
|
|
|
|
|
|
|
+AllocatorBlock* AllocatorInitialize(i32 nodeSize, i32 initialCapacity)
|
|
|
{
|
|
{
|
|
|
- AllocatorBlock* block = AllocatorReserveBlock(nullptr, nodeSize, initialCapacity);
|
|
|
|
|
- return block;
|
|
|
|
|
|
|
+ return AllocatorReserveBlock(nullptr, nodeSize, initialCapacity);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void AllocatorUninitialize(AllocatorBlock* allocator)
|
|
void AllocatorUninitialize(AllocatorBlock* allocator)
|
|
@@ -60,7 +61,7 @@ void AllocatorUninitialize(AllocatorBlock* allocator)
|
|
|
while (allocator)
|
|
while (allocator)
|
|
|
{
|
|
{
|
|
|
AllocatorBlock* next = allocator->next_;
|
|
AllocatorBlock* next = allocator->next_;
|
|
|
- delete[] reinterpret_cast<unsigned char*>(allocator);
|
|
|
|
|
|
|
+ delete[] reinterpret_cast<u8*>(allocator);
|
|
|
allocator = next;
|
|
allocator = next;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -73,14 +74,14 @@ void* AllocatorReserve(AllocatorBlock* allocator)
|
|
|
if (!allocator->free_)
|
|
if (!allocator->free_)
|
|
|
{
|
|
{
|
|
|
// Free nodes have been exhausted. Allocate a new larger block
|
|
// Free nodes have been exhausted. Allocate a new larger block
|
|
|
- unsigned newCapacity = (allocator->capacity_ + 1) >> 1u;
|
|
|
|
|
|
|
+ i32 newCapacity = (allocator->capacity_ + 1) >> 1u; // * 0.5 and round up
|
|
|
AllocatorReserveBlock(allocator, allocator->nodeSize_, newCapacity);
|
|
AllocatorReserveBlock(allocator, allocator->nodeSize_, newCapacity);
|
|
|
allocator->capacity_ += newCapacity;
|
|
allocator->capacity_ += newCapacity;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// We should have new free node(s) chained
|
|
// We should have new free node(s) chained
|
|
|
AllocatorNode* freeNode = allocator->free_;
|
|
AllocatorNode* freeNode = allocator->free_;
|
|
|
- void* ptr = (reinterpret_cast<unsigned char*>(freeNode)) + sizeof(AllocatorNode);
|
|
|
|
|
|
|
+ void* ptr = reinterpret_cast<u8*>(freeNode) + sizeof(AllocatorNode);
|
|
|
allocator->free_ = freeNode->next_;
|
|
allocator->free_ = freeNode->next_;
|
|
|
freeNode->next_ = nullptr;
|
|
freeNode->next_ = nullptr;
|
|
|
|
|
|
|
@@ -92,8 +93,8 @@ void AllocatorFree(AllocatorBlock* allocator, void* ptr)
|
|
|
if (!allocator || !ptr)
|
|
if (!allocator || !ptr)
|
|
|
return;
|
|
return;
|
|
|
|
|
|
|
|
- auto* dataPtr = static_cast<unsigned char*>(ptr);
|
|
|
|
|
- auto* node = reinterpret_cast<AllocatorNode*>(dataPtr - sizeof(AllocatorNode));
|
|
|
|
|
|
|
+ u8* dataPtr = static_cast<u8*>(ptr);
|
|
|
|
|
+ AllocatorNode* node = reinterpret_cast<AllocatorNode*>(dataPtr - sizeof(AllocatorNode));
|
|
|
|
|
|
|
|
// Chain the node back to free nodes
|
|
// Chain the node back to free nodes
|
|
|
node->next_ = allocator->free_;
|
|
node->next_ = allocator->free_;
|