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"
31 * hashtable - the hash table array.
33 static struct ll *hashtable[HASHSIZE];
36 * elements - the number of elements in the hash table.
38 static unsigned long elements;
41 * inithash - Initialize the hash ready for use.
47 for (i = 0; i < HASHSIZE; i++) {
54 * destroyhash - Clean up the hash after use.
56 * This function destroys the hash after use, freeing any memory that was
57 * used during its lifetime.
59 void destroyhash(void)
62 struct ll *curll = NULL;
64 for (i = 0; i < HASHSIZE; i++) {
67 * TODO: The problem is the object has pointers that
70 llfree(curll, (void (*)(void *)) free_statskey);
77 * addtohash - Adds a key to the hash.
78 * @key: The key to add.
80 * Takes a key and stores it in the hash.
82 void addtohash(struct stats_key *key)
85 hashtable[key->keyid & HASHMASK]=
86 lladd(hashtable[key->keyid & HASHMASK], key);
90 * createandaddtohash - Creates a key and adds it to the hash.
91 * @keyid: The key to create and add.
93 * Takes a key, checks if it exists in the hash and if not creates it
94 * and adds it to the hash. Returns the key from the hash whether it
95 * already existed or we just created it.
97 struct stats_key *createandaddtohash(uint64_t keyid)
99 struct stats_key *tmpkey;
102 * Check if the key already exists and if not create and add it.
104 tmpkey = findinhash(keyid);
105 if (tmpkey == NULL) {
106 tmpkey = malloc(sizeof(*tmpkey));
107 memset(tmpkey, 0, sizeof(*tmpkey));
108 tmpkey->keyid = keyid;
114 int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
116 return !(key != NULL && key->keyid == *keyid);
119 struct stats_key *findinhash(uint64_t keyid)
125 if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
128 return found->object;
131 unsigned long hashelements(void)
136 struct ll *gethashtableentry(unsigned int entry)
138 return hashtable[entry];