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