/*1* A hash table (hashtab) maintains associations between2* key values and datum values. The type of the key values3* and the type of the datum values is arbitrary. The4* functions for hash computation and key comparison are5* provided by the creator of the table.6*7* Author : Stephen Smalley, <[email protected]>8*/9#ifndef _SS_HASHTAB_H_10#define _SS_HASHTAB_H_1112#define HASHTAB_MAX_NODES 0xffffffff1314struct hashtab_node {15void *key;16void *datum;17struct hashtab_node *next;18};1920struct hashtab {21struct hashtab_node **htable; /* hash table */22u32 size; /* number of slots in hash table */23u32 nel; /* number of elements in hash table */24u32 (*hash_value)(struct hashtab *h, const void *key);25/* hash function */26int (*keycmp)(struct hashtab *h, const void *key1, const void *key2);27/* key comparison function */28};2930struct hashtab_info {31u32 slots_used;32u32 max_chain_len;33};3435/*36* Creates a new hash table with the specified characteristics.37*38* Returns NULL if insufficent space is available or39* the new hash table otherwise.40*/41struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, const void *key),42int (*keycmp)(struct hashtab *h, const void *key1, const void *key2),43u32 size);4445/*46* Inserts the specified (key, datum) pair into the specified hash table.47*48* Returns -ENOMEM on memory allocation error,49* -EEXIST if there is already an entry with the same key,50* -EINVAL for general errors or510 otherwise.52*/53int hashtab_insert(struct hashtab *h, void *k, void *d);5455/*56* Searches for the entry with the specified key in the hash table.57*58* Returns NULL if no entry has the specified key or59* the datum of the entry otherwise.60*/61void *hashtab_search(struct hashtab *h, const void *k);6263/*64* Destroys the specified hash table.65*/66void hashtab_destroy(struct hashtab *h);6768/*69* Applies the specified apply function to (key,datum,args)70* for each entry in the specified hash table.71*72* The order in which the function is applied to the entries73* is dependent upon the internal structure of the hash table.74*75* If apply returns a non-zero status, then hashtab_map will cease76* iterating through the hash table and will propagate the error77* return to its caller.78*/79int hashtab_map(struct hashtab *h,80int (*apply)(void *k, void *d, void *args),81void *args);8283/* Fill info with some hash table statistics */84void hashtab_stat(struct hashtab *h, struct hashtab_info *info);8586#endif /* _SS_HASHTAB_H */878889