-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkickstart.h
More file actions
169 lines (157 loc) · 4.86 KB
/
kickstart.h
File metadata and controls
169 lines (157 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef KICKSTART_H
#define KICKSTART_H
#include <inttypes.h>
#define ITERATION_LIMIT_MAX 65536
#define ITERATION_LIMIT_SOFT 2048
#define ITERATION_LIMIT_HARD 256
typedef enum POOL_TAG {
POOL_STATIC=0,
POOL_DYNAMIC=1,
NO_POOL
} POOL_TAG;
typedef struct pool {
POOL_TAG tag;
void* buffer;
void* ptr;
uint64_t left;
struct pool* next;
void* ptr_save;
uint64_t left_save;
} pool;
pool pool_alloc(uint64_t cap, POOL_TAG t);
void pool_empty(pool* const p);
void pool_dealloc(pool* const p);
void* pool_request(pool* const p, uint64_t bytes);
void* pool_request_aligned(pool* const p, uint64_t bytes, uint16_t alignment);
void* pool_byte(pool* const p);
void pool_save(pool* const p);
void pool_load(pool* const p);
typedef struct string {
char* str;
uint64_t len;
} string;
string string_init(pool* const mem, char* src);
string string_copy(pool* const mem, string* const src);
void string_set(pool* const mem, string* const str, char* src);
void string_print(string* const str);
void string_cat(pool* const mem, string* const a, string* const b);
int8_t string_compare(string* const a, string* const b);
int8_t cstring_compare(string* const a, char* b);
string string_escape(pool* const mem, string* const src);
#define MAP_BUCKET_COUNT 128
void string_print(string* const key);
typedef enum MAP_BUCKET_TAG {
MAP_BUCKET_EMPTY,
MAP_BUCKET_FULL
} MAP_BUCKET_TAG;
#define MAP_DECL(type)\
typedef struct type##_map_bucket type##_map_bucket;\
typedef struct type##_map type##_map;\
\
typedef struct type##_map_bucket {\
type data;\
type##_map_bucket* left;\
type##_map_bucket* right;\
uint64_t hash;\
string key;\
MAP_BUCKET_TAG tag;\
} type##_map_bucket;\
\
uint8_t type##_map_bucket_insert(type##_map* map, type##_map_bucket* bucket, string* key, uint64_t hash, type value);\
type* type##_map_bucket_access(type##_map* map, type##_map_bucket* bucket, string* key, uint64_t hash);\
\
typedef struct type##_map {\
type##_map_bucket buckets[MAP_BUCKET_COUNT];\
pool* mem;\
} type##_map;\
\
type##_map type##_map_init(pool* mem);\
uint8_t type##_map_insert(type##_map* map, string key, type value);\
type* type##_map_access(type##_map* map, string key);\
void type##_map_clear(type##_map* map);
#define MAP_IMPL(type)\
uint8_t type##_map_bucket_insert(type##_map* map, type##_map_bucket* bucket, string* key, uint64_t hash, type value){\
if (bucket->tag == MAP_BUCKET_EMPTY){\
bucket->data = value;\
bucket->hash = hash;\
bucket->key = string_copy(map->mem, key);\
bucket->tag = MAP_BUCKET_FULL;\
bucket->left = pool_request(map->mem, sizeof(type##_map_bucket));\
bucket->right = pool_request(map->mem, sizeof(type##_map_bucket));\
bucket->left->tag = MAP_BUCKET_EMPTY;\
bucket->right->tag = MAP_BUCKET_EMPTY;\
return 0;\
}\
if (hash < bucket->hash){\
return type##_map_bucket_insert(map, bucket->left, key, hash, value);\
}\
else if (hash > bucket->hash){\
return type##_map_bucket_insert(map, bucket->right, key, hash, value);\
}\
if (string_compare(key, &bucket->key) < 0){\
return type##_map_bucket_insert(map, bucket->left, key, hash, value);\
}\
else if (string_compare(key, &bucket->key) > 0){\
return type##_map_bucket_insert(map, bucket->right, key, hash, value);\
}\
bucket->data = value;\
return 1;\
}\
\
type* type##_map_bucket_access(type##_map* map, type##_map_bucket* bucket, string* key, uint64_t hash){\
if (bucket->tag == MAP_BUCKET_EMPTY){\
return NULL;\
}\
if (hash < bucket->hash){\
return type##_map_bucket_access(map, bucket->left, key, hash);\
}\
if (hash > bucket->hash){\
return type##_map_bucket_access(map, bucket->right, key, hash);\
}\
if (string_compare(key, &bucket->key) < 0){\
return type##_map_bucket_access(map, bucket->left, key, hash);\
}\
else if (string_compare(key, &bucket->key) > 0){\
return type##_map_bucket_access(map, bucket->right, key, hash);\
}\
return &bucket->data;\
}\
\
type##_map type##_map_init(pool* mem){\
type##_map map = {\
.mem=mem\
};\
for (uint64_t i = 0;i<MAP_BUCKET_COUNT;++i){\
map.buckets[i].tag = MAP_BUCKET_EMPTY;\
}\
return map;\
}\
\
uint8_t type##_map_insert(type##_map* map, string key, type value){\
uint32_t hash = 5381;\
int16_t c;\
for (uint64_t i = 0;i<key.len;++i){\
c = key.str[i];\
hash = ((hash<<5)+hash)+c;\
}\
uint64_t bucket = hash % MAP_BUCKET_COUNT;\
return type##_map_bucket_insert(map, &map->buckets[bucket], &key, hash, value);\
}\
\
type* type##_map_access(type##_map* map, string key){\
uint32_t hash = 5381;\
int16_t c;\
for (uint64_t i = 0;i<key.len;++i){\
c = key.str[i];\
hash = ((hash<<5)+hash)+c;\
}\
uint64_t bucket = hash % MAP_BUCKET_COUNT;\
return type##_map_bucket_access(map, &map->buckets[bucket], &key, hash);\
}\
\
void type##_map_clear(type##_map* map){\
for (uint64_t i = 0;i<MAP_BUCKET_COUNT;++i){\
map->buckets[i].tag = MAP_BUCKET_EMPTY;\
}\
}
#endif