Update Debian Vcs-* fields to point to git repository
[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 #include "stats.h"
29
30 /**
31  *      hashtable - the hash table array.
32  */
33 static struct ll *hashtable[HASHSIZE];
34
35 /**
36  *      elements - the number of elements in the hash table.
37  */
38 static unsigned long elements;
39
40 /**
41  *      inithash - Initialize the hash ready for use.
42  */
43 void inithash(void)
44 {
45         unsigned int i;
46
47         for (i = 0; i < HASHSIZE; i++) {
48                 hashtable[i] = NULL;
49         }
50         elements = 0;
51 }
52
53 /**
54  *      destroyhash - Clean up the hash after use.
55  *
56  *      This function destroys the hash after use, freeing any memory that was
57  *      used during its lifetime.
58  */
59 void destroyhash(void)
60 {
61         int i;
62         struct ll *curll = NULL;
63
64         for (i = 0; i < HASHSIZE; i++) {
65                 curll = hashtable[i];
66                 /*
67                  * TODO: The problem is the object has pointers that
68                  * need freed too.
69                  */
70                 llfree(curll, (void (*)(void *)) free_statskey);
71                 hashtable[i] = NULL;
72         }
73         elements = 0;
74 }
75
76 /**
77  *      addtohash - Adds a key to the hash.
78  *      @key: The key to add.
79  *
80  *      Takes a key and stores it in the hash.
81  */
82 void addtohash(struct stats_key *key)
83 {
84         ++elements;
85         hashtable[key->keyid & HASHMASK]=
86                 lladd(hashtable[key->keyid & HASHMASK], key);
87 }
88
89 /**
90  *      createandaddtohash - Creates a key and adds it to the hash.
91  *      @keyid: The key to create and add.
92  *
93  *      Takes a key, checks if it exists in the hash and if not creates it
94  *      and adds it to the hash. Returns the key from the hash whether it
95  *      already existed or we just created it.
96  */
97 struct stats_key *createandaddtohash(uint64_t keyid)
98 {
99         struct stats_key *tmpkey;
100
101         /*
102          * Check if the key already exists and if not create and add it.
103          */
104         tmpkey = findinhash(keyid);
105         if (tmpkey == NULL) {
106                 tmpkey = malloc(sizeof(*tmpkey));
107                 memset(tmpkey, 0, sizeof(*tmpkey));
108                 tmpkey->keyid = keyid;
109                 addtohash(tmpkey);
110         }
111         return tmpkey;
112 }
113
114 int stats_key_cmp(struct stats_key *key, uint64_t *keyid)
115 {
116         return !(key != NULL && key->keyid == *keyid);
117 }
118
119 struct stats_key *findinhash(uint64_t keyid)
120 {
121         int (*p)();
122         struct ll *found;
123
124         p = stats_key_cmp;
125         if ((found = llfind(hashtable[keyid & HASHMASK], &keyid, p))==NULL) {
126                 return NULL;
127         }
128         return found->object;
129 }
130
131 unsigned long hashelements(void)
132 {
133         return elements;
134 }
135
136 struct ll *gethashtableentry(unsigned int entry)
137 {
138         return hashtable[entry];
139 }