Subastra
Loading...
Searching...
No Matches
names.h
Go to the documentation of this file.
1#ifndef __H__NAMES__
2#define __H__NAMES__
3
4#include "arena.h"
5#include "defs.h"
6#include "list.h"
7#include "map.h"
8#include "memory.h"
9
10#ifndef NAME_ARENA_SIZE
11#define NAME_ARENA_SIZE 0x10000
12#endif
13
14typedef u64 name_t;
15
16static arena_t _name_arena = {};
19static bool _name_list_is_locked = false;
20
21static void names_init() {
23 allocator_t alloc = arena_as_allocator(&_name_arena);
24
25 list_init_ty(const char *, &_name_list, alloc);
26 map_init_ty(const char *, &_name_map, alloc);
27}
28
29// Lock names at some point since all the fast strings
30// should have been initialized and created
31static void names_lock() { _name_list_is_locked = true; }
32
33static name_t as_name(const char *str) {
34 for (name_t i = 0; i < _name_list.size; i++)
35 if (strcmp(str, list_get_ty(const char *, &_name_list, i)) == 0)
36 return i;
37
39 "Name list must be unlocked to add new name `%s`", str);
40
42
43 allocator_t alloc = arena_as_allocator(&_name_arena);
44 char *str_copy = allocator_alloc_copy_ty(char, alloc, str, strlen(str) + 1);
45 list_push(&_name_list, &str_copy);
46 map_insert_ty(const char *, &_name_map, idx, &str_copy);
47
48 return idx;
49}
50
51static const char *name_as_str(name_t name) {
52 return *map_get_ty(const char *, &_name_map, name);
53}
54
55static void names_cleanup() {
57 map_cleanup(&_name_map);
58 arena_cleanup(&_name_arena);
59}
60
61#endif
#define ASSERT(expr, msg,...)
Definition defs.h:35
uint64_t u64
Definition defs.h:46
#define list_init_ty(ty, ls, alloc)
Definition list.h:36
#define list_get_ty(ty, ls, idx)
Definition list.h:130
static void list_cleanup(list_t *ls)
Definition list.h:166
static void list_push(list_t *ls, const void *data)
Definition list.h:72
#define map_get_ty(ty, map, key)
Returns a typed pointer to a member at key key. Returns NULL if the key is not present.
Definition map.h:218
#define map_insert_ty(ty, map, key, data)
Definition map.h:196
static allocator_t allocator_new_malloc()
Wrap malloc into an generic allocator interface.
Definition memory.h:99
static name_t as_name(const char *str)
Definition names.h:33
static void names_lock()
Definition names.h:31
static void names_init()
Definition names.h:21
static map_t _name_map
Definition names.h:18
static const char * name_as_str(name_t name)
Definition names.h:51
u64 name_t
Definition names.h:14
static list_t _name_list
Definition names.h:17
static arena_t _name_arena
Definition names.h:16
static bool _name_list_is_locked
Definition names.h:19
#define NAME_ARENA_SIZE
Definition names.h:11
static void names_cleanup()
Definition names.h:55
A generic allocator type passed by value. Contains a fallback allocator and a set of function pointer...
Definition memory.h:30
The Arena allocator is a single continous block of memory.
Definition arena.h:17
Definition list.h:13
sz size
Definition list.h:17
Definition map.h:28