| .. | .. |
|---|
| 11 | 11 | #ifndef _SS_HASHTAB_H_ |
|---|
| 12 | 12 | #define _SS_HASHTAB_H_ |
|---|
| 13 | 13 | |
|---|
| 14 | | -#define HASHTAB_MAX_NODES 0xffffffff |
|---|
| 14 | +#include <linux/types.h> |
|---|
| 15 | +#include <linux/errno.h> |
|---|
| 16 | +#include <linux/sched.h> |
|---|
| 17 | + |
|---|
| 18 | +#define HASHTAB_MAX_NODES U32_MAX |
|---|
| 19 | + |
|---|
| 20 | +struct hashtab_key_params { |
|---|
| 21 | + u32 (*hash)(const void *key); /* hash function */ |
|---|
| 22 | + int (*cmp)(const void *key1, const void *key2); |
|---|
| 23 | + /* key comparison function */ |
|---|
| 24 | +}; |
|---|
| 15 | 25 | |
|---|
| 16 | 26 | struct hashtab_node { |
|---|
| 17 | 27 | void *key; |
|---|
| .. | .. |
|---|
| 23 | 33 | struct hashtab_node **htable; /* hash table */ |
|---|
| 24 | 34 | u32 size; /* number of slots in hash table */ |
|---|
| 25 | 35 | u32 nel; /* number of elements in hash table */ |
|---|
| 26 | | - u32 (*hash_value)(struct hashtab *h, const void *key); |
|---|
| 27 | | - /* hash function */ |
|---|
| 28 | | - int (*keycmp)(struct hashtab *h, const void *key1, const void *key2); |
|---|
| 29 | | - /* key comparison function */ |
|---|
| 30 | 36 | }; |
|---|
| 31 | 37 | |
|---|
| 32 | 38 | struct hashtab_info { |
|---|
| .. | .. |
|---|
| 35 | 41 | }; |
|---|
| 36 | 42 | |
|---|
| 37 | 43 | /* |
|---|
| 38 | | - * Creates a new hash table with the specified characteristics. |
|---|
| 44 | + * Initializes a new hash table with the specified characteristics. |
|---|
| 39 | 45 | * |
|---|
| 40 | | - * Returns NULL if insufficent space is available or |
|---|
| 41 | | - * the new hash table otherwise. |
|---|
| 46 | + * Returns -ENOMEM if insufficient space is available or 0 otherwise. |
|---|
| 42 | 47 | */ |
|---|
| 43 | | -struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key), |
|---|
| 44 | | - int (*keycmp)(struct hashtab *h, const void *key1, const void *key2), |
|---|
| 45 | | - u32 size); |
|---|
| 48 | +int hashtab_init(struct hashtab *h, u32 nel_hint); |
|---|
| 49 | + |
|---|
| 50 | +int __hashtab_insert(struct hashtab *h, struct hashtab_node **dst, |
|---|
| 51 | + void *key, void *datum); |
|---|
| 46 | 52 | |
|---|
| 47 | 53 | /* |
|---|
| 48 | 54 | * Inserts the specified (key, datum) pair into the specified hash table. |
|---|
| .. | .. |
|---|
| 52 | 58 | * -EINVAL for general errors or |
|---|
| 53 | 59 | 0 otherwise. |
|---|
| 54 | 60 | */ |
|---|
| 55 | | -int hashtab_insert(struct hashtab *h, void *k, void *d); |
|---|
| 61 | +static inline int hashtab_insert(struct hashtab *h, void *key, void *datum, |
|---|
| 62 | + struct hashtab_key_params key_params) |
|---|
| 63 | +{ |
|---|
| 64 | + u32 hvalue; |
|---|
| 65 | + struct hashtab_node *prev, *cur; |
|---|
| 66 | + |
|---|
| 67 | + cond_resched(); |
|---|
| 68 | + |
|---|
| 69 | + if (!h->size || h->nel == HASHTAB_MAX_NODES) |
|---|
| 70 | + return -EINVAL; |
|---|
| 71 | + |
|---|
| 72 | + hvalue = key_params.hash(key) & (h->size - 1); |
|---|
| 73 | + prev = NULL; |
|---|
| 74 | + cur = h->htable[hvalue]; |
|---|
| 75 | + while (cur) { |
|---|
| 76 | + int cmp = key_params.cmp(key, cur->key); |
|---|
| 77 | + |
|---|
| 78 | + if (cmp == 0) |
|---|
| 79 | + return -EEXIST; |
|---|
| 80 | + if (cmp < 0) |
|---|
| 81 | + break; |
|---|
| 82 | + prev = cur; |
|---|
| 83 | + cur = cur->next; |
|---|
| 84 | + } |
|---|
| 85 | + |
|---|
| 86 | + return __hashtab_insert(h, prev ? &prev->next : &h->htable[hvalue], |
|---|
| 87 | + key, datum); |
|---|
| 88 | +} |
|---|
| 56 | 89 | |
|---|
| 57 | 90 | /* |
|---|
| 58 | 91 | * Searches for the entry with the specified key in the hash table. |
|---|
| .. | .. |
|---|
| 60 | 93 | * Returns NULL if no entry has the specified key or |
|---|
| 61 | 94 | * the datum of the entry otherwise. |
|---|
| 62 | 95 | */ |
|---|
| 63 | | -void *hashtab_search(struct hashtab *h, const void *k); |
|---|
| 96 | +static inline void *hashtab_search(struct hashtab *h, const void *key, |
|---|
| 97 | + struct hashtab_key_params key_params) |
|---|
| 98 | +{ |
|---|
| 99 | + u32 hvalue; |
|---|
| 100 | + struct hashtab_node *cur; |
|---|
| 101 | + |
|---|
| 102 | + if (!h->size) |
|---|
| 103 | + return NULL; |
|---|
| 104 | + |
|---|
| 105 | + hvalue = key_params.hash(key) & (h->size - 1); |
|---|
| 106 | + cur = h->htable[hvalue]; |
|---|
| 107 | + while (cur) { |
|---|
| 108 | + int cmp = key_params.cmp(key, cur->key); |
|---|
| 109 | + |
|---|
| 110 | + if (cmp == 0) |
|---|
| 111 | + return cur->datum; |
|---|
| 112 | + if (cmp < 0) |
|---|
| 113 | + break; |
|---|
| 114 | + cur = cur->next; |
|---|
| 115 | + } |
|---|
| 116 | + return NULL; |
|---|
| 117 | +} |
|---|
| 64 | 118 | |
|---|
| 65 | 119 | /* |
|---|
| 66 | 120 | * Destroys the specified hash table. |
|---|
| .. | .. |
|---|
| 82 | 136 | int (*apply)(void *k, void *d, void *args), |
|---|
| 83 | 137 | void *args); |
|---|
| 84 | 138 | |
|---|
| 139 | +int hashtab_duplicate(struct hashtab *new, struct hashtab *orig, |
|---|
| 140 | + int (*copy)(struct hashtab_node *new, |
|---|
| 141 | + struct hashtab_node *orig, void *args), |
|---|
| 142 | + int (*destroy)(void *k, void *d, void *args), |
|---|
| 143 | + void *args); |
|---|
| 144 | + |
|---|
| 85 | 145 | /* Fill info with some hash table statistics */ |
|---|
| 86 | 146 | void hashtab_stat(struct hashtab *h, struct hashtab_info *info); |
|---|
| 87 | 147 | |
|---|