annotate gcc/objc/objc-map.h @ 111:04ced10e8804

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