Hash_InitTable.c
2.58 KB
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
/*
* Hash_InitTable.c --
*
* Source code for the Hash_InitTable library procedure.
*
* Copyright 1988 Regents of the University of California
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies. The University of California
* makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without
* express or implied warranty.
*/
#ifndef lint
static char rcsid[] = "$Header: /root/leakn64/depot/rf/sw/bbplayer/simos/apps/unix/ethersim/common/Hash_InitTable.c,v 1.1.1.1 2002/05/29 01:09:09 blythe Exp $ SPRITE (Berkeley)";
#endif not lint
#include <hash.h>
#include <list.h>
#include <stdlib.h>
/*
*---------------------------------------------------------
*
* Hash_InitTable --
*
* This routine just sets up the hash table.
*
* Results:
* None.
*
* Side Effects:
* Memory is allocated for the initial bucket area.
*
*---------------------------------------------------------
*/
void
Hash_InitTable(
register Hash_Table *tablePtr, /* Structure to use to hold table. */
int numBuckets, /* How many buckets to create for
* starters. This number is rounded
* up to a power of two. If <= 0,
* a reasonable default is chosen.
* The table will grow in size later
* as needed. */
int keyType /* HASH_STRING_KEYS means that key
* values passed to HashFind will be
* strings, passed via a (char *)
* pointer. HASH_ONE_WORD_KEYS means
* that key values will be any
* one-word value passed as Address.
* > 1 means that key values will be
* multi-word values whose address is
* passed as Address. In this last
* case, keyType is the number of
* words in the key, not the number
* of bytes. */
)
{
register int i;
register List_Links *bucketPtr;
/*
* Round up the size to a power of two.
*/
if (numBuckets <= 0) {
numBuckets = 16;
}
tablePtr->numEntries = 0;
tablePtr->keyType = keyType;
tablePtr->size = 2;
tablePtr->mask = 1;
tablePtr->downShift = 29;
while (tablePtr->size < numBuckets) {
tablePtr->size <<= 1;
tablePtr->mask = (tablePtr->mask << 1) + 1;
tablePtr->downShift--;
}
tablePtr->bucketPtr = (List_Links *) malloc((unsigned) (sizeof(List_Links)
* tablePtr->size));
for (i=0, bucketPtr = tablePtr->bucketPtr; i < tablePtr->size;
i++, bucketPtr++) {
List_Init(bucketPtr);
}
}