forked from autopilot-rs/autopy-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUTHashTable.c
56 lines (46 loc) · 1.6 KB
/
UTHashTable.c
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
#include "UTHashTable.h"
#include <stdlib.h>
#include <assert.h>
/* Base struct class (all nodes must contain at least the elements in
* this struct). */
struct _UTHashNode {
UTHashNode_HEAD
};
typedef struct _UTHashNode UTHashNode;
void initHashTable(UTHashTable *table, size_t initialCount, size_t nodeSize)
{
assert(table != NULL);
assert(nodeSize >= sizeof(UTHashNode));
table->uttable = NULL; /* Must be set to NULL for uthash. */
table->allocedNodeCount = (initialCount == 0) ? 1 : initialCount;
table->nodeCount = 0;
table->nodeSize = nodeSize;
table->nodes = calloc(table->nodeSize, nodeSize * table->allocedNodeCount);
}
void destroyHashTable(UTHashTable *table)
{
UTHashNode *uttable = table->uttable;
UTHashNode *node;
/* Let uthash do its magic. */
while (uttable != NULL) {
node = uttable; /* Grab pointer to first item. */
HASH_DEL(uttable, node); /* Delete it (table advances to next). */
}
/* Only giant malloc'd block containing each node must be freed. */
if (table->nodes != NULL) free(table->nodes);
table->uttable = table->nodes = NULL;
}
void *getNewNode(UTHashTable *table)
{
/* Increment node count, resizing table if necessary. */
const size_t newNodeCount = ++(table->nodeCount);
if (table->allocedNodeCount < newNodeCount) {
do {
/* Double size each time to avoid calls to realloc(). */
table->allocedNodeCount <<= 1;
} while (table->allocedNodeCount < newNodeCount);
table->nodes = realloc(table->nodes, table->nodeSize *
table->allocedNodeCount);
}
return (char *)table->nodes + (table->nodeSize * (table->nodeCount - 1));
}