2 MiddleMan filtering proxy server
3 Copyright (C) 2002 Jason McLaughlin
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 return a slot index for a string
28 unsigned int hash_key(int max, char *string)
46 HASH_TABLE *hash_create(int size)
48 HASH_TABLE *hash_table;
50 hash_table = xmalloc(sizeof(HASH_TABLE));
54 hash_table->size = size;
56 hash_table->hash_list = xmalloc(sizeof(struct HASH_LIST *) * size);
58 memset(hash_table->hash_list, 0, sizeof(struct HASH_LIST *) * size);
64 insert item into hash table
66 void hash_insert(HASH_TABLE * hash_table, char *ref, void *string)
69 struct HASH_LIST *hash_list;
73 if (hash_search(hash_table, ref) != NULL)
76 key = hash_key(hash_table->size, ref);
78 hash_list = hash_table->hash_list[key];
80 HASH_LIST_ADD(hash_list, ref, string);
82 if (hash_table->hash_list[key] == NULL)
83 hash_table->hash_list[key] = hash_list;
87 purge item from hash table
89 void hash_purge(HASH_TABLE * hash_table, char *ref)
92 struct HASH_LIST *hash_list, *prev = NULL;
96 key = hash_key(hash_table->size, ref);
98 hash_list = hash_table->hash_list[key];
100 while (hash_list != NULL) {
101 if (!strcasecmp(hash_list->ref, ref)) {
102 if (hash_table->hash_list[key] == hash_list)
103 hash_table->hash_list[key] = hash_list->next;
104 else if (prev != NULL)
105 prev->next = hash_list->next;
107 xfree(hash_list->ref);
108 xfree(hash_list->data);
115 hash_list = hash_list->next;
120 search for item in hash table
122 char *hash_search(HASH_TABLE * hash_table, char *ref)
125 struct HASH_LIST *hash_list;
129 key = hash_key(hash_table->size, ref);
131 hash_list = hash_table->hash_list[key];
133 while (hash_list != NULL) {
134 if (!strcasecmp(hash_list->ref, ref)) {
137 return hash_list->data;
140 hash_list = hash_list->next;
149 wipe out a hash table and free all used memory
151 void hash_destroy(HASH_TABLE * hash_table)
154 struct HASH_LIST *hash_list, *tmp_list;
158 for (i = 0; i < hash_table->size; i++) {
159 hash_list = hash_table->hash_list[i];
161 while (hash_list != NULL) {
162 tmp_list = hash_list->next;
164 xfree(hash_list->ref);
165 xfree(hash_list->data);
168 hash_list = tmp_list;
172 xfree(hash_table->hash_list);
177 remove old items from hash table
179 int hash_expire(HASH_TABLE * hash_table, unsigned int age)
183 struct HASH_LIST *hash_list, *tmp_list, *prev = NULL;
189 for (i = 0; i < hash_table->size; i++) {
190 hash_list = hash_table->hash_list[i];
192 while (hash_list != NULL) {
193 tmp_list = hash_list->next;
195 if (hash_list->age <= (t - age)) {
196 if (hash_table->hash_list[i] == hash_list)
197 hash_table->hash_list[i] = tmp_list;
198 else if (prev != NULL)
199 prev->next = tmp_list;
201 xfree(hash_list->ref);
202 xfree(hash_list->data);
209 hash_list = tmp_list;