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, see <https://www.gnu.org/licenses/>.
24 #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 * free_statskey - free an stats key structure.
41 * @key: The key to free.
43 * Takes a stats key and frees the memory used by it and the linked list
44 * of sigs under it. Doesn't recurse into the list as it's assumed all the
45 * objects referenced also exist in the hash.
47 static void free_statskey(struct stats_key *key)
50 if (key->sigs != NULL) {
51 llfree(key->sigs, NULL);
54 if (key->signs != NULL) {
55 llfree(key->signs, NULL);
63 * inithash - Initialize the hash ready for use.
69 for (i = 0; i < HASHSIZE; i++) {
76 * destroyhash - Clean up the hash after use.
78 * This function destroys the hash after use, freeing any memory that was
79 * used during its lifetime.
81 void destroyhash(void)
84 struct ll *curll = NULL;
86 for (i = 0; i < HASHSIZE; i++) {
89 * TODO: The problem is the object has pointers that
92 llfree(curll, (void (*)(void *)) free_statskey);
99 * addtohash - Adds a key to the hash.
100 * @key: The key to add.
102 * Takes a key and stores it in the hash.
104 void addtohash(struct stats_key *key)
107 hashtable[key->keyid & HASHMASK]=
108 lladd(hashtable[key->keyid & HASHMASK], key);
112 * createandaddtohash - Creates a key and adds it to the hash.
113 * @keyid: The key to create and add.
115 * Takes a key, checks if it exists in the hash and if not creates it
116 * and adds it to the hash. Returns the key from the hash whether it
117 * already existed or we just created it.
119 struct stats_key *createandaddtohash(uint64_t keyid)
121 struct stats_key *tmpkey;
124 * Check if the key already exists and if not create and add it.
126 tmpkey = findinhash(keyid);
127 if (tmpkey == NULL) {
128 tmpkey = malloc(sizeof(*tmpkey));
129 memset(tmpkey, 0, sizeof(*tmpkey));
130 tmpkey->keyid = keyid;
136 static int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
138 return !(key != NULL && key->keyid == *keyid);
141 struct stats_key *findinhash(uint64_t keyid)
147 if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
150 return found->object;
153 unsigned long hashelements(void)
158 struct ll *gethashtableentry(unsigned int entry)
160 return hashtable[entry];