gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
symbol.c
Go to the documentation of this file.
1#include "gwion_util.h"
2
3#define TABLE_SZ(sz) (sizeof(struct SymTable_) + sz * SZ_INT)
4
5ANN SymTable *new_symbol_table(MemPool p, const size_t sz) {
6 SymTable *const st = mp_malloc2(p, TABLE_SZ(sz));
7 st->sz = sz;
9 st->p = p;
10 return st;
11}
12
13ANN static void free_symbol(MemPool p, const Symbol s) {
14 const Symbol next = s->next;
15 mp_free2(p, sizeof(struct Symbol_ *) + strlen(s->name) + 1, s);
16 if (next) free_symbol(p, next);
17}
18
19ANN void free_symbols(SymTable *const ht) {
21 for (uint i = ht->sz + 1; --i;) {
22 const Symbol s = ht->sym[i - 1];
23 if (s) free_symbol(ht->p, s);
24 }
25 gwt_lock_end(&ht->mutex);
26 mp_free2(ht->p, TABLE_SZ(ht->sz), ht);
27}
28
29#undef TABLE_SZ
30
31ANN2(1, 2)
32static inline Symbol mksymbol(MemPool p, const m_str name, const Symbol next) {
33 const Symbol s = mp_malloc2(p, sizeof(struct Symbol_ *) + strlen(name) + 1);
34 s->next = next;
35 strcpy(s->name, name);
36 return s;
37}
38
39ANN Symbol insert_symbol(SymTable *const ht, const m_str name) {
40 const uint index = hash(name) % ht->sz;
41 const Symbol syms = ht->sym[index];
42 Symbol *const addr = &ht->sym[index];
44 for (Symbol sym = syms; sym; sym = sym->next)
45 if (!strcmp(sym->name, name)) return sym;
46 gwt_lock(&ht->mutex);
47 *addr = mksymbol(ht->p, name, syms);
48 gwt_unlock(&ht->mutex);
49 return ht->sym[index];
50}
unsigned int uint
Definition defs.h:63
#define ANN
Definition defs.h:19
#define LOOP_OPTIM
Definition defs.h:34
#define ANN2(...)
Definition defs.h:20
char * m_str
Definition gwcommon.h:14
meta header (use this to include the whole library)
ANN unsigned int hash(const m_str s0)
#define mp_free2(p, sz, a)
Definition mpool.h:28
#define mp_malloc2(p, sz)
Definition mpool.h:30
size_t sz
Definition symbol.h:16
MemPool p
Definition symbol.h:15
gwtlock_t mutex
Definition symbol.h:14
Symbol sym[]
Definition symbol.h:17
char name[]
Definition symbol.h:11
Symbol next
Definition symbol.h:10
static Symbol mksymbol(MemPool p, const m_str name, const Symbol next)
Definition symbol.c:32
static ANN void free_symbol(MemPool p, const Symbol s)
Definition symbol.c:13
ANN void free_symbols(SymTable *const ht)
Definition symbol.c:19
ANN SymTable * new_symbol_table(MemPool p, const size_t sz)
Definition symbol.c:5
#define TABLE_SZ(sz)
Definition symbol.c:3
ANN Symbol insert_symbol(SymTable *const ht, const m_str name)
Definition symbol.c:39
static ANN int gwt_unlock(gwtlock_t *lock)
Definition threadpool.h:74
static ANN void gwt_lock_end(gwtlock_t *lock)
Definition threadpool.h:95
static ANN int gwt_lock_ini(gwtlock_t *lock)
Definition threadpool.h:89
static ANN int gwt_lock(gwtlock_t *lock)
Definition threadpool.h:71