2 * hash.c - hashing routines mainly used for caching key details.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2000-2002 Project Purple
18 * hashtable - the hash table array.
20 static struct ll *hashtable[HASHSIZE];
23 * elements - the number of elements in the hash table.
25 static unsigned long elements;
28 * inithash - Initialize the hash ready for use.
34 for (i = 0; i < HASHSIZE; i++) {
40 void addtohash(struct stats_key *key)
43 hashtable[key->keyid & HASHMASK]=
44 lladd(hashtable[key->keyid & HASHMASK], key);
48 * createandaddtohash - Creates a key and adds it to the hash.
49 * @keyid: The key to create and add.
51 * Takes a key, checks if it exists in the hash and if not creates it
52 * and adds it to the hash. Returns the key from the hash whether it
53 * already existed or we just created it.
55 struct stats_key *createandaddtohash(uint64_t keyid)
57 struct stats_key *tmpkey;
60 * Check if the key already exists and if not create and add it.
62 tmpkey = findinhash(keyid);
64 tmpkey = malloc(sizeof(*tmpkey));
65 memset(tmpkey, 0, sizeof(*tmpkey));
66 tmpkey->keyid = keyid;
72 int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
74 return !(key != NULL && key->keyid == *keyid);
77 struct stats_key *findinhash(uint64_t keyid)
83 if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
89 unsigned long hashelements(void)
94 struct ll *gethashtableentry(int entry)
96 return hashtable[entry];
100 * hash_getkeysigs - Gets the signatures on a key.
101 * @keyid: The key we want the signatures for.
103 * This function gets the signatures on a key. It's the same as the
104 * getkeysigs function from the keydb module except we also cache the data
105 * so that if we need it again we already have it available.
107 struct ll *hash_getkeysigs(uint64_t keyid)
109 struct stats_key *key = NULL;
111 key = findinhash(keyid);
113 key = malloc(sizeof(*key));
119 key->gotsigs = false;
122 perror("hash_getkeysigs()");
126 if (key->gotsigs == false) {
127 key->sigs = getkeysigs(key->keyid);