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