4 * Copyright (C) 2011 Gabor Kovesdan <gabor@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "hashtable.h"
40 * Return a 32-bit hash of the given buffer. The init
41 * value should be 0, or the previous hash value to extend
45 hash32_buf(const void *buf
, size_t len
, uint32_t hash
)
47 const unsigned char *p
= buf
;
50 hash
= HASHSTEP(hash
, *p
++);
56 * Initializes a hash table that can hold table_size number of entries,
57 * each of which has a key of key_size bytes and a value of value_size
58 * bytes. On successful allocation returns a pointer to the hash table.
59 * Otherwise, returns NULL and sets errno to indicate the error.
62 *hashtable_init(size_t table_size
, size_t key_size
, size_t value_size
)
66 DPRINT(("hashtable_init: table_size %zu, key_size %zu, value_size %zu\n",
67 table_size
, key_size
, value_size
));
69 tbl
= malloc(sizeof(hashtable
));
73 tbl
->entries
= calloc(sizeof(hashtable_entry
*), table_size
);
74 if (tbl
->entries
== NULL
)
77 tbl
->table_size
= table_size
;
79 tbl
->key_size
= key_size
;
80 tbl
->value_size
= value_size
;
87 DPRINT(("hashtable_init: allocation failed\n"));
93 * Places the key-value pair to the hashtable tbl.
95 * HASH_OK: if the key was not present in the hash table yet
96 * but the kay-value pair has been successfully added.
97 * HASH_UPDATED: if the value for the key has been updated with the
99 * HASH_FULL: if the hash table is full and the entry could not
101 * HASH_FAIL: if an error has occurred and errno has been set to
102 * indicate the error.
105 hashtable_put(hashtable
*tbl
, const void *key
, const void *value
)
109 if (tbl
->table_size
== tbl
->usage
)
111 DPRINT(("hashtable_put: hashtable is full\n"));
115 hash
= hash32_buf(key
, tbl
->key_size
, hash
) % tbl
->table_size
;
116 DPRINT(("hashtable_put: calculated hash %" PRIu32
"\n", hash
));
119 * On hash collision entries are inserted at the next free space,
120 * so we have to increase the index until we either find an entry
121 * with the same key (and update it) or we find a free space.
125 if (tbl
->entries
[hash
] == NULL
)
127 else if (memcmp(tbl
->entries
[hash
]->key
, key
, tbl
->key_size
) == 0)
129 memcpy(tbl
->entries
[hash
]->value
, value
, tbl
->value_size
);
130 DPRINT(("hashtable_put: effective location is %" PRIu32
131 ", entry updated\n", hash
));
132 return (HASH_UPDATED
);
134 if (++hash
== tbl
->table_size
)
138 DPRINT(("hashtable_put: effective location is %" PRIu32
"\n", hash
));
140 tbl
->entries
[hash
] = malloc(sizeof(hashtable_entry
));
141 if (tbl
->entries
[hash
] == NULL
)
147 tbl
->entries
[hash
]->key
= malloc(tbl
->key_size
);
148 if (tbl
->entries
[hash
]->key
== NULL
)
154 tbl
->entries
[hash
]->value
= malloc(tbl
->value_size
);
155 if (tbl
->entries
[hash
]->value
== NULL
)
161 memcpy(tbl
->entries
[hash
]->key
, key
, tbl
->key_size
);
162 memcpy(tbl
->entries
[hash
]->value
, value
, tbl
->value_size
);
165 DPRINT(("hashtable_put: entry successfully inserted\n"));
170 free(tbl
->entries
[hash
]->key
);
172 free(tbl
->entries
[hash
]);
174 DPRINT(("hashtable_put: insertion failed\n"));
178 static hashtable_entry
179 **hashtable_lookup(const hashtable
*tbl
, const void *key
)
183 hash
= hash32_buf(key
, tbl
->key_size
, hash
) % tbl
->table_size
;
187 if (tbl
->entries
[hash
] == NULL
)
189 else if (memcmp(key
, tbl
->entries
[hash
]->key
, tbl
->key_size
) == 0)
191 DPRINT(("hashtable_lookup: entry found at location %" PRIu32
"\n", hash
));
192 return (&tbl
->entries
[hash
]);
195 if (++hash
== tbl
->table_size
)
201 * Retrieves the value for key from the hash table tbl and places
202 * it to the space indicated by the value argument.
203 * Returns HASH_OK if the value has been found and retrieved or
204 * HASH_NOTFOUND otherwise.
207 hashtable_get(hashtable
*tbl
, const void *key
, void *value
)
209 hashtable_entry
**entry
;
211 entry
= hashtable_lookup(tbl
, key
);
214 DPRINT(("hashtable_get: entry is not available in the hashtable\n"));
215 return (HASH_NOTFOUND
);
218 memcpy(value
, (*entry
)->value
, tbl
->value_size
);
219 DPRINT(("hashtable_get: entry successfully copied into output buffer\n"));
224 * Removes the entry with the specifified key from the hash table
225 * tbl. Returns HASH_OK if the entry has been found and removed
226 * or HASH_NOTFOUND otherwise.
229 hashtable_remove(hashtable
*tbl
, const void *key
)
231 hashtable_entry
**entry
;
233 entry
= hashtable_lookup(tbl
, key
);
236 DPRINT(("hashtable_remove: entry is not available in the hashtable\n"));
237 return (HASH_NOTFOUND
);
241 free((*entry
)->value
);
246 DPRINT(("hashtable_remove: entry successfully removed\n"));
251 * Frees the resources associated with the hash table tbl.
254 hashtable_free(hashtable
*tbl
)
259 for (unsigned int i
= 0; i
< tbl
->table_size
; i
++)
260 if ((tbl
->entries
[i
] != NULL
))
262 free(tbl
->entries
[i
]->key
);
263 free(tbl
->entries
[i
]->value
);
267 DPRINT(("hashtable_free: resources are successfully freed\n"));