Skip to content

Commit f1962e5

Browse files
committed
Add new resizable lrucache
1 parent 2c90578 commit f1962e5

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

include/nbl/core/containers/LRUCache.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,23 @@ class ResizableLRUCache : protected impl::LRUCacheBase<Key, Value, MapHash, MapE
389389
*/
390390
inline bool grow(uint32_t newCapacity)
391391
{
392+
m_shortcut_map.reserve(newCapacity);
392393
return base_t::m_list.grow(newCapacity);
393394
}
394395

396+
/**
397+
* @brief Empties cache and resets its state
398+
*/
399+
inline void clear()
400+
{
401+
base_t::searchedKey = nullptr;
402+
base_t::m_list.clear();
403+
404+
auto mapBegin = m_shortcut_map.begin();
405+
auto mapEnd = m_shortcut_map.end();
406+
m_shortcut_map.erase(mapBegin, mapEnd);
407+
}
408+
395409
protected:
396410
unordered_set<uint32_t, WrapHash, WrapEquals> m_shortcut_map;
397411
};

include/nbl/core/containers/lists/common.h

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ class ContiguousMemoryLinkedListBase
6767
insertAt(reserveAddress(), value_t(std::forward<Args>(args)...));
6868
}
6969

70+
/**
71+
* @brief Resets list to initial state.
72+
*/
73+
inline void clear()
74+
{
75+
disposeAll();
76+
77+
m_addressAllocator = std::unique_ptr<address_allocator_t>(new address_allocator_t(m_reservedSpace, 0u, 0u, 1u, m_cap, 1u));
78+
m_back = invalid_iterator;
79+
m_begin = invalid_iterator;
80+
81+
}
82+
7083
//remove the last element in the list
7184
virtual void popBack() = 0;
7285

@@ -117,17 +130,7 @@ class ContiguousMemoryLinkedListBase
117130

118131
~ContiguousMemoryLinkedListBase()
119132
{
120-
if (m_dispose_f && m_begin != invalid_iterator)
121-
{
122-
auto* begin = getBegin();
123-
auto* back = getBack();
124-
while(begin != back)
125-
{
126-
m_dispose_f(begin->data);
127-
begin = get(begin->next);
128-
}
129-
m_dispose_f(back->data);
130-
}
133+
disposeAll();
131134
_NBL_ALIGNED_FREE(m_reservedSpace);
132135
}
133136

@@ -148,6 +151,24 @@ class ContiguousMemoryLinkedListBase
148151
return addr;
149152
}
150153

154+
/**
155+
* @brief Calls disposal function on all elements of the list.
156+
*/
157+
inline void disposeAll()
158+
{
159+
if (m_dispose_f && m_begin != invalid_iterator)
160+
{
161+
auto* begin = getBegin();
162+
auto* back = getBack();
163+
while (begin != back)
164+
{
165+
m_dispose_f(begin->data);
166+
begin = get(begin->next);
167+
}
168+
m_dispose_f(back->data);
169+
}
170+
}
171+
151172
//create a new node which stores data at already allocated address,
152173
virtual void insertAt(uint32_t addr, value_t&& val) = 0;
153174

0 commit comments

Comments
 (0)