objc-map.h revision 1.10 1 1.1 mrg /* objc-map.h -- Implementation of map data structures for ObjC compiler
2 1.10 mrg Copyright (C) 2011-2022 Free Software Foundation, Inc.
3 1.1 mrg Written by Nicola Pero <nicola.pero (at) meta-innovation.com>
4 1.1 mrg
5 1.1 mrg This program is free software; you can redistribute it and/or modify it
6 1.1 mrg under the terms of the GNU Lesser Public License as published by the
7 1.1 mrg Free Software Foundation; either version 3, or (at your option) any
8 1.1 mrg later version.
9 1.1 mrg
10 1.1 mrg This program is distributed in the hope that it will be useful,
11 1.1 mrg but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 1.1 mrg GNU Lesser Public License for more details.
14 1.1 mrg
15 1.1 mrg You should have received a copy of the GNU Lesser Public License
16 1.1 mrg along with this program; if not, write to the Free Software
17 1.1 mrg Foundation, 51 Franklin Street - Fifth Floor,
18 1.1 mrg Boston, MA 02110-1301, USA. */
19 1.1 mrg
20 1.1 mrg #ifndef OBJC_MAP_H
21 1.1 mrg #define OBJC_MAP_H
22 1.1 mrg
23 1.1 mrg /* A map is a data structure that maps a key to a value. In this file
24 1.1 mrg we currently have maps that can map a GCC identifier (a tree) to
25 1.1 mrg some other GCC tree. This is what the ObjC frontend mostly needs:
26 1.1 mrg being able to look up an identifier into an ObjC data structure. A
27 1.1 mrg typical usage is mapping ObjC class names (as identifiers) to a
28 1.1 mrg tree representing the class.
29 1.1 mrg
30 1.1 mrg This implementation is fast. :-) */
31 1.1 mrg
32 1.1 mrg /**
33 1.1 mrg ** Private definitions.
34 1.1 mrg **/
35 1.1 mrg
36 1.1 mrg /* We include private declaration and definitions that are required to
37 1.1 mrg provide the implementation of inline functions. You should ignore
38 1.1 mrg these definitions (and the implementation of the inline functions)
39 1.1 mrg as they are not part of the public API and may change. */
40 1.1 mrg typedef unsigned int objc_map_private_hash_t;
41 1.1 mrg
42 1.1 mrg /* This is used as sentinel. */
43 1.1 mrg #define OBJC_MAP_PRIVATE_EMPTY_SLOT (tree)0
44 1.1 mrg
45 1.1 mrg struct GTY(()) objc_map_private {
46 1.1 mrg /* Total number of slots. This is the maximum number of elements
47 1.1 mrg that can be currently stored in the map before resizing. This is
48 1.1 mrg the number of slots in the C array. Important: this is
49 1.1 mrg guaranteed to be a power of 2. When we create (or resize) the
50 1.1 mrg map, we round up the size to the next power of 2. This allows us
51 1.1 mrg to convert a hash to a position in the hashtable by simply doing
52 1.1 mrg "position = hash & mask", where mask is number_of_slots - 1
53 1.1 mrg instead of using a modulo (which requires a division). */
54 1.1 mrg size_t number_of_slots;
55 1.1 mrg
56 1.1 mrg /* This is number_of_slots - 1, precomputed. */
57 1.1 mrg size_t mask;
58 1.1 mrg
59 1.1 mrg /* Number of slots that are not empty (ie, that are active). We
60 1.1 mrg keep counts using this variable which can easily be checked
61 1.1 mrg against max_number_of_non_empty_slots. */
62 1.1 mrg size_t number_of_non_empty_slots;
63 1.1 mrg
64 1.1 mrg /* This is the load factor limit. When the number of non empty
65 1.1 mrg slots equals this number, we need to resize the array. This is
66 1.1 mrg calculated once, when the slots are resized, and then kept cached
67 1.1 mrg so it can be compared quickly when elements are added. */
68 1.1 mrg size_t max_number_of_non_empty_slots;
69 1.1 mrg
70 1.1 mrg /* The maximum load factor. */
71 1.1 mrg int maximum_load_factor;
72 1.1 mrg
73 1.1 mrg /* These are the keys. */
74 1.1 mrg tree * GTY ((length ("%h.number_of_slots"))) slots;
75 1.1 mrg
76 1.4 mrg /* These are the values. values[i] is the value corresponding
77 1.1 mrg to slots[i]. */
78 1.1 mrg tree * GTY ((length ("%h.number_of_slots"))) values;
79 1.1 mrg };
80 1.1 mrg
81 1.1 mrg /* Private functions used to resize the map. They may be called by
82 1.1 mrg the inline functions when adding elements. */
83 1.1 mrg extern void
84 1.1 mrg objc_map_private_grow (struct objc_map_private *map);
85 1.1 mrg
86 1.1 mrg
87 1.1 mrg /**
88 1.1 mrg ** The definition of a map.
89 1.1 mrg **/
90 1.1 mrg typedef struct objc_map_private *objc_map_t;
91 1.1 mrg
92 1.1 mrg
93 1.1 mrg /**
94 1.1 mrg ** Creating a map.
95 1.1 mrg **/
96 1.1 mrg
97 1.1 mrg /* objc_map_alloc_ggc() creates a new map which is under GGC. The initial
98 1.1 mrg capacity must be specified as an argument; this is used to size the map
99 1.1 mrg when it is created. */
100 1.1 mrg objc_map_t objc_map_alloc_ggc (size_t initial_capacity);
101 1.1 mrg
102 1.1 mrg /**
103 1.1 mrg ** Performance tuning.
104 1.1 mrg **/
105 1.1 mrg
106 1.1 mrg /* Set a maximum load factor for the data structure. This is the main
107 1.1 mrg tuning parameter to improve performance (at the expense of
108 1.1 mrg memory). */
109 1.1 mrg void objc_map_set_maximum_load_factor (objc_map_t map, int number_between_zero_and_one_hundred);
110 1.1 mrg
111 1.1 mrg /* Read the maximum load factor. */
112 1.1 mrg int objc_map_maximum_load_factor (objc_map_t map);
113 1.1 mrg
114 1.1 mrg
115 1.1 mrg /**
116 1.1 mrg ** Getting the value corresponding to a key.
117 1.1 mrg **/
118 1.1 mrg
119 1.1 mrg /* This is the value returned by objc_map_get() when the value
120 1.1 mrg corresponding to a key is not found. */
121 1.1 mrg #define OBJC_MAP_NOT_FOUND (tree)1
122 1.1 mrg
123 1.1 mrg /* objc_map_get() returns the value associated with a certain key,
124 1.1 mrg or OBJC_MAP_NOT_FOUND if there is no value associated with that key.
125 1.1 mrg Note that you can also use it to simply check if the map contains a
126 1.1 mrg pair with a certain key; just compare the result of calling
127 1.1 mrg objc_map_get() to OBJC_MAP_NOT_FOUND.
128 1.1 mrg
129 1.1 mrg It is essential to always check the results of the call to make
130 1.1 mrg sure it is not OBJC_MAP_NOT_FOUND.
131 1.1 mrg
132 1.1 mrg NULL is a valid value, so a key can be inserted into a map with
133 1.1 mrg value NULL, and objc_map_get() will return NULL in that case.
134 1.1 mrg So a result of NULL means that they key *was* found, and the value
135 1.1 mrg associated with it was NULL. */
136 1.1 mrg static inline tree
137 1.1 mrg objc_map_get (objc_map_t map, /* struct tree_identifier * */tree key)
138 1.1 mrg {
139 1.1 mrg /* The inline implementation is private and may change without notice. */
140 1.1 mrg objc_map_private_hash_t hash = IDENTIFIER_HASH_VALUE (key);
141 1.1 mrg size_t i = hash & map->mask;
142 1.1 mrg size_t j = 1;
143 1.1 mrg
144 1.1 mrg if (map->slots[i] == OBJC_MAP_PRIVATE_EMPTY_SLOT)
145 1.1 mrg return OBJC_MAP_NOT_FOUND;
146 1.1 mrg
147 1.1 mrg if (map->slots[i] == key)
148 1.1 mrg return map->values[i];
149 1.1 mrg
150 1.1 mrg while (1)
151 1.1 mrg {
152 1.1 mrg i = (i + j) & map->mask;
153 1.1 mrg
154 1.1 mrg if (map->slots[i] == OBJC_MAP_PRIVATE_EMPTY_SLOT)
155 1.1 mrg return OBJC_MAP_NOT_FOUND;
156 1.1 mrg
157 1.1 mrg if (map->slots[i] == key)
158 1.1 mrg return map->values[i];
159 1.1 mrg
160 1.1 mrg j++;
161 1.1 mrg }
162 1.1 mrg }
163 1.1 mrg
164 1.1 mrg /* objc_map_put() puts a key/value pair into the map. If the map does
165 1.1 mrg not contain the key, it is added to it with the specified value.
166 1.1 mrg If the map already contains the key, the previous value is replaced
167 1.1 mrg with the new one.
168 1.1 mrg
169 1.1 mrg You can use any identifier as key, with the exception of NULL.
170 1.1 mrg
171 1.1 mrg You can use any tree as value, including NULL. */
172 1.1 mrg static inline
173 1.1 mrg void objc_map_put (objc_map_t map, /*struct tree_identifier * */tree key, tree value)
174 1.1 mrg {
175 1.1 mrg /* The inline implementation is private and may change without notice. */
176 1.1 mrg objc_map_private_hash_t hash = IDENTIFIER_HASH_VALUE (key);
177 1.1 mrg size_t i, j = 0;
178 1.1 mrg
179 1.1 mrg if (map->number_of_non_empty_slots == map->max_number_of_non_empty_slots)
180 1.1 mrg objc_map_private_grow (map);
181 1.1 mrg
182 1.1 mrg i = hash & map->mask;
183 1.1 mrg
184 1.1 mrg while (1)
185 1.1 mrg {
186 1.1 mrg if (map->slots[i] == OBJC_MAP_PRIVATE_EMPTY_SLOT)
187 1.1 mrg {
188 1.1 mrg map->number_of_non_empty_slots++;
189 1.1 mrg map->slots[i] = key;
190 1.1 mrg map->values[i] = value;
191 1.1 mrg return;
192 1.1 mrg }
193 1.1 mrg if (map->slots[i] == key)
194 1.1 mrg {
195 1.1 mrg map->values[i] = value;
196 1.1 mrg return;
197 1.1 mrg }
198 1.1 mrg
199 1.1 mrg j++;
200 1.1 mrg i = (i + j) & map->mask;
201 1.1 mrg }
202 1.1 mrg }
203 1.1 mrg
204 1.1 mrg /**
205 1.1 mrg ** Iterating over a map using an iterator.
206 1.1 mrg **/
207 1.1 mrg
208 1.1 mrg /* When using iterators you can iterate directly on the elements in
209 1.1 mrg the map, and take an action over each one.
210 1.1 mrg
211 1.1 mrg Here is how you iterate over a hmap_pointer using iterators:
212 1.1 mrg
213 1.1 mrg objc_map_iterator_t i;
214 1.1 mrg
215 1.1 mrg objc_map_iterator_initialize (map, &i);
216 1.1 mrg
217 1.1 mrg while (objc_map_iterator_move_to_next (map, &i))
218 1.1 mrg {
219 1.1 mrg tree p = objc_map_iterator_current_key (map, i);
220 1.1 mrg tree q = objc_map_iterator_current_value (map, i);
221 1.1 mrg
222 1.1 mrg ... do something with p and q ...
223 1.1 mrg }
224 1.1 mrg
225 1.1 mrg You'll notice that the functions that modify the iterator (to
226 1.1 mrg initialize it, or move it to the next element) take a pointer to it
227 1.1 mrg as argument (as in "&i"), while the functions that only read its
228 1.1 mrg state (to read the current key/value, or remove the current
229 1.1 mrg key/value from the map) take it as a direct argument (as in "i").
230 1.1 mrg
231 1.1 mrg Note that all the objc_map_iterator_*() functions are inline and if
232 1.1 mrg you follow the pattern above, the compiler should be able to inline
233 1.1 mrg everything into a very efficient loop, roughly equivalent to
234 1.1 mrg hand-writing a C loop that iterates directly onto the hmap_pointer
235 1.1 mrg internal data structures. */
236 1.1 mrg
237 1.1 mrg /* A objc_map_iterator_t variable encapsulates the state of an
238 1.1 mrg iteration. The fact that this is actually a size_t (pointing to
239 1.1 mrg the index of the slot that we return next) is an internal, private
240 1.1 mrg detail of the implementation and may change without notice. */
241 1.1 mrg typedef size_t objc_map_iterator_t;
242 1.1 mrg
243 1.1 mrg /* Initialize an iterator to iterate over the specified objc_map. You
244 1.1 mrg must use this before starting the iteration, to get a working
245 1.1 mrg iterator. */
246 1.1 mrg static inline
247 1.1 mrg void
248 1.1 mrg objc_map_iterator_initialize (objc_map_t map ATTRIBUTE_UNUSED, objc_map_iterator_t *i)
249 1.1 mrg {
250 1.1 mrg /* The inline implementation is private and may change without notice. */
251 1.1 mrg /* This is trivial, but the same API would work to initialize more
252 1.1 mrg complicated iterators. */
253 1.1 mrg *i = 0;
254 1.1 mrg }
255 1.1 mrg
256 1.1 mrg #define OBJC_MAP_FAILURE 0
257 1.1 mrg #define OBJC_MAP_SUCCESS 1
258 1.1 mrg
259 1.1 mrg /* Move the iterator to the next key/value pair, and return
260 1.1 mrg OBJC_MAP_SUCCESS if there is such a key/value pair, and
261 1.1 mrg OBJC_MAP_FAILURE if there are no more ones. The iterator must have
262 1.1 mrg been initialized using objc_map_iterator_initialize(). Note that
263 1.1 mrg because this function is modifying the iterator, you need to pass a
264 1.1 mrg pointer to it. */
265 1.1 mrg static inline
266 1.1 mrg int
267 1.1 mrg objc_map_iterator_move_to_next (objc_map_t map, objc_map_iterator_t *i)
268 1.1 mrg {
269 1.1 mrg /* The inline implementation is private and may change without notice. */
270 1.1 mrg while (1)
271 1.1 mrg {
272 1.1 mrg void *slot;
273 1.1 mrg if (*i == map->number_of_slots)
274 1.1 mrg return OBJC_MAP_FAILURE;
275 1.1 mrg
276 1.1 mrg slot = map->slots[*i];
277 1.1 mrg *i = *i + 1;
278 1.1 mrg if (slot != OBJC_MAP_PRIVATE_EMPTY_SLOT)
279 1.1 mrg return OBJC_MAP_SUCCESS;
280 1.1 mrg }
281 1.1 mrg }
282 1.1 mrg
283 1.1 mrg /* Return the current key. You can only call it after you have called
284 1.1 mrg objc_map_iterator_move_to_next() at least once (to move to the
285 1.1 mrg first element), and only if the last call returned
286 1.4 mrg OBJC_MAP_SUCCESS. The behavior is otherwise undefined, probably a
287 1.1 mrg segmentation fault. */
288 1.1 mrg static inline
289 1.1 mrg tree
290 1.1 mrg objc_map_iterator_current_key (objc_map_t map, objc_map_iterator_t i)
291 1.1 mrg {
292 1.1 mrg /* The inline implementation is private and may change without notice. */
293 1.1 mrg return map->slots[i - 1];
294 1.1 mrg }
295 1.1 mrg
296 1.1 mrg /* Return the current value. You can only call it after you have
297 1.1 mrg called objc_map_iterator_move_to_next() at least once (to move to
298 1.1 mrg the first element), and only if the last call returned
299 1.4 mrg OBJC_MAP_SUCCESS. The behavior is otherwise undefined, probably a
300 1.1 mrg segmentation fault. */
301 1.1 mrg static inline
302 1.1 mrg tree
303 1.1 mrg objc_map_iterator_current_value (objc_map_t map, objc_map_iterator_t i)
304 1.1 mrg {
305 1.1 mrg /* The inline implementation is private and may change without notice. */
306 1.1 mrg return map->values[i - 1];
307 1.1 mrg }
308 1.1 mrg
309 1.1 mrg #endif /* OBJC_MAP_H */
310