2 * hash.c - hashing routines mainly used for caching key details.
4 * Copyright 2000-2002 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "keystructs.h"
30 * hashtable - the hash table array.
32 static struct ll *hashtable[HASHSIZE];
35 * elements - the number of elements in the hash table.
37 static unsigned long elements;
40 * inithash - Initialize the hash ready for use.
46 for (i = 0; i < HASHSIZE; i++) {
53 * destroyhash - Clean up the hash after use.
55 * This function destroys the hash after use, freeing any memory that was
56 * used during its lifetime.
58 void destroyhash(void)
61 struct ll *curll = NULL;
63 for (i = 0; i < HASHSIZE; i++) {
66 * TODO: The problem is the object has pointers that
69 llfree(curll, (void (*)(void *)) free_statskey);
76 * addtohash - Adds a key to the hash.
77 * @key: The key to add.
79 * Takes a key and stores it in the hash.
81 void addtohash(struct stats_key *key)
84 hashtable[key->keyid & HASHMASK]=
85 lladd(hashtable[key->keyid & HASHMASK], key);
89 * createandaddtohash - Creates a key and adds it to the hash.
90 * @keyid: The key to create and add.
92 * Takes a key, checks if it exists in the hash and if not creates it
93 * and adds it to the hash. Returns the key from the hash whether it
94 * already existed or we just created it.
96 struct stats_key *createandaddtohash(uint64_t keyid)
98 struct stats_key *tmpkey;
101 * Check if the key already exists and if not create and add it.
103 tmpkey = findinhash(keyid);
104 if (tmpkey == NULL) {
105 tmpkey = malloc(sizeof(*tmpkey));
106 memset(tmpkey, 0, sizeof(*tmpkey));
107 tmpkey->keyid = keyid;
113 int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
115 return !(key != NULL && key->keyid == *keyid);
118 struct stats_key *findinhash(uint64_t keyid)
124 if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
127 return found->object;
130 unsigned long hashelements(void)
135 struct ll *gethashtableentry(unsigned int entry)
137 return hashtable[entry];