sfricke-samsung | 8f658d4 | 2020-05-03 20:12:24 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015-2017, 2019-2020 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2017, 2019-2020 Valve Corporation |
| 3 | * Copyright (c) 2015-2017, 2019-2020 LunarG, Inc. |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 4 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 8 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 10 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 16 | * |
Jon Ashburn | e922f71 | 2015-11-03 13:41:23 -0700 | [diff] [blame] | 17 | * Author: Mark Lobodzinski <mark@lunarg.com> |
| 18 | * Author: Courtney Goeltzenleuchter <courtney@LunarG.com> |
Dave Houlton | 59a2070 | 2017-02-02 17:26:23 -0700 | [diff] [blame] | 19 | * Author: Dave Houlton <daveh@lunarg.com> |
Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 22 | #pragma once |
John Zulauf | f05b377 | 2019-04-03 18:04:23 -0600 | [diff] [blame] | 23 | |
| 24 | #include <cassert> |
| 25 | #include <cstddef> |
| 26 | #include <functional> |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 27 | #include <stdbool.h> |
John Zulauf | 965d88d | 2018-04-12 15:47:26 -0600 | [diff] [blame] | 28 | #include <string> |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 29 | #include <vector> |
Mark Lobodzinski | a055501 | 2018-08-15 16:43:49 -0600 | [diff] [blame] | 30 | #include <set> |
John Zulauf | 2c2ccd4 | 2019-04-05 13:13:13 -0600 | [diff] [blame] | 31 | #include "cast_utils.h" |
Dave Houlton | 3c9fca7 | 2017-03-27 17:25:54 -0600 | [diff] [blame] | 32 | #include "vk_format_utils.h" |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 33 | #include "vk_layer_logging.h" |
| 34 | |
Courtney Goeltzenleuchter | d263550 | 2015-10-21 17:08:06 -0600 | [diff] [blame] | 35 | #ifndef WIN32 |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 36 | #include <strings.h> // For ffs() |
Courtney Goeltzenleuchter | 3698c62 | 2015-10-27 11:23:21 -0600 | [diff] [blame] | 37 | #else |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 38 | #include <intrin.h> // For __lzcnt() |
Courtney Goeltzenleuchter | d263550 | 2015-10-21 17:08:06 -0600 | [diff] [blame] | 39 | #endif |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 40 | |
Petr Kraus | c3382e9 | 2019-12-14 00:41:30 +0100 | [diff] [blame] | 41 | #define STRINGIFY(s) STRINGIFY_HELPER(s) |
| 42 | #define STRINGIFY_HELPER(s) #s |
| 43 | |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 44 | #ifdef __cplusplus |
John Zulauf | 1507ee4 | 2020-05-18 11:33:09 -0600 | [diff] [blame^] | 45 | static inline VkExtent3D CastTo3D(const VkExtent2D &d2) { |
| 46 | VkExtent3D d3 = {d2.width, d2.height, 1}; |
| 47 | return d3; |
| 48 | } |
| 49 | |
| 50 | static inline VkOffset3D CastTo3D(const VkOffset2D &d2) { |
| 51 | VkOffset3D d3 = {d2.x, d2.y, 0}; |
| 52 | return d3; |
| 53 | } |
| 54 | |
John Zulauf | 965d88d | 2018-04-12 15:47:26 -0600 | [diff] [blame] | 55 | // Traits objects to allow string_join to operate on collections of const char * |
| 56 | template <typename String> |
| 57 | struct StringJoinSizeTrait { |
| 58 | static size_t size(const String &str) { return str.size(); } |
| 59 | }; |
| 60 | |
| 61 | template <> |
| 62 | struct StringJoinSizeTrait<const char *> { |
| 63 | static size_t size(const char *str) { |
| 64 | if (!str) return 0; |
| 65 | return strlen(str); |
| 66 | } |
| 67 | }; |
| 68 | // Similar to perl/python join |
| 69 | // * String must support size, reserve, append, and be default constructable |
| 70 | // * StringCollection must support size, const forward iteration, and store |
| 71 | // strings compatible with String::append |
| 72 | // * Accessor trait can be set if default accessors (compatible with string |
| 73 | // and const char *) don't support size(StringCollection::value_type &) |
| 74 | // |
| 75 | // Return type based on sep type |
| 76 | template <typename String = std::string, typename StringCollection = std::vector<String>, |
| 77 | typename Accessor = StringJoinSizeTrait<typename StringCollection::value_type>> |
| 78 | static inline String string_join(const String &sep, const StringCollection &strings) { |
| 79 | String joined; |
| 80 | const size_t count = strings.size(); |
| 81 | if (!count) return joined; |
| 82 | |
| 83 | // Prereserved storage, s.t. we will execute in linear time (avoids reallocation copies) |
| 84 | size_t reserve = (count - 1) * sep.size(); |
| 85 | for (const auto &str : strings) { |
| 86 | reserve += Accessor::size(str); // abstracted to allow const char * type in StringCollection |
| 87 | } |
| 88 | joined.reserve(reserve + 1); |
| 89 | |
| 90 | // Seps only occur *between* strings entries, so first is special |
| 91 | auto current = strings.cbegin(); |
| 92 | joined.append(*current); |
| 93 | ++current; |
| 94 | for (; current != strings.cend(); ++current) { |
| 95 | joined.append(sep); |
| 96 | joined.append(*current); |
| 97 | } |
| 98 | return joined; |
| 99 | } |
| 100 | |
| 101 | // Requires StringCollection::value_type has a const char * constructor and is compatible the string_join::String above |
| 102 | template <typename StringCollection = std::vector<std::string>, typename SepString = std::string> |
| 103 | static inline SepString string_join(const char *sep, const StringCollection &strings) { |
| 104 | return string_join<SepString, StringCollection>(SepString(sep), strings); |
| 105 | } |
| 106 | |
Petr Kraus | 168417e | 2019-09-07 16:45:40 +0200 | [diff] [blame] | 107 | static inline std::string string_trim(const std::string &s) { |
| 108 | const char *whitespace = " \t\f\v\n\r"; |
| 109 | |
| 110 | const auto trimmed_beg = s.find_first_not_of(whitespace); |
| 111 | if (trimmed_beg == std::string::npos) return ""; |
| 112 | |
| 113 | const auto trimmed_end = s.find_last_not_of(whitespace); |
| 114 | assert(trimmed_end != std::string::npos && trimmed_beg <= trimmed_end); |
| 115 | |
| 116 | return s.substr(trimmed_beg, trimmed_end - trimmed_beg + 1); |
| 117 | } |
| 118 | |
John Zulauf | df851b1 | 2018-06-12 14:49:04 -0600 | [diff] [blame] | 119 | // Perl/Python style join operation for general types using stream semantics |
| 120 | // Note: won't be as fast as string_join above, but simpler to use (and code) |
| 121 | // Note: Modifiable reference doesn't match the google style but does match std style for stream handling and algorithms |
| 122 | template <typename Stream, typename String, typename ForwardIt> |
| 123 | Stream &stream_join(Stream &stream, const String &sep, ForwardIt first, ForwardIt last) { |
| 124 | if (first != last) { |
| 125 | stream << *first; |
| 126 | ++first; |
| 127 | while (first != last) { |
| 128 | stream << sep << *first; |
| 129 | ++first; |
| 130 | } |
| 131 | } |
| 132 | return stream; |
| 133 | } |
| 134 | |
| 135 | // stream_join For whole collections with forward iterators |
| 136 | template <typename Stream, typename String, typename Collection> |
| 137 | Stream &stream_join(Stream &stream, const String &sep, const Collection &values) { |
| 138 | return stream_join(stream, sep, values.cbegin(), values.cend()); |
| 139 | } |
| 140 | |
Mark Lobodzinski | c1b5b88 | 2018-06-25 14:54:04 -0600 | [diff] [blame] | 141 | typedef void *dispatch_key; |
| 142 | static inline dispatch_key get_dispatch_key(const void *object) { return (dispatch_key) * (VkLayerDispatchTable **)object; } |
| 143 | |
| 144 | VK_LAYER_EXPORT VkLayerInstanceCreateInfo *get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func); |
| 145 | VK_LAYER_EXPORT VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCreateInfo, VkLayerFunction func); |
| 146 | |
Chris Mayer | 334e72f | 2018-11-29 14:25:41 +0100 | [diff] [blame] | 147 | static inline bool IsPowerOfTwo(unsigned x) { return x && !(x & (x - 1)); } |
Chris Mayer | 9bc9d09 | 2018-11-12 12:29:10 +0100 | [diff] [blame] | 148 | |
sfricke-samsung | 8f658d4 | 2020-05-03 20:12:24 -0700 | [diff] [blame] | 149 | static inline uint32_t SampleCountSize(VkSampleCountFlagBits sample_count) { |
| 150 | uint32_t size = 0; |
| 151 | switch (sample_count) { |
| 152 | case VK_SAMPLE_COUNT_1_BIT: |
| 153 | size = 1; |
| 154 | break; |
| 155 | case VK_SAMPLE_COUNT_2_BIT: |
| 156 | size = 2; |
| 157 | break; |
| 158 | case VK_SAMPLE_COUNT_4_BIT: |
| 159 | size = 4; |
| 160 | break; |
| 161 | case VK_SAMPLE_COUNT_8_BIT: |
| 162 | size = 8; |
| 163 | break; |
| 164 | case VK_SAMPLE_COUNT_16_BIT: |
| 165 | size = 16; |
| 166 | break; |
| 167 | case VK_SAMPLE_COUNT_32_BIT: |
| 168 | size = 32; |
| 169 | break; |
| 170 | case VK_SAMPLE_COUNT_64_BIT: |
| 171 | size = 64; |
| 172 | break; |
| 173 | default: |
| 174 | size = 0; |
| 175 | } |
| 176 | return size; |
| 177 | } |
| 178 | |
sfricke-samsung | bd0e805 | 2020-06-06 01:36:39 -0700 | [diff] [blame] | 179 | static inline bool IsIdentitySwizzle(VkComponentMapping components) { |
| 180 | // clang-format off |
| 181 | return ( |
| 182 | ((components.r == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.r == VK_COMPONENT_SWIZZLE_R)) && |
| 183 | ((components.g == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.g == VK_COMPONENT_SWIZZLE_G)) && |
| 184 | ((components.b == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.b == VK_COMPONENT_SWIZZLE_B)) && |
| 185 | ((components.a == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.a == VK_COMPONENT_SWIZZLE_A)) |
| 186 | ); |
| 187 | // clang-format on |
| 188 | } |
| 189 | |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 190 | extern "C" { |
| 191 | #endif |
| 192 | |
Jon Ashburn | d883d81 | 2016-03-24 08:32:09 -0600 | [diff] [blame] | 193 | #define VK_LAYER_API_VERSION VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION) |
Mark Lobodzinski | adaac9d | 2016-01-08 11:07:56 -0700 | [diff] [blame] | 194 | |
Mark Lobodzinski | a9f3349 | 2016-01-11 14:17:05 -0700 | [diff] [blame] | 195 | typedef enum VkStringErrorFlagBits { |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 196 | VK_STRING_ERROR_NONE = 0x00000000, |
| 197 | VK_STRING_ERROR_LENGTH = 0x00000001, |
| 198 | VK_STRING_ERROR_BAD_DATA = 0x00000002, |
Mark Lobodzinski | a9f3349 | 2016-01-11 14:17:05 -0700 | [diff] [blame] | 199 | } VkStringErrorFlagBits; |
| 200 | typedef VkFlags VkStringErrorFlags; |
Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 201 | |
Petr Kraus | 4ed81e3 | 2019-09-02 23:41:19 +0200 | [diff] [blame] | 202 | VK_LAYER_EXPORT void layer_debug_report_actions(debug_report_data *report_data, const VkAllocationCallbacks *pAllocator, |
| 203 | const char *layer_identifier); |
Mark Young | 6ba8abe | 2017-11-09 10:37:04 -0700 | [diff] [blame] | 204 | |
Petr Kraus | 4ed81e3 | 2019-09-02 23:41:19 +0200 | [diff] [blame] | 205 | VK_LAYER_EXPORT void layer_debug_messenger_actions(debug_report_data *report_data, const VkAllocationCallbacks *pAllocator, |
| 206 | const char *layer_identifier); |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 207 | |
Mike Stroyan | a551bc0 | 2016-09-28 09:42:28 -0600 | [diff] [blame] | 208 | VK_LAYER_EXPORT VkStringErrorFlags vk_string_validate(const int max_length, const char *char_array); |
Mark Lobodzinski | a055501 | 2018-08-15 16:43:49 -0600 | [diff] [blame] | 209 | VK_LAYER_EXPORT bool white_list(const char *item, const std::set<std::string> &whitelist); |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 210 | |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 211 | static inline int u_ffs(int val) { |
Courtney Goeltzenleuchter | d263550 | 2015-10-21 17:08:06 -0600 | [diff] [blame] | 212 | #ifdef WIN32 |
Mark Lobodzinski | 5ddf6c3 | 2015-12-16 17:47:28 -0700 | [diff] [blame] | 213 | unsigned long bit_pos = 0; |
Mike Stroyan | debb984 | 2016-01-07 10:05:21 -0700 | [diff] [blame] | 214 | if (_BitScanForward(&bit_pos, val) != 0) { |
Mark Lobodzinski | 5ddf6c3 | 2015-12-16 17:47:28 -0700 | [diff] [blame] | 215 | bit_pos += 1; |
| 216 | } |
| 217 | return bit_pos; |
Courtney Goeltzenleuchter | d263550 | 2015-10-21 17:08:06 -0600 | [diff] [blame] | 218 | #else |
Mark Lobodzinski | 5ddf6c3 | 2015-12-16 17:47:28 -0700 | [diff] [blame] | 219 | return ffs(val); |
Courtney Goeltzenleuchter | d263550 | 2015-10-21 17:08:06 -0600 | [diff] [blame] | 220 | #endif |
| 221 | } |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 222 | |
| 223 | #ifdef __cplusplus |
| 224 | } |
| 225 | #endif |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 226 | |
Bruce Dawson | 5fab7f8 | 2020-06-09 16:23:07 -0700 | [diff] [blame] | 227 | // Minimum Visual Studio 2015 Update 2, or libc++ with C++17 |
| 228 | #if defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023918 && NTDDI_VERSION > NTDDI_WIN10_RS2 && \ |
| 229 | (!defined(_LIBCPP_VERSION) || __cplusplus >= 201703) |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 230 | #include <shared_mutex> |
| 231 | #endif |
| 232 | |
Jeff Bolz | caeccc7 | 2019-10-15 15:35:26 -0500 | [diff] [blame] | 233 | class ReadWriteLock { |
| 234 | private: |
Bruce Dawson | 5fab7f8 | 2020-06-09 16:23:07 -0700 | [diff] [blame] | 235 | #if defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023918 && NTDDI_VERSION > NTDDI_WIN10_RS2 && \ |
| 236 | (!defined(_LIBCPP_VERSION) || __cplusplus >= 201703) |
Jeff Bolz | caeccc7 | 2019-10-15 15:35:26 -0500 | [diff] [blame] | 237 | typedef std::shared_mutex lock_t; |
| 238 | #else |
| 239 | typedef std::mutex lock_t; |
| 240 | #endif |
| 241 | |
| 242 | public: |
| 243 | void lock() { m_lock.lock(); } |
| 244 | bool try_lock() { return m_lock.try_lock(); } |
| 245 | void unlock() { m_lock.unlock(); } |
Bruce Dawson | 5fab7f8 | 2020-06-09 16:23:07 -0700 | [diff] [blame] | 246 | #if defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023918 && NTDDI_VERSION > NTDDI_WIN10_RS2 && \ |
| 247 | (!defined(_LIBCPP_VERSION) || __cplusplus >= 201703) |
Jeff Bolz | caeccc7 | 2019-10-15 15:35:26 -0500 | [diff] [blame] | 248 | void lock_shared() { m_lock.lock_shared(); } |
| 249 | bool try_lock_shared() { return m_lock.try_lock_shared(); } |
| 250 | void unlock_shared() { m_lock.unlock_shared(); } |
| 251 | #else |
| 252 | void lock_shared() { lock(); } |
| 253 | bool try_lock_shared() { return try_lock(); } |
| 254 | void unlock_shared() { unlock(); } |
| 255 | #endif |
| 256 | private: |
| 257 | lock_t m_lock; |
| 258 | }; |
| 259 | |
Bruce Dawson | 5fab7f8 | 2020-06-09 16:23:07 -0700 | [diff] [blame] | 260 | #if defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023918 && NTDDI_VERSION > NTDDI_WIN10_RS2 && \ |
| 261 | (!defined(_LIBCPP_VERSION) || __cplusplus >= 201703) |
Jeff Bolz | caeccc7 | 2019-10-15 15:35:26 -0500 | [diff] [blame] | 262 | typedef std::shared_lock<ReadWriteLock> read_lock_guard_t; |
| 263 | typedef std::unique_lock<ReadWriteLock> write_lock_guard_t; |
| 264 | #else |
| 265 | typedef std::unique_lock<ReadWriteLock> read_lock_guard_t; |
| 266 | typedef std::unique_lock<ReadWriteLock> write_lock_guard_t; |
| 267 | #endif |
| 268 | |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 269 | // Limited concurrent_unordered_map that supports internally-synchronized |
| 270 | // insert/erase/access. Splits locking across N buckets and uses shared_mutex |
| 271 | // for read/write locking. Iterators are not supported. The following |
| 272 | // operations are supported: |
| 273 | // |
| 274 | // insert_or_assign: Insert a new element or update an existing element. |
Jeff Bolz | fd3bb24 | 2019-08-22 06:10:49 -0500 | [diff] [blame] | 275 | // insert: Insert a new element and return whether it was inserted. |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 276 | // erase: Remove an element. |
Jeff Bolz | fd3bb24 | 2019-08-22 06:10:49 -0500 | [diff] [blame] | 277 | // contains: Returns true if the key is in the map. |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 278 | // find: Returns != end() if found, value is in ret->second. |
| 279 | // pop: Erases and returns the erased value if found. |
| 280 | // |
| 281 | // find/end: find returns a vaguely iterator-like type that can be compared to |
| 282 | // end and can use iter->second to retrieve the reference. This is to ease porting |
| 283 | // for existing code that combines the existence check and lookup in a single |
| 284 | // operation (and thus a single lock). i.e.: |
| 285 | // |
| 286 | // auto iter = map.find(key); |
| 287 | // if (iter != map.end()) { |
| 288 | // T t = iter->second; |
| 289 | // ... |
Jeff Bolz | fd3bb24 | 2019-08-22 06:10:49 -0500 | [diff] [blame] | 290 | // |
| 291 | // snapshot: Return an array of elements (key, value pairs) that satisfy an optional |
| 292 | // predicate. This can be used as a substitute for iterators in exceptional cases. |
Jeff Bolz | b351411 | 2019-08-23 21:46:18 -0500 | [diff] [blame] | 293 | template <typename Key, typename T, int BUCKETSLOG2 = 2, typename Hash = std::hash<Key>> |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 294 | class vl_concurrent_unordered_map { |
Petr Kraus | 4ed81e3 | 2019-09-02 23:41:19 +0200 | [diff] [blame] | 295 | public: |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 296 | void insert_or_assign(const Key &key, const T &value) { |
| 297 | uint32_t h = ConcurrentMapHashObject(key); |
| 298 | write_lock_guard_t lock(locks[h].lock); |
| 299 | maps[h][key] = value; |
| 300 | } |
| 301 | |
Jeff Bolz | fd3bb24 | 2019-08-22 06:10:49 -0500 | [diff] [blame] | 302 | bool insert(const Key &key, const T &value) { |
| 303 | uint32_t h = ConcurrentMapHashObject(key); |
| 304 | write_lock_guard_t lock(locks[h].lock); |
| 305 | auto ret = maps[h].insert(typename std::unordered_map<Key, T>::value_type(key, value)); |
| 306 | return ret.second; |
| 307 | } |
| 308 | |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 309 | // returns size_type |
| 310 | size_t erase(const Key &key) { |
| 311 | uint32_t h = ConcurrentMapHashObject(key); |
| 312 | write_lock_guard_t lock(locks[h].lock); |
| 313 | return maps[h].erase(key); |
| 314 | } |
| 315 | |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 316 | bool contains(const Key &key) const { |
Jeff Bolz | fd3bb24 | 2019-08-22 06:10:49 -0500 | [diff] [blame] | 317 | uint32_t h = ConcurrentMapHashObject(key); |
| 318 | read_lock_guard_t lock(locks[h].lock); |
| 319 | return maps[h].count(key) != 0; |
| 320 | } |
| 321 | |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 322 | // type returned by find() and end(). |
| 323 | class FindResult { |
Petr Kraus | 4ed81e3 | 2019-09-02 23:41:19 +0200 | [diff] [blame] | 324 | public: |
Jeff Bolz | fd3bb24 | 2019-08-22 06:10:49 -0500 | [diff] [blame] | 325 | FindResult(bool a, T b) : result(a, std::move(b)) {} |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 326 | |
| 327 | // == and != only support comparing against end() |
| 328 | bool operator==(const FindResult &other) const { |
| 329 | if (result.first == false && other.result.first == false) { |
| 330 | return true; |
| 331 | } |
| 332 | return false; |
| 333 | } |
| 334 | bool operator!=(const FindResult &other) const { return !(*this == other); } |
| 335 | |
| 336 | // Make -> act kind of like an iterator. |
| 337 | std::pair<bool, T> *operator->() { return &result; } |
| 338 | const std::pair<bool, T> *operator->() const { return &result; } |
| 339 | |
Petr Kraus | 4ed81e3 | 2019-09-02 23:41:19 +0200 | [diff] [blame] | 340 | private: |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 341 | // (found, reference to element) |
| 342 | std::pair<bool, T> result; |
| 343 | }; |
| 344 | |
Jeff Bolz | fd3bb24 | 2019-08-22 06:10:49 -0500 | [diff] [blame] | 345 | // find()/end() return a FindResult containing a copy of the value. For end(), |
| 346 | // return a default value. |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 347 | FindResult end() const { return FindResult(false, T()); } |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 348 | |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 349 | FindResult find(const Key &key) const { |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 350 | uint32_t h = ConcurrentMapHashObject(key); |
| 351 | read_lock_guard_t lock(locks[h].lock); |
| 352 | |
| 353 | auto itr = maps[h].find(key); |
| 354 | bool found = itr != maps[h].end(); |
| 355 | |
Jeff Bolz | fd3bb24 | 2019-08-22 06:10:49 -0500 | [diff] [blame] | 356 | if (found) { |
| 357 | return FindResult(true, itr->second); |
| 358 | } else { |
| 359 | return end(); |
| 360 | } |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | FindResult pop(const Key &key) { |
| 364 | uint32_t h = ConcurrentMapHashObject(key); |
| 365 | write_lock_guard_t lock(locks[h].lock); |
| 366 | |
| 367 | auto itr = maps[h].find(key); |
| 368 | bool found = itr != maps[h].end(); |
| 369 | |
| 370 | if (found) { |
Jeff Bolz | fd3bb24 | 2019-08-22 06:10:49 -0500 | [diff] [blame] | 371 | auto ret = std::move(FindResult(true, itr->second)); |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 372 | maps[h].erase(itr); |
| 373 | return ret; |
| 374 | } else { |
| 375 | return end(); |
| 376 | } |
| 377 | } |
| 378 | |
Jeff Bolz | 46c0ea0 | 2019-10-09 13:06:29 -0500 | [diff] [blame] | 379 | std::vector<std::pair<const Key, T>> snapshot(std::function<bool(T)> f = nullptr) const { |
Jeff Bolz | fd3bb24 | 2019-08-22 06:10:49 -0500 | [diff] [blame] | 380 | std::vector<std::pair<const Key, T>> ret; |
| 381 | for (int h = 0; h < BUCKETS; ++h) { |
| 382 | read_lock_guard_t lock(locks[h].lock); |
| 383 | for (auto j : maps[h]) { |
| 384 | if (!f || f(j.second)) { |
| 385 | ret.push_back(j); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | return ret; |
| 390 | } |
| 391 | |
Petr Kraus | 4ed81e3 | 2019-09-02 23:41:19 +0200 | [diff] [blame] | 392 | private: |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 393 | static const int BUCKETS = (1 << BUCKETSLOG2); |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 394 | |
Jeff Bolz | b351411 | 2019-08-23 21:46:18 -0500 | [diff] [blame] | 395 | std::unordered_map<Key, T, Hash> maps[BUCKETS]; |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 396 | struct { |
Jeff Bolz | caeccc7 | 2019-10-15 15:35:26 -0500 | [diff] [blame] | 397 | mutable ReadWriteLock lock; |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 398 | // Put each lock on its own cache line to avoid false cache line sharing. |
Jeff Bolz | caeccc7 | 2019-10-15 15:35:26 -0500 | [diff] [blame] | 399 | char padding[(-int(sizeof(ReadWriteLock))) & 63]; |
Jeff Bolz | 89b9a50 | 2019-08-20 08:58:51 -0500 | [diff] [blame] | 400 | } locks[BUCKETS]; |
| 401 | |
| 402 | uint32_t ConcurrentMapHashObject(const Key &object) const { |
| 403 | uint64_t u64 = (uint64_t)(uintptr_t)object; |
| 404 | uint32_t hash = (uint32_t)(u64 >> 32) + (uint32_t)u64; |
| 405 | hash ^= (hash >> BUCKETSLOG2) ^ (hash >> (2 * BUCKETSLOG2)); |
| 406 | hash &= (BUCKETS - 1); |
| 407 | return hash; |
| 408 | } |
| 409 | }; |