• 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    Searches for the entry with the specified key in the hash table.
84 
85    Returns NULL if no entry has the specified key or
86    the datum of the entry otherwise.
87  */
88 extern hashtab_datum_t hashtab_search(hashtab_t h, const_hashtab_key_t k);
89 
90 /*
91    Destroys the specified hash table.
92  */
93 extern void hashtab_destroy(hashtab_t h);
94 
95 /*
96    Applies the specified apply function to (key,datum,args)
97    for each entry in the specified hash table.
98 
99    The order in which the function is applied to the entries
100    is dependent upon the internal structure of the hash table.
101 
102    If apply returns a non-zero status, then hashtab_map will cease
103    iterating through the hash table and will propagate the error
104    return to its caller.
105  */
106 extern int hashtab_map(hashtab_t h,
107 		       int (*apply) (hashtab_key_t k,
108 				     hashtab_datum_t d,
109 				     void *args), void *args);
110 
111 extern void hashtab_hash_eval(hashtab_t h, char *tag);
112 
113 #ifdef __cplusplus
114 }
115 #endif
116 
117 #endif
118