struct trie_node *data[1];
};
typedef struct trie_node *map_t;
+#define TRIE_MAX_KEY 256
struct trie_find {
struct trie_node *n;
void *value;
size_t len;
- size_t cap;
- char key[1];
+ char key[TRIE_MAX_KEY];
};
typedef struct trie_find *map_find_t;
#define map_findfree(f) (free(*(f)), *(f) = 0)
int map_set(map_t *root, const char *key, const void *value, void **oldvalue)
{
int rv = 0;
+ if (strlen(key)+1 > TRIE_MAX_KEY)
+ return -1;
if (oldvalue)
*oldvalue = NULL;
struct trie_node *n = *root;
int ent = 0; // go to the first child node
for (;;) {
if (!trie_is_end(ent, f->n)) {
- if (f->len == f->cap) {
- f->cap *= 2;
- void *nf = realloc(f, sizeof(*f)+f->cap);
- if (!nf) {
- *pf = NULL;
- free(f);
- return -1;
- }
- *pf = f = nf;
- }
f->n = f->n->data[ent];
f->key[f->len++] = f->n->key;
if (f->n->value) { // found a value
if (*pf) {
f = *pf;
} else {
- f = *pf = malloc(sizeof(*f)+16);
+ f = *pf = malloc(sizeof(*f));
if (!f)
return -1;
f->len = 0;
- f->cap = 16;
}
f->n = *root;
if (f->n->value) {