PatchworkOS
Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1#pragma once
2
3#include <assert.h>
4#include <stdbool.h>
5#include <stdint.h>
6#include <string.h>
7#include <sys/io.h>
8
20#define MAP_MIN_CAPACITY 16
21
25#define MAP_MAX_LOAD_PERCENTAGE 75
26
30#define MAP_TOMBSTONE ((map_entry_t*)1)
31
35#define MAP_KEY_MAX_LENGTH 40
36
50
56typedef struct
57{
60
67#define MAP_ENTRY_PTR_IS_VALID(entryPtr) ((entryPtr) != NULL && (entryPtr) != MAP_TOMBSTONE)
68
82
90uint64_t hash_object(const void* object, uint64_t length);
91
99static inline map_key_t map_key_buffer(const void* buffer, uint64_t length)
100{
101 assert(length <= MAP_KEY_MAX_LENGTH);
102 map_key_t key;
103 memcpy(key.key, buffer, length);
104 key.len = length;
105 key.hash = hash_object(buffer, length);
106 return key;
107}
108
115static inline map_key_t map_key_uint64(uint64_t uint64)
116{
117 map_key_t key;
118 memcpy(key.key, &uint64, sizeof(uint64_t));
119 key.len = sizeof(uint64_t);
120 key.hash = hash_object(&uint64, sizeof(uint64_t));
121 return key;
122}
123
130static inline map_key_t map_key_string(const char* str)
131{
132 return map_key_buffer(str, strlen(str));
133}
134
140void map_entry_init(map_entry_t* entry);
141
149
155void map_deinit(map_t* map);
156
167uint64_t map_insert(map_t* map, const map_key_t* key, map_entry_t* value);
168
176map_entry_t* map_get(map_t* map, const map_key_t* key);
177
186void map_remove(map_t* map, const map_key_t* key);
187
194uint64_t map_size(const map_t* map);
195
203
210bool map_is_empty(const map_t* map);
211
219bool map_contains(map_t* map, const map_key_t* key);
220
228void map_clear(map_t* map);
229
237uint64_t map_reserve(map_t* map, uint64_t minCapacity);
238
#define assert(expression)
Definition assert.h:29
uint64_t hash_object(const void *object, uint64_t length)
Hash a object.
Definition map.c:42
static map_key_t map_key_buffer(const void *buffer, uint64_t length)
Create a map key from a buffer.
Definition map.h:99
uint64_t map_capacity(const map_t *map)
Get the capacity of the map.
Definition map.c:284
bool map_contains(map_t *map, const map_key_t *key)
Check if the map contains a key.
Definition map.c:294
void map_clear(map_t *map)
Clear all entries from the map.
Definition map.c:299
void map_deinit(map_t *map)
Deinitialize a map.
Definition map.c:182
#define MAP_KEY_MAX_LENGTH
The maximum length of a key in the map.
Definition map.h:35
void map_entry_init(map_entry_t *entry)
Initialize a map entry.
Definition map.c:71
static map_key_t map_key_uint64(uint64_t uint64)
Create a map key from a uint64_t.
Definition map.h:115
uint64_t map_size(const map_t *map)
Get the number of entries in the map.
Definition map.c:279
uint64_t map_insert(map_t *map, const map_key_t *key, map_entry_t *value)
Insert a key-value pair into the map.
Definition map.c:191
uint64_t map_init(map_t *map)
Initialize a map.
Definition map.c:172
void map_remove(map_t *map, const map_key_t *key)
Remove a key-value pair from the map.
Definition map.c:258
bool map_is_empty(const map_t *map)
Check if the map is empty.
Definition map.c:289
map_entry_t * map_get(map_t *map, const map_key_t *key)
Get a value from the map by key.
Definition map.c:236
uint64_t map_reserve(map_t *map, uint64_t minCapacity)
Reserve space in the map for at least minCapacity entries.
Definition map.c:310
static map_key_t map_key_string(const char *str)
Create a map key from a string.
Definition map.h:130
boot_memory_map_t * map
Definition mem.c:19
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
_PUBLIC void * memcpy(void *_RESTRICT s1, const void *_RESTRICT s2, size_t n)
Definition memcpy.c:4
_PUBLIC size_t strlen(const char *s)
Definition strlen.c:3
Map entry structure.
Definition map.h:57
map_key_t key
Definition map.h:58
Map key stucture.
Definition map.h:45
uint8_t key[MAP_KEY_MAX_LENGTH]
Definition map.h:46
uint64_t len
Definition map.h:47
uint64_t hash
Definition map.h:48
Hash map structure.
Definition map.h:76
uint64_t capacity
Definition map.h:78
map_entry_t ** entries
Definition map.h:77
uint64_t tombstones
Definition map.h:80
uint64_t length
Definition map.h:79