2 * Copyright (C) 1998 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2, or (at your
9 * option) any later version.
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
27 /* Allocate a new hash. */
29 hash_create_size (unsigned int size, unsigned int (*hash_key) (void *),
30 int (*hash_cmp) (const void *, const void *))
34 assert ((size & (size-1)) == 0);
35 hash = XMALLOC (MTYPE_HASH, sizeof (struct hash));
36 hash->index = XCALLOC (MTYPE_HASH_INDEX,
37 sizeof (struct hash_backet *) * size);
40 hash->hash_key = hash_key;
41 hash->hash_cmp = hash_cmp;
47 /* Allocate a new hash with default hash size. */
49 hash_create (unsigned int (*hash_key) (void *),
50 int (*hash_cmp) (const void *, const void *))
52 return hash_create_size (HASH_INITIAL_SIZE, hash_key, hash_cmp);
55 /* Utility function for hash_get(). When this function is specified
56 as alloc_func, return arugment as it is. This function is used for
57 intern already allocated value. */
59 hash_alloc_intern (void *arg)
64 /* Expand hash if the chain length exceeds the threshold. */
65 static void hash_expand (struct hash *hash)
67 unsigned int i, new_size, losers;
68 struct hash_backet *hb, *hbnext, **new_index;
70 new_size = hash->size * 2;
71 new_index = XCALLOC(MTYPE_HASH_INDEX, sizeof(struct hash_backet *) * new_size);
72 if (new_index == NULL)
75 for (i = 0; i < hash->size; i++)
76 for (hb = hash->index[i]; hb; hb = hbnext)
78 unsigned int h = hb->key & (new_size - 1);
81 hb->next = new_index[h];
85 /* Switch to new table */
86 XFREE(MTYPE_HASH_INDEX, hash->index);
87 hash->size = new_size;
88 hash->index = new_index;
90 /* Ideally, new index should have chains half as long as the original.
91 If expansion didn't help, then not worth expanding again,
92 the problem is the hash function. */
94 for (i = 0; i < hash->size; i++)
97 for (hb = hash->index[i]; hb; hb = hb->next)
99 if (++len > HASH_THRESHOLD/2)
101 if (len >= HASH_THRESHOLD)
106 if (losers > hash->count / 2)
110 /* Lookup and return hash backet in hash. If there is no
111 corresponding hash backet and alloc_func is specified, create new
114 hash_get (struct hash *hash, void *data, void * (*alloc_func) (void *))
120 struct hash_backet *backet;
122 key = (*hash->hash_key) (data);
123 index = key & (hash->size - 1);
126 for (backet = hash->index[index]; backet != NULL; backet = backet->next)
128 if (backet->key == key && (*hash->hash_cmp) (backet->data, data))
135 newdata = (*alloc_func) (data);
139 if (len > HASH_THRESHOLD && !hash->no_expand)
142 index = key & (hash->size - 1);
145 backet = XMALLOC (MTYPE_HASH_BACKET, sizeof (struct hash_backet));
146 backet->data = newdata;
148 backet->next = hash->index[index];
149 hash->index[index] = backet;
158 hash_lookup (struct hash *hash, void *data)
160 return hash_get (hash, data, NULL);
163 /* Simple Bernstein hash which is simple and fast for common case */
164 unsigned int string_hash_make (const char *str)
166 unsigned int hash = 0;
169 hash = (hash * 33) ^ (unsigned int) *str++;
174 /* This function release registered value from specified hash. When
175 release is successfully finished, return the data pointer in the
178 hash_release (struct hash *hash, void *data)
183 struct hash_backet *backet;
184 struct hash_backet *pp;
186 key = (*hash->hash_key) (data);
187 index = key & (hash->size - 1);
189 for (backet = pp = hash->index[index]; backet; backet = backet->next)
191 if (backet->key == key && (*hash->hash_cmp) (backet->data, data))
194 hash->index[index] = backet->next;
196 pp->next = backet->next;
199 XFREE (MTYPE_HASH_BACKET, backet);
208 /* Iterator function for hash. */
210 hash_iterate (struct hash *hash,
211 void (*func) (struct hash_backet *, void *), void *arg)
214 struct hash_backet *hb;
215 struct hash_backet *hbnext;
217 for (i = 0; i < hash->size; i++)
218 for (hb = hash->index[i]; hb; hb = hbnext)
220 /* get pointer to next hash backet here, in case (*func)
221 * decides to delete hb by calling hash_release
230 hash_clean (struct hash *hash, void (*free_func) (void *))
233 struct hash_backet *hb;
234 struct hash_backet *next;
236 for (i = 0; i < hash->size; i++)
238 for (hb = hash->index[i]; hb; hb = next)
243 (*free_func) (hb->data);
245 XFREE (MTYPE_HASH_BACKET, hb);
248 hash->index[i] = NULL;
252 /* Free hash memory. You may call hash_clean before call this
255 hash_free (struct hash *hash)
257 XFREE (MTYPE_HASH_INDEX, hash->index);
258 XFREE (MTYPE_HASH, hash);