function_hooks: add add_function_param_hook()
[smatch.git] / cwchash / hashtable.h
blobb90781abd4a75e6f910cf1450c8ad159bdeac33a
1 /* Copyright (C) 2002 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */
3 #ifndef __HASHTABLE_CWC22_H__
4 #define __HASHTABLE_CWC22_H__
6 struct hashtable;
8 /* Example of use:
10 * struct hashtable *h;
11 * struct some_key *k;
12 * struct some_value *v;
14 * static unsigned int hash_from_key_fn( void *k );
15 * static int keys_equal_fn ( void *key1, void *key2 );
17 * h = create_hashtable(16, hash_from_key_fn, keys_equal_fn);
18 * k = (struct some_key *) malloc(sizeof(struct some_key));
19 * v = (struct some_value *) malloc(sizeof(struct some_value));
21 * (initialise k and v to suitable values)
23 * if (! hashtable_insert(h,k,v) )
24 * { exit(-1); }
26 * if (NULL == (found = hashtable_search(h,k) ))
27 * { printf("not found!"); }
29 * if (NULL == (found = hashtable_remove(h,k) ))
30 * { printf("Not found\n"); }
34 /* Macros may be used to define type-safe(r) hashtable access functions, with
35 * methods specialized to take known key and value types as parameters.
37 * Example:
39 * Insert this at the start of your file:
41 * DEFINE_HASHTABLE_INSERT(insert_some, struct some_key, struct some_value);
42 * DEFINE_HASHTABLE_SEARCH(search_some, struct some_key, struct some_value);
43 * DEFINE_HASHTABLE_REMOVE(remove_some, struct some_key, struct some_value);
45 * This defines the functions 'insert_some', 'search_some' and 'remove_some'.
46 * These operate just like hashtable_insert etc., with the same parameters,
47 * but their function signatures have 'struct some_key *' rather than
48 * 'void *', and hence can generate compile time errors if your program is
49 * supplying incorrect data as a key (and similarly for value).
51 * Note that the hash and key equality functions passed to create_hashtable
52 * still take 'void *' parameters instead of 'some key *'. This shouldn't be
53 * a difficult issue as they're only defined and passed once, and the other
54 * functions will ensure that only valid keys are supplied to them.
56 * The cost for this checking is increased code size and runtime overhead
57 * - if performance is important, it may be worth switching back to the
58 * unsafe methods once your program has been debugged with the safe methods.
59 * This just requires switching to some simple alternative defines - eg:
60 * #define insert_some hashtable_insert
64 /*****************************************************************************
65 * create_hashtable
67 * @name create_hashtable
68 * @param minsize minimum initial size of hashtable
69 * @param hashfunction function for hashing keys
70 * @param key_eq_fn function for determining key equality
71 * @return newly created hashtable or NULL on failure
74 struct hashtable *
75 create_hashtable(unsigned int minsize,
76 unsigned int (*hashfunction) (void*),
77 int (*key_eq_fn) (void*,void*));
79 /*****************************************************************************
80 * hashtable_insert
82 * @name hashtable_insert
83 * @param h the hashtable to insert into
84 * @param k the key - hashtable claims ownership and will free on removal
85 * @param v the value - does not claim ownership
86 * @return non-zero for successful insertion
88 * This function will cause the table to expand if the insertion would take
89 * the ratio of entries to table size over the maximum load factor.
91 * This function does not check for repeated insertions with a duplicate key.
92 * The value returned when using a duplicate key is undefined -- when
93 * the hashtable changes size, the order of retrieval of duplicate key
94 * entries is reversed.
95 * If in doubt, remove before insert.
98 int
99 hashtable_insert(struct hashtable *h, void *k, void *v);
101 #define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \
102 int fnname (struct hashtable *h, keytype *k, valuetype *v) \
104 return hashtable_insert(h,k,v); \
107 /*****************************************************************************
108 * hashtable_search
110 * @name hashtable_search
111 * @param h the hashtable to search
112 * @param k the key to search for - does not claim ownership
113 * @return the value associated with the key, or NULL if none found
116 void *
117 hashtable_search(struct hashtable *h, void *k);
119 #define DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \
120 valuetype * fnname (struct hashtable *h, keytype *k) \
122 return (valuetype *) (hashtable_search(h,k)); \
125 /*****************************************************************************
126 * hashtable_remove
128 * @name hashtable_remove
129 * @param h the hashtable to remove the item from
130 * @param k the key to search for - does not claim ownership
131 * @return the value associated with the key, or NULL if none found
134 void * /* returns value */
135 hashtable_remove(struct hashtable *h, void *k);
137 #define DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype) \
138 valuetype * fnname (struct hashtable *h, keytype *k) \
140 return (valuetype *) (hashtable_remove(h,k)); \
144 /*****************************************************************************
145 * hashtable_count
147 * @name hashtable_count
148 * @param h the hashtable
149 * @return the number of items stored in the hashtable
151 unsigned int
152 hashtable_count(struct hashtable *h);
155 /*****************************************************************************
156 * hashtable_destroy
158 * @name hashtable_destroy
159 * @param h the hashtable
160 * @param free_values whether to call 'free' on the remaining values
163 void
164 hashtable_destroy(struct hashtable *h, int free_values);
166 #endif /* __HASHTABLE_CWC22_H__ */
169 * Copyright (c) 2002, Christopher Clark
170 * All rights reserved.
172 * Redistribution and use in source and binary forms, with or without
173 * modification, are permitted provided that the following conditions
174 * are met:
176 * * Redistributions of source code must retain the above copyright
177 * notice, this list of conditions and the following disclaimer.
179 * * Redistributions in binary form must reproduce the above copyright
180 * notice, this list of conditions and the following disclaimer in the
181 * documentation and/or other materials provided with the distribution.
183 * * Neither the name of the original author; nor the names of any contributors
184 * may be used to endorse or promote products derived from this software
185 * without specific prior written permission.
188 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
189 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
190 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
192 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
193 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
194 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
195 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
196 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
197 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
198 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.