1 2 /* Author : Stephen Smalley, <[email protected]> */ 3 4 /* FLASK */ 5 6 /* 7 * A hash table (hashtab) maintains associations between 8 * key values and datum values. The type of the key values 9 * and the type of the datum values is arbitrary. The 10 * functions for hash computation and key comparison are 11 * provided by the creator of the table. 12 */ 13 14 #ifndef _NEWROLE_HASHTAB_H_ 15 #define _NEWROLE_HASHTAB_H_ 16 17 #include <stdint.h> 18 #include <errno.h> 19 #include <stdio.h> 20 21 typedef char *hashtab_key_t; /* generic key type */ 22 typedef const char *const_hashtab_key_t; /* constant generic key type */ 23 typedef void *hashtab_datum_t; /* generic datum type */ 24 25 typedef struct hashtab_node *hashtab_ptr_t; 26 27 typedef struct hashtab_node { 28 hashtab_key_t key; 29 hashtab_datum_t datum; 30 hashtab_ptr_t next; 31 } hashtab_node_t; 32 33 typedef struct hashtab_val { 34 hashtab_ptr_t *htable; /* hash table */ 35 unsigned int size; /* number of slots in hash table */ 36 uint32_t nel; /* number of elements in hash table */ 37 unsigned int (*hash_value) (struct hashtab_val * h, const_hashtab_key_t key); /* hash function */ 38 int (*keycmp) (struct hashtab_val * h, const_hashtab_key_t key1, const_hashtab_key_t key2); /* key comparison function */ 39 } hashtab_val_t; 40 41 typedef hashtab_val_t *hashtab_t; 42 43 /* Define status codes for hash table functions */ 44 #define HASHTAB_SUCCESS 0 45 #define HASHTAB_OVERFLOW -ENOMEM 46 #define HASHTAB_PRESENT -EEXIST 47 #define HASHTAB_MISSING -ENOENT 48 49 /* 50 Creates a new hash table with the specified characteristics. 51 52 Returns NULL if insufficient space is available or 53 the new hash table otherwise. 54 */ 55 extern hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h, 56 const_hashtab_key_t 57 key), 58 int (*keycmp) (hashtab_t h, 59 const_hashtab_key_t key1, 60 const_hashtab_key_t key2), 61 unsigned int size); 62 /* 63 Inserts the specified (key, datum) pair into the specified hash table. 64 65 Returns HASHTAB_OVERFLOW if insufficient space is available or 66 HASHTAB_PRESENT if there is already an entry with the same key or 67 HASHTAB_SUCCESS otherwise. 68 */ 69 extern int hashtab_insert(hashtab_t h, hashtab_key_t k, hashtab_datum_t d); 70 71 /* 72 Removes the entry with the specified key from the hash table. 73 Applies the specified destroy function to (key,datum,args) for 74 the entry. 75 76 Returns HASHTAB_MISSING if no entry has the specified key or 77 HASHTAB_SUCCESS otherwise. 78 */ 79 extern int hashtab_remove(hashtab_t h, hashtab_key_t k, 80 void (*destroy) (hashtab_key_t k, 81 hashtab_datum_t d, 82 void *args), void *args); 83 84 /* 85 Searches for the entry with the specified key in the hash table. 86 87 Returns NULL if no entry has the specified key or 88 the datum of the entry otherwise. 89 */ 90 extern hashtab_datum_t hashtab_search(hashtab_t h, const_hashtab_key_t k); 91 92 /* 93 Destroys the specified hash table. 94 */ 95 extern void hashtab_destroy(hashtab_t h); 96 97 /* 98 Applies the specified apply function to (key,datum,args) 99 for each entry in the specified hash table. 100 101 The order in which the function is applied to the entries 102 is dependent upon the internal structure of the hash table. 103 104 If apply returns a non-zero status, then hashtab_map will cease 105 iterating through the hash table and will propagate the error 106 return to its caller. 107 */ 108 extern int hashtab_map(hashtab_t h, 109 int (*apply) (hashtab_key_t k, 110 hashtab_datum_t d, 111 void *args), void *args); 112 113 extern void hashtab_hash_eval(hashtab_t h, char *tag); 114 115 #endif 116