ripd: fix compiler warnings
[jleu-quagga.git] / lib / hash.c
blob672327ec5b990726e13212cfe4184efc3d054918
1 /* Hash routine.
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.
22 #include <zebra.h>
24 #include "hash.h"
25 #include "memory.h"
27 /* Allocate a new hash. */
28 struct hash *
29 hash_create_size (unsigned int size, unsigned int (*hash_key) (void *),
30 int (*hash_cmp) (const void *, const void *))
32 struct hash *hash;
34 hash = XMALLOC (MTYPE_HASH, sizeof (struct hash));
35 hash->index = XCALLOC (MTYPE_HASH_INDEX,
36 sizeof (struct hash_backet *) * size);
37 hash->size = size;
38 hash->hash_key = hash_key;
39 hash->hash_cmp = hash_cmp;
40 hash->count = 0;
42 return hash;
45 /* Allocate a new hash with default hash size. */
46 struct hash *
47 hash_create (unsigned int (*hash_key) (void *),
48 int (*hash_cmp) (const void *, const void *))
50 return hash_create_size (HASHTABSIZE, hash_key, hash_cmp);
53 /* Utility function for hash_get(). When this function is specified
54 as alloc_func, return arugment as it is. This function is used for
55 intern already allocated value. */
56 void *
57 hash_alloc_intern (void *arg)
59 return arg;
62 /* Lookup and return hash backet in hash. If there is no
63 corresponding hash backet and alloc_func is specified, create new
64 hash backet. */
65 void *
66 hash_get (struct hash *hash, void *data, void * (*alloc_func) (void *))
68 unsigned int key;
69 unsigned int index;
70 void *newdata;
71 struct hash_backet *backet;
73 key = (*hash->hash_key) (data);
74 index = key % hash->size;
76 for (backet = hash->index[index]; backet != NULL; backet = backet->next)
77 if (backet->key == key && (*hash->hash_cmp) (backet->data, data))
78 return backet->data;
80 if (alloc_func)
82 newdata = (*alloc_func) (data);
83 if (newdata == NULL)
84 return NULL;
86 backet = XMALLOC (MTYPE_HASH_BACKET, sizeof (struct hash_backet));
87 backet->data = newdata;
88 backet->key = key;
89 backet->next = hash->index[index];
90 hash->index[index] = backet;
91 hash->count++;
92 return backet->data;
94 return NULL;
97 /* Hash lookup. */
98 void *
99 hash_lookup (struct hash *hash, void *data)
101 return hash_get (hash, data, NULL);
104 /* This function release registered value from specified hash. When
105 release is successfully finished, return the data pointer in the
106 hash backet. */
107 void *
108 hash_release (struct hash *hash, void *data)
110 void *ret;
111 unsigned int key;
112 unsigned int index;
113 struct hash_backet *backet;
114 struct hash_backet *pp;
116 key = (*hash->hash_key) (data);
117 index = key % hash->size;
119 for (backet = pp = hash->index[index]; backet; backet = backet->next)
121 if (backet->key == key && (*hash->hash_cmp) (backet->data, data))
123 if (backet == pp)
124 hash->index[index] = backet->next;
125 else
126 pp->next = backet->next;
128 ret = backet->data;
129 XFREE (MTYPE_HASH_BACKET, backet);
130 hash->count--;
131 return ret;
133 pp = backet;
135 return NULL;
138 /* Iterator function for hash. */
139 void
140 hash_iterate (struct hash *hash,
141 void (*func) (struct hash_backet *, void *), void *arg)
143 unsigned int i;
144 struct hash_backet *hb;
145 struct hash_backet *hbnext;
147 for (i = 0; i < hash->size; i++)
148 for (hb = hash->index[i]; hb; hb = hbnext)
150 /* get pointer to next hash backet here, in case (*func)
151 * decides to delete hb by calling hash_release
153 hbnext = hb->next;
154 (*func) (hb, arg);
158 /* Clean up hash. */
159 void
160 hash_clean (struct hash *hash, void (*free_func) (void *))
162 unsigned int i;
163 struct hash_backet *hb;
164 struct hash_backet *next;
166 for (i = 0; i < hash->size; i++)
168 for (hb = hash->index[i]; hb; hb = next)
170 next = hb->next;
172 if (free_func)
173 (*free_func) (hb->data);
175 XFREE (MTYPE_HASH_BACKET, hb);
176 hash->count--;
178 hash->index[i] = NULL;
182 /* Free hash memory. You may call hash_clean before call this
183 function. */
184 void
185 hash_free (struct hash *hash)
187 XFREE (MTYPE_HASH_INDEX, hash->index);
188 XFREE (MTYPE_HASH, hash);