2 * hash.c - hashing routines mainly used for caching key details.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2000-2002 Project Purple
20 * hashtable - the hash table array.
22 static struct ll *hashtable[HASHSIZE];
25 * elements - the number of elements in the hash table.
27 static unsigned long elements;
30 * inithash - Initialize the hash ready for use.
36 for (i = 0; i < HASHSIZE; i++) {
43 * destroyhash - Clean up the hash after use.
45 * This function destroys the hash after use, freeing any memory that was
46 * used during its lifetime.
48 void destroyhash(void)
51 struct ll *curll = NULL;
53 for (i = 0; i < HASHSIZE; i++) {
56 * TODO: The problem is the object has pointers that
59 llfree(curll, free_statskey);
66 * addtohash - Adds a key to the hash.
67 * @key: The key to add.
69 * Takes a key and stores it in the hash.
71 void addtohash(struct stats_key *key)
74 hashtable[key->keyid & HASHMASK]=
75 lladd(hashtable[key->keyid & HASHMASK], key);
79 * createandaddtohash - Creates a key and adds it to the hash.
80 * @keyid: The key to create and add.
82 * Takes a key, checks if it exists in the hash and if not creates it
83 * and adds it to the hash. Returns the key from the hash whether it
84 * already existed or we just created it.
86 struct stats_key *createandaddtohash(uint64_t keyid)
88 struct stats_key *tmpkey;
91 * Check if the key already exists and if not create and add it.
93 tmpkey = findinhash(keyid);
95 tmpkey = malloc(sizeof(*tmpkey));
96 memset(tmpkey, 0, sizeof(*tmpkey));
97 tmpkey->keyid = keyid;
103 int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
105 return !(key != NULL && key->keyid == *keyid);
108 struct stats_key *findinhash(uint64_t keyid)
114 if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
117 return found->object;
120 unsigned long hashelements(void)
125 struct ll *gethashtableentry(int entry)
127 return hashtable[entry];
131 * hash_getkeysigs - Gets the signatures on a key.
132 * @keyid: The key we want the signatures for.
134 * This function gets the signatures on a key. It's the same as the
135 * getkeysigs function from the keydb module except we also cache the data
136 * so that if we need it again we already have it available.
138 struct ll *hash_getkeysigs(uint64_t keyid)
140 struct stats_key *key = NULL;
146 key = findinhash(keyid);
148 key = malloc(sizeof(*key));
154 key->gotsigs = false;
157 perror("hash_getkeysigs()");
161 if (key->gotsigs == false) {
162 key->sigs = getkeysigs(key->keyid);