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