gcc/gcc/hash-set.h

150 lines
3.6 KiB
C
Raw Normal View History

/* A type-safe hash set.
Copyright (C) 2014-2017 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC 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 General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#ifndef hash_set_h
#define hash_set_h
template<typename KeyId, typename Traits = default_hash_traits<KeyId> >
class hash_set
{
public:
typedef typename Traits::value_type Key;
New memory allocation statistics infrastructure. * Makefile.in: Add additional dependencies related to memory report enhancement. * alloc-pool.c (allocate_pool_descriptor): Use new ctor. * bitmap.c (struct bitmap_descriptor_d): Remove. (struct loc): Likewise. (struct bitmap_desc_hasher): Likewise. (bitmap_desc_hasher::hash): Likewise. (bitmap_desc_hasher::equal): Likewise. (get_bitmap_descriptor): Likewise. (bitmap_register): User new memory descriptor API. (register_overhead): Likewise. (bitmap_find_bit): Register nsearches and search_iter statistics. (struct bitmap_output_info): Remove. (print_statistics): Likewise. (dump_bitmap_statistics): Use new memory descriptor. * bitmap.h (struct bitmap_usage): New class. * genmatch.c: Extend header file inclusion. * genpreds.c: Likewise. * ggc-common.c (struct ggc_usage): New class. (struct ggc_loc_desc_hasher): Remove. (ggc_loc_desc_hasher::hash): Likewise. (ggc_loc_desc_hasher::equal): Likewise. (struct ggc_ptr_hash_entry): Likewise. (struct ptr_hash_hasher): Likewise. (ptr_hash_hasher::hash): Likewise. (ptr_hash_hasher::equal): Likewise. (make_loc_descriptor): Likewise. (ggc_prune_ptr): Likewise. (dump_ggc_loc_statistics): Use new memory descriptor. (ggc_record_overhead): Likewise. (ggc_free_overhead): Likewise. (final_cmp_statistic): Remove. (cmp_statistic): Likewise. (ggc_add_statistics): Liekwise. (ggc_prune_overhead_list): Likewise. * hash-map-traits.h: New file. * hash-map.h (struct default_hashmap_traits): Move the traits to a separate header file. * hash-set.h: Pass memory statistics info to ctor. * hash-table.c (void dump_hash_table_loc_statistics): New function. * hash-table.h (hash_table::hash_table): Add new ctor arguments. (hash_table::~hash_table): Register memory release operation. (hash_table::alloc_entries): Handle memory allocation operation. (hash_table::expand): Likewise. * inchash.c (iterative_hash_hashval_t): Move implementation to header file. (iterative_hash_host_wide_int): Likewise. * inchash.h (class hash): Likewise. * mem-stats-traits.h: New file. * mem-stats.h: New file. (mem_location): Add new class. (mem_usage): Likewise. (mem_alloc_description): Likewise. * sese.c: Add new header file inclusision. * toplev.c (dump_memory_report): Add report for hash_table, hash_map and hash_set. * tree-sra.c: Add new header file inclusision. * vec.c (struct vec_descriptor): Remove. (hash_descriptor): Likewise. (struct vec_usage): Likewise. (struct ptr_hash_entry): Likewise. (hash_ptr): Likewise. (eq_ptr): Likewise. (vec_prefix::register_overhead): Use new memory descriptor API. (vec_prefix::release_overhead): Likewise. (add_statistics): Remove. (dump_vec_loc_statistics): Use new memory descriptor API. * vec.h (struct vec_prefix): Likewise. (va_heap::reserve): Likewise. (va_heap::release): Likewise. * emit-rtl.c (gen_raw_REG): Fix passing MEM_STAT. From-SVN: r223748
2015-05-27 14:47:22 +02:00
explicit hash_set (size_t n = 13, bool ggc = false CXX_MEM_STAT_INFO)
: m_table (n, ggc, GATHER_STATISTICS, HASH_SET_ORIGIN PASS_MEM_STAT) {}
/* Create a hash_set in gc memory with space for at least n elements. */
static hash_set *
create_ggc (size_t n)
{
hash_set *set = ggc_alloc<hash_set> ();
new (set) hash_set (n, true);
return set;
}
/* If key k isn't already in the map add it to the map, and
return false. Otherwise return true. */
bool add (const Key &k)
{
Key *e = m_table.find_slot_with_hash (k, Traits::hash (k), INSERT);
bool existed = !Traits::is_empty (*e);
if (!existed)
*e = k;
return existed;
}
/* if the passed in key is in the map return its value otherwise NULL. */
bool contains (const Key &k)
{
Key &e = m_table.find_with_hash (k, Traits::hash (k));
return !Traits::is_empty (e);
}
void remove (const Key &k)
{
m_table.remove_elt_with_hash (k, Traits::hash (k));
}
/* Call the call back on each pair of key and value with the passed in
arg. */
template<typename Arg, bool (*f)(const typename Traits::value_type &, Arg)>
void traverse (Arg a) const
{
for (typename hash_table<Traits>::iterator iter = m_table.begin ();
iter != m_table.end (); ++iter)
f (*iter, a);
}
/* Return the number of elements in the set. */
size_t elements () const { return m_table.elements (); }
class iterator
{
public:
explicit iterator (const typename hash_table<Traits>::iterator &iter) :
m_iter (iter) {}
iterator &operator++ ()
{
++m_iter;
return *this;
}
Key
operator* ()
{
return *m_iter;
}
bool
operator != (const iterator &other) const
{
return m_iter != other.m_iter;
}
private:
typename hash_table<Traits>::iterator m_iter;
};
/* Standard iterator retrieval methods. */
iterator begin () const { return iterator (m_table.begin ()); }
iterator end () const { return iterator (m_table.end ()); }
private:
template<typename T, typename U> friend void gt_ggc_mx (hash_set<T, U> *);
template<typename T, typename U> friend void gt_pch_nx (hash_set<T, U> *);
template<typename T, typename U> friend void gt_pch_nx (hash_set<T, U> *, gt_pointer_operator, void *);
hash_table<Traits> m_table;
};
/* ggc marking routines. */
template<typename K, typename H>
static inline void
gt_ggc_mx (hash_set<K, H> *h)
{
gt_ggc_mx (&h->m_table);
}
template<typename K, typename H>
static inline void
gt_pch_nx (hash_set<K, H> *h)
{
gt_pch_nx (&h->m_table);
}
template<typename K, typename H>
static inline void
gt_pch_nx (hash_set<K, H> *h, gt_pointer_operator op, void *cookie)
{
op (&h->m_table.m_entries, cookie);
}
#endif