summaryrefslogtreecommitdiff
path: root/gcc/objc/objc-map.h
blob: c4565de186e267f27eafd0d445e2cba4a3b5223f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* objc-map.h -- Implementation of map data structures for ObjC compiler
   Copyright 2011 Free Software Foundation, Inc.
   Written by Nicola Pero <nicola.pero@meta-innovation.com>

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser Public License for more details.

You should have received a copy of the GNU Lesser Public License
along with this program; if not, write to the Free Software
Foundation, 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA.  */

#ifndef OBJC_MAP_H
#define OBJC_MAP_H

/* A map is a data structure that maps a key to a value.  In this file
   we currently have maps that can map a GCC identifier (a tree) to
   some other GCC tree.  This is what the ObjC frontend mostly needs:
   being able to look up an identifier into an ObjC data structure.  A
   typical usage is mapping ObjC class names (as identifiers) to a
   tree representing the class.

   This implementation is fast.  :-) */

/**
 ** Private definitions.
 **/

/* We include private declaration and definitions that are required to
   provide the implementation of inline functions.  You should ignore
   these definitions (and the implementation of the inline functions)
   as they are not part of the public API and may change.  */
typedef unsigned int objc_map_private_hash_t;

/* This is used as sentinel.  */
#define OBJC_MAP_PRIVATE_EMPTY_SLOT (tree)0

struct GTY(()) objc_map_private {
  /* Total number of slots.  This is the maximum number of elements
     that can be currently stored in the map before resizing.  This is
     the number of slots in the C array.  Important: this is
     guaranteed to be a power of 2.  When we create (or resize) the
     map, we round up the size to the next power of 2.  This allows us
     to convert a hash to a position in the hashtable by simply doing
     "position = hash & mask", where mask is number_of_slots - 1
     instead of using a modulo (which requires a division).  */
  size_t number_of_slots;

  /* This is number_of_slots - 1, precomputed.  */
  size_t mask;

  /* Number of slots that are not empty (ie, that are active).  We
     keep counts using this variable which can easily be checked
     against max_number_of_non_empty_slots.  */
  size_t number_of_non_empty_slots;

  /* This is the load factor limit.  When the number of non empty
     slots equals this number, we need to resize the array.  This is
     calculated once, when the slots are resized, and then kept cached
     so it can be compared quickly when elements are added.  */
  size_t max_number_of_non_empty_slots;

  /* The maximum load factor.  */
  int maximum_load_factor;

  /* These are the keys.  */
  tree * GTY ((length ("%h.number_of_slots"))) slots;

  /* These are the values.  values[i] is the the value corresponding
     to slots[i].  */
  tree * GTY ((length ("%h.number_of_slots"))) values;
};

/* Private functions used to resize the map.  They may be called by
   the inline functions when adding elements.  */
extern void
objc_map_private_grow (struct objc_map_private *map);


/**
 ** The definition of a map.
 **/
typedef struct objc_map_private *objc_map_t;


/**
 ** Creating a map.
 **/

/* objc_map_alloc_ggc() creates a new map which is under GGC.  The initial
   capacity must be specified as an argument; this is used to size the map
   when it is created.  */
objc_map_t objc_map_alloc_ggc (size_t initial_capacity);

/**
 ** Performance tuning.
 **/

/* Set a maximum load factor for the data structure.  This is the main
   tuning parameter to improve performance (at the expense of
   memory).  */
void objc_map_set_maximum_load_factor (objc_map_t map, int number_between_zero_and_one_hundred);

/* Read the maximum load factor.  */
int objc_map_maximum_load_factor (objc_map_t map);


/**
 ** Getting the value corresponding to a key.
 **/

/* This is the value returned by objc_map_get() when the value
   corresponding to a key is not found.  */
#define OBJC_MAP_NOT_FOUND (tree)1

/* objc_map_get() returns the value associated with a certain key,
   or OBJC_MAP_NOT_FOUND if there is no value associated with that key.
   Note that you can also use it to simply check if the map contains a
   pair with a certain key; just compare the result of calling
   objc_map_get() to OBJC_MAP_NOT_FOUND.

   It is essential to always check the results of the call to make
   sure it is not OBJC_MAP_NOT_FOUND.

   NULL is a valid value, so a key can be inserted into a map with
   value NULL, and objc_map_get() will return NULL in that case.
   So a result of NULL means that they key *was* found, and the value
   associated with it was NULL.  */
static inline tree
objc_map_get (objc_map_t map, /* struct tree_identifier * */tree key)
{
  /* The inline implementation is private and may change without notice.  */
  objc_map_private_hash_t hash = IDENTIFIER_HASH_VALUE (key);
  size_t i = hash & map->mask;
  size_t j = 1;

  if (map->slots[i] == OBJC_MAP_PRIVATE_EMPTY_SLOT)
    return OBJC_MAP_NOT_FOUND;

  if (map->slots[i] == key)
    return map->values[i];
  
  while (1)
    {
      i = (i + j) & map->mask;
      
      if (map->slots[i] == OBJC_MAP_PRIVATE_EMPTY_SLOT)
	return OBJC_MAP_NOT_FOUND;
      
      if (map->slots[i] == key)
	return map->values[i];

      j++;
    }
}

