gcc/
[official-gcc.git] / gcc / hash-traits.h
blob1bac581ee6ca87cbc955e41952edb7a627669f55
1 /* Traits for hashable types.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef hash_traits_h
21 #define hash_traits_h
23 /* Helpful type for removing with free. */
25 template <typename Type>
26 struct typed_free_remove
28 static inline void remove (Type *p);
32 /* Remove with free. */
34 template <typename Type>
35 inline void
36 typed_free_remove <Type>::remove (Type *p)
38 free (p);
42 /* Helpful type for a no-op remove. */
44 template <typename Type>
45 struct typed_noop_remove
47 static inline void remove (Type *p);
51 /* Remove doing nothing. */
53 template <typename Type>
54 inline void
55 typed_noop_remove <Type>::remove (Type *p ATTRIBUTE_UNUSED)
60 /* Pointer hash with a no-op remove method. */
62 template <typename Type>
63 struct pointer_hash : typed_noop_remove <Type>
65 typedef Type *value_type;
66 typedef Type *compare_type;
68 static inline hashval_t hash (const value_type &);
70 static inline bool equal (const value_type &existing,
71 const compare_type &candidate);
74 template <typename Type>
75 inline hashval_t
76 pointer_hash <Type>::hash (const value_type &candidate)
78 /* This is a really poor hash function, but it is what the current code uses,
79 so I am reusing it to avoid an additional axis in testing. */
80 return (hashval_t) ((intptr_t)candidate >> 3);
83 template <typename Type>
84 inline bool
85 pointer_hash <Type>::equal (const value_type &existing,
86 const compare_type &candidate)
88 return existing == candidate;
91 /* Hasher for entry in gc memory. */
93 template<typename T>
94 struct ggc_hasher
96 typedef T value_type;
97 typedef T compare_type;
99 static void remove (T) {}
101 static void
102 ggc_mx (T p)
104 extern void gt_ggc_mx (T &);
105 gt_ggc_mx (p);
108 static void
109 pch_nx (T &p)
111 extern void gt_pch_nx (T &);
112 gt_pch_nx (p);
115 static void
116 pch_nx (T &p, gt_pointer_operator op, void *cookie)
118 op (&p, cookie);
122 /* Hasher for cache entry in gc memory. */
124 template<typename T>
125 struct ggc_cache_hasher
127 typedef T value_type;
128 typedef T compare_type;
130 static void remove (T &) {}
132 /* Entries are weakly held because this is for caches. */
134 static void ggc_mx (T &) {}
136 static void
137 pch_nx (T &p)
139 extern void gt_pch_nx (T &);
140 gt_pch_nx (p);
143 static void
144 pch_nx (T &p, gt_pointer_operator op, void *cookie)
146 op (&p, cookie);
149 /* Clear out entries if they are about to be gc'd. */
151 static void
152 handle_cache_entry (T &e)
154 if (e != HTAB_EMPTY_ENTRY && e != HTAB_DELETED_ENTRY && !ggc_marked_p (e))
155 e = static_cast<T> (HTAB_DELETED_ENTRY);
159 #endif