summaryrefslogtreecommitdiff
path: root/cbtcommon/hash.c
blob: ddc081b90427bfc02ae1c1279bf6f273b5f6dfca (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
 * See COPYING file for license information 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "debug.h"
#include "hash.h"
#include "rcsid.h"

RCSID("$Id: hash.c,v 1.6 2003/05/07 15:42:38 david Exp $");

#define HASH_CONST 37

static unsigned int hash_string(const char *);
static struct hash_entry *scan_list(struct list_head *, const char *); 
static struct hash_entry *get_hash_entry(struct hash_table *tbl, const char *key);

struct hash_table *create_hash_table(unsigned int sz)
{
    struct hash_table *tbl;
    unsigned int i;

    tbl = (struct hash_table *)malloc(sizeof(*tbl) + sz*sizeof(struct list_head));

    if (!tbl)
    {
	debug(DEBUG_APPERROR, "malloc for hash_table failed");
	return NULL;
    }
	
    tbl->ht_size  = sz;
    tbl->ht_lists = (struct list_head *)(tbl + 1);
    tbl->iterator = 0;

    for (i = 0; i < sz; i++)
	INIT_LIST_HEAD(&tbl->ht_lists[i]);

    return tbl;
}

void destroy_hash_table(struct hash_table *tbl, void (*delete_obj)(void *))
{
    struct list_head  *head, *next, *tmp;
    struct hash_entry *entry;
    int i;

    for (i = 0; i < tbl->ht_size; i++)
    {
	head = &tbl->ht_lists[i];
	next = head->next;

	while (next != head)
	{
	    tmp = next->next;
	    entry = list_entry(next, struct hash_entry, he_list);
	    if (delete_obj)
		delete_obj(entry->he_obj);
	    free(entry);

	    next = tmp;
	}
    }

    free(tbl);
}

/* FIXME: there is no way for the user of this to determine the difference
 *        between a put to a new key value and a malloc failure
 */
void *put_hash_object(struct hash_table *tbl, const char *key, void *obj)
{
    void * retval;
    put_hash_object_ex(tbl, key, obj, HT_KEYCOPY, NULL, &retval);
    return retval;
}

static struct hash_entry *get_hash_entry(struct hash_table *tbl, const char *key)
{
    struct list_head  *head;
    struct hash_entry *entry;
    unsigned int hash;

    hash  = hash_string(key) % tbl->ht_size;
    head  = &tbl->ht_lists[hash];
    entry = scan_list(head, key);

    return entry;
}

void *get_hash_object(struct hash_table *tbl, const char *key)
{
    struct hash_entry *entry = get_hash_entry(tbl, key);
    return (entry) ? entry->he_obj : NULL;
}

void *remove_hash_object(struct hash_table *tbl, const char *key)
{
    struct hash_entry *entry = get_hash_entry(tbl, key);
    void *retval = NULL;

    if (entry)
    {
	list_del(&entry->he_list);
	retval = entry->he_obj;
	free(entry);
    }

    return retval;
}

static unsigned int hash_string(register const char *key)
{
    register unsigned int hash = 0;
    
    while(*key)
	hash = hash * HASH_CONST + *key++; 
    
    return hash;
}

static struct hash_entry *scan_list(struct list_head *head, const char *key)
{
    struct list_head  *next = head->next;
    struct hash_entry *entry;

    while (next != head)
    {
	entry = list_entry(next, struct hash_entry, he_list);
	if (strcmp(entry->he_key, key) == 0)
	    return entry;

	next = next->next;
    }

    return NULL;
}

void reset_hash_iterator(struct hash_table *tbl)
{
    tbl->iterator = 0;
    tbl->iterator_ptr = NULL;
}

struct hash_entry *next_hash_entry(struct hash_table *tbl)
{
    while( tbl->iterator < tbl->ht_size )
    {
	struct list_head *head = &tbl->ht_lists[ tbl->iterator ];

	if( tbl->iterator_ptr == NULL )
	    tbl->iterator_ptr = head->next;

	if( tbl->iterator_ptr != head )
	{
	    struct list_head *tmp = tbl->iterator_ptr;
	    tbl->iterator_ptr = tbl->iterator_ptr->next;
	    return( list_entry( tmp, struct hash_entry, he_list ) );
	}

	else
	{
	    tbl->iterator++;
	    tbl->iterator_ptr = NULL;
	}
    }

    return( NULL );
}

int put_hash_object_ex(struct hash_table *tbl, const char *key, void *obj, int copy, 
		       char ** oldkey, void ** oldobj)
{
    struct list_head *head;
    struct hash_entry *entry;
    unsigned int hash;
    int retval = 0;

    /* FIXME: how can get_hash_entry be changed to be usable here? 
     * we need the value of head later if the entry is not found...
     */
    hash  = hash_string(key) % tbl->ht_size;
    head  = &tbl->ht_lists[hash];
    entry = scan_list(head, key);

    if (entry)
    {
	if (oldkey)
	    *oldkey = entry->he_key;
	if (oldobj)
	    *oldobj = entry->he_obj;

	/* if 'copy' is set, then we already have an exact
	 * private copy of the key (by definition of having
	 * found the match in scan_list) so we do nothing.
	 * if !copy, then we can simply assign the new
	 * key
	 */
	if (!copy)
	    entry->he_key = (char*)key; /* discard the const */
	entry->he_obj = obj;
    }
    else
    {
	size_t s = sizeof(*entry);

	if (oldkey)
	    *oldkey = NULL;
	if (oldobj)
	    *oldobj = NULL;

	if (copy)
	    s +=  strlen(key) + 1;

	entry = (struct hash_entry *)malloc(s);
	
	if (!entry)
	{
	    debug(DEBUG_APPERROR,"malloc failed put_hash_object key='%s'",key);
	    retval = -1;
	}
	else
	{
	    if (copy)
	    {
		entry->he_key = (char *)(entry + 1);
		strcpy(entry->he_key, key);
	    }
	    else
	    {
		entry->he_key = (char*)key; /* discard the const */
	    }

	    entry->he_obj = obj;

	    list_add(&entry->he_list, head);
	}
    }

    return retval;
}

void destroy_hash_table_ex(struct hash_table *tbl, 
			   void (*delete_entry)(const void *, char *, void *), 
			   const void * cookie)
{
    struct list_head  *head, *next, *tmp;
    struct hash_entry *entry;
    int i;
    
    for (i = 0; i < tbl->ht_size; i++)
    {
	head = &tbl->ht_lists[i];
	next = head->next;
	
	while (next != head)
	{
	    tmp = next->next;
	    entry = list_entry(next, struct hash_entry, he_list);
	    if (delete_entry)
		delete_entry(cookie, entry->he_key, entry->he_obj);
	    free(entry);

	    next = tmp;
	}
    }

    free(tbl);
}