Clock: Move the clock and related functions to ClockedObject
[gem5.git] / src / base / hashmap.hh
1 /*
2 * Copyright (c) 2012 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Nathan Binkert
41 * Andreas Hansson
42 */
43
44 #ifndef __HASHMAP_HH__
45 #define __HASHMAP_HH__
46
47 #if defined(__GNUC__)
48
49 // for compilers that deprecate ext/hash_map, i.e. gcc >= 4.3 and
50 // clang, use unordered_map
51
52 // we need to determine what is available, as in the non-c++0x case,
53 // e.g. gcc >= 4.3 and <= 4.5, the containers are in the std::tr1
54 // namespace, and only gcc >= 4.6 (with -std=c++0x) adds the final
55 // container implementation in the std namespace
56
57 #if defined(__clang__)
58 // align with -std=c++0x only for clang >= 3.0 in CCFLAGS and also
59 // check if the header is present as this depends on what clang was
60 // built against, using XCode clang 3.1, for example, the header is
61 // not present without adding -stdlib=libc++
62 #if (__clang_major__ >= 3 && __has_include(<unordered_map>))
63 #define HAVE_STD_UNORDERED_MAP 1
64 #else
65 // we only support clang versions above 2.9 and these all have the tr1
66 // unordered_map
67 #define HAVE_STD_TR1_UNORDERED_MAP 1
68 #endif
69 #else
70 // align with -std=c++0x only for gcc >= 4.6 in CCFLAGS, contrary to
71 // clang we can rely entirely on the compiler version
72 #if ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
73 #define HAVE_STD_UNORDERED_MAP 1
74 #else
75 #define HAVE_STD_TR1_UNORDERED_MAP 1
76 #endif
77 #endif
78
79 // set a default value of 0
80 #ifndef HAVE_STD_UNORDERED_MAP
81 #define HAVE_STD_UNORDERED_MAP 0
82 #endif
83
84 // set a default value of 0
85 #ifndef HAVE_STD_TR1_UNORDERED_MAP
86 #define HAVE_STD_TR1_UNORDERED_MAP 0
87 #endif
88
89 // now we are ready to deal with the actual includes based on what is
90 // available
91 #if (HAVE_STD_UNORDERED_MAP || HAVE_STD_TR1_UNORDERED_MAP)
92
93 #define hash_map unordered_map
94 #define hash_multimap unordered_multimap
95 #define hash_set unordered_set
96 #define hash_multiset unordered_multiset
97
98 // these versions also have an existing hash function for strings and
99 // 64-bit integer types
100 #define HAVE_HASH_FUNCTIONS 1
101
102 #if HAVE_STD_UNORDERED_MAP
103
104 // clang or gcc >= 4.6
105 #include <unordered_map>
106 #include <unordered_set>
107 // note that this assumes that -std=c++0x is added to the command line
108 // which is done in the SConstruct CXXFLAGS for gcc >= 4.6 and clang
109 // >= 3.0
110 #define __hash_namespace std
111 #define __hash_namespace_begin namespace std {
112 #define __hash_namespace_end }
113 #else
114 // clang <= 3.0, gcc >= 4.3 and < 4.6
115 #include <tr1/unordered_map>
116 #include <tr1/unordered_set>
117 #define __hash_namespace std::tr1
118 #define __hash_namespace_begin namespace std { namespace tr1 {
119 #define __hash_namespace_end } }
120 #endif
121 #else
122 // gcc < 4.3
123 #include <ext/hash_map>
124 #include <ext/hash_set>
125 #define __hash_namespace __gnu_cxx
126 #define __hash_namespace_begin namespace __gnu_cxx {
127 #define __hash_namespace_end }
128 #endif
129 #else
130 // non GNU compiler
131 #include <hash_map>
132 #include <hash_set>
133 #define __hash_namsepace std
134 #define __hash_namespace_begin namespace std {
135 #define __hash_namespace_end }
136 #endif
137
138 #include <string>
139
140 #include "base/types.hh"
141
142 namespace m5 {
143 using ::__hash_namespace::hash_multimap;
144 using ::__hash_namespace::hash_multiset;
145 using ::__hash_namespace::hash_map;
146 using ::__hash_namespace::hash_set;
147 using ::__hash_namespace::hash;
148 }
149
150
151 ///////////////////////////////////
152 // Some default Hashing Functions
153 //
154
155 __hash_namespace_begin
156
157 // if the hash functions for 64-bit integer types and strings are not
158 // already available, then declare them here
159 #if !defined(HAVE_HASH_FUNCTIONS)
160
161 #if !defined(__LP64__) && !defined(__alpha__) && !defined(__SUNPRO_CC)
162 template<>
163 struct hash<uint64_t> {
164 size_t operator()(uint64_t r) const {
165 return r;
166 }
167 };
168
169 template<>
170 struct hash<int64_t> {
171 size_t operator()(int64_t r) const {
172 return r;
173 };
174 };
175 #endif
176
177 template<>
178 struct hash<std::string> {
179 size_t operator()(const std::string &s) const {
180 return(__stl_hash_string(s.c_str()));
181 }
182 };
183
184 template <>
185 struct hash<std::pair<std::string, uint64_t> > {
186 size_t operator() (std::pair<std::string, uint64_t> r) const {
187 return (__stl_hash_string(r.first.c_str())) ^ r.second;
188 }
189 };
190 #endif
191 __hash_namespace_end
192
193 #endif // __HASHMAP_HH__