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