2 * hash.c - hashing routines mainly used for caching key details.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2000-2002 Project Purple
19 * hashtable - the hash table array.
21 static struct ll *hashtable[HASHSIZE];
24 * elements - the number of elements in the hash table.
26 static unsigned long elements;
29 * inithash - Initialize the hash ready for use.
35 for (i = 0; i < HASHSIZE; i++) {
42 * destroyhash - Clean up the hash after use.
44 * This function destroys the hash after use, freeing any memory that was
45 * used during its lifetime.
47 void destroyhash(void)
50 struct ll *curll = NULL;
51 struct ll *nextll = NULL;
53 for (i = 0; i < HASHSIZE; i++) {
55 while (curll != NULL) {
58 * TODO: The problem is the object has pointers that
71 * addtohash - Adds a key to the hash.
72 * @key: The key to add.
74 * Takes a key and stores it in the hash.
76 void addtohash(struct stats_key *key)
79 hashtable[key->keyid & HASHMASK]=
80 lladd(hashtable[key->keyid & HASHMASK], key);
84 * createandaddtohash - Creates a key and adds it to the hash.
85 * @keyid: The key to create and add.
87 * Takes a key, checks if it exists in the hash and if not creates it
88 * and adds it to the hash. Returns the key from the hash whether it
89 * already existed or we just created it.
91 struct stats_key *createandaddtohash(uint64_t keyid)
93 struct stats_key *tmpkey;
96 * Check if the key already exists and if not create and add it.
98 tmpkey = findinhash(keyid);
100 tmpkey = malloc(sizeof(*tmpkey));
101 memset(tmpkey, 0, sizeof(*tmpkey));
102 tmpkey->keyid = keyid;
108 int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
110 return !(key != NULL && key->keyid == *keyid);
113 struct stats_key *findinhash(uint64_t keyid)
119 if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
122 return found->object;
125 unsigned long hashelements(void)
130 struct ll *gethashtableentry(int entry)
132 return hashtable[entry];
136 * hash_getkeysigs - Gets the signatures on a key.
137 * @keyid: The key we want the signatures for.
139 * This function gets the signatures on a key. It's the same as the
140 * getkeysigs function from the keydb module except we also cache the data
141 * so that if we need it again we already have it available.
143 struct ll *hash_getkeysigs(uint64_t keyid)
145 struct stats_key *key = NULL;
147 key = findinhash(keyid);
149 key = malloc(sizeof(*key));
155 key->gotsigs = false;
158 perror("hash_getkeysigs()");
162 if (key->gotsigs == false) {
163 key->sigs = getkeysigs(key->keyid);