2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
11 /* Hash table implementation from the GIT project. */
12 /* Copyright 2008 (C) Linus Torvalds, GPL version 2 */
14 * These are some simple generic hash table helper functions.
15 * Not necessarily suitable for all users, but good for things
16 * where you want to just keep track of a list of things, and
17 * have a good hash to use on them.
19 * It keeps the hash table at roughly 50-75% free, so the memory
20 * cost of the hash table itself is roughly
22 * 3 * 2*sizeof(void *) * nr_of_objects
26 * FIXME: on 64-bit architectures, we waste memory. It would be
27 * good to have just 32-bit pointers, requiring a special allocator
28 * for hashed entries or something.
33 #define alloc_nr(x) (((x) + 16) * 3 / 2)
34 #define INSERT_HASH_PROTOS(ops, table) \
36 void **pos = insert_hash((ops).key, &(ops), &(table)); \
37 /* We already had an entry there? */ \
44 struct hash_table_entry
{
50 unsigned int size
, nr
;
51 struct hash_table_entry
*array
;
54 extern void *lookup_hash(unsigned int hash
, const struct hash_table
*table
);
55 extern void **insert_hash(unsigned int hash
, void *ptr
,
56 struct hash_table
*table
);
57 extern void *remove_hash(unsigned int hash
, void *ptr
, void *ptr_next
,
58 struct hash_table
*table
);
59 extern int for_each_hash(const struct hash_table
*table
, int (*fn
)(void *));
60 extern int for_each_hash_int(const struct hash_table
*table
,
61 int (*fn
)(void *, int), int arg
);
62 extern void free_hash(struct hash_table
*table
);
64 static inline void init_hash(struct hash_table
*table
)
71 static inline unsigned char icase_hash(unsigned char c
)
73 return c
& ~((c
& 0x40) >> 1);
76 static inline unsigned int hash_name(const char *name
, int namelen
)
78 unsigned int hash
= 0x123;
80 unsigned char c
= *name
++;
82 hash
= hash
* 101 + c
;