Home | History | Annotate | Download | only in policydb
      1 /* Author : Stephen Smalley, <sds (at) epoch.ncsc.mil> */
      2 
      3 /* FLASK */
      4 
      5 /*
      6  * A hash table (hashtab) maintains associations between
      7  * key values and datum values.  The type of the key values
      8  * and the type of the datum values is arbitrary.  The
      9  * functions for hash computation and key comparison are
     10  * provided by the creator of the table.
     11  */
     12 
     13 #ifndef _SEPOL_POLICYDB_HASHTAB_H_
     14 #define _SEPOL_POLICYDB_HASHTAB_H_
     15 
     16 #include <sepol/errcodes.h>
     17 
     18 #include <stdint.h>
     19 #include <stdio.h>
     20 #include <sys/cdefs.h>
     21 
     22 __BEGIN_DECLS
     23 
     24 typedef char *hashtab_key_t;	/* generic key type */
     25 typedef void *hashtab_datum_t;	/* generic datum type */
     26 
     27 typedef struct hashtab_node *hashtab_ptr_t;
     28 
     29 typedef struct hashtab_node {
     30 	hashtab_key_t key;
     31 	hashtab_datum_t datum;
     32 	hashtab_ptr_t next;
     33 } hashtab_node_t;
     34 
     35 typedef struct hashtab_val {
     36 	hashtab_ptr_t *htable;	/* hash table */
     37 	unsigned int size;	/* number of slots in hash table */
     38 	uint32_t nel;		/* number of elements in hash table */
     39 	unsigned int (*hash_value) (struct hashtab_val * h, hashtab_key_t key);	/* hash function */
     40 	int (*keycmp) (struct hashtab_val * h, hashtab_key_t key1, hashtab_key_t key2);	/* key comparison function */
     41 } hashtab_val_t;
     42 
     43 typedef hashtab_val_t *hashtab_t;
     44 
     45 /*
     46    Creates a new hash table with the specified characteristics.
     47 
     48    Returns NULL if insufficent space is available or
     49    the new hash table otherwise.
     50  */
     51 extern hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h,
     52 							    const hashtab_key_t
     53 							    key),
     54 				int (*keycmp) (hashtab_t h,
     55 					       const hashtab_key_t key1,
     56 					       const hashtab_key_t key2),
     57 				unsigned int size);
     58 /*
     59    Inserts the specified (key, datum) pair into the specified hash table.
     60 
     61    Returns SEPOL_ENOMEM if insufficient space is available or
     62    SEPOL_EEXIST  if there is already an entry with the same key or
     63    SEPOL_OK otherwise.
     64  */
     65 extern int hashtab_insert(hashtab_t h, hashtab_key_t k, hashtab_datum_t d);
     66 
     67 /*
     68    Removes the entry with the specified key from the hash table.
     69    Applies the specified destroy function to (key,datum,args) for
     70    the entry.
     71 
     72    Returns SEPOL_ENOENT if no entry has the specified key or
     73    SEPOL_OK otherwise.
     74  */
     75 extern int hashtab_remove(hashtab_t h, hashtab_key_t k,
     76 			  void (*destroy) (hashtab_key_t k,
     77 					   hashtab_datum_t d,
     78 					   void *args), void *args);
     79 
     80 /*
     81    Insert or replace the specified (key, datum) pair in the specified
     82    hash table.  If an entry for the specified key already exists,
     83    then the specified destroy function is applied to (key,datum,args)
     84    for the entry prior to replacing the entry's contents.
     85 
     86    Returns SEPOL_ENOMEM if insufficient space is available or
     87    SEPOL_OK otherwise.
     88  */
     89 extern int hashtab_replace(hashtab_t h, hashtab_key_t k, hashtab_datum_t d,
     90 			   void (*destroy) (hashtab_key_t k,
     91 					    hashtab_datum_t d,
     92 					    void *args), void *args);
     93 
     94 /*
     95    Searches for the entry with the specified key in the hash table.
     96 
     97    Returns NULL if no entry has the specified key or
     98    the datum of the entry otherwise.
     99  */
    100 extern hashtab_datum_t hashtab_search(hashtab_t h, const hashtab_key_t k);
    101 
    102 /*
    103    Destroys the specified hash table.
    104  */
    105 extern void hashtab_destroy(hashtab_t h);
    106 
    107 /*
    108    Applies the specified apply function to (key,datum,args)
    109    for each entry in the specified hash table.
    110 
    111    The order in which the function is applied to the entries
    112    is dependent upon the internal structure of the hash table.
    113 
    114    If apply returns a non-zero status, then hashtab_map will cease
    115    iterating through the hash table and will propagate the error
    116    return to its caller.
    117  */
    118 extern int hashtab_map(hashtab_t h,
    119 		       int (*apply) (hashtab_key_t k,
    120 				     hashtab_datum_t d,
    121 				     void *args), void *args);
    122 
    123 /*
    124    Same as hashtab_map, except that if apply returns a non-zero status,
    125    then the (key,datum) pair will be removed from the hashtab and the
    126    destroy function will be applied to (key,datum,args).
    127  */
    128 extern void hashtab_map_remove_on_error(hashtab_t h,
    129 					int (*apply) (hashtab_key_t k,
    130 						      hashtab_datum_t d,
    131 						      void *args),
    132 					void (*destroy) (hashtab_key_t k,
    133 							 hashtab_datum_t d,
    134 							 void *args),
    135 					void *args);
    136 
    137 extern void hashtab_hash_eval(hashtab_t h, char *tag);
    138 
    139 __END_DECLS
    140 #endif
    141