Skip to content

Commit a634252

Browse files
committed
Use allocator_traits destructor, refactor address allocator storage calculation
1 parent c093c4d commit a634252

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

include/nbl/core/containers/DoublyLinkedList.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,19 @@ class DoublyLinkedList
192192
if (newCapacity <= m_cap)
193193
return false;
194194
// Have to consider allocating enough space for list AND state of the address allocator
195-
const auto firstPart = core::alignUp(address_allocator_t::reserved_size(1u, newCapacity, 1u), alignof(node_t));
196-
// Allocator can only allocate in terms of nodes
197-
const size_t firstPartNodes = (firstPart + sizeof(node_t) - 1) / sizeof(node_t);
198-
const size_t newAllocationSize = firstPartNodes + newCapacity;
195+
// Allocator can only allocate in terms of nodes, so we do `addressAllocatorStorageNodes = ceil(reserved_size / sizeof(node_t))`.
196+
// This means that the storage for the address allocator fits in `addressAllocatorStorageNodes * sizeof(node_t)` bytes of memory
197+
// All `Size`s given in terms of nodes
198+
const size_t addressAllocatorStorageSize = (address_allocator_t::reserved_size(1u, newCapacity, 1u) + sizeof(node_t) - 1) / sizeof(node_t);
199+
const size_t newAllocationSize = addressAllocatorStorageSize + newCapacity;
199200
void* newReservedSpace = reinterpret_cast<void*>(allocator_traits_t::allocate(m_allocator, newAllocationSize));
200201

201202
// Allocation failed, not possible to grow
202203
if (!newReservedSpace)
203204
return false;
204205

205-
node_t* newArray = reinterpret_cast<node_t*>(reinterpret_cast<uint8_t*>(newReservedSpace) + firstPart);
206+
// Offset the array start by the storage used by the address allocator
207+
node_t* newArray = reinterpret_cast<node_t*>(reinterpret_cast<uint8_t*>(newReservedSpace) + addressAllocatorStorageSize * sizeof(node_t));
206208
// Copy memory over to new buffer
207209
memcpy(newArray, m_array, m_cap * sizeof(node_t));
208210
// Create new address allocator from old one
@@ -222,12 +224,14 @@ class DoublyLinkedList
222224
: m_dispose_f(std::move(dispose_f)), m_allocator(_allocator)
223225
{
224226
// Have to consider allocating enough space for list AND state of the address allocator
225-
const auto firstPart = core::alignUp(address_allocator_t::reserved_size(1u, capacity, 1u), alignof(node_t));
226-
// Allocator can only allocate in terms of nodes
227-
const size_t firstPartNodes = (firstPart + sizeof(node_t) - 1) / sizeof(node_t);
228-
m_currentAllocationSize = firstPartNodes + capacity;
227+
// Allocator can only allocate in terms of nodes, so we do `addressAllocatorStorageNodes = ceil(reserved_size / sizeof(node_t))`.
228+
// This means that the storage for the address allocator fits in `addressAllocatorStorageNodes * sizeof(node_t)` bytes of memory
229+
// All `Size`s given in terms of nodes
230+
const size_t addressAllocatorStorageSize = (address_allocator_t::reserved_size(1u, capacity, 1u) + sizeof(node_t) - 1) / sizeof(node_t);
231+
m_currentAllocationSize = addressAllocatorStorageSize + capacity;
229232
m_reservedSpace = reinterpret_cast<void*>(allocator_traits_t::allocate(m_allocator, m_currentAllocationSize));
230-
m_array = reinterpret_cast<node_t*>(reinterpret_cast<uint8_t*>(m_reservedSpace) + firstPart);
233+
// Offset the array start by the storage used by the address allocator
234+
m_array = reinterpret_cast<node_t*>(reinterpret_cast<uint8_t*>(m_reservedSpace) + addressAllocatorStorageSize * sizeof(node_t));
231235

232236
m_addressAllocator = address_allocator_t(m_reservedSpace, 0u, 0u, 1u, capacity, 1u);
233237
// If allocation failed, create list with no capacity to indicate creation failed
@@ -306,7 +310,7 @@ class DoublyLinkedList
306310
node_t* currentNode = get(currentAddress);
307311
uint32_t nextAddress = currentNode->next;
308312
if (m_dispose_f) m_dispose_f(currentNode->data);
309-
currentNode->~node_t();
313+
allocator_traits_t::destroy(m_allocator, currentNode);
310314
currentAddress = nextAddress;
311315
}
312316
}
@@ -315,7 +319,7 @@ class DoublyLinkedList
315319
{
316320
if (m_dispose_f)
317321
m_dispose_f(get(address)->data);
318-
get(address)->~node_t();
322+
allocator_traits_t::destroy(m_allocator, get(address));
319323
m_addressAllocator.free_addr(address, 1u);
320324
}
321325

0 commit comments

Comments
 (0)