cscvs to tla changeset 109
[onak.git] / hash.c
1 /*
2  * hash.c - hashing routines mainly used for caching key details.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2000-2002 Project Purple
7  *
8  * $Id: hash.c,v 1.10 2003/10/11 21:52:18 noodles Exp $
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "hash.h"
16 #include "keystructs.h"
17 #include "ll.h"
18 #include "mem.h"
19
20 /**
21  *      hashtable - the hash table array.
22  */
23 static struct ll *hashtable[HASHSIZE];
24
25 /**
26  *      elements - the number of elements in the hash table.
27  */
28 static unsigned long elements;
29
30 /**
31  *      inithash - Initialize the hash ready for use.
32  */
33 void inithash(void)
34 {
35         unsigned int i;
36
37         for (i = 0; i < HASHSIZE; i++) {
38                 hashtable[i] = NULL;
39         }
40         elements = 0;
41 }
42
43 /**
44  *      destroyhash - Clean up the hash after use.
45  *
46  *      This function destroys the hash after use, freeing any memory that was
47  *      used during its lifetime.
48  */
49 void destroyhash(void)
50 {
51         int i;
52         struct ll *curll = NULL;
53
54         for (i = 0; i < HASHSIZE; i++) {
55                 curll = hashtable[i];
56                 /*
57                  * TODO: The problem is the object has pointers that
58                  * need freed too.
59                  */
60                 llfree(curll, (void (*)(void *)) free_statskey);
61                 hashtable[i] = NULL;
62         }
63         elements = 0;
64 }
65
66 /**
67  *      addtohash - Adds a key to the hash.
68  *      @key: The key to add.
69  *
70  *      Takes a key and stores it in the hash.
71  */
72 void addtohash(struct stats_key *key)
73 {
74         ++elements;
75         hashtable[key->keyid & HASHMASK]=
76                 lladd(hashtable[key->keyid & HASHMASK], key);
77 }
78
79 /**
80  *      createandaddtohash - Creates a key and adds it to the hash.
81  *      @keyid: The key to create and add.
82  *
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.
86  */
87 struct stats_key *createandaddtohash(uint64_t keyid)
88 {
89         struct stats_key *tmpkey;
90
91         /*
92          * Check if the key already exists and if not create and add it.
93          */
94         tmpkey = findinhash(keyid);
95         if (tmpkey == NULL) {
96                 tmpkey = malloc(sizeof(*tmpkey));
97                 memset(tmpkey, 0, sizeof(*tmpkey));
98                 tmpkey->keyid = keyid;
99                 addtohash(tmpkey);
100         }
101         return tmpkey;
102 }
103
104 int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
105 {
106         return !(key != NULL && key->keyid == *keyid);
107 }
108
109 struct stats_key *findinhash(uint64_t keyid)
110 {
111         int (*p)();
112         struct ll *found;
113
114         p = stats_key_cmp;
115         if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
116                 return NULL;
117         }
118         return found->object;
119 }
120
121 unsigned long hashelements(void)
122 {
123         return elements;
124 }
125
126 struct ll *gethashtableentry(unsigned int entry)
127 {
128         return hashtable[entry];
129 }