2 * hash.c - hashing routines mainly used for caching key details.
4 * Jonathan McDowell <noodles@earth.li>
6 * Copyright 2000-2002 Project Purple
8 * $Id: hash.c,v 1.10 2003/10/11 21:52:18 noodles Exp $
16 #include "keystructs.h"
21 * hashtable - the hash table array.
23 static struct ll *hashtable[HASHSIZE];
26 * elements - the number of elements in the hash table.
28 static unsigned long elements;
31 * inithash - Initialize the hash ready for use.
37 for (i = 0; i < HASHSIZE; i++) {
44 * destroyhash - Clean up the hash after use.
46 * This function destroys the hash after use, freeing any memory that was
47 * used during its lifetime.
49 void destroyhash(void)
52 struct ll *curll = NULL;
54 for (i = 0; i < HASHSIZE; i++) {
57 * TODO: The problem is the object has pointers that
60 llfree(curll, (void (*)(void *)) free_statskey);
67 * addtohash - Adds a key to the hash.
68 * @key: The key to add.
70 * Takes a key and stores it in the hash.
72 void addtohash(struct stats_key *key)
75 hashtable[key->keyid & HASHMASK]=
76 lladd(hashtable[key->keyid & HASHMASK], key);
80 * createandaddtohash - Creates a key and adds it to the hash.
81 * @keyid: The key to create and add.
83 * Takes a key, checks if it exists in the hash and if not creates it
84 * and adds it to the hash. Returns the key from the hash whether it
85 * already existed or we just created it.
87 struct stats_key *createandaddtohash(uint64_t keyid)
89 struct stats_key *tmpkey;
92 * Check if the key already exists and if not create and add it.
94 tmpkey = findinhash(keyid);
96 tmpkey = malloc(sizeof(*tmpkey));
97 memset(tmpkey, 0, sizeof(*tmpkey));
98 tmpkey->keyid = keyid;
104 int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
106 return !(key != NULL && key->keyid == *keyid);
109 struct stats_key *findinhash(uint64_t keyid)
115 if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
118 return found->object;
121 unsigned long hashelements(void)
126 struct ll *gethashtableentry(unsigned int entry)
128 return hashtable[entry];