File: genhash.c

package info (click to toggle)
centerim 4.22.10-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,520 kB
  • sloc: cpp: 50,525; ansic: 44,676; sh: 12,243; makefile: 723; perl: 327; yacc: 288; python: 93; sed: 16; awk: 14
file content (490 lines) | stat: -rw-r--r-- 13,909 bytes parent folder | download | duplicates (8)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
 *  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; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  Jabber
 *  Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
 */
#include <libxode.h>

/*****************************************************************************
 * Internal type definitions
 */

typedef struct tagHNODE
{
    struct tagHNODE *next;             /* next node in list */
    const void *key;                   /* key pointer */
    void *value;                       /* value pointer */
} HNODE;

#define SLAB_NUM_NODES     64        /* allocate this many nodes per slab */

typedef struct tagHSLAB
{
    struct tagHSLAB *next;             /* next slab pointer */
    HNODE nodes[SLAB_NUM_NODES];       /* the actual nodes */
} HSLAB;

#define HASH_NUM_BUCKETS   509       /* should be a prime number; see Knuth */

typedef struct tagHASHTABLE_INTERNAL
{
    unsigned long sig1;                /* first signature word */
    KEYHASHFUNC hash;                  /* hash function */
    KEYCOMPAREFUNC cmp;                /* comparison function */
    int count;                         /* table entry count */
    int bcount;                        /* bucket count */
    HNODE **buckets;                   /* the hash buckets */
    unsigned long sig2;                /* second signature word */

} HASHTABLE_INTERNAL;

#define HASH_SIG1      0x68736148UL  /* "Hash" */
#define HASH_SIG2      0x6F627245UL  /* "Erbo" */

#define do_hash(tb,key)     ((*((tb)->hash))(key) % ((tb)->bcount))

static HNODE *s_free_nodes = NULL;   /* free nodes list */
static HSLAB *s_slabs = NULL;        /* node slabs list */

/*****************************************************************************
 * Internal functions
 */

static HNODE *allocate_node(
    const void *key,   /* key pointer for this node */
    void *value)       /* value pointer for this node */
/*
    allocate_node allocates a new hash node and fills it.  Returns NULL if the
    node could not be allocated.
*/
{
    HNODE *rc;   /* return from this function */

    if (!s_free_nodes)
    { /* allocate a new slabful of nodes and chain them to make a new free list */
        register int i;  /* loop counter */
        HSLAB *slab = (HSLAB *)malloc(sizeof(HSLAB));
        if (!slab)
            return NULL;
        memset(slab,0,sizeof(HSLAB));
        slab->next = s_slabs;
        for (i=0; i<(SLAB_NUM_NODES-1); i++)
            slab->nodes[i].next = &(slab->nodes[i+1]);
        s_free_nodes = &(slab->nodes[0]);
        s_slabs = slab;

    } /* end if */

    /* grab a node off the fron of the free list and fill it */
    rc = s_free_nodes;
    s_free_nodes = rc->next;
    rc->next = NULL;
    rc->key = key;
    rc->value = value;
    return rc;

} /* end allocate_node */

static void free_node(
    HNODE *node)   /* node to be freed */
/*
    free_node returns a hash node to the list.
*/
{
    /* zap the node contents to avoid problems later */
    memset(node,0,sizeof(HNODE));

    /* chain it onto the free list */
    node->next = s_free_nodes;
    s_free_nodes = node;

} /* end free_node */

static HNODE *find_node(
    HASHTABLE_INTERNAL *tab,  /* pointer to hash table */
    const void *key,          /* key value to look up */
    int bucket)               /* bucket number (-1 to have function compute it) */
/*
    find_node walks a hash bucket to find a node whose key matches the named key value.
    Returns the node pointer, or NULL if it's not found.
*/
{
    register HNODE *p;  /* search pointer/return from this function */

    if (bucket<0)  /* compute hash value if we don't know it already */
        bucket = do_hash(tab,key);

    /* search through the bucket contents */
    for (p=tab->buckets[bucket]; p; p=p->next)
        if ((*(tab->cmp))(key,p->key)==0)
            return p;  /* found! */

    return NULL;   /* not found */

} /* end find_node */

static HASHTABLE_INTERNAL *handle2ptr(
    HASHTABLE tbl)  /* hash table handle */
/*
    handle2ptr converts a hash table handle into a pointer and checks its signatures
    to make sure someone's not trying to pull a whizzer on this module.
*/
{
    register HASHTABLE_INTERNAL *rc = (HASHTABLE_INTERNAL *)tbl;
    if ((rc->sig1==HASH_SIG1) && (rc->sig2==HASH_SIG2))
        return rc;     /* signatures match */
    else
        return NULL;   /* yIkes! */
}

