Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 1 | #ifndef HASH_H |
| 2 | #define HASH_H |
| 3 | |
| 4 | /* |
| 5 | * These are some simple generic hash table helper functions. |
| 6 | * Not necessarily suitable for all users, but good for things |
| 7 | * where you want to just keep track of a list of things, and |
| 8 | * have a good hash to use on them. |
| 9 | * |
| 10 | * It keeps the hash table at roughly 50-75% free, so the memory |
| 11 | * cost of the hash table itself is roughly |
| 12 | * |
| 13 | * 3 * 2*sizeof(void *) * nr_of_objects |
| 14 | * |
| 15 | * bytes. |
| 16 | * |
| 17 | * FIXME: on 64-bit architectures, we waste memory. It would be |
| 18 | * good to have just 32-bit pointers, requiring a special allocator |
| 19 | * for hashed entries or something. |
| 20 | */ |
| 21 | struct hash_table_entry { |
| 22 | unsigned int hash; |
| 23 | void *ptr; |
| 24 | }; |
| 25 | |
| 26 | struct hash_table { |
| 27 | unsigned int size, nr; |
| 28 | struct hash_table_entry *array; |
| 29 | }; |
| 30 | |
Linus Torvalds | d1f128b | 2008-03-06 12:46:09 -0800 | [diff] [blame] | 31 | extern void *lookup_hash(unsigned int hash, const struct hash_table *table); |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 32 | extern void **insert_hash(unsigned int hash, void *ptr, struct hash_table *table); |
Linus Torvalds | 11f944d | 2011-02-18 19:55:19 -0800 | [diff] [blame] | 33 | extern int for_each_hash(const struct hash_table *table, int (*fn)(void *, void *), void *data); |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 34 | extern void free_hash(struct hash_table *table); |
| 35 | |
| 36 | static inline void init_hash(struct hash_table *table) |
| 37 | { |
| 38 | table->size = 0; |
| 39 | table->nr = 0; |
| 40 | table->array = NULL; |
| 41 | } |
| 42 | |
| 43 | #endif |