libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
static_lru_hash_map.h
Go to the documentation of this file.
1/*
2 * This file is part of the Aaru Data Preservation Suite.
3 * Copyright (c) 2019-2026 Natalia Portillo.
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18
33
34#ifndef LIBAARUFORMAT_STATIC_LRU_HASH_MAP_H
35#define LIBAARUFORMAT_STATIC_LRU_HASH_MAP_H
36
37#include <stdbool.h>
38#include <stdint.h>
39#include <stdlib.h>
40
46#ifndef STATIC_LRU_EVICTION_LOAD_FACTOR
47#define STATIC_LRU_EVICTION_LOAD_FACTOR 0.90
48#endif
49
50#ifndef STATIC_LRU_TARGET_LOAD_FACTOR
51#define STATIC_LRU_TARGET_LOAD_FACTOR 0.75
52#endif
53
54#ifndef STATIC_LRU_AGING_INTERVAL
55#define STATIC_LRU_AGING_INTERVAL 100000
56#endif
57
58#ifndef STATIC_LRU_MIN_SIZE
59#define STATIC_LRU_MIN_SIZE 1024
60#endif
61
71typedef struct
72{
73 uint64_t key;
74 uint64_t value;
75 uint8_t access_count;
76 uint8_t _padding[7];
78
97typedef struct
98{
100 size_t size;
101 size_t count;
102 size_t max_count;
104 uint32_t age_counter;
105 uint32_t _padding;
106
107 // Optional statistics (can be compiled out if not needed)
108#ifdef STATIC_LRU_ENABLE_STATS
109 uint64_t total_lookups;
110 uint64_t cache_hits;
111 uint64_t total_inserts;
112 uint64_t eviction_count;
113 uint64_t eviction_cycles;
114#endif
116
137
150
176bool static_lru_insert_map(static_lru_hash_map_t *map, uint64_t key, uint64_t value);
177
199bool static_lru_lookup_map(static_lru_hash_map_t *map, uint64_t key, uint64_t *out_value);
200
215bool static_lru_contains_key(const static_lru_hash_map_t *map, uint64_t key);
216
228size_t static_lru_evict(static_lru_hash_map_t *map, size_t entries_to_keep);
229
240
249
258
259#ifdef STATIC_LRU_ENABLE_STATS
267double static_lru_hit_rate(const static_lru_hash_map_t *map);
268
274void static_lru_reset_stats(static_lru_hash_map_t *map);
275#endif
276
277#endif // LIBAARUFORMAT_STATIC_LRU_HASH_MAP_H
void static_lru_free_map(static_lru_hash_map_t *map)
Frees all memory associated with a static LRU hash map.
void static_lru_age_counts(static_lru_hash_map_t *map)
Manually ages all access counts.
size_t static_lru_free_slots(const static_lru_hash_map_t *map)
Returns the number of free slots available.
static_lru_hash_map_t * static_lru_create_map(size_t size)
Creates a new static LRU hash map with fixed size.
size_t static_lru_evict(static_lru_hash_map_t *map, size_t entries_to_keep)
Manually triggers eviction of least-used entries.
bool static_lru_lookup_map(static_lru_hash_map_t *map, uint64_t key, uint64_t *out_value)
Looks up a value by key in the static LRU hash map.
double static_lru_load_factor(const static_lru_hash_map_t *map)
Returns the current load factor of the map.
bool static_lru_insert_map(static_lru_hash_map_t *map, uint64_t key, uint64_t value)
Inserts a key-value pair into the static LRU hash map.
bool static_lru_contains_key(const static_lru_hash_map_t *map, uint64_t key)
Checks if a key exists in the map WITHOUT updating access count.
Single key/value slot with access tracking for the static LRU hash map.
uint8_t access_count
Access frequency counter (0-255, saturates at 255).
uint8_t _padding[7]
Padding for 8-byte alignment (24 bytes total per entry).
uint64_t value
Associated value payload (64-bit).
uint64_t key
Stored key (64-bit). 0 indicates an empty slot.
Fixed-size hash map with LRU-like eviction for bounded memory usage.
size_t max_count
Eviction trigger threshold (size * EVICTION_LOAD_FACTOR).
uint32_t age_counter
Operations since last aging.
size_t target_count
Target count after eviction (size * TARGET_LOAD_FACTOR).
size_t count
Number of active (filled) entries.
lru_kv_pair_t * table
Array of key/value slots of length == size.
size_t size
Allocated slot capacity (FIXED at creation).
uint32_t _padding
Padding for alignment.