From c89844e5d30a5235960a2c627abc9369306fda61 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Wed, 2 Oct 2019 15:26:47 -0400 Subject: 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 --- gcc/hash-map.h | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'gcc/hash-map.h') 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, 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 *h, gt_pointer_operator op, void *cookie) op (&h->m_table.m_entries, cookie); } +enum hm_alloc { hm_heap = false, hm_ggc = true }; +template +inline hash_map * +hash_map_maybe_create (hash_map *&h, + size_t size = default_hash_map_size) +{ + if (!h) + { + if (ggc) + h = hash_map::create_ggc (size); + else + h = new hash_map (size); + } + return h; +} + +/* Like h->get, but handles null h. */ +template +inline V* +hash_map_safe_get (hash_map *h, const K& k) +{ + return h ? h->get (k) : NULL; +} + +/* Like h->get, but handles null h. */ +template +inline V& +hash_map_safe_get_or_insert (hash_map *&h, const K& k, bool *e = NULL, + size_t size = default_hash_map_size) +{ + return hash_map_maybe_create (h, size)->get_or_insert (k, e); +} + +/* Like h->put, but handles null h. */ +template +inline bool +hash_map_safe_put (hash_map *&h, const K& k, const V& v, + size_t size = default_hash_map_size) +{ + return hash_map_maybe_create (h, size)->put (k, v); +} + #endif -- cgit v1.1