1 /* $Id: strmap.c,v 1.1 2007-12-02 11:30:28 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <zebra_strmap.h>
32 struct strmap_entry *next;
39 struct strmap_entry **entries;
40 struct strmap_entry *free_entries;
43 zebra_strmap_t zebra_strmap_create(void)
46 NMEM nmem_ent = nmem_create();
47 zebra_strmap_t st = nmem_malloc(nmem_ent, sizeof(*st));
48 st->nmem_ent = nmem_ent;
49 st->nmem_str = nmem_create();
52 st->entries = nmem_malloc(nmem_ent, st->hsize * sizeof(*st->entries));
53 for (i = 0; i < st->hsize; i++)
58 void zebra_strmap_destroy(zebra_strmap_t st)
62 nmem_destroy(st->nmem_str);
63 nmem_destroy(st->nmem_ent);
67 static struct strmap_entry **hash(zebra_strmap_t st, const char *name)
71 for (i = 0; name[i]; i++)
72 hash += hash*65519 + name[i];
73 hash = hash % st->hsize;
74 return st->entries + hash;
77 void zebra_strmap_add(zebra_strmap_t st, const char *name,
78 void *data_buf, size_t data_len)
80 struct strmap_entry **e = hash(st, name);
81 struct strmap_entry *ne = st->free_entries;
84 st->free_entries = ne->next;
86 ne = nmem_malloc(st->nmem_ent, sizeof(*ne));
89 ne->name = nmem_strdup(st->nmem_str, name);
90 ne->data_buf = nmem_malloc(st->nmem_str, data_len);
91 memcpy(ne->data_buf, data_buf, data_len);
92 ne->data_len = data_len;
95 void *zebra_strmap_lookup(zebra_strmap_t st, const char *name, int no,
98 struct strmap_entry *e = *hash(st, name);
100 for (; e ; e = e->next)
101 if (!strcmp(name, e->name))
106 *data_len = e->data_len;
114 int zebra_strmap_remove(zebra_strmap_t st, const char *name)
116 struct strmap_entry **e = hash(st, name);
117 for (; *e ; e = &(*e)->next)
118 if (!strcmp(name, (*e)->name))
120 struct strmap_entry *tmp = *e;
123 tmp->next = st->free_entries;
124 st->free_entries = tmp;
133 * indent-tabs-mode: nil
135 * vim: shiftwidth=4 tabstop=8 expandtab