1
/* $Id: netsys_c_htab.c 1497 2010-11-28 22:13:46Z gerd $ */
3
#include "netsys_c_htab.h"
8
/* Define an FNV-1 hash function
9
(see http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash)
11
All artithmetic is unsigned!
15
#define P 1099511628211
16
#define S 0xcbf29ce484222325
18
((((((((((((((((S * P) ^ ((x >> 56) & 0xff)) * \
19
P) ^ ((x >> 48) & 0xff)) * \
20
P) ^ ((x >> 40) & 0xff)) * \
21
P) ^ ((x >> 32) & 0xff)) * \
22
P) ^ ((x >> 24) & 0xff)) * \
23
P) ^ ((x >> 16) & 0xff)) * \
24
P) ^ ((x >> 8) & 0xff)) * \
31
((((((((S * P) ^ ((x >> 24) & 0xff)) * P) ^ ((x >> 16) & 0xff)) * \
32
P) ^ ((x >> 8) & 0xff)) * P) ^ (x & 0xff)) % n
36
static void netsys_htab_add_1(struct htab *t, void *a1, void *a2)
38
unsigned long i1, h1, size;
39
struct htab_cell *table;
43
i1 = (unsigned long) a1;
45
while (table[h1].orig_addr != NULL) {
47
if (h1 == size) h1 = 0;
49
table[h1].orig_addr = a1;
50
table[h1].relo_addr = a2;
55
static int netsys_htab_grow(struct htab *t, unsigned long n)
57
struct htab_cell *old_table;
58
struct htab_cell *new_table;
59
unsigned long k, old_size;
61
if (n < t->table_size) return (-2);
64
old_size = t->table_size;
66
new_table = (struct htab_cell *) malloc(n * sizeof(struct htab_cell));
67
if (new_table == NULL) {
73
new_table[k].orig_addr = NULL;
74
new_table[k].relo_addr = NULL;
81
if (old_table != NULL) {
82
for (k=0; k<old_size; k++) {
83
if (old_table[k].orig_addr != NULL) {
85
old_table[k].orig_addr,
86
old_table[k].relo_addr);
97
int netsys_htab_init(struct htab *t, unsigned long n)
102
return netsys_htab_grow(t, n);
106
void netsys_htab_clear(struct htab *t)
108
unsigned long k, size;
109
struct htab_cell *table;
111
size = t->table_size;
113
memset(table, 0, size * sizeof(struct htab_cell));
118
int netsys_htab_add(struct htab *t, void *a1, void *a2)
122
if (a1 == NULL || a2 == NULL) return (-2);
124
if (2 * t->table_used > t->table_size) {
125
code = netsys_htab_grow(t, 2 * t->table_size);
126
if (code < 0) return code;
129
netsys_htab_add_1(t, a1, a2);
134
int netsys_htab_lookup(struct htab *t,
135
void *a1, void **a2p)
137
unsigned long i1, h1, size;
138
struct htab_cell *table;
141
size = t->table_size;
142
i1 = (unsigned long) a1;
144
while (table[h1].orig_addr != NULL && table[h1].orig_addr != a1) {
146
if (h1 == size) h1 = 0;
148
if (table[h1].orig_addr == NULL)
151
*a2p = table[h1].relo_addr;
157
void netsys_htab_free(struct htab *t)