2023-08-16 12:54:13 +00:00
|
|
|
#include "fs/StorageCache.h"
|
|
|
|
#include "Log.h"
|
2023-08-17 18:14:33 +00:00
|
|
|
#include <luna/Heap.h>
|
2023-08-16 12:54:13 +00:00
|
|
|
#include <luna/ScopeGuard.h>
|
|
|
|
|
|
|
|
static LinkedList<StorageCache> g_storage_caches;
|
|
|
|
|
|
|
|
Result<StorageCache::CacheEntry*> StorageCache::fetch_entry(u64 block)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
CacheEntry* entry = m_cache_entries.try_get_ref(block);
|
|
|
|
if (entry && !entry->buffer.is_empty()) return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheEntry entry {};
|
|
|
|
TRY(m_cache_entries.try_set(block, move(entry)));
|
|
|
|
|
|
|
|
#ifdef CACHE_DEBUG
|
|
|
|
kdbgln("cache: Created new cache entry for block %lu", block);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return m_cache_entries.try_get_ref(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageCache::clear()
|
|
|
|
{
|
2023-08-17 18:14:33 +00:00
|
|
|
m_mutex.lock();
|
2023-08-16 12:54:13 +00:00
|
|
|
|
2023-08-17 18:14:33 +00:00
|
|
|
kdbgln("cache: clearing %lu entries, out of %lu buckets", m_cache_entries.size(), m_cache_entries.capacity());
|
2023-08-16 12:54:13 +00:00
|
|
|
m_cache_entries.clear();
|
2023-08-17 18:14:33 +00:00
|
|
|
kdbgln("cache: done");
|
|
|
|
|
|
|
|
m_mutex.unlock();
|
2023-08-16 12:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StorageCache::StorageCache()
|
|
|
|
{
|
|
|
|
g_storage_caches.append(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
StorageCache::~StorageCache()
|
|
|
|
{
|
|
|
|
g_storage_caches.remove(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StorageCache::clear_caches()
|
|
|
|
{
|
|
|
|
for (auto* cache : g_storage_caches) { cache->clear(); }
|
|
|
|
}
|