2 * hash.c - hashing routines mainly used for caching key details.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2000-2002 Project Purple
13 #include "keystructs.h"
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++) {
41 * destroyhash - Clean up the hash after use.
43 * This function destroys the hash after use, freeing any memory that was
44 * used during its lifetime.
46 void destroyhash(void)
49 struct ll *curll = NULL;
51 for (i = 0; i < HASHSIZE; i++) {
54 * TODO: The problem is the object has pointers that
57 llfree(curll, free_statskey);
64 * addtohash - Adds a key to the hash.
65 * @key: The key to add.
67 * Takes a key and stores it in the hash.
69 void addtohash(struct stats_key *key)
72 hashtable[key->keyid & HASHMASK]=
73 lladd(hashtable[key->keyid & HASHMASK], key);
77 * createandaddtohash - Creates a key and adds it to the hash.
78 * @keyid: The key to create and add.
80 * Takes a key, checks if it exists in the hash and if not creates it
81 * and adds it to the hash. Returns the key from the hash whether it
82 * already existed or we just created it.
84 struct stats_key *createandaddtohash(uint64_t keyid)
86 struct stats_key *tmpkey;
89 * Check if the key already exists and if not create and add it.
91 tmpkey = findinhash(keyid);
93 tmpkey = malloc(sizeof(*tmpkey));
94 memset(tmpkey, 0, sizeof(*tmpkey));
95 tmpkey->keyid = keyid;
101 int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
103 return !(key != NULL && key->keyid == *keyid);
106 struct stats_key *findinhash(uint64_t keyid)
112 if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
115 return found->object;
118 unsigned long hashelements(void)
123 struct ll *gethashtableentry(int entry)
125 return hashtable[entry];