@@ -40,22 +40,37 @@ class GrowableDoublyLinkedList : public DoublyLinkedListBase<Value>
4040
4141 ~GrowableDoublyLinkedList () = default ;
4242
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)
4449 {
50+ // Must at least make list grow
51+ if (newCapacity <= this ->m_cap )
52+ return false ;
4553 // Same as code found in ContiguousMemoryLinkedListBase to create aligned space
4654 const auto firstPart = core::alignUp (address_allocator_t::reserved_size (1u , newCapacity, 1u ), alignof (node_t ));
4755 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+
4861 node_t * newArray = reinterpret_cast <node_t *>(reinterpret_cast <uint8_t *>(newReservedSpace) + firstPart);
4962
5063 // 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 );
5366
5467 // Finally, create new address allocator from old one
5568 this ->m_addressAllocator = std::unique_ptr<address_allocator_t >(new address_allocator_t (newCapacity, std::move (*(this ->m_addressAllocator )), newReservedSpace));
5669 this ->m_cap = newCapacity;
5770 this ->m_array = newArray;
5871 this ->m_reservedSpace = newReservedSpace;
72+
73+ return true ;
5974 }
6075};
6176
0 commit comments