paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Hash routine. |
| 2 | * Copyright (C) 1998 Kunihiro Ishiguro |
| 3 | * |
| 4 | * This file is part of GNU Zebra. |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include "hash.h" |
| 25 | #include "memory.h" |
| 26 | |
| 27 | /* Allocate a new hash. */ |
| 28 | struct hash * |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 29 | hash_create_size (unsigned int size, unsigned int (*hash_key) (void *), |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 30 | int (*hash_cmp) (const void *, const void *)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | { |
| 32 | struct hash *hash; |
| 33 | |
Stephen Hemminger | 90645f5 | 2013-01-04 22:29:21 +0000 | [diff] [blame] | 34 | assert ((size & (size-1)) == 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | hash = XMALLOC (MTYPE_HASH, sizeof (struct hash)); |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 36 | hash->index = XCALLOC (MTYPE_HASH_INDEX, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | sizeof (struct hash_backet *) * size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 38 | hash->size = size; |
| 39 | hash->hash_key = hash_key; |
| 40 | hash->hash_cmp = hash_cmp; |
| 41 | hash->count = 0; |
| 42 | |
| 43 | return hash; |
| 44 | } |
| 45 | |
| 46 | /* Allocate a new hash with default hash size. */ |
| 47 | struct hash * |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 48 | hash_create (unsigned int (*hash_key) (void *), |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 49 | int (*hash_cmp) (const void *, const void *)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 50 | { |
Stephen Hemminger | 97c84db | 2013-01-11 18:25:26 +0000 | [diff] [blame] | 51 | return hash_create_size (HASH_INITIAL_SIZE, hash_key, hash_cmp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* Utility function for hash_get(). When this function is specified |
| 55 | as alloc_func, return arugment as it is. This function is used for |
| 56 | intern already allocated value. */ |
| 57 | void * |
| 58 | hash_alloc_intern (void *arg) |
| 59 | { |
| 60 | return arg; |
| 61 | } |
| 62 | |
Stephen Hemminger | 97c84db | 2013-01-11 18:25:26 +0000 | [diff] [blame] | 63 | /* Expand hash if the chain length exceeds the threshold. */ |
| 64 | static void hash_expand (struct hash *hash) |
| 65 | { |
| 66 | unsigned int i, new_size, losers; |
| 67 | struct hash_backet *hb, *hbnext, **new_index; |
| 68 | |
| 69 | new_size = hash->size * 2; |
| 70 | new_index = XCALLOC(MTYPE_HASH_INDEX, sizeof(struct hash_backet *) * new_size); |
| 71 | if (new_index == NULL) |
| 72 | return; |
| 73 | |
| 74 | for (i = 0; i < hash->size; i++) |
| 75 | for (hb = hash->index[i]; hb; hb = hbnext) |
| 76 | { |
| 77 | unsigned int h = hb->key & (new_size - 1); |
| 78 | |
| 79 | hbnext = hb->next; |
| 80 | hb->next = new_index[h]; |
| 81 | new_index[h] = hb; |
| 82 | } |
| 83 | |
| 84 | /* Switch to new table */ |
| 85 | XFREE(MTYPE_HASH_INDEX, hash->index); |
| 86 | hash->size = new_size; |
| 87 | hash->index = new_index; |
| 88 | |
| 89 | /* Ideally, new index should have chains half as long as the original. |
| 90 | If expansion didn't help, then not worth expanding again, |
| 91 | the problem is the hash function. */ |
| 92 | losers = 0; |
| 93 | for (i = 0; i < hash->size; i++) |
| 94 | { |
| 95 | unsigned int len = 0; |
| 96 | for (hb = hash->index[i]; hb; hb = hb->next) |
| 97 | { |
| 98 | if (++len > HASH_THRESHOLD/2) |
| 99 | ++losers; |
| 100 | if (len >= HASH_THRESHOLD) |
| 101 | hash->no_expand = 1; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (losers > hash->count / 2) |
| 106 | hash->no_expand = 1; |
| 107 | } |
| 108 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 109 | /* Lookup and return hash backet in hash. If there is no |
| 110 | corresponding hash backet and alloc_func is specified, create new |
| 111 | hash backet. */ |
| 112 | void * |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 113 | hash_get (struct hash *hash, void *data, void * (*alloc_func) (void *)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 114 | { |
| 115 | unsigned int key; |
| 116 | unsigned int index; |
| 117 | void *newdata; |
Stephen Hemminger | 97c84db | 2013-01-11 18:25:26 +0000 | [diff] [blame] | 118 | unsigned int len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | struct hash_backet *backet; |
| 120 | |
| 121 | key = (*hash->hash_key) (data); |
Stephen Hemminger | 90645f5 | 2013-01-04 22:29:21 +0000 | [diff] [blame] | 122 | index = key & (hash->size - 1); |
Stephen Hemminger | 97c84db | 2013-01-11 18:25:26 +0000 | [diff] [blame] | 123 | len = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | |
Stephen Hemminger | 97c84db | 2013-01-11 18:25:26 +0000 | [diff] [blame] | 125 | for (backet = hash->index[index]; backet != NULL; backet = backet->next) |
| 126 | { |
| 127 | if (backet->key == key && (*hash->hash_cmp) (backet->data, data)) |
| 128 | return backet->data; |
| 129 | ++len; |
| 130 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 131 | |
| 132 | if (alloc_func) |
| 133 | { |
| 134 | newdata = (*alloc_func) (data); |
| 135 | if (newdata == NULL) |
| 136 | return NULL; |
| 137 | |
Stephen Hemminger | 97c84db | 2013-01-11 18:25:26 +0000 | [diff] [blame] | 138 | if (len > HASH_THRESHOLD && !hash->no_expand) |
| 139 | { |
| 140 | hash_expand (hash); |
| 141 | index = key & (hash->size - 1); |
| 142 | } |
| 143 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 144 | backet = XMALLOC (MTYPE_HASH_BACKET, sizeof (struct hash_backet)); |
| 145 | backet->data = newdata; |
| 146 | backet->key = key; |
| 147 | backet->next = hash->index[index]; |
| 148 | hash->index[index] = backet; |
| 149 | hash->count++; |
| 150 | return backet->data; |
| 151 | } |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | /* Hash lookup. */ |
| 156 | void * |
| 157 | hash_lookup (struct hash *hash, void *data) |
| 158 | { |
| 159 | return hash_get (hash, data, NULL); |
| 160 | } |
| 161 | |
Stephen Hemminger | 6392aa8 | 2010-08-27 14:11:14 -0700 | [diff] [blame] | 162 | /* Simple Bernstein hash which is simple and fast for common case */ |
| 163 | unsigned int string_hash_make (const char *str) |
| 164 | { |
| 165 | unsigned int hash = 0; |
| 166 | |
| 167 | while (*str) |
| 168 | hash = (hash * 33) ^ (unsigned int) *str++; |
| 169 | |
| 170 | return hash; |
| 171 | } |
| 172 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 173 | /* This function release registered value from specified hash. When |
| 174 | release is successfully finished, return the data pointer in the |
| 175 | hash backet. */ |
| 176 | void * |
| 177 | hash_release (struct hash *hash, void *data) |
| 178 | { |
| 179 | void *ret; |
| 180 | unsigned int key; |
| 181 | unsigned int index; |
| 182 | struct hash_backet *backet; |
| 183 | struct hash_backet *pp; |
| 184 | |
| 185 | key = (*hash->hash_key) (data); |
Stephen Hemminger | 90645f5 | 2013-01-04 22:29:21 +0000 | [diff] [blame] | 186 | index = key & (hash->size - 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 187 | |
| 188 | for (backet = pp = hash->index[index]; backet; backet = backet->next) |
| 189 | { |
| 190 | if (backet->key == key && (*hash->hash_cmp) (backet->data, data)) |
| 191 | { |
| 192 | if (backet == pp) |
| 193 | hash->index[index] = backet->next; |
| 194 | else |
| 195 | pp->next = backet->next; |
| 196 | |
| 197 | ret = backet->data; |
| 198 | XFREE (MTYPE_HASH_BACKET, backet); |
| 199 | hash->count--; |
| 200 | return ret; |
| 201 | } |
| 202 | pp = backet; |
| 203 | } |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | /* Iterator function for hash. */ |
| 208 | void |
| 209 | hash_iterate (struct hash *hash, |
| 210 | void (*func) (struct hash_backet *, void *), void *arg) |
| 211 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 212 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | struct hash_backet *hb; |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 214 | struct hash_backet *hbnext; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 215 | |
| 216 | for (i = 0; i < hash->size; i++) |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 217 | for (hb = hash->index[i]; hb; hb = hbnext) |
| 218 | { |
| 219 | /* get pointer to next hash backet here, in case (*func) |
| 220 | * decides to delete hb by calling hash_release |
| 221 | */ |
| 222 | hbnext = hb->next; |
| 223 | (*func) (hb, arg); |
| 224 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* Clean up hash. */ |
| 228 | void |
| 229 | hash_clean (struct hash *hash, void (*free_func) (void *)) |
| 230 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 231 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 232 | struct hash_backet *hb; |
| 233 | struct hash_backet *next; |
| 234 | |
| 235 | for (i = 0; i < hash->size; i++) |
| 236 | { |
| 237 | for (hb = hash->index[i]; hb; hb = next) |
| 238 | { |
| 239 | next = hb->next; |
| 240 | |
| 241 | if (free_func) |
| 242 | (*free_func) (hb->data); |
| 243 | |
| 244 | XFREE (MTYPE_HASH_BACKET, hb); |
| 245 | hash->count--; |
| 246 | } |
| 247 | hash->index[i] = NULL; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /* Free hash memory. You may call hash_clean before call this |
| 252 | function. */ |
| 253 | void |
| 254 | hash_free (struct hash *hash) |
| 255 | { |
| 256 | XFREE (MTYPE_HASH_INDEX, hash->index); |
| 257 | XFREE (MTYPE_HASH, hash); |
| 258 | } |