diff options
author | Nicola Pero <nicola.pero@meta-innovation.com> | 2011-10-14 10:19:01 +0000 |
---|---|---|
committer | Nicola Pero <nicola@gcc.gnu.org> | 2011-10-14 10:19:01 +0000 |
commit | 3cc2dd4b7eb3d60c24b9cebecb9cd3fb1ab94184 (patch) | |
tree | ef62f9d9f5ec292c126959f551815e48323b3a22 /gcc/objc/objc-map.h | |
parent | 5e678de8bada6db595df5addc0a2db6e7fc9cf32 (diff) | |
download | gcc-3cc2dd4b7eb3d60c24b9cebecb9cd3fb1ab94184.zip gcc-3cc2dd4b7eb3d60c24b9cebecb9cd3fb1ab94184.tar.gz gcc-3cc2dd4b7eb3d60c24b9cebecb9cd3fb1ab94184.tar.bz2 |
In gcc/: 2011-10-14 Nicola Pero <nicola.pero@meta-innovation.com>
In gcc/:
2011-10-14 Nicola Pero <nicola.pero@meta-innovation.com>
* gengtype.c (files_rules): Added rules for objc/objc-map.h and
objc/objc-map.c.
In gcc/objc/:
2011-10-14 Nicola Pero <nicola.pero@meta-innovation.com>
* objc-map.h: New file.
* objc-map.c: New file.
* config-lang.in (gtfiles): Added objc-map.h.
* Make-lang.in (OBJC_OBJS): Added objc-map.o.
(objc/objc-map.o): New rule.
(objc/objc-act.o): Depend on objc/objc-map.h.
* objc-next-runtime-abi-02.c: Added a TODO comment.
* objc-act.c: Include objc-map.h.
(nst_method_hash_list, cls_method_hash_list): Removed.
(instance_method_map, class_method_map): New.
(cls_name_hash_list, als_name_hash_list): Removed.
(class_name_map, alias_name_map): Removed.
(ivar_offset_hash_list): Removed.
(hash_class_name_enter, hash_class_name_lookup, hash_enter,
hash_lookup, hash_add_attr, add_method_to_hash_list): Removed.
(interface_hash_init): New.
(objc_init): Call interface_hash_init.
(objc_write_global_declarations): Iterate over class_method_map
and instance_method_map instead of cls_method_hash_list and
nst_method_hash_list.
(objc_declare_alias): Use alias_name_map instead of
cls_name_hash_list.
(objc_is_class_name): Use class_name_map and alias_name_map
instead of cls_name_hash_list and als_name_hash_list.
(interface_tuple, interface_htab, hash_interface, eq_interface):
Removed.
(interface_map): New.
(add_class): Renamed to add_interface. Use interface_map instead
of interface_htab.
(lookup_interface): Use interface_map instead of interface_htab.
(check_duplicates): Changed first argument to be a tree,
potentially a TREE_VEC, instead of a hash. Changed implementation
to match.
(lookup_method_in_hash_lists): Use class_method_map and
instance_method_map instead of cls_method_hash_list and
nst_method_hash_list.
(objc_build_selector_expr): Likewise.
(hash_func): Removed.
(hash_init): Create instance_method_map, class_method_map,
class_name_map, and alias_name_map. Do not create
nst_method_hash_list, cls_method_hash_list, cls_name_hash_list,
als_name_hash_list, and ivar_offset_hash_list.
(insert_method_into_method_map): New.
(objc_add_method): Use insert_method_into_method_map instead of
add_method_to_hash_list.
(start_class): Call add_interface instead of add_class.
* objc-act.h (cls_name_hash_list, als_name_hash_list,
nst_method_hash_list, cls_method_hash_list): Removed.
In gcc/objcp/:
2011-10-14 Nicola Pero <nicola.pero@meta-innovation.com>
* Make-lang.in (OBJCXX_OBJS): Added objc-map.o.
(objcp/objc-map.o): New rule.
(objcp/objcp-act.o): Depend on objc/objc-map.h.
* config-lang.in (gtfiles): Added objc-map.h.
From-SVN: r179965
Diffstat (limited to 'gcc/objc/objc-map.h')
-rw-r--r-- | gcc/objc/objc-map.h | 309 |
1 files changed, 309 insertions, 0 deletions
diff --git a/gcc/objc/objc-map.h b/gcc/objc/objc-map.h new file mode 100644 index 0000000..c4565de --- /dev/null +++ b/gcc/objc/objc-map.h @@ -0,0 +1,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 */ |