• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Author : Stephen Smalley, <sds@tycho.nsa.gov> */
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 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 typedef char *hashtab_key_t;	/* generic key type */
26 typedef const char *const_hashtab_key_t;	/* constant generic key type */
27 typedef void *hashtab_datum_t;	/* generic datum type */
28 
29 typedef struct hashtab_node *hashtab_ptr_t;
30 
31 typedef struct hashtab_node {
32 	hashtab_key_t key;
33 	hashtab_datum_t datum;
34 	hashtab_ptr_t next;
35 } hashtab_node_t;
36 
37 typedef struct hashtab_val {
38 	hashtab_ptr_t *htable;	/* hash table */
39 	unsigned int size;	/* number of slots in hash table */
40 	uint32_t nel;		/* number of elements in hash table */
41 	unsigned int (*hash_value) (struct hashtab_val * h, const_hashtab_key_t key);	/* hash function */
42 	int (*keycmp) (struct hashtab_val * h, const_hashtab_key_t key1, const_hashtab_key_t key2);	/* key comparison function */
43 } hashtab_val_t;
44 
45 typedef hashtab_val_t *hashtab_t;
46 
47 /*
48    Creates a new hash table with the specified characteristics.
49 
50    Returns NULL if insufficient space is available or
51    the new hash table otherwise.
52  */
53 extern hashtab_t hashtab_create(unsigned int (*hash_value) (hashtab_t h,
54 							    const_hashtab_key_t
55 							    key),
56 				int (*keycmp) (hashtab_t h,
57 					       const_hashtab_key_t key1,
58 					       const_hashtab_key_t key2),
59 				unsigned int size);
60 /*
61    Inserts the specified (key, datum) pair into the specified hash table.
62 
63    Returns SEPOL_ENOMEM if insufficient space is available or
64    SEPOL_EEXIST  if there is already an entry with the same key or
65    SEPOL_OK otherwise.
66  */
67 extern int hashtab_insert(hashtab_t h, hashtab_key_t k, hashtab_datum_t d);
68 
69 /*
70    Removes the entry with the specified key from the hash table.
71    Applies the specified destroy function to (key,datum,args) for
72    the entry.
73 
74    Returns SEPOL_ENOENT if no entry has the specified key or
75    SEPOL_OK otherwise.
76  */
77 extern int hashtab_remove(hashtab_t h, hashtab_key_t k,
78 			  void (*destroy) (hashtab_key_t k,
79 					   hashtab_datum_t d,
80 					   void *args), void *args);
81 
82 /*
83    Insert or replace the specified (key, datum) pair in the specified
84    hash table.  If an entry for the specified key already exists,
85    then the specified destroy function is applied to (key,datum,args)
86    for the entry prior to replacing the entry's contents.
87 
88    Returns SEPOL_ENOMEM if insufficient space is available or
89    SEPOL_OK otherwise.
90  */
91 extern int hashtab_replace(hashtab_t h, hashtab_key_t k, hashtab_datum_t d,
92 			   void (*destroy) (hashtab_key_t k,
93 					    hashtab_datum_t d,
94 					    void *args), void *args);
95 
96 /*
97    Searches for the entry with the specified key in the hash table.
98 
99    Returns NULL if no entry has the specified key or
100    the datum of the entry otherwise.
101  */
102 extern hashtab_datum_t hashtab_search(hashtab_t h, const_hashtab_key_t k);
103 
104 /*
105    Destroys the specified hash table.
106  */
107 extern void hashtab_destroy(hashtab_t h);
108 
109 /*
110    Applies the specified apply function to (key,datum,args)
111    for each entry in the specified hash table.
112 
113    The order in which the function is applied to the entries
114    is dependent upon the internal structure of the hash table.
115 
116    If apply returns a non-zero status, then hashtab_map will cease
117    iterating through the hash table and will propagate the error
118    return to its caller.
119  */
120 extern int hashtab_map(hashtab_t h,
121 		       int (*apply) (hashtab_key_t k,
122 				     hashtab_datum_t d,
123 				     void *args), void *args);
124 
125 /*
126    Same as hashtab_map, except that if apply returns a non-zero status,
127    then the (key,datum) pair will be removed from the hashtab and the
128    destroy function will be applied to (key,datum,args).
129  */
130 extern void hashtab_map_remove_on_error(hashtab_t h,
131 					int (*apply) (hashtab_key_t k,
132 						      hashtab_datum_t d,
133 						      void *args),
134 					void (*destroy) (hashtab_key_t k,
135 							 hashtab_datum_t d,
136 							 void *args),
137 					void *args);
138 
139 extern void hashtab_hash_eval(hashtab_t h, char *tag);
140 
141 #ifdef __cplusplus
142 }
143 #endif
144 
145 #endif
146