@@ -192,17 +192,19 @@ class DoublyLinkedList
192
192
if (newCapacity <= m_cap)
193
193
return false ;
194
194
// 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;
199
200
void * newReservedSpace = reinterpret_cast <void *>(allocator_traits_t::allocate (m_allocator, newAllocationSize));
200
201
201
202
// Allocation failed, not possible to grow
202
203
if (!newReservedSpace)
203
204
return false ;
204
205
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 ));
206
208
// Copy memory over to new buffer
207
209
memcpy (newArray, m_array, m_cap * sizeof (node_t ));
208
210
// Create new address allocator from old one
@@ -222,12 +224,14 @@ class DoublyLinkedList
222
224
: m_dispose_f(std::move(dispose_f)), m_allocator(_allocator)
223
225
{
224
226
// 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;
229
232
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 ));
231
235
232
236
m_addressAllocator = address_allocator_t (m_reservedSpace, 0u , 0u , 1u , capacity, 1u );
233
237
// If allocation failed, create list with no capacity to indicate creation failed
@@ -306,7 +310,7 @@ class DoublyLinkedList
306
310
node_t * currentNode = get (currentAddress);
307
311
uint32_t nextAddress = currentNode->next ;
308
312
if (m_dispose_f) m_dispose_f (currentNode->data );
309
- currentNode-> ~node_t ( );
313
+ allocator_traits_t::destroy (m_allocator, currentNode );
310
314
currentAddress = nextAddress;
311
315
}
312
316
}
@@ -315,7 +319,7 @@ class DoublyLinkedList
315
319
{
316
320
if (m_dispose_f)
317
321
m_dispose_f (get (address)->data );
318
- get (address)-> ~node_t ( );
322
+ allocator_traits_t::destroy (m_allocator, get (address));
319
323
m_addressAllocator.free_addr (address, 1u );
320
324
}
321
325
0 commit comments