/[LeafOK_CVS]/lbbs/src/trie_dict.c
ViewVC logotype

Contents of /lbbs/src/trie_dict.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (show annotations)
Sat Jan 3 10:27:14 2026 UTC (2 months, 1 week ago) by sysadm
Branch: MAIN
CVS Tags: HEAD
Changes since 1.26: +1 -1 lines
Content type: text/x-csrc
Update copyright info

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * trie_dict
4 * - trie-tree based dict feature
5 *
6 * Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com>
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include "common.h"
14 #include "log.h"
15 #include "trie_dict.h"
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include <sys/mman.h>
24 #include <sys/stat.h>
25
26 struct trie_node_pool_t
27 {
28 size_t shm_size;
29 int node_count_limit;
30 int node_count;
31 TRIE_NODE *p_node_free_list;
32 };
33 typedef struct trie_node_pool_t TRIE_NODE_POOL;
34
35 static char trie_node_shm_name[FILE_NAME_LEN];
36 static TRIE_NODE_POOL *p_trie_node_pool;
37
38 int trie_dict_init(const char *filename, int node_count_limit)
39 {
40 char filepath[FILE_PATH_LEN];
41 int fd;
42 size_t size;
43 void *p_shm;
44 TRIE_NODE *p_trie_nodes;
45 int i;
46
47 if (filename == NULL)
48 {
49 log_error("NULL pointer error");
50 return -1;
51 }
52
53 if (p_trie_node_pool != NULL)
54 {
55 log_error("trie_dict_pool already initialized");
56 return -1;
57 }
58
59 if (node_count_limit <= 0 || node_count_limit > TRIE_NODE_PER_POOL)
60 {
61 log_error("trie_dict_init(%d) error: invalid node_count_limit", node_count_limit);
62 return -1;
63 }
64
65 // Allocate shared memory
66 size = sizeof(TRIE_NODE_POOL) + sizeof(TRIE_NODE) * (size_t)node_count_limit;
67
68 strncpy(filepath, filename, sizeof(filepath) - 1);
69 filepath[sizeof(filepath) - 1] = '\0';
70 snprintf(trie_node_shm_name, sizeof(trie_node_shm_name), "/TRIE_DICT_SHM_%s", basename(filepath));
71
72 if (shm_unlink(trie_node_shm_name) == -1 && errno != ENOENT)
73 {
74 log_error("shm_unlink(%s) error (%d)", trie_node_shm_name, errno);
75 return -2;
76 }
77
78 if ((fd = shm_open(trie_node_shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
79 {
80 log_error("shm_open(%s) error (%d)", trie_node_shm_name, errno);
81 return -2;
82 }
83 if (ftruncate(fd, (off_t)size) == -1)
84 {
85 log_error("ftruncate(size=%d) error (%d)", size, errno);
86 close(fd);
87 return -2;
88 }
89
90 p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
91 if (p_shm == MAP_FAILED)
92 {
93 log_error("mmap() error (%d)", errno);
94 close(fd);
95 return -2;
96 }
97
98 if (close(fd) < 0)
99 {
100 log_error("close(fd) error (%d)", errno);
101 return -1;
102 }
103
104 p_trie_node_pool = p_shm;
105 p_trie_node_pool->shm_size = size;
106 p_trie_node_pool->node_count_limit = node_count_limit;
107 p_trie_node_pool->node_count = 0;
108
109 p_trie_nodes = (TRIE_NODE *)((char *)p_shm + sizeof(TRIE_NODE_POOL));
110 p_trie_node_pool->p_node_free_list = &(p_trie_nodes[0]);
111 for (i = 0; i < node_count_limit - 1; i++)
112 {
113 p_trie_nodes[i].p_nodes[0] = &(p_trie_nodes[i + 1]);
114 }
115 p_trie_nodes[node_count_limit - 1].p_nodes[0] = NULL;
116
117 return 0;
118 }
119
120 void trie_dict_cleanup(void)
121 {
122 if (p_trie_node_pool == NULL)
123 {
124 return;
125 }
126
127 detach_trie_dict_shm();
128
129 if (shm_unlink(trie_node_shm_name) == -1 && errno != ENOENT)
130 {
131 log_error("shm_unlink(%s) error (%d)", trie_node_shm_name, errno);
132 }
133
134 trie_node_shm_name[0] = '\0';
135 }
136
137 int set_trie_dict_shm_readonly(void)
138 {
139 if (p_trie_node_pool != NULL && mprotect(p_trie_node_pool, p_trie_node_pool->shm_size, PROT_READ) < 0)
140 {
141 log_error("mprotect() error (%d)", errno);
142 return -1;
143 }
144
145 return 0;
146 }
147
148 int detach_trie_dict_shm(void)
149 {
150 if (p_trie_node_pool != NULL && munmap(p_trie_node_pool, p_trie_node_pool->shm_size) < 0)
151 {
152 log_error("munmap() error (%d)", errno);
153 return -1;
154 }
155
156 p_trie_node_pool = NULL;
157
158 return 0;
159 }
160
161 TRIE_NODE *trie_dict_create(void)
162 {
163 TRIE_NODE *p_dict = NULL;
164
165 if (p_trie_node_pool != NULL && p_trie_node_pool->p_node_free_list != NULL)
166 {
167 p_dict = p_trie_node_pool->p_node_free_list;
168 p_trie_node_pool->p_node_free_list = p_dict->p_nodes[0];
169
170 memset(p_dict, 0, sizeof(*p_dict));
171
172 p_trie_node_pool->node_count++;
173 }
174 else if (p_trie_node_pool != NULL)
175 {
176 log_error("trie_dict_create() error: node depleted %d >= %d", p_trie_node_pool->node_count, p_trie_node_pool->node_count_limit);
177 }
178
179 return p_dict;
180 }
181
182 void trie_dict_destroy(TRIE_NODE *p_dict)
183 {
184 if (p_trie_node_pool == NULL || p_dict == NULL)
185 {
186 return;
187 }
188
189 for (int i = 0; i < TRIE_CHILDREN; i++)
190 {
191 if (p_dict->p_nodes[i] != NULL)
192 {
193 trie_dict_destroy(p_dict->p_nodes[i]);
194 }
195 }
196
197 memset(p_dict, 0, sizeof(*p_dict));
198
199 p_dict->p_nodes[0] = p_trie_node_pool->p_node_free_list;
200 p_trie_node_pool->p_node_free_list = p_dict;
201
202 p_trie_node_pool->node_count--;
203 }
204
205 int trie_dict_set(TRIE_NODE *p_dict, const char *key, int64_t value)
206 {
207 int offset;
208
209 if (p_dict == NULL)
210 {
211 return -1;
212 }
213
214 while (key != NULL && *key != '\0')
215 {
216 offset = (unsigned char)(*key);
217 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
218 {
219 return -1;
220 }
221
222 if (*(key + 1) == '\0')
223 {
224 if (p_dict->flags[offset] == 0 || p_dict->values[offset] != value)
225 {
226 p_dict->values[offset] = value;
227 p_dict->flags[offset] = 1;
228 return 1; // Set to new value
229 }
230 return 0; // Unchanged
231 }
232 else
233 {
234 if (p_dict->p_nodes[offset] == NULL)
235 {
236 if ((p_dict->p_nodes[offset] = trie_dict_create()) == NULL)
237 {
238 return -2; // OOM
239 }
240 }
241 p_dict = p_dict->p_nodes[offset];
242 key++;
243 }
244 }
245
246 return -1; // NULL key
247 }
248
249 int trie_dict_get(TRIE_NODE *p_dict, const char *key, int64_t *p_value)
250 {
251 int offset;
252
253 if (p_dict == NULL)
254 {
255 return -1;
256 }
257
258 while (key != NULL && *key != '\0')
259 {
260 offset = (unsigned char)(*key);
261 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
262 {
263 return -1;
264 }
265
266 if (*(key + 1) == '\0')
267 {
268 if (p_dict->flags[offset] != 0)
269 {
270 *p_value = p_dict->values[offset];
271 return 1; // Found
272 }
273 else
274 {
275 return 0; // Not exist
276 }
277 }
278 else if (p_dict->p_nodes[offset] == NULL)
279 {
280 return 0; // Not exist
281 }
282 else
283 {
284 p_dict = p_dict->p_nodes[offset];
285 key++;
286 }
287 }
288
289 return -1; // NULL key
290 }
291
292 int trie_dict_del(TRIE_NODE *p_dict, const char *key)
293 {
294 int offset;
295
296 if (p_dict == NULL)
297 {
298 return -1;
299 }
300
301 while (key != NULL && *key != '\0')
302 {
303 offset = (unsigned char)(*key);
304 if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
305 {
306 return -1;
307 }
308
309 if (*(key + 1) == '\0')
310 {
311 if (p_dict->flags[offset] != 0)
312 {
313 p_dict->flags[offset] = 0;
314 p_dict->values[offset] = 0;
315 return 1; // Done
316 }
317 else
318 {
319 return 0; // Not exist
320 }
321 }
322 else if (p_dict->p_nodes[offset] == NULL)
323 {
324 return 0; // Not exist
325 }
326 else
327 {
328 p_dict = p_dict->p_nodes[offset];
329 key++;
330 }
331 }
332
333 return -1; // NULL key
334 }
335
336 static void _trie_dict_traverse(TRIE_NODE *p_dict, trie_dict_traverse_cb cb, char *key, int depth)
337 {
338 if (p_dict == NULL || depth >= TRIE_MAX_KEY_LEN)
339 {
340 return;
341 }
342
343 for (int i = 0; i < TRIE_CHILDREN; i++)
344 {
345 if (p_dict->flags[i] != 0)
346 {
347 key[depth] = (char)i;
348 key[depth + 1] = '\0';
349 (*cb)(key, p_dict->values[i]);
350 }
351
352 if (p_dict->p_nodes[i] != NULL && depth + 1 < TRIE_MAX_KEY_LEN)
353 {
354 key[depth] = (char)i;
355 _trie_dict_traverse(p_dict->p_nodes[i], cb, key, depth + 1);
356 }
357 }
358 }
359
360 void trie_dict_traverse(TRIE_NODE *p_dict, trie_dict_traverse_cb cb)
361 {
362 char key[TRIE_MAX_KEY_LEN + 1];
363
364 if (p_dict == NULL)
365 {
366 return;
367 }
368
369 _trie_dict_traverse(p_dict, cb, key, 0);
370 }
371
372 int trie_dict_used_nodes(void)
373 {
374 return (p_trie_node_pool == NULL ? -1 : p_trie_node_pool->node_count);
375 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1