-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHashTable.cpp
178 lines (151 loc) · 4.48 KB
/
HashTable.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// HashTable.cpp
// DNS Proxy Filter Server
// Copyright (c) 2010-2019 Untangle, Inc.
// All Rights Reserved
// Written by Michael A. Hotz
#include "common.h"
/*--------------------------------------------------------------------------*/
HashTable::HashTable(int aBuckets)
{
int x;
// save the number of buckets
buckets = aBuckets;
// allocate the bucket array
table = (HashObject **)calloc(buckets,sizeof(HashObject *));
// allocate and initialize the bucket locks
control = (pthread_mutex_t *)calloc(buckets,sizeof(pthread_mutex_t));
for(x = 0;x < buckets;x++)
{
memset(&control[0],0,sizeof(pthread_mutex_t));
pthread_mutex_init(&control[x],NULL);
}
}
/*--------------------------------------------------------------------------*/
HashTable::~HashTable(void)
{
HashObject *work,*hold;
int x;
// walk through all the buckets and delete everything
for(x = 0;x < buckets;x++)
{
if (table[x] == NULL) continue;
work = table[x];
while (work != NULL)
{
hold = work->next;
delete(work);
work = hold;
}
}
// free the bucket array
free(table);
// free the bucket locks
for(x = 0;x < buckets;x++) pthread_mutex_destroy(&control[x]);
free(control);
}
/*--------------------------------------------------------------------------*/
int HashTable::InsertObject(HashObject *argObject)
{
int key;
// calculate bucket using the active hash function
key = HashKey(argObject->ObjectName);
// save existing item in new item next pointer
argObject->next = table[key];
// put new item at front of list
table[key] = argObject;
return(key);
}
/*--------------------------------------------------------------------------*/
HashObject* HashTable::SearchObject(const char *argString)
{
HashObject *find;
int key;
// calculate bucket using the active hash function
key = HashKey(argString);
// if the bucket is empty return nothing
if (table[key] == NULL) return(NULL);
// search for exact match or default
for(find = table[key];find != NULL;find = find->next) if (strcmp(argString,find->ObjectName) == 0) return(find);
// return NULL if nothing found
return(NULL);
}
/*--------------------------------------------------------------------------*/
unsigned int HashTable::HashKey(const char *argString)
{
const unsigned char *key = (const unsigned char *)argString;
unsigned int hash;
hash = 0;
while (*key) hash = ((*key++) + (hash << 6) + (hash << 16) - hash);
return(hash % buckets);
}
/*--------------------------------------------------------------------------*/
void HashTable::GetTableSize(int &aCount,int &aBytes)
{
HashObject *work;
int x;
aCount = 0;
aBytes = 0;
// start with our size
aBytes = sizeof(*this);
aBytes+=(buckets * sizeof(HashObject *));
aBytes+=(buckets * sizeof(pthread_mutex_t));
// walk through all of the table entries
for(x = 0;x < buckets;x++)
{
// lock the bucket
pthread_mutex_lock(&control[x]);
// count and add the size of every object in active tables
if (table[x] != NULL)
{
for(work = table[x];work != NULL;work = work->next)
{
aBytes+=work->GetObjectSize();
aCount++;
}
}
// unlock the bucket
pthread_mutex_unlock(&control[x]);
}
}
/*--------------------------------------------------------------------------*/
/****************************************************************************/
/*--------------------------------------------------------------------------*/
HashObject::HashObject(const char *argTitle)
{
ObjectName = NULL;
next = NULL;
if (argTitle == NULL) return;
ObjectName = newstr(argTitle);
}
/*--------------------------------------------------------------------------*/
HashObject::~HashObject(void)
{
freestr(ObjectName);
}
/*--------------------------------------------------------------------------*/
int HashObject::GetObjectSize(void)
{
int mysize;
mysize = sizeof(*this);
return(mysize);
}
/*--------------------------------------------------------------------------*/
/****************************************************************************/
/*--------------------------------------------------------------------------*/
NetworkEntry::NetworkEntry(unsigned long aObject,unsigned long aOwner,const char* aNetwork) : HashObject(aNetwork)
{
Object = aObject;
Owner = aOwner;
};
/*--------------------------------------------------------------------------*/
NetworkEntry::~NetworkEntry(void)
{
}
/*--------------------------------------------------------------------------*/
int NetworkEntry::GetObjectSize(void)
{
int mysize;
mysize = sizeof(*this);
return(mysize);
}
/*--------------------------------------------------------------------------*/