ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / security / selinux / ss / hashtab.h
1 /*
2  * A hash table (hashtab) maintains associations between
3  * key values and datum values.  The type of the key values
4  * and the type of the datum values is arbitrary.  The
5  * functions for hash computation and key comparison are
6  * provided by the creator of the table.
7  *
8  * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
9  */
10 #ifndef _SS_HASHTAB_H_
11 #define _SS_HASHTAB_H_
12
13 #define HASHTAB_MAX_NODES       0xffffffff
14
15 struct hashtab_node {
16         void *key;
17         void *datum;
18         struct hashtab_node *next;
19 };
20
21 struct hashtab {
22         struct hashtab_node **htable;   /* hash table */
23         u32 size;                       /* number of slots in hash table */
24         u32 nel;                        /* number of elements in hash table */
25         u32 (*hash_value)(struct hashtab *h, void *key);
26                                         /* hash function */
27         int (*keycmp)(struct hashtab *h, void *key1, void *key2);
28                                         /* key comparison function */
29 };
30
31 struct hashtab_info {
32         u32 slots_used;
33         u32 max_chain_len;
34 };
35
36 /*
37  * Creates a new hash table with the specified characteristics.
38  *
39  * Returns NULL if insufficent space is available or
40  * the new hash table otherwise.
41  */
42 struct hashtab *hashtab_create(u32 (*hash_value)(struct hashtab *h, void *key),
43                                int (*keycmp)(struct hashtab *h, void *key1, void *key2),
44                                u32 size);
45
46 /*
47  * Inserts the specified (key, datum) pair into the specified hash table.
48  *
49  * Returns -ENOMEM on memory allocation error,
50  * -EEXIST if there is already an entry with the same key,
51  * -EINVAL for general errors or
52  * 0 otherwise.
53  */
54 int hashtab_insert(struct hashtab *h, void *k, void *d);
55
56 /*
57  * Removes the entry with the specified key from the hash table.
58  * Applies the specified destroy function to (key,datum,args) for
59  * the entry.
60  *
61  * Returns -ENOENT if no entry has the specified key,
62  * -EINVAL for general errors or
63  *0 otherwise.
64  */
65 int hashtab_remove(struct hashtab *h, void *k,
66                    void (*destroy)(void *k, void *d, void *args),
67                    void *args);
68
69 /*
70  * Insert or replace the specified (key, datum) pair in the specified
71  * hash table.  If an entry for the specified key already exists,
72  * then the specified destroy function is applied to (key,datum,args)
73  * for the entry prior to replacing the entry's contents.
74  *
75  * Returns -ENOMEM if insufficient space is available,
76  * -EINVAL for general errors or
77  * 0 otherwise.
78  */
79 int hashtab_replace(struct hashtab *h, void *k, void *d,
80                     void (*destroy)(void *k, void *d, void *args),
81                     void *args);
82
83 /*
84  * Searches for the entry with the specified key in the hash table.
85  *
86  * Returns NULL if no entry has the specified key or
87  * the datum of the entry otherwise.
88  */
89 void *hashtab_search(struct hashtab *h, void *k);
90
91 /*
92  * Destroys the specified hash table.
93  */
94 void hashtab_destroy(struct hashtab *h);
95
96 /*
97  * Applies the specified apply function to (key,datum,args)
98  * for each entry in the specified hash table.
99  *
100  * The order in which the function is applied to the entries
101  * is dependent upon the internal structure of the hash table.
102  *
103  * If apply returns a non-zero status, then hashtab_map will cease
104  * iterating through the hash table and will propagate the error
105  * return to its caller.
106  */
107 int hashtab_map(struct hashtab *h,
108                 int (*apply)(void *k, void *d, void *args),
109                 void *args);
110
111 /*
112  * Same as hashtab_map, except that if apply returns a non-zero status,
113  * then the (key,datum) pair will be removed from the hashtab and the
114  * destroy function will be applied to (key,datum,args).
115  */
116 void hashtab_map_remove_on_error(struct hashtab *h,
117                                  int (*apply)(void *k, void *d, void *args),
118                                  void (*destroy)(void *k, void *d, void *args),
119                                  void *args);
120
121
122 /* Fill info with some hash table statistics */
123 void hashtab_stat(struct hashtab *h, struct hashtab_info *info);
124
125 #endif  /* _SS_HASHTAB_H */