/*****************************************************************************
 * External functions
 */

HASHTABLE ghash_create(int buckets, KEYHASHFUNC hash, KEYCOMPAREFUNC cmp)
/*
    Description:
        Creates a new hash table.

    Input:
        Parameters:
        buckets - Number of buckets to allocate for the hash table; this value
                  should be a prime number for maximum efficiency.
        hash - Key hash code function to use.
        cmp - Key comparison function to use.

    Output:
        Returns:
        NULL - Table could not be allocated.
        Other - Handle to the new hashtable.
*/
{
    HASHTABLE_INTERNAL *tab;  /* new table structure */
    char *allocated;

    if (!hash || !cmp)
        return NULL;  /* bogus! */

    if (buckets<=0)
        buckets = HASH_NUM_BUCKETS;

    /* allocate a hash table structure */
    allocated = malloc(sizeof(HASHTABLE_INTERNAL) + (buckets * sizeof(HNODE *)));
    if (!allocated)
        return NULL;  /* memory error */

    /* fill the fields of the hash table */
    tab = (HASHTABLE_INTERNAL *)allocated;
    allocated += sizeof(HASHTABLE_INTERNAL);
    memset(tab,0,sizeof(HASHTABLE_INTERNAL));
    memset(allocated,0,buckets * sizeof(HNODE *));
    tab->sig1 = HASH_SIG1;
    tab->hash = hash;
    tab->cmp = cmp;
    tab->bcount = buckets;
    tab->buckets = (HNODE **)allocated;
    tab->sig2 = HASH_SIG2;

    return (HASHTABLE)tab;  /* Qa'pla! */

} /* end ghash_create */

void ghash_destroy(HASHTABLE tbl)
/*
    Description:
        Destroys a hash table.

    Input:
        Parameters:
        tbl - Table to be destroyed.

    Output:
        Returns:
        Nothing.
*/
{
    HASHTABLE_INTERNAL *tab;  /* new table structure */
    int i;                    /* loop counter */
    HNODE *p, *p2;            /* temporary pointers */

    if (!tbl)
        return;  /* bogus! */

    /* Convert the handle to a table pointer. */
    tab = handle2ptr(tbl);
    if (!tab)
        return;

    /* Nuke the nodes it contains. */
    for (i=0; i<tab->bcount; i++)
    { /* free the contents of each bucket */
        p = tab->buckets[i];
        while (p)
        { /* free each node in turn */
            p2 = p->next;
            free_node(p);
            p = p2;

        } /* end while */

    } /* end for */

    free(tab);  /* bye bye now! */

} /* end ghash_destroy */

void *ghash_get(HASHTABLE tbl, const void *key)
/*
    Description:
        Retrieves a value stored in the hash table.

    Input:
        Parameters:
        tbl - The hash table to look in.
        key - The key value to search on.

    Output:
        Returns:
        NULL - Value not found.
        Other - Value corresponding to the specified key.
*/
{
    HASHTABLE_INTERNAL *tab;  /* internal table pointer */
    HNODE *node;              /* hash node */
    void *rc = NULL;          /* return from this function */

    if (!tbl || !key)
        return NULL;  /* bogus! */

    /* Convert the handle to a table pointer. */
    tab = handle2ptr(tbl);
    if (!tab)
        return NULL;  /* error */

    /* Attempt to find the node. */
    node = find_node(tab,key,-1);
    if (node)
        rc = node->value;  /* found it! */

    return rc;

} /* end ghash_get */

int ghash_put(HASHTABLE tbl, const void *key, void *value)
/*
    Description:
        Associates a key with a value in this hash table.

    Input:
        Parameters:
        tbl - Hash table to add.
        key - Key to use for the value in the table.
        value - Value to add for this key.

    Output:
        Returns:
        1 - Success.
        0 - Failure.

    Notes:
        If the specified key is already in the hashtable, its value will be replaced.
*/
{
    HASHTABLE_INTERNAL *tab;  /* internal table pointer */
    int bucket;               /* bucket value goes into */
    HNODE *node;              /* hash node */
    int rc = 1;               /* return from this function */

    if (!tbl || !key || !value)
        return 0;  /* bogus! */

    /* Convert the handle to a table pointer. */
    tab = handle2ptr(tbl);
    if (!tab)
        return 0;  /* error */


    /* Compute the hash bucket and try to find an existing node. */
    bucket = do_hash(tab,key);
    node = find_node(tab,key,bucket);
    if (!node)
    { /* OK, try to allocate a new node. */
        node = allocate_node(key,value);
        if (node)
        { /* Chain the new node into the hash table. */
            node->next = tab->buckets[bucket];
            tab->buckets[bucket] = node;
            tab->count++;

        } /* end if */
        else  /* allocation error */
            rc = 0;

    } /* end if */
    else  /* already in table - just reassign value */
        node->value = value;

    return rc;

} /* end ghash_put */

