@@ -40,22 +40,37 @@ class GrowableDoublyLinkedList : public DoublyLinkedListBase<Value>
40
40
41
41
~GrowableDoublyLinkedList () = default ;
42
42
43
- inline void grow (uint32_t newCapacity)
43
+ /* *
44
+ * @brief Resizes the list by extending its capacity so it can hold more elements. Returns a bool indicating if capacity was indeed increased.
45
+ *
46
+ * @param [in] newCapacity New number of elements to hold. MUST be greater than current list capacity.
47
+ */
48
+ inline bool grow (uint32_t newCapacity)
44
49
{
50
+ // Must at least make list grow
51
+ if (newCapacity <= this ->m_cap )
52
+ return false ;
45
53
// Same as code found in ContiguousMemoryLinkedListBase to create aligned space
46
54
const auto firstPart = core::alignUp (address_allocator_t::reserved_size (1u , newCapacity, 1u ), alignof (node_t ));
47
55
void * newReservedSpace = _NBL_ALIGNED_MALLOC (firstPart + newCapacity * sizeof (node_t ), alignof (node_t ));
56
+
57
+ // Malloc failed, not possible to grow
58
+ if (!newReservedSpace)
59
+ return false ;
60
+
48
61
node_t * newArray = reinterpret_cast <node_t *>(reinterpret_cast <uint8_t *>(newReservedSpace) + firstPart);
49
62
50
63
// Copy memory over to new buffer, then free old one
51
- memcpy (reinterpret_cast <void *>(newArray), reinterpret_cast <void *>(this ->m_array ), m_cap * sizeof (node_t ));
52
- _NBL_ALIGNED_FREE (m_reservedSpace);
64
+ memcpy (reinterpret_cast <void *>(newArray), reinterpret_cast <void *>(this ->m_array ), this -> m_cap * sizeof (node_t ));
65
+ _NBL_ALIGNED_FREE (this -> m_reservedSpace );
53
66
54
67
// Finally, create new address allocator from old one
55
68
this ->m_addressAllocator = std::unique_ptr<address_allocator_t >(new address_allocator_t (newCapacity, std::move (*(this ->m_addressAllocator )), newReservedSpace));
56
69
this ->m_cap = newCapacity;
57
70
this ->m_array = newArray;
58
71
this ->m_reservedSpace = newReservedSpace;
72
+
73
+ return true ;
59
74
}
60
75
};
61
76
0 commit comments