2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
11 /* Hash table implementation from the GIT project. */
12 /* Copyright xxxx (C) Linus Torvalds (?), GPL version 2 */
15 * Look up a hash entry in the hash table. Return the pointer to
16 * the existing entry, or the empty slot if none existed. The caller
17 * can then look at the (*ptr) to see whether it existed or not.
19 static struct hash_table_entry
*lookup_hash_entry(unsigned int hash
,
20 const struct hash_table
23 unsigned int size
= table
->size
, nr
= hash
% size
;
24 struct hash_table_entry
*array
= table
->array
;
26 while (array
[nr
].ptr
) {
27 if (array
[nr
].hash
== hash
)
38 * Insert a new hash entry pointer into the table.
40 * If that hash entry already existed, return the pointer to
41 * the existing entry (and the caller can create a list of the
42 * pointers or do anything else). If it didn't exist, return
43 * NULL (and the caller knows the pointer has been inserted).
45 static void **insert_hash_entry(unsigned int hash
, void *ptr
,
46 struct hash_table
*table
)
48 struct hash_table_entry
*entry
= lookup_hash_entry(hash
, table
);
60 static void grow_hash_table(struct hash_table
*table
)
63 unsigned int old_size
= table
->size
, new_size
;
64 struct hash_table_entry
*old_array
= table
->array
, *new_array
;
66 new_size
= alloc_nr(old_size
);
67 new_array
= xcalloc(sizeof(struct hash_table_entry
), new_size
);
68 table
->size
= new_size
;
69 table
->array
= new_array
;
72 for (i
= 0; i
< old_size
; i
++) {
73 unsigned int hash
= old_array
[i
].hash
;
74 void *ptr
= old_array
[i
].ptr
;
76 insert_hash_entry(hash
, ptr
, table
);
83 void *lookup_hash(unsigned int hash
, const struct hash_table
*table
)
87 return lookup_hash_entry(hash
, table
)->ptr
;
90 void **insert_hash(unsigned int hash
, void *ptr
, struct hash_table
*table
)
92 unsigned int nr
= table
->nr
;
94 if (nr
>= table
->size
/2)
95 grow_hash_table(table
);
96 return insert_hash_entry(hash
, ptr
, table
);
99 int for_each_hash(const struct hash_table
*table
, int (*fn
)(void *))
103 unsigned int size
= table
->size
;
104 struct hash_table_entry
*array
= table
->array
;
106 for (i
= 0; i
< size
; i
++) {
107 void *ptr
= array
->ptr
;
120 void free_hash(struct hash_table
*table
)