Changeset 23 for trunk/kernel/libk/htab.h
- Timestamp:
- Jun 18, 2017, 10:06:41 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/libk/htab.h
r1 r23 2 2 * htab.h - Generic embedded hash table definition. 3 3 * 4 * Author Ghassan Almalles (2008,2009,2010,2011,2012) 5 * Mohamed Lamine Karaoui (2015) 6 * Alain Greiner (2016) 4 * Authors Alain Greiner (2016,2017) 7 5 * 8 6 * Copyright (c) UPMC Sorbonne Universites … … 29 27 30 28 #include <hal_types.h> 29 #include <rwlock.h> 31 30 #include <list.h> 32 31 32 ///////////////////////////////////////////////////////////////////////////////////////// 33 // This file define a generic, embedded, hash table. 34 // 35 // It can only be accessed by threads running in the local cluster. 36 // It is generic as it can be used to register various types of items. 37 // The main goal is to provide fast retrieval for a large number of items of same type. 38 // For this purpose the set of all registered items is split in several subsets. 39 // Each subset is organised as a double linked lists. 40 // - an item is uniquely identified by a <key>, that can be a single uint32_t, 41 // a character string, or a more complex structure. 42 // - From the pointer on <key>, we use an item type specific htab_index() function, 43 // to compute an <index> value, defining a subset of registered items. 44 // - As several items can have the same <index>, we use the item type specific defined 45 // htab_scan() function for a final associative search on the subset. 46 // - Each registered item is a structure, that must contain an embedded list_entry_t, 47 // that is part of a rooted double linked list. 48 // 49 // Implementation Note: for each supported item type ***, you must define the two 50 // htab_***_index() and htab_***_scan() functions, and 51 // update the htab_init() function. 52 ///////////////////////////////////////////////////////////////////////////////////////// 53 54 #define HASHTAB_SIZE 64 // number of subsets 55 33 56 /**************************************************************************************** 34 * This file define a generic, embedded, hash table. 35 * The main goal is to provide fast retrieval for a large number of items of same type 36 * in a given cluster. For this purpose the set of all registered items is split 37 * in several subsets. Each subset is organised as a double linked lists. 38 * - an item is uniquely identified by a <key>, that can be a single uint32_t, 39 * a character string, or a more complex structure. 40 * - From the pointer on the <key>, the hash table uses an user defined ht_hash() 41 * function, to compute an <index> value, defining a subset of registered 42 * items, to restrict the associative search. 43 * - As several items can have the same <index>, the hash table uses the user defined 44 * ht_compare() function for a final associative search on the subset. 45 * - Each registered item is a structure, that must contain an embedded ht_node_t, 46 * This ht_node_t is part of a rooted double linked list, and contains the <index>. 47 * - Finally, the hash table returns a pointer on the embedded ht_node_t, and the 48 * pointer on the searched item can be obtained by a simple offset. 57 * These typedef define the two item type specific function prototypes. 49 58 ***************************************************************************************/ 50 59 60 struct htab_s; 61 62 typedef void * htab_scan_t( struct htab_s * htab , uint32_t index , void * key ); 63 64 typedef uint32_t htab_index_t( void * key ); 65 51 66 /**************************************************************************************** 52 * This define the generic, user defined, functiontypes.67 * This define the supported item types. 53 68 ***************************************************************************************/ 54 69 55 struct ht_node_s; 56 57 typedef bool_t ht_compare_t( struct ht_node_s * node , void * key ); 58 typedef uint32_t ht_hash_t( void * key ); 70 typedef enum 71 { 72 HTAB_INODE_TYPE = 1, /*! item is a vfs_inode_t */ 73 } 74 htab_item_type_t; 59 75 60 76 /**************************************************************************************** 61 * This structure defines the hash table header.77 * This structure defines the the root of a local hash table. 62 78 ***************************************************************************************/ 63 79 64 typedef struct ht _header_s80 typedef struct htab_s 65 81 { 66 uint32_t nb_buckets; /*! number of subsets (one linked list per subset) */ 67 ht_hash_t * hash; /*! user defined hash function */ 68 ht_compare_t * compare; /*! user defined compare function */ 69 list_entry_t * buckets; /*! array of root of partial lists of ht_node_t */ 82 list_entry_t roots[HASHTAB_SIZE]; /*! array of roots of partial lists */ 83 htab_index_t * index; /*! item type specific function */ 84 htab_scan_t * scan; /*! item type specific function */ 85 uint32_t items; /*! number of registered items */ 86 rwlock_t lock; /*! lock protecting hash table accesses */ 70 87 } 71 ht _header_t;88 htab_t; 72 89 73 90 /**************************************************************************************** 74 * This structure defines one hash table node. 91 * This function initialises an empty hash table (zero registered item). 92 **************************************************************************************** 93 * @ htab : pointer on hash table. 94 * @ type : item type. 75 95 ***************************************************************************************/ 76 77 typedef struct ht_node_s 78 { 79 uint32_t index; /*! integer value computed from the key */ 80 list_entry_t list; /*! member of list of all nodes in same bucket */ 81 } 82 ht_node_t; 96 void htab_init( htab_t * htab, 97 htab_item_type_t type ); 83 98 84 99 /**************************************************************************************** 85 * This function initialises one hash table node.100 * This function register a new item in the hash table. 86 101 **************************************************************************************** 87 ccccc 102 * @ htab : pointer on the hash table. 103 * @ key : pointer on the item identifier. 104 * @ list_entry : pointer on list_entry_t embedded in item to be registered. 105 * @ return 0 if success / return EINVAL if item already registered. 88 106 ***************************************************************************************/ 89 void ht_node_init( ht_node_t * node ); 107 error_t htab_insert( htab_t * htab, 108 void * key, 109 list_entry_t * list_entry ); 90 110 91 111 /**************************************************************************************** 92 * This function allocates memory for the buckets array, and initializes the header. 93 * The number of buckets (number of subsets) is PAGE_SIZE / sizeof(list_entry_t) 112 * This function remove an item from the hash table. 94 113 **************************************************************************************** 95 * @ header : pointer on the hash table header.96 * @ ht_hash : pointer on the user defined hash function.97 * @ ht_compare : pointer on the user defined compare function.98 * @ return 0 if success / return E NOMEM if error.114 * @ header : pointer on the hash table. 115 * @ key : pointer on the item identifier. 116 * @ list_entry : pointer on list_entry_t embedded in item to be removed. 117 * @ return 0 if success / return EINVAL if item not found. 99 118 ***************************************************************************************/ 100 error_t ht_header_init( ht_header_t * header, 101 ht_hash_t * ht_hash, 102 ht_compare_t * ht_compare ); 103 104 /**************************************************************************************** 105 * This function register a new node in the hash table. 106 **************************************************************************************** 107 * @ header : pointer on the hash table header. 108 * @ node : pointer on the node to be registered (embedded in item). 109 * @ key : pointer on the item identifier. 110 * @ return 0 if success / return EINVAL if node already registered... 111 ***************************************************************************************/ 112 error_t ht_insert( ht_header_t * header, 113 ht_node_t * node, 114 void * key ); 119 error_t htab_remove( htab_t * htab, 120 void * key, 121 list_entry_t * list_entry ); 115 122 116 123 /**************************************************************************************** … … 118 125 * identified by its key. 119 126 **************************************************************************************** 120 * @ h eader : pointer on the hash table header.121 * @ key 122 * @ return pointer on nodeif found / return NULL if not found.127 * @ htab : pointer on the hash table. 128 * @ key : pointer on the item identifier. 129 * @ return pointer on item if found / return NULL if not found. 123 130 ***************************************************************************************/ 124 ht_node_t * ht_find( ht_header_t * header,125 void* key);131 void * htab_lookup( htab_t * htab, 132 void * key); 126 133 127 /****************************************************************************************128 * This function remove an item from the hash table.129 ****************************************************************************************130 * @ header : pointer on the hash table header.131 * @ key : pointer on the item identifier.132 * @ return 0 if success / return EINVAL if not found.133 ***************************************************************************************/134 error_t ht_remove( ht_header_t * header,135 void * key );136 134 137 #endif /* _HTAB LE_H_ */135 #endif /* _HTAB_H_ */
Note: See TracChangeset
for help on using the changeset viewer.