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 | { |
| 51 | return hash_create_size (HASHTABSIZE, hash_key, hash_cmp); |
| 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 | |
| 63 | /* Lookup and return hash backet in hash. If there is no |
| 64 | corresponding hash backet and alloc_func is specified, create new |
| 65 | hash backet. */ |
| 66 | void * |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 67 | hash_get (struct hash *hash, void *data, void * (*alloc_func) (void *)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | { |
| 69 | unsigned int key; |
| 70 | unsigned int index; |
| 71 | void *newdata; |
| 72 | struct hash_backet *backet; |
| 73 | |
| 74 | key = (*hash->hash_key) (data); |
Stephen Hemminger | 90645f5 | 2013-01-04 22:29:21 +0000 | [diff] [blame^] | 75 | index = key & (hash->size - 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 76 | |
| 77 | for (backet = hash->index[index]; backet != NULL; backet = backet->next) |
| 78 | if (backet->key == key && (*hash->hash_cmp) (backet->data, data)) |
| 79 | return backet->data; |
| 80 | |
| 81 | if (alloc_func) |
| 82 | { |
| 83 | newdata = (*alloc_func) (data); |
| 84 | if (newdata == NULL) |
| 85 | return NULL; |
| 86 | |
| 87 | backet = XMALLOC (MTYPE_HASH_BACKET, sizeof (struct hash_backet)); |
| 88 | backet->data = newdata; |
| 89 | backet->key = key; |
| 90 | backet->next = hash->index[index]; |
| 91 | hash->index[index] = backet; |
| 92 | hash->count++; |
| 93 | return backet->data; |
| 94 | } |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | /* Hash lookup. */ |
| 99 | void * |
| 100 | hash_lookup (struct hash *hash, void *data) |
| 101 | { |
| 102 | return hash_get (hash, data, NULL); |
| 103 | } |
| 104 | |
Stephen Hemminger | 6392aa8 | 2010-08-27 14:11:14 -0700 | [diff] [blame] | 105 | /* Simple Bernstein hash which is simple and fast for common case */ |
| 106 | unsigned int string_hash_make (const char *str) |
| 107 | { |
| 108 | unsigned int hash = 0; |
| 109 | |
| 110 | while (*str) |
| 111 | hash = (hash * 33) ^ (unsigned int) *str++; |
| 112 | |
| 113 | return hash; |
| 114 | } |
| 115 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 116 | /* This function release registered value from specified hash. When |
| 117 | release is successfully finished, return the data pointer in the |
| 118 | hash backet. */ |
| 119 | void * |
| 120 | hash_release (struct hash *hash, void *data) |
| 121 | { |
| 122 | void *ret; |
| 123 | unsigned int key; |
| 124 | unsigned int index; |
| 125 | struct hash_backet *backet; |
| 126 | struct hash_backet *pp; |
| 127 | |
| 128 | key = (*hash->hash_key) (data); |
Stephen Hemminger | 90645f5 | 2013-01-04 22:29:21 +0000 | [diff] [blame^] | 129 | index = key & (hash->size - 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 130 | |
| 131 | for (backet = pp = hash->index[index]; backet; backet = backet->next) |
| 132 | { |
| 133 | if (backet->key == key && (*hash->hash_cmp) (backet->data, data)) |
| 134 | { |
| 135 | if (backet == pp) |
| 136 | hash->index[index] = backet->next; |
| 137 | else |
| 138 | pp->next = backet->next; |
| 139 | |
| 140 | ret = backet->data; |
| 141 | XFREE (MTYPE_HASH_BACKET, backet); |
| 142 | hash->count--; |
| 143 | return ret; |
| 144 | } |
| 145 | pp = backet; |
| 146 | } |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | /* Iterator function for hash. */ |
| 151 | void |
| 152 | hash_iterate (struct hash *hash, |
| 153 | void (*func) (struct hash_backet *, void *), void *arg) |
| 154 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 155 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | struct hash_backet *hb; |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 157 | struct hash_backet *hbnext; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | |
| 159 | for (i = 0; i < hash->size; i++) |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 160 | for (hb = hash->index[i]; hb; hb = hbnext) |
| 161 | { |
| 162 | /* get pointer to next hash backet here, in case (*func) |
| 163 | * decides to delete hb by calling hash_release |
| 164 | */ |
| 165 | hbnext = hb->next; |
| 166 | (*func) (hb, arg); |
| 167 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* Clean up hash. */ |
| 171 | void |
| 172 | hash_clean (struct hash *hash, void (*free_func) (void *)) |
| 173 | { |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 174 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 175 | struct hash_backet *hb; |
| 176 | struct hash_backet *next; |
| 177 | |
| 178 | for (i = 0; i < hash->size; i++) |
| 179 | { |
| 180 | for (hb = hash->index[i]; hb; hb = next) |
| 181 | { |
| 182 | next = hb->next; |
| 183 | |
| 184 | if (free_func) |
| 185 | (*free_func) (hb->data); |
| 186 | |
| 187 | XFREE (MTYPE_HASH_BACKET, hb); |
| 188 | hash->count--; |
| 189 | } |
| 190 | hash->index[i] = NULL; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /* Free hash memory. You may call hash_clean before call this |
| 195 | function. */ |
| 196 | void |
| 197 | hash_free (struct hash *hash) |
| 198 | { |
| 199 | XFREE (MTYPE_HASH_INDEX, hash->index); |
| 200 | XFREE (MTYPE_HASH, hash); |
| 201 | } |