pbs-tools: LruCache: implement Drop
this fixes the leaked memory for the cache, as we had only pointers
in the map/list which were freed, not the underlying chunks
moves the 'clear' implementation out of the trait bounds so that
Drop can reuse it
this is used e.g. for file download from a pxar
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
(cherry picked from commit 98983a9dab
)
This commit is contained in:
parent
9b00099ead
commit
a1cffef503
|
@ -100,9 +100,25 @@ pub struct LruCache<K, V> {
|
||||||
_marker: PhantomData<Box<CacheNode<K, V>>>,
|
_marker: PhantomData<Box<CacheNode<K, V>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<K, V> Drop for LruCache<K, V> {
|
||||||
|
fn drop (&mut self) {
|
||||||
|
self.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// trivial: if our contents are Send, the whole cache is Send
|
// trivial: if our contents are Send, the whole cache is Send
|
||||||
unsafe impl<K: Send, V: Send> Send for LruCache<K, V> {}
|
unsafe impl<K: Send, V: Send> Send for LruCache<K, V> {}
|
||||||
|
|
||||||
|
impl<K, V> LruCache<K, V> {
|
||||||
|
/// Clear all the entries from the cache.
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
// This frees only the HashMap with the node pointers.
|
||||||
|
self.map.clear();
|
||||||
|
// This frees the actual nodes and resets the list head and tail.
|
||||||
|
self.list.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<K: std::cmp::Eq + std::hash::Hash + Copy, V> LruCache<K, V> {
|
impl<K: std::cmp::Eq + std::hash::Hash + Copy, V> LruCache<K, V> {
|
||||||
/// Create LRU cache instance which holds up to `capacity` nodes at once.
|
/// Create LRU cache instance which holds up to `capacity` nodes at once.
|
||||||
pub fn new(capacity: usize) -> Self {
|
pub fn new(capacity: usize) -> Self {
|
||||||
|
@ -115,14 +131,6 @@ impl<K: std::cmp::Eq + std::hash::Hash + Copy, V> LruCache<K, V> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clear all the entries from the cache.
|
|
||||||
pub fn clear(&mut self) {
|
|
||||||
// This frees only the HashMap with the node pointers.
|
|
||||||
self.map.clear();
|
|
||||||
// This frees the actual nodes and resets the list head and tail.
|
|
||||||
self.list.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Insert or update an entry identified by `key` with the given `value`.
|
/// Insert or update an entry identified by `key` with the given `value`.
|
||||||
/// This entry is placed as the most recently used node at the head.
|
/// This entry is placed as the most recently used node at the head.
|
||||||
pub fn insert(&mut self, key: K, value: V) {
|
pub fn insert(&mut self, key: K, value: V) {
|
||||||
|
|
Loading…
Reference in New Issue