2 * hash.c - hashing routines mainly used for caching key details.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2000-2002 Project Purple
14 #include "keystructs.h"
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;
52 for (i = 0; i < HASHSIZE; i++) {
55 * TODO: The problem is the object has pointers that
58 llfree(curll, free_statskey);
65 * addtohash - Adds a key to the hash.
66 * @key: The key to add.
68 * Takes a key and stores it in the hash.
70 void addtohash(struct stats_key *key)
73 hashtable[key->keyid & HASHMASK]=
74 lladd(hashtable[key->keyid & HASHMASK], key);
78 * createandaddtohash - Creates a key and adds it to the hash.
79 * @keyid: The key to create and add.
81 * Takes a key, checks if it exists in the hash and if not creates it
82 * and adds it to the hash. Returns the key from the hash whether it
83 * already existed or we just created it.
85 struct stats_key *createandaddtohash(uint64_t keyid)
87 struct stats_key *tmpkey;
90 * Check if the key already exists and if not create and add it.
92 tmpkey = findinhash(keyid);
94 tmpkey = malloc(sizeof(*tmpkey));
95 memset(tmpkey, 0, sizeof(*tmpkey));
96 tmpkey->keyid = keyid;
102 int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
104 return !(key != NULL && key->keyid == *keyid);
107 struct stats_key *findinhash(uint64_t keyid)
113 if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
116 return found->object;
119 unsigned long hashelements(void)
124 struct ll *gethashtableentry(int entry)
126 return hashtable[entry];