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