libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
lru.c File Reference
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <uthash.h>
#include <aaruformat.h>

Go to the source code of this file.

Functions

static void drop_entry (struct CacheHeader *cache, struct CacheEntry *entry)
 Frees a cache entry's value and unlinks it from the cache.
void * find_in_cache_uint64 (struct CacheHeader *cache, const uint64_t key)
 Finds a value in the cache by uint64_t key.
void add_to_cache_uint64 (struct CacheHeader *cache, const uint64_t key, void *value, const size_t size)
 Adds a value to the cache with a uint64_t key, evicting LRU entries if over budget.
void free_cache (struct CacheHeader *cache)
 Frees all entries in the cache and clears it.

Function Documentation

◆ add_to_cache_uint64()

void add_to_cache_uint64 ( struct CacheHeader * cache,
const uint64_t key,
void * value,
const size_t size )

Adds a value to the cache with a uint64_t key, evicting LRU entries if over budget.

Adds a new entry to the cache. If an entry with the same key is already present, its value is freed and replaced. If the cache exceeds its memory budget, the least recently used entries are evicted until it fits again. The entry just inserted is never evicted, so the caller may keep using the pointer it handed over for the remainder of the call.

Parameters
cachePointer to the cache header.
key64-bit integer key to add.
valuePointer to the value to store.
sizeSize in bytes of the memory pointed to by value.

Definition at line 68 of file lru.c.

References add_to_cache_uint64(), CacheHeader::cache, CacheHeader::cur_bytes, drop_entry(), CacheHeader::free_func, CacheEntry::hh, CacheEntry::key, CacheHeader::max_bytes, CacheEntry::size, and CacheEntry::value.

Referenced by aaruf_read_sector(), add_to_cache_uint64(), and ec_recover_data_block().

◆ drop_entry()

void drop_entry ( struct CacheHeader * cache,
struct CacheEntry * entry )
static

Frees a cache entry's value and unlinks it from the cache.

Parameters
cachePointer to the cache header.
entryEntry to drop. Must be linked in cache.

Definition at line 20 of file lru.c.

References CacheHeader::cache, CacheHeader::cur_bytes, drop_entry(), CacheHeader::free_func, CacheEntry::size, and CacheEntry::value.

Referenced by add_to_cache_uint64(), drop_entry(), and free_cache().

◆ find_in_cache_uint64()

void * find_in_cache_uint64 ( struct CacheHeader * cache,
const uint64_t key )

Finds a value in the cache by uint64_t key.

Searches for a value using a native 64-bit integer key and promotes it to the front of the insertion-order list (LRU refresh) if found.

Parameters
cachePointer to the cache header.
key64-bit integer key to search for.
Returns
Pointer to the value if found, or NULL if not found.

Definition at line 41 of file lru.c.

References CacheHeader::cache, find_in_cache_uint64(), CacheEntry::hh, CacheEntry::key, and CacheEntry::value.

Referenced by aaruf_read_sector(), and find_in_cache_uint64().

◆ free_cache()

void free_cache ( struct CacheHeader * cache)

Frees all entries in the cache and clears it.

Iterates through all cache entries, frees them, then clears the hash table. Uses the cache's free_func if set to free cached values.

Parameters
cachePointer to the cache header.

Definition at line 118 of file lru.c.

References CacheHeader::cache, CacheHeader::cur_bytes, drop_entry(), free_cache(), and CacheEntry::hh.

Referenced by aaruf_close(), and free_cache().