]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/hash-traits.h
hash-traits.h (typed_noop_remove): Don't require a pointer type.
[thirdparty/gcc.git] / gcc / hash-traits.h
CommitLineData
f11c3779
RS
1/* Traits for hashable types.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef hash_traits_h
21#define hash_traits_h
22
23/* Helpful type for removing with free. */
24
25template <typename Type>
26struct typed_free_remove
27{
28 static inline void remove (Type *p);
29};
30
31
32/* Remove with free. */
33
34template <typename Type>
35inline void
36typed_free_remove <Type>::remove (Type *p)
37{
38 free (p);
39}
40
41
42/* Helpful type for a no-op remove. */
43
44template <typename Type>
45struct typed_noop_remove
46{
fc17926a 47 static inline void remove (Type &);
f11c3779
RS
48};
49
50
51/* Remove doing nothing. */
52
53template <typename Type>
54inline void
fc17926a 55typed_noop_remove <Type>::remove (Type &)
f11c3779
RS
56{
57}
58
59
8d67ee55
RS
60/* Pointer hasher based on pointer equality. Other types of pointer hash
61 can inherit this and override the hash and equal functions with some
62 other form of equality (such as string equality). */
f11c3779
RS
63
64template <typename Type>
8d67ee55 65struct pointer_hash
f11c3779
RS
66{
67 typedef Type *value_type;
68 typedef Type *compare_type;
69
70 static inline hashval_t hash (const value_type &);
f11c3779
RS
71 static inline bool equal (const value_type &existing,
72 const compare_type &candidate);
843adf88
RS
73 static inline void mark_deleted (Type *&);
74 static inline void mark_empty (Type *&);
75 static inline bool is_deleted (Type *);
76 static inline bool is_empty (Type *);
f11c3779
RS
77};
78
79template <typename Type>
80inline hashval_t
81pointer_hash <Type>::hash (const value_type &candidate)
82{
83 /* This is a really poor hash function, but it is what the current code uses,
84 so I am reusing it to avoid an additional axis in testing. */
85 return (hashval_t) ((intptr_t)candidate >> 3);
86}
87
88template <typename Type>
89inline bool
90pointer_hash <Type>::equal (const value_type &existing,
91 const compare_type &candidate)
92{
93 return existing == candidate;
94}
95
843adf88
RS
96template <typename Type>
97inline void
98pointer_hash <Type>::mark_deleted (Type *&e)
99{
100 e = reinterpret_cast<Type *> (1);
101}
102
103template <typename Type>
104inline void
105pointer_hash <Type>::mark_empty (Type *&e)
106{
107 e = NULL;
108}
109
110template <typename Type>
111inline bool
112pointer_hash <Type>::is_deleted (Type *e)
113{
114 return e == reinterpret_cast<Type *> (1);
115}
116
117template <typename Type>
118inline bool
119pointer_hash <Type>::is_empty (Type *e)
120{
121 return e == NULL;
122}
123
ca752f39 124/* Remover and marker for entries in gc memory. */
f11c3779
RS
125
126template<typename T>
ca752f39 127struct ggc_remove
f11c3779 128{
5ac6389b 129 static void remove (T &) {}
f11c3779
RS
130
131 static void
5ac6389b 132 ggc_mx (T &p)
f11c3779
RS
133 {
134 extern void gt_ggc_mx (T &);
135 gt_ggc_mx (p);
136 }
137
138 static void
139 pch_nx (T &p)
140 {
141 extern void gt_pch_nx (T &);
142 gt_pch_nx (p);
143 }
144
145 static void
146 pch_nx (T &p, gt_pointer_operator op, void *cookie)
147 {
148 op (&p, cookie);
149 }
150};
151
6c907cff
RS
152/* Remover and marker for "cache" entries in gc memory. These entries can
153 be deleted if there are no non-cache references to the data. */
f11c3779
RS
154
155template<typename T>
6c907cff 156struct ggc_cache_remove : ggc_remove<T>
f11c3779 157{
f11c3779 158 /* Entries are weakly held because this is for caches. */
f11c3779
RS
159 static void ggc_mx (T &) {}
160
08ec2754
RS
161 static int
162 keep_cache_entry (T &e)
f11c3779 163 {
08ec2754 164 return ggc_marked_p (e) ? -1 : 0;
f11c3779
RS
165 }
166};
167
8d67ee55
RS
168/* Traits for pointer elements that should not be freed when an element
169 is deleted. */
170
171template <typename T>
fc17926a 172struct nofree_ptr_hash : pointer_hash <T>, typed_noop_remove <T *> {};
8d67ee55 173
95fbe13e
RS
174/* Traits for pointer elements that should be freed via free() when an
175 element is deleted. */
176
177template <typename T>
178struct free_ptr_hash : pointer_hash <T>, typed_free_remove <T> {};
179
ca752f39
RS
180/* Traits for elements that point to gc memory. The pointed-to data
181 must be kept across collections. */
182
183template <typename T>
184struct ggc_ptr_hash : pointer_hash <T>, ggc_remove <T *> {};
185
6c907cff
RS
186/* Traits for elements that point to gc memory. The elements don't
187 in themselves keep the pointed-to data alive and they can be deleted
188 if the pointed-to data is going to be collected. */
189
190template <typename T>
191struct ggc_cache_ptr_hash : pointer_hash <T>, ggc_cache_remove <T *> {};
192
f11c3779 193#endif