Skip to content

Commit 5a86bd3

Browse files
committed
Fixed allocator move constructor bug
1 parent 7cec5bf commit 5a86bd3

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

include/nbl/core/alloc/PoolAddressAllocator.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,34 @@ class PoolAddressAllocator : public AddressAllocatorBase<PoolAddressAllocator<_s
3636

3737
for (_size_type i=0; i<other.freeStackCtr; i++)
3838
{
39-
_size_type freeEntry = other.getFreeStack(i)-other.combinedOffset;
39+
_size_type freeEntry = other.getFreeStack(i)-other.Base::combinedOffset;
4040
// check in case of shrink
4141
if (freeEntry<blockCount*blockSize)
4242
getFreeStack(freeStackCtr++) = freeEntry+Base::combinedOffset;
4343
}
4444
}
45+
46+
void copyStateMove(_size_type otherBlockCount, _size_type otherFreeStackCtr, _size_type newBuffSz)
47+
{
48+
if (blockCount > otherBlockCount)
49+
freeStackCtr = blockCount - otherBlockCount;
50+
51+
#ifdef _NBL_DEBUG
52+
assert(Base::checkResize(newBuffSz, Base::alignOffset));
53+
#endif // _NBL_DEBUG
54+
55+
for (_size_type i = 0u; i < freeStackCtr; i++)
56+
getFreeStack(i) = (blockCount - 1u - i) * blockSize + Base::combinedOffset;
57+
58+
for (_size_type i = 0; i < otherFreeStackCtr; i++)
59+
{
60+
_size_type freeEntry = getFreeStack(i) - Base::combinedOffset;
61+
// check in case of shrink
62+
if (freeEntry < blockCount * blockSize)
63+
getFreeStack(freeStackCtr++) = freeEntry + Base::combinedOffset;
64+
}
65+
}
66+
4567
inline bool safe_shrink_size_common(_size_type& sizeBound, _size_type newBuffAlignmentWeCanGuarantee) noexcept
4668
{
4769
_size_type capacity = get_total_size()-Base::alignOffset;
@@ -82,7 +104,7 @@ class PoolAddressAllocator : public AddressAllocatorBase<PoolAddressAllocator<_s
82104
Base(std::move(other),std::forward<Args>(args)...),
83105
blockCount((newBuffSz-Base::alignOffset)/other.blockSize), blockSize(other.blockSize), freeStackCtr(0u)
84106
{
85-
copyState(other, newBuffSz);
107+
copyStateMove(other.blockCount, other.freeStackCtr, newBuffSz);
86108

87109
other.blockCount = invalid_address;
88110
other.blockSize = invalid_address;

include/nbl/core/containers/DoublyLinkedList.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,16 @@ class DoublyLinkedList
195195
return false;
196196

197197
node_t* newArray = reinterpret_cast<node_t*>(reinterpret_cast<uint8_t*>(newReservedSpace) + firstPart);
198-
199-
// Copy memory over to new buffer, then free old one
198+
// Copy memory over to new buffer
200199
memcpy(newArray, m_array, m_cap * sizeof(node_t));
201-
_NBL_ALIGNED_FREE(m_reservedSpace);
202-
203-
// Finally, create new address allocator from old one
200+
// Create new address allocator from old one
204201
m_addressAllocator = std::unique_ptr<address_allocator_t>(new address_allocator_t(newCapacity, std::move(*(m_addressAllocator)), newReservedSpace));
202+
// After address allocator creation we can free the old buffer
203+
_NBL_ALIGNED_FREE(m_reservedSpace);
205204
m_cap = newCapacity;
206205
m_array = newArray;
207206
m_reservedSpace = newReservedSpace;
207+
208208

209209
return true;
210210
}

include/nbl/core/containers/LRUCache.h

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,10 @@ class ResizableLRUCache : protected impl::LRUCacheBase<Key, Value, MapHash, MapE
281281
using assoc_t = typename base_t::list_value_t;
282282

283283
//Constructor
284-
ResizableLRUCache(const uint32_t capacity, disposal_func_t&& _df = disposal_func_t(), MapHash&& _hash = MapHash(), MapEquals&& _equals = MapEquals()) :
285-
base_t(capacity, std::move(_hash), std::move(_equals), std::move(_df)),
284+
ResizableLRUCache(uint32_t capacity, disposal_func_t&& _df = disposal_func_t(), MapHash&& _hash = MapHash(), MapEquals&& _equals = MapEquals()) :
285+
base_t(capacity, std::move(_hash), std::move(_equals), std::move(_df)), m_capacity(capacity),
286286
m_shortcut_map(capacity >> 2, WrapHash{ this }, WrapEquals{ this }) // 4x less buckets than capacity seems reasonable
287287
{
288-
assert(capacity > 1);
289288
m_shortcut_map.reserve(capacity);
290289
}
291290
ResizableLRUCache() = delete;
@@ -301,10 +300,29 @@ class ResizableLRUCache : protected impl::LRUCacheBase<Key, Value, MapHash, MapE
301300
stringStream << "k: '" << node->data.first << "', v: '" << node->data.second << "'\t prev: " << node->prev << " | curr: " << nodeAddr << " | next: " << node->next;
302301
logger->log(stringStream.str());
303302
nodeAddr = node->prev;
304-
node = base_t::m_list.get(node->prev);
305303
}
306304
}
307305

306+
/**
307+
* @brief Returns a string representing the elements currently in the cache in LRU order
308+
*
309+
* @param [in] newCapacity New number of elements to hold. MUST be greater than current list capacity.
310+
*/
311+
inline std::string getState()
312+
{
313+
std::ostringstream stringStream;
314+
auto nodeAddr = base_t::m_list.getLastAddress();
315+
while (nodeAddr != invalid_iterator)
316+
{
317+
auto node = base_t::m_list.get(nodeAddr);
318+
stringStream << "{" << node->data.first << ", " << node->data.second << "}";
319+
nodeAddr = node->prev;
320+
if (nodeAddr != invalid_iterator)
321+
stringStream << ", ";
322+
}
323+
return stringStream.str();
324+
}
325+
308326
template<typename K, typename V, std::invocable<const Value&> EvictionCallback> requires std::is_constructible_v<Value, V> // && (std::is_same_v<Value,V> || std::is_assignable_v<Value,V>) // is_assignable_v<int, int&> returns false :(
309327
inline Value* insert(K&& k, V&& v, EvictionCallback&& evictCallback)
310328
{
@@ -390,6 +408,8 @@ class ResizableLRUCache : protected impl::LRUCacheBase<Key, Value, MapHash, MapE
390408
*/
391409
inline bool grow(uint32_t newCapacity)
392410
{
411+
if (newCapacity <= m_capacity)
412+
return false;
393413
m_shortcut_map.reserve(newCapacity);
394414
return base_t::m_list.grow(newCapacity);
395415
}
@@ -409,6 +429,7 @@ class ResizableLRUCache : protected impl::LRUCacheBase<Key, Value, MapHash, MapE
409429

410430
protected:
411431
unordered_set<uint32_t, WrapHash, WrapEquals> m_shortcut_map;
432+
uint32_t m_capacity;
412433
};
413434

414435
} //namespace core

0 commit comments

Comments
 (0)