/* objc_map_put() puts a key/value pair into the map.  If the map does
   not contain the key, it is added to it with the specified value.
   If the map already contains the key, the previous value is replaced
   with the new one.

   You can use any identifier as key, with the exception of NULL.

   You can use any tree as value, including NULL.  */
static inline
void objc_map_put (objc_map_t map, /*struct tree_identifier * */tree key, tree value)
{
  /* The inline implementation is private and may change without notice.  */
  objc_map_private_hash_t hash = IDENTIFIER_HASH_VALUE (key);
  size_t i, j = 0;

  if (map->number_of_non_empty_slots == map->max_number_of_non_empty_slots)
    objc_map_private_grow (map);

  i = hash & map->mask;
    
  while (1)
    {
      if (map->slots[i] == OBJC_MAP_PRIVATE_EMPTY_SLOT)
	{
	  map->number_of_non_empty_slots++;
	  map->slots[i] = key;
	  map->values[i] = value;
	  return;
	}
      if (map->slots[i] == key)
	{
	  map->values[i] = value;
	  return;
	}

      j++;
      i = (i + j) & map->mask;
    }
}

/**
 ** Iterating over a map using an iterator.
 **/

/* When using iterators you can iterate directly on the elements in
   the map, and take an action over each one.

   Here is how you iterate over a hmap_pointer using iterators:

   objc_map_iterator_t i;

   objc_map_iterator_initialize (map, &i);

   while (objc_map_iterator_move_to_next (map, &i))
     {
       tree p = objc_map_iterator_current_key (map, i);
       tree q = objc_map_iterator_current_value (map, i);

       ... do something with p and q ...
     }

   You'll notice that the functions that modify the iterator (to
   initialize it, or move it to the next element) take a pointer to it
   as argument (as in "&i"), while the functions that only read its
   state (to read the current key/value, or remove the current
   key/value from the map) take it as a direct argument (as in "i").

   Note that all the objc_map_iterator_*() functions are inline and if
   you follow the pattern above, the compiler should be able to inline
   everything into a very efficient loop, roughly equivalent to
   hand-writing a C loop that iterates directly onto the hmap_pointer
   internal data structures.  */

/* A objc_map_iterator_t variable encapsulates the state of an
   iteration.  The fact that this is actually a size_t (pointing to
   the index of the slot that we return next) is an internal, private
   detail of the implementation and may change without notice.  */
typedef size_t objc_map_iterator_t;

/* Initialize an iterator to iterate over the specified objc_map.  You
   must use this before starting the iteration, to get a working
   iterator.  */
static inline
void
objc_map_iterator_initialize (objc_map_t map ATTRIBUTE_UNUSED, objc_map_iterator_t *i)
{
  /* The inline implementation is private and may change without notice.  */
  /* This is trivial, but the same API would work to initialize more
     complicated iterators.  */
  *i = 0;
}

#define OBJC_MAP_FAILURE 0
#define OBJC_MAP_SUCCESS 1

/* Move the iterator to the next key/value pair, and return
   OBJC_MAP_SUCCESS if there is such a key/value pair, and
   OBJC_MAP_FAILURE if there are no more ones.  The iterator must have
   been initialized using objc_map_iterator_initialize().  Note that
   because this function is modifying the iterator, you need to pass a
   pointer to it.  */
static inline
int
objc_map_iterator_move_to_next (objc_map_t map, objc_map_iterator_t *i)
{
  /* The inline implementation is private and may change without notice.  */
  while (1)
    {
      void *slot;
      if (*i == map->number_of_slots)
	return OBJC_MAP_FAILURE;
      
      slot = map->slots[*i];
      *i = *i + 1;
      if (slot != OBJC_MAP_PRIVATE_EMPTY_SLOT)
	return OBJC_MAP_SUCCESS;
    }
}

/* Return the current key.  You can only call it after you have called
   objc_map_iterator_move_to_next() at least once (to move to the
   first element), and only if the last call returned
   OBJC_MAP_SUCCESS.  The behaviour is otherwise undefined, probably a
   segmentation fault.  */
static inline
tree
objc_map_iterator_current_key (objc_map_t map, objc_map_iterator_t i)
{
  /* The inline implementation is private and may change without notice.  */
  return map->slots[i - 1];
}

/* Return the current value.  You can only call it after you have
   called objc_map_iterator_move_to_next() at least once (to move to
   the first element), and only if the last call returned
   OBJC_MAP_SUCCESS.  The behaviour is otherwise undefined, probably a
   segmentation fault.  */
static inline
tree
objc_map_iterator_current_value (objc_map_t map, objc_map_iterator_t i)
{
  /* The inline implementation is private and may change without notice.  */
  return map->values[i - 1];
}

#endif /* OBJC_MAP_H */