Use C99 uint32_t rather than u_int32_t
[onak.git] / hash.c
1 /*
2  * hash.c - hashing routines mainly used for caching key details.
3  *
4  * Copyright 2000-2002 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "hash.h"
25 #include "keystructs.h"
26 #include "ll.h"
27 #include "mem.h"
28
29 /**
30  *      hashtable - the hash table array.
31  */
32 static struct ll *hashtable[HASHSIZE];
33
34 /**
35  *      elements - the number of elements in the hash table.
36  */
37 static unsigned long elements;
38
39 /**
40  *      inithash - Initialize the hash ready for use.
41  */
42 void inithash(void)
43 {
44         unsigned int i;
45
46         for (i = 0; i < HASHSIZE; i++) {
47                 hashtable[i] = NULL;
48         }
49         elements = 0;
50 }
51
52 /**
53  *      destroyhash - Clean up the hash after use.
54  *
55  *      This function destroys the hash after use, freeing any memory that was
56  *      used during its lifetime.
57  */
58 void destroyhash(void)
59 {
60         int i;
61         struct ll *curll = NULL;
62
63         for (i = 0; i < HASHSIZE; i++) {
64                 curll = hashtable[i];
65                 /*
66                  * TODO: The problem is the object has pointers that
67                  * need freed too.
68                  */
69                 llfree(curll, (void (*)(void *)) free_statskey);
70                 hashtable[i] = NULL;
71         }
72         elements = 0;
73 }
74
75 /**
76  *      addtohash - Adds a key to the hash.
77  *      @key: The key to add.
78  *
79  *      Takes a key and stores it in the hash.
80  */
81 void addtohash(struct stats_key *key)
82 {
83         ++elements;
84         hashtable[key->keyid & HASHMASK]=
85                 lladd(hashtable[key->keyid & HASHMASK], key);
86 }
87
88 /**
89  *      createandaddtohash - Creates a key and adds it to the hash.
90  *      @keyid: The key to create and add.
91  *
92  *      Takes a key, checks if it exists in the hash and if not creates it
93  *      and adds it to the hash. Returns the key from the hash whether it
94  *      already existed or we just created it.
95  */
96 struct stats_key *createandaddtohash(uint64_t keyid)
97 {
98         struct stats_key *tmpkey;
99
100         /*
101          * Check if the key already exists and if not create and add it.
102          */
103         tmpkey = findinhash(keyid);
104         if (tmpkey == NULL) {
105                 tmpkey = malloc(sizeof(*tmpkey));
106                 memset(tmpkey, 0, sizeof(*tmpkey));
107                 tmpkey->keyid = keyid;
108                 addtohash(tmpkey);
109         }
110         return tmpkey;
111 }
112
113 int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
114 {
115         return !(key != NULL && key->keyid == *keyid);
116 }
117
118 struct stats_key *findinhash(uint64_t keyid)
119 {
120         int (*p)();
121         struct ll *found;
122
123         p = stats_key_cmp;
124         if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
125                 return NULL;
126         }
127         return found->object;
128 }
129
130 unsigned long hashelements(void)
131 {
132         return elements;
133 }
134
135 struct ll *gethashtableentry(unsigned int entry)
136 {
137         return hashtable[entry];
138 }