-
Notifications
You must be signed in to change notification settings - Fork 4
/
atom_table.c
265 lines (221 loc) · 5.45 KB
/
atom_table.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright (c) 2023 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include <stdio.h>
#include <stdlib.h> /* malloc, free */
#include <string.h> /* memset */
#include <stdint.h>
#include "deconz/atom_table.h"
#define ATOM_PAGE_SIZE (4096 - 8)
typedef struct AT_Page
{
struct AT_Page *next;
unsigned char data[ATOM_PAGE_SIZE];
} AT_Page;
/*
* TODO(mpi): make thread safe via u_threads.
*/
static unsigned atom_table_size;
static unsigned char *atom_page_beg;
static unsigned char *atom_page_end;
static unsigned atom_pages_byte_count;
static AT_Page *atom_pages;
static AT_Atom *atom_table;
static unsigned atom_count;
/*
* https://maskray.me/blog/2023-04-12-elf-hash-function
*
* PJW hash adapted from musl libc.
*/
static uint32_t AT_Hash(const void *s0, unsigned size)
{
uint32_t h;
const unsigned char *s;
h = 0;
s = (const unsigned char*)s0;
while (size--)
{
h = 16 * h + *s++;
h ^= h >> 24 & 0xF0;
}
return h & 0xfffffff;
}
void *AT_Alloc(unsigned long size)
{
return malloc(size);
}
void AT_Free(void *ptr)
{
if (ptr)
free(ptr);
}
static void AT_AllocPage(void)
{
AT_Page *page;
page = (AT_Page*)AT_Alloc(sizeof(*page));
page->next = atom_pages;
atom_pages = page;
atom_page_beg = &page->data[0];
atom_page_end = &page->data[sizeof(page->data)];
atom_pages_byte_count += sizeof(*page);
// printf("atom pages: %u bytes\n", atom_pages_byte_count);
}
static unsigned char *AT_AllocPageData(unsigned size)
{
unsigned char *p = 0;
size += 1; /* terminating '\0' */
if (!atom_pages || (atom_page_beg + size > atom_page_end))
{
AT_AllocPage();
}
if (atom_page_beg + size <= atom_page_end)
{
p = atom_page_beg;
atom_page_beg += size;
}
return p;
}
void AT_Init(unsigned max_atoms)
{
unsigned i;
unsigned long size;
atom_table_size = max_atoms;
atom_pages = 0;
atom_pages_byte_count = 0;
atom_count = 0;
size = max_atoms * sizeof(*atom_table);
atom_table = (AT_Atom*)AT_Alloc(size);
for (i = 0; i < atom_table_size; i++)
{
atom_table[i].len = 0;
atom_table[i].data = 0;
}
atom_table[0].len = 3;
atom_table[0].data = AT_AllocPageData(3); /* allocates also '\0' */;
atom_table[0].data[0] = 'N';
atom_table[0].data[1] = 'U';
atom_table[0].data[2] = 'L';
atom_table[0].data[3] = '\0';
}
void AT_Destroy(void)
{
AT_Page *np;
for (; atom_pages;)
{
np = atom_pages;
atom_pages = np->next;
AT_Free(np);
}
atom_pages_byte_count = 0;
AT_Free(atom_table);
atom_table = 0;
atom_table_size = 0;
atom_count = 0;
}
int AT_AddAtomString(const void *data)
{
unsigned size = 0;
const char *s = data;
if (s)
{
for (size = 0; s[size]; size++)
{}
return AT_AddAtom(data, size, 0);
}
return 0;
}
int AT_AddAtom(const void *data, unsigned size, AT_AtomIndex *ati)
{
unsigned i;
unsigned idx;
unsigned long hash;
const unsigned char *bytes;
AT_Atom *a;
if (data && size && size <= AT_MAX_ATOM_SIZE && atom_count < atom_table_size)
{
hash = AT_Hash(data, size);
for (i = 0; i < atom_table_size; i++)
{
idx = (hash + i) % atom_table_size;
a = &atom_table[idx];
if (a->len == size)
{
if (memcmp(data, a->data, size) == 0)
{
/* already existing */
if (ati)
ati->index = idx;
return 1;
}
}
else if (a->len == 0)
{
a->data = AT_AllocPageData(size); /* allocates also '\0' */
if (a->data)
{
a->len = size;
bytes = data;
for (i = 0; i < size; i++)
a->data[i] = bytes[i];
a->data[size] = '\0';
// printf("add atom[%u] %s\n", atom_count, (const char*)a->data);
atom_count++;
if (ati)
ati->index = idx;
return 1;
}
}
}
}
if (ati)
ati->index = 0;
return 0;
}
int AT_GetAtomIndex(const void *data, unsigned size, AT_AtomIndex *ati)
{
unsigned i;
unsigned idx;
unsigned long hash;
AT_Atom *a;
if (data && size && size <= AT_MAX_ATOM_SIZE && ati)
{
hash = AT_Hash(data, size);
for (i = 0; i < atom_table_size; i++)
{
idx = (hash + i) % atom_table_size;
a = &atom_table[idx];
if (a->len == size)
{
if (memcmp(data, a->data, size) == 0)
{
ati->index = idx;
return 1;
}
}
else if (a->len == 0)
{
break; /* nothing found */
}
}
}
if (ati)
ati->index = 0;
return 0;
}
AT_Atom AT_GetAtomByIndex(AT_AtomIndex ati)
{
if (ati.index < atom_table_size)
{
return atom_table[ati.index];
}
AT_Atom a;
a.data = 0;
a.len = 0;
return a;
}