diff options
author | Jason Merrill <jason@redhat.com> | 2019-10-02 15:26:47 -0400 |
---|---|---|
committer | Jason Merrill <jason@gcc.gnu.org> | 2019-10-02 15:26:47 -0400 |
commit | c89844e5d30a5235960a2c627abc9369306fda61 (patch) | |
tree | 49f593d8b44f2a0038aa8a77148c902c63381b29 /gcc/hash-map.h | |
parent | d61bff850d13ff103de3c2fb13d5e371996e1a3c (diff) | |
download | gcc-c89844e5d30a5235960a2c627abc9369306fda61.zip gcc-c89844e5d30a5235960a2c627abc9369306fda61.tar.gz gcc-c89844e5d30a5235960a2c627abc9369306fda61.tar.bz2 |
Add some hash_map_safe_* functions like vec_safe_*.
gcc/
* hash-map.h (default_hash_map_size): New variable.
(create_ggc): Use it as default argument.
(hash_map_maybe_create, hash_map_safe_get)
(hash_map_safe_get_or_insert, hash_map_safe_put): New fns.
gcc/cp/
* constexpr.c (maybe_initialize_fundef_copies_table): Remove.
(get_fundef_copy): Use hash_map_safe_get_or_insert.
* cp-objcp-common.c (cp_get_debug_type): Use hash_map_safe_*.
* decl.c (store_decomp_type): Remove.
(cp_finish_decomp): Use hash_map_safe_put.
* init.c (get_nsdmi): Use hash_map_safe_*.
* pt.c (store_defaulted_ttp, lookup_defaulted_ttp): Remove.
(add_defaults_to_ttp): Use hash_map_safe_*.
From-SVN: r276484
Diffstat (limited to 'gcc/hash-map.h')
-rw-r--r-- | gcc/hash-map.h | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/gcc/hash-map.h b/gcc/hash-map.h index ba20fe7..73ce6a1 100644 --- a/gcc/hash-map.h +++ b/gcc/hash-map.h @@ -32,6 +32,7 @@ along with GCC; see the file COPYING3. If not see removed. Objects of hash_map type are copy-constructible but not assignable. */ +const size_t default_hash_map_size = 13; template<typename KeyId, typename Value, typename Traits /* = simple_hashmap_traits<default_hash_traits<Key>, Value> */> @@ -129,7 +130,7 @@ class GTY((user)) hash_map }; public: - explicit hash_map (size_t n = 13, bool ggc = false, + explicit hash_map (size_t n = default_hash_map_size, bool ggc = false, bool sanitize_eq_and_hash = true, bool gather_mem_stats = GATHER_STATISTICS CXX_MEM_STAT_INFO) @@ -146,7 +147,7 @@ public: HASH_MAP_ORIGIN PASS_MEM_STAT) {} /* Create a hash_map in ggc memory. */ - static hash_map *create_ggc (size_t size, + static hash_map *create_ggc (size_t size = default_hash_map_size, bool gather_mem_stats = GATHER_STATISTICS CXX_MEM_STAT_INFO) { @@ -326,4 +327,46 @@ gt_pch_nx (hash_map<K, V, H> *h, gt_pointer_operator op, void *cookie) op (&h->m_table.m_entries, cookie); } +enum hm_alloc { hm_heap = false, hm_ggc = true }; +template<bool ggc, typename K, typename V, typename H> +inline hash_map<K,V,H> * +hash_map_maybe_create (hash_map<K,V,H> *&h, + size_t size = default_hash_map_size) +{ + if (!h) + { + if (ggc) + h = hash_map<K,V,H>::create_ggc (size); + else + h = new hash_map<K,V,H> (size); + } + return h; +} + +/* Like h->get, but handles null h. */ +template<typename K, typename V, typename H> +inline V* +hash_map_safe_get (hash_map<K,V,H> *h, const K& k) +{ + return h ? h->get (k) : NULL; +} + +/* Like h->get, but handles null h. */ +template<bool ggc, typename K, typename V, typename H> +inline V& +hash_map_safe_get_or_insert (hash_map<K,V,H> *&h, const K& k, bool *e = NULL, + size_t size = default_hash_map_size) +{ + return hash_map_maybe_create<ggc> (h, size)->get_or_insert (k, e); +} + +/* Like h->put, but handles null h. */ +template<bool ggc, typename K, typename V, typename H> +inline bool +hash_map_safe_put (hash_map<K,V,H> *&h, const K& k, const V& v, + size_t size = default_hash_map_size) +{ + return hash_map_maybe_create<ggc> (h, size)->put (k, v); +} + #endif |