2022-10-02 16:05:25 +01:00
|
|
|
//
|
|
|
|
|
// Created by claunia on 2/10/22.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#ifndef LIBAARUFORMAT_LRU_H
|
|
|
|
|
#define LIBAARUFORMAT_LRU_H
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <uthash.h>
|
|
|
|
|
|
2025-10-01 05:35:39 +01:00
|
|
|
/** \struct CacheEntry
|
|
|
|
|
* \brief Single hash entry in the in-memory cache.
|
|
|
|
|
*
|
|
|
|
|
* This structure is managed by uthash (open addressing with chaining semantics provided by macros).
|
|
|
|
|
* It represents one key/value association tracked by the cache. The cache implementation supports
|
|
|
|
|
* both string keys (null-terminated) and 64-bit numeric keys; numeric keys are stored by casting
|
|
|
|
|
* to a temporary string buffer upstream (see implementation). Callers do not allocate or free
|
|
|
|
|
* individual entries directly; use the cache API helpers.
|
|
|
|
|
*
|
|
|
|
|
* Lifetime & ownership:
|
|
|
|
|
* - key points either to a heap-allocated C string owned by the cache or to a short-lived buffer
|
|
|
|
|
* duplicated internally; callers must not free it after insertion.
|
|
|
|
|
* - value is an opaque pointer supplied by caller; the cache does not take ownership of the pointee
|
|
|
|
|
* (caller remains responsible for the underlying object unless documented otherwise).
|
|
|
|
|
*/
|
2022-10-02 16:05:25 +01:00
|
|
|
struct CacheEntry
|
|
|
|
|
{
|
2025-10-01 05:35:39 +01:00
|
|
|
char *key; ///< Null-terminated key string (unique within the cache). May encode numeric keys.
|
|
|
|
|
void *value; ///< Opaque value pointer associated with key (not freed automatically on eviction/clear).
|
|
|
|
|
UT_hash_handle hh; ///< uthash handle linking this entry into the hash table (must remain last or per uthash docs).
|
2022-10-02 16:05:25 +01:00
|
|
|
};
|
|
|
|
|
|
2025-10-01 05:35:39 +01:00
|
|
|
/** \struct CacheHeader
|
|
|
|
|
* \brief Cache top-level descriptor encapsulating the hash table root and capacity limit.
|
|
|
|
|
*
|
|
|
|
|
* The cache enforces an upper bound (max_items) on the number of tracked entries. Insert helpers are expected
|
|
|
|
|
* to evict (or refuse) when the limit is exceeded (strategy defined in implementation; current behavior may be
|
|
|
|
|
* simple non-evicting if not yet implemented as a true LRU). The cache pointer holds the uthash root (NULL when
|
|
|
|
|
* empty).
|
|
|
|
|
*
|
|
|
|
|
* Fields:
|
|
|
|
|
* - max_items: Maximum number of entries allowed; 0 means "no explicit limit" if accepted by implementation.
|
|
|
|
|
* - cache: uthash root pointer; NULL when the cache is empty.
|
|
|
|
|
*/
|
2022-10-02 16:05:25 +01:00
|
|
|
struct CacheHeader
|
|
|
|
|
{
|
2025-10-01 05:35:39 +01:00
|
|
|
uint64_t max_items; ///< Hard limit for number of entries (policy: enforce/ignore depends on implementation).
|
|
|
|
|
struct CacheEntry *cache; ///< Hash root (uthash). NULL when empty.
|
2022-10-02 16:05:25 +01:00
|
|
|
};
|
|
|
|
|
|
2025-09-30 13:48:31 +01:00
|
|
|
void *find_in_cache(struct CacheHeader *cache, const char *key);
|
2025-10-01 05:35:39 +01:00
|
|
|
void add_to_cache(struct CacheHeader *cache, const char *key, void *value);
|
2024-04-30 15:51:32 +01:00
|
|
|
void *find_in_cache_uint64(struct CacheHeader *cache, uint64_t key);
|
2025-10-01 05:35:39 +01:00
|
|
|
void add_to_cache_uint64(struct CacheHeader *cache, uint64_t key, void *value);
|
2022-10-02 16:05:25 +01:00
|
|
|
|
2024-04-30 15:51:32 +01:00
|
|
|
#endif // LIBAARUFORMAT_LRU_H
|