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++) {
41 void addtohash(struct stats_key *key)
44 hashtable[key->keyid & HASHMASK]=
45 lladd(hashtable[key->keyid & HASHMASK], key);
49 * createandaddtohash - Creates a key and adds it to the hash.
50 * @keyid: The key to create and add.
52 * Takes a key, checks if it exists in the hash and if not creates it
53 * and adds it to the hash. Returns the key from the hash whether it
54 * already existed or we just created it.
56 struct stats_key *createandaddtohash(uint64_t keyid)
58 struct stats_key *tmpkey;
61 * Check if the key already exists and if not create and add it.
63 tmpkey = findinhash(keyid);
65 tmpkey = malloc(sizeof(*tmpkey));
66 memset(tmpkey, 0, sizeof(*tmpkey));
67 tmpkey->keyid = keyid;
73 int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
75 return !(key != NULL && key->keyid == *keyid);
78 struct stats_key *findinhash(uint64_t keyid)
84 if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
90 unsigned long hashelements(void)
95 struct ll *gethashtableentry(int entry)
97 return hashtable[entry];
101 * hash_getkeysigs - Gets the signatures on a key.
102 * @keyid: The key we want the signatures for.
104 * This function gets the signatures on a key. It's the same as the
105 * getkeysigs function from the keydb module except we also cache the data
106 * so that if we need it again we already have it available.
108 struct ll *hash_getkeysigs(uint64_t keyid)
110 struct stats_key *key = NULL;
112 key = findinhash(keyid);
114 key = malloc(sizeof(*key));
120 key->gotsigs = false;
123 perror("hash_getkeysigs()");
127 if (key->gotsigs == false) {
128 key->sigs = getkeysigs(key->keyid);