New memory allocation statistics infrastructure.
[gcc.git] / gcc / hash-map-traits.h
1 /* A hash map traits.
2 Copyright (C) 2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #ifndef HASH_MAP_TRAITS_H
21 #define HASH_MAP_TRAITS_H
22
23 /* Bacause mem-stats.h uses default hashmap traits, we have to
24 put the class to this separate header file. */
25
26 /* implement default behavior for traits when types allow it. */
27
28 struct default_hashmap_traits
29 {
30 /* Hashes the passed in key. */
31
32 template<typename T>
33 static hashval_t
34 hash (T *p)
35 {
36 return uintptr_t (p) >> 3;
37 }
38
39 /* If the value converts to hashval_t just use it. */
40
41 template<typename T> static hashval_t hash (T v) { return v; }
42
43 /* Return true if the two keys passed as arguments are equal. */
44
45 template<typename T>
46 static bool
47 equal_keys (const T &a, const T &b)
48 {
49 return a == b;
50 }
51
52 /* Called to dispose of the key and value before marking the entry as
53 deleted. */
54
55 template<typename T> static void remove (T &v) { v.~T (); }
56
57 /* Mark the passed in entry as being deleted. */
58
59 template<typename T>
60 static void
61 mark_deleted (T &e)
62 {
63 mark_key_deleted (e.m_key);
64 }
65
66 /* Mark the passed in entry as being empty. */
67
68 template<typename T>
69 static void
70 mark_empty (T &e)
71 {
72 mark_key_empty (e.m_key);
73 }
74
75 /* Return true if the passed in entry is marked as deleted. */
76
77 template<typename T>
78 static bool
79 is_deleted (T &e)
80 {
81 return e.m_key == (void *)1;
82 }
83
84 /* Return true if the passed in entry is marked as empty. */
85
86 template<typename T> static bool is_empty (T &e) { return e.m_key == NULL; }
87
88 private:
89 template<typename T>
90 static void
91 mark_key_deleted (T *&k)
92 {
93 k = reinterpret_cast<T *> (1);
94 }
95
96 template<typename T>
97 static void
98 mark_key_empty (T *&k)
99 {
100 k = static_cast<T *> (0);
101 }
102 };
103
104 #endif // HASH_MAP_TRAITS_H