libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
lru.c
Go to the documentation of this file.
1#include <stddef.h>
2#include <stdint.h>
3#include <stdlib.h>
4#include <uthash.h>
5
6#include <aaruformat.h>
7
8// LRU cache using uthash with native integer keys.
9// Based on uthash LRU example by Jehiah Czebotar 2011 (public domain).
10// Rewritten to use HASH_FIND/HASH_ADD with uint64_t keys directly,
11// eliminating all string conversion, malloc, and snprintf overhead
12// from the hot path.
13
24void *find_in_cache_uint64(struct CacheHeader *cache, const uint64_t key)
25{
26 struct CacheEntry *entry = NULL;
27 HASH_FIND(hh, cache->cache, &key, sizeof(uint64_t), entry);
28 if(entry)
29 {
30 // Remove and re-add to move to front of insertion-order list (LRU refresh).
31 HASH_DELETE(hh, cache->cache, entry);
32 HASH_ADD(hh, cache->cache, key, sizeof(uint64_t), entry);
33 return entry->value;
34 }
35 return NULL;
36}
37
48void add_to_cache_uint64(struct CacheHeader *cache, const uint64_t key, void *value)
49{
50 struct CacheEntry *entry = malloc(sizeof(struct CacheEntry));
51 if(!entry) return;
52
53 entry->key = key;
54 entry->value = value;
55 HASH_ADD(hh, cache->cache, key, sizeof(uint64_t), entry);
56
57 // Evict oldest entry if cache exceeded capacity.
58 if(HASH_COUNT(cache->cache) > cache->max_items)
59 {
60 struct CacheEntry *tmp_entry;
61 HASH_ITER(hh, cache->cache, entry, tmp_entry)
62 {
63 HASH_DELETE(hh, cache->cache, entry);
64
65 if(cache->free_func && entry->value)
66 cache->free_func(entry->value);
67
68 free(entry);
69 break;
70 }
71 }
72}
73
82void free_cache(struct CacheHeader *cache)
83{
84 struct CacheEntry *entry, *tmp;
85
86 if(!cache || !cache->cache) return;
87
88 HASH_ITER(hh, cache->cache, entry, tmp)
89 {
90 HASH_DELETE(hh, cache->cache, entry);
91
92 if(cache->free_func && entry->value)
93 cache->free_func(entry->value);
94
95 free(entry);
96 }
97
98 cache->cache = NULL;
99}
void add_to_cache_uint64(struct CacheHeader *cache, const uint64_t key, void *value)
Adds a value to the cache with a uint64_t key, evicting LRU if full.
Definition lru.c:48
void free_cache(struct CacheHeader *cache)
Frees all entries in the cache and clears it.
Definition lru.c:82
void * find_in_cache_uint64(struct CacheHeader *cache, const uint64_t key)
Finds a value in the cache by uint64_t key.
Definition lru.c:24
Single hash entry in the in-memory cache.
Definition lru.h:24
void * value
Opaque value pointer associated with key.
Definition lru.h:26
uint64_t key
64-bit integer key (unique within the cache).
Definition lru.h:25
UT_hash_handle hh
uthash handle (must remain per uthash docs).
Definition lru.h:27
Cache top-level descriptor encapsulating the hash table root and capacity limit.
Definition lru.h:42
struct CacheEntry * cache
Hash root (uthash). NULL when empty.
Definition lru.h:44
uint64_t max_items
Hard limit for number of entries.
Definition lru.h:43
void(* free_func)(void *)
Optional callback to free cached values. NULL if not needed.
Definition lru.h:45