int ghash_remove(HASHTABLE tbl, const void *key)
/*
    Description:
        Removes an entry from a hash table, given its key.
    
    Input:
        Parameters:
        tbl - Hash table to remove from.
        key - Key of value to remove.

    Output:
        Returns:
        1 - Success.
        0 - Failure; key not present in hash table.
*/
{
    HASHTABLE_INTERNAL *tab;  /* internal table pointer */
    int bucket;               /* bucket value goes into */
    HNODE *node;              /* hash node */
    register HNODE *p;        /* removal pointer */
    int rc = 1;               /* return from this function */

    if (!tbl || !key)
        return 0;  /* bogus! */

    /* Convert the handle to a table pointer. */
    tab = handle2ptr(tbl);
    if (!tab)
        return 0;  /* error */


    /* Compute the hash bucket and try to find an existing node. */
    bucket = do_hash(tab,key);
    node = find_node(tab,key,bucket);
    if (node)
    { /* look to unchain it from the bucket it's in */
        if (node==tab->buckets[bucket])
            tab->buckets[bucket] = node->next;  /* unchain at head */
        else
        { /* unchain in middle of list */
            for (p=tab->buckets[bucket]; p->next!=node; p=p->next) ;
            p->next = node->next;

        } /* end else */

        free_node(node);  /* bye bye now! */
        tab->count--;

    } /* end if */
    else  /* node not found */
        rc = 0;

    return rc;

} /* end ghash_remove */

int ghash_walk(HASHTABLE tbl, TABLEWALKFUNC func, void *user_data)
/*
    Description:
        "Walks" through a hash table, calling a callback function for each element
    stored in it.

    Input:
        Parameters:
        tbl - Hash table to walk.
        func - Function to be called for each node.  It takes three parameters,
                   a user data pointer, a key value pointer, and a data value pointer.
           It returns 0 to stop the enumeration or 1 to keep it going.
        user_data - Value to use as the first parameter for the callback
                    function.

    Output:
        Returns:
        0 - Error occurred.
        Other - Number of nodes visited up to and including the one for which
                the callback function returned 0, if it did; ranges from 1
            to the number of nodes in the hashtable.
*/
{
    HASHTABLE_INTERNAL *tab;  /* internal table pointer */
    int i;                    /* loop counter */
    int running = 1;          /* we're still running */
    int count = 0;            /* number of nodes visited before stop node */
    register HNODE *p, *p2;   /* loop pointer */

    if (!tbl || !func)
        return -1;  /* bogus values! */

    /* Convert the handle to a table pointer. */
    tab = handle2ptr(tbl);
    if (!tab)
        return -1;  /* error */


    for (i=0; running && (i<tab->bcount); i++)
    { /* visit the contents of each bucket */
        p = tab->buckets[i];
        while (running && p)
        { /* visit each node in turn */
            p2 = p->next;
            count++;
            running = (*func)(user_data,p->key,p->value);
            p = p2;

        } /* end while */

    } /* end for */

    return count;

} /* end ghash_walk */

int str_hash_code(const char *s)
/*
    Description:
        Generates a hash code for a string.  This function uses the ELF hashing
        algorithm as reprinted in Andrew Binstock, "Hashing Rehashed," _Dr.
        Dobb's Journal_, April 1996.
 
    Input:
        Parameters:
            s - The string to be hashed.
 
    Output:
        Returns:
            A hash code for the string.
*/
{
    /* ELF hash uses unsigned chars and unsigned arithmetic for portability */
    const unsigned char *name = (const unsigned char *)s;
    unsigned long h = 0, g;

    if (!name)
        return 0;  /* anti-NULL guard not in the original */

    while (*name)
    { /* do some fancy bitwanking on the string */
        h = (h << 4) + (unsigned long)(*name++);
        if ((g = (h & 0xF0000000UL))!=0)
            h ^= (g >> 24);
        h &= ~g;

    } /* end while */

    return (int)h;

}