blob: 53730273fa4b264faf065f9a386922bbd5e1edb1 [file] [log] [blame]
Jeremy Gebben65975ed2021-10-29 11:16:10 -06001/* Copyright (c) 2015-2017, 2019-2022 The Khronos Group Inc.
2 * Copyright (c) 2015-2017, 2019-2022 Valve Corporation
3 * Copyright (c) 2015-2017, 2019-2022 LunarG, Inc.
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -06004 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06005 * 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 Lobodzinski6f2274e2015-09-22 09:33:21 -06008 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060010 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060011 * 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 Lobodzinski6f2274e2015-09-22 09:33:21 -060016 *
Jon Ashburne922f712015-11-03 13:41:23 -070017 * Author: Mark Lobodzinski <mark@lunarg.com>
18 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
Dave Houlton59a20702017-02-02 17:26:23 -070019 * Author: Dave Houlton <daveh@lunarg.com>
Mark Lobodzinski6eda00a2016-02-02 15:55:36 -070020 */
21
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060022#pragma once
John Zulauff05b3772019-04-03 18:04:23 -060023
24#include <cassert>
25#include <cstddef>
26#include <functional>
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060027#include <stdbool.h>
John Zulauf965d88d2018-04-12 15:47:26 -060028#include <string>
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -060029#include <vector>
Mark Lobodzinski44145db2020-08-11 08:01:47 -060030#include <iomanip>
John Zulauf2c2ccd42019-04-05 13:13:13 -060031#include "cast_utils.h"
Dave Houlton3c9fca72017-03-27 17:25:54 -060032#include "vk_format_utils.h"
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -060033#include "vk_layer_logging.h"
34
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -060035#ifndef WIN32
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070036#include <strings.h> // For ffs()
Courtney Goeltzenleuchter3698c622015-10-27 11:23:21 -060037#else
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070038#include <intrin.h> // For __lzcnt()
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -060039#endif
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060040
Petr Krausc3382e92019-12-14 00:41:30 +010041#define STRINGIFY(s) STRINGIFY_HELPER(s)
42#define STRINGIFY_HELPER(s) #s
43
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -060044#ifdef __cplusplus
John Zulauf1507ee42020-05-18 11:33:09 -060045static inline VkExtent3D CastTo3D(const VkExtent2D &d2) {
46 VkExtent3D d3 = {d2.width, d2.height, 1};
47 return d3;
48}
49
50static inline VkOffset3D CastTo3D(const VkOffset2D &d2) {
51 VkOffset3D d3 = {d2.x, d2.y, 0};
52 return d3;
53}
54
Mark Lobodzinski44145db2020-08-11 08:01:47 -060055// Convert integer API version to a string
56static inline std::string StringAPIVersion(uint32_t version) {
57 std::stringstream version_name;
58 uint32_t major = VK_VERSION_MAJOR(version);
59 uint32_t minor = VK_VERSION_MINOR(version);
60 uint32_t patch = VK_VERSION_PATCH(version);
61 version_name << major << "." << minor << "." << patch << " (0x" << std::setfill('0') << std::setw(8) << std::hex << version
62 << ")";
63 return version_name.str();
64}
65
John Zulauf965d88d2018-04-12 15:47:26 -060066// Traits objects to allow string_join to operate on collections of const char *
67template <typename String>
68struct StringJoinSizeTrait {
69 static size_t size(const String &str) { return str.size(); }
70};
71
72template <>
73struct StringJoinSizeTrait<const char *> {
74 static size_t size(const char *str) {
75 if (!str) return 0;
76 return strlen(str);
77 }
78};
79// Similar to perl/python join
80// * String must support size, reserve, append, and be default constructable
81// * StringCollection must support size, const forward iteration, and store
82// strings compatible with String::append
83// * Accessor trait can be set if default accessors (compatible with string
84// and const char *) don't support size(StringCollection::value_type &)
85//
86// Return type based on sep type
87template <typename String = std::string, typename StringCollection = std::vector<String>,
88 typename Accessor = StringJoinSizeTrait<typename StringCollection::value_type>>
89static inline String string_join(const String &sep, const StringCollection &strings) {
90 String joined;
91 const size_t count = strings.size();
92 if (!count) return joined;
93
94 // Prereserved storage, s.t. we will execute in linear time (avoids reallocation copies)
95 size_t reserve = (count - 1) * sep.size();
96 for (const auto &str : strings) {
97 reserve += Accessor::size(str); // abstracted to allow const char * type in StringCollection
98 }
99 joined.reserve(reserve + 1);
100
101 // Seps only occur *between* strings entries, so first is special
102 auto current = strings.cbegin();
103 joined.append(*current);
104 ++current;
105 for (; current != strings.cend(); ++current) {
106 joined.append(sep);
107 joined.append(*current);
108 }
109 return joined;
110}
111
112// Requires StringCollection::value_type has a const char * constructor and is compatible the string_join::String above
113template <typename StringCollection = std::vector<std::string>, typename SepString = std::string>
114static inline SepString string_join(const char *sep, const StringCollection &strings) {
115 return string_join<SepString, StringCollection>(SepString(sep), strings);
116}
117
Petr Kraus168417e2019-09-07 16:45:40 +0200118static inline std::string string_trim(const std::string &s) {
119 const char *whitespace = " \t\f\v\n\r";
120
121 const auto trimmed_beg = s.find_first_not_of(whitespace);
122 if (trimmed_beg == std::string::npos) return "";
123
124 const auto trimmed_end = s.find_last_not_of(whitespace);
125 assert(trimmed_end != std::string::npos && trimmed_beg <= trimmed_end);
126
127 return s.substr(trimmed_beg, trimmed_end - trimmed_beg + 1);
128}
129
John Zulaufdf851b12018-06-12 14:49:04 -0600130// Perl/Python style join operation for general types using stream semantics
131// Note: won't be as fast as string_join above, but simpler to use (and code)
132// Note: Modifiable reference doesn't match the google style but does match std style for stream handling and algorithms
133template <typename Stream, typename String, typename ForwardIt>
134Stream &stream_join(Stream &stream, const String &sep, ForwardIt first, ForwardIt last) {
135 if (first != last) {
136 stream << *first;
137 ++first;
138 while (first != last) {
139 stream << sep << *first;
140 ++first;
141 }
142 }
143 return stream;
144}
145
146// stream_join For whole collections with forward iterators
147template <typename Stream, typename String, typename Collection>
148Stream &stream_join(Stream &stream, const String &sep, const Collection &values) {
149 return stream_join(stream, sep, values.cbegin(), values.cend());
150}
151
Mark Lobodzinskic1b5b882018-06-25 14:54:04 -0600152typedef void *dispatch_key;
153static inline dispatch_key get_dispatch_key(const void *object) { return (dispatch_key) * (VkLayerDispatchTable **)object; }
154
155VK_LAYER_EXPORT VkLayerInstanceCreateInfo *get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func);
156VK_LAYER_EXPORT VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCreateInfo, VkLayerFunction func);
157
Chris Mayer334e72f2018-11-29 14:25:41 +0100158static inline bool IsPowerOfTwo(unsigned x) { return x && !(x & (x - 1)); }
Chris Mayer9bc9d092018-11-12 12:29:10 +0100159
jweinstein2b8e95f2022-03-03 19:23:36 -0800160// Returns the 0-based index of the MSB, like the x86 bit scan reverse (bsr) instruction
161// Note: an input mask of 0 yields -1
162static inline int MostSignificantBit(uint32_t mask) {
163#if defined __GNUC__
164 return mask ? __builtin_clz(mask) ^ 31 : -1;
165#elif defined _MSC_VER
166 unsigned long bit_pos;
167 return _BitScanReverse(&bit_pos, mask) ? int(bit_pos) : -1;
168#else
169 for (int k = 31; k >= 0; --k) {
Aaron Hagan01370922021-11-12 19:22:50 -0500170 if (((mask >> k) & 1) != 0) {
jweinstein2b8e95f2022-03-03 19:23:36 -0800171 return k;
Aaron Hagan01370922021-11-12 19:22:50 -0500172 }
173 }
jweinstein2b8e95f2022-03-03 19:23:36 -0800174 return -1;
175#endif
Aaron Hagan01370922021-11-12 19:22:50 -0500176}
177
sfricke-samsung8f658d42020-05-03 20:12:24 -0700178static inline uint32_t SampleCountSize(VkSampleCountFlagBits sample_count) {
179 uint32_t size = 0;
180 switch (sample_count) {
181 case VK_SAMPLE_COUNT_1_BIT:
182 size = 1;
183 break;
184 case VK_SAMPLE_COUNT_2_BIT:
185 size = 2;
186 break;
187 case VK_SAMPLE_COUNT_4_BIT:
188 size = 4;
189 break;
190 case VK_SAMPLE_COUNT_8_BIT:
191 size = 8;
192 break;
193 case VK_SAMPLE_COUNT_16_BIT:
194 size = 16;
195 break;
196 case VK_SAMPLE_COUNT_32_BIT:
197 size = 32;
198 break;
199 case VK_SAMPLE_COUNT_64_BIT:
200 size = 64;
201 break;
202 default:
203 size = 0;
204 }
205 return size;
206}
207
sfricke-samsungbd0e8052020-06-06 01:36:39 -0700208static inline bool IsIdentitySwizzle(VkComponentMapping components) {
209 // clang-format off
210 return (
211 ((components.r == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.r == VK_COMPONENT_SWIZZLE_R)) &&
212 ((components.g == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.g == VK_COMPONENT_SWIZZLE_G)) &&
213 ((components.b == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.b == VK_COMPONENT_SWIZZLE_B)) &&
214 ((components.a == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.a == VK_COMPONENT_SWIZZLE_A))
215 );
216 // clang-format on
217}
218
sfricke-samsungdcb31412021-08-29 22:16:34 -0700219static inline VkDeviceSize GetIndexAlignment(VkIndexType indexType) {
220 switch (indexType) {
221 case VK_INDEX_TYPE_UINT16:
222 return 2;
223 case VK_INDEX_TYPE_UINT32:
224 return 4;
225 case VK_INDEX_TYPE_UINT8_EXT:
226 return 1;
227 default:
228 // Not a real index type. Express no alignment requirement here; we expect upper layer
229 // to have already picked up on the enum being nonsense.
230 return 1;
231 }
232}
233
sfricke-samsunge3086292021-11-18 23:02:35 -0800234static inline uint32_t GetPlaneIndex(VkImageAspectFlags aspect) {
235 // Returns an out of bounds index on error
236 switch (aspect) {
237 case VK_IMAGE_ASPECT_PLANE_0_BIT:
238 return 0;
239 break;
240 case VK_IMAGE_ASPECT_PLANE_1_BIT:
241 return 1;
242 break;
243 case VK_IMAGE_ASPECT_PLANE_2_BIT:
244 return 2;
245 break;
246 default:
247 // If more than one plane bit is set, return error condition
248 return FORMAT_MAX_PLANES;
249 break;
250 }
251}
252
sfricke-samsunged028b02021-09-06 23:14:51 -0700253// Perform a zero-tolerant modulo operation
254static inline VkDeviceSize SafeModulo(VkDeviceSize dividend, VkDeviceSize divisor) {
255 VkDeviceSize result = 0;
256 if (divisor != 0) {
257 result = dividend % divisor;
258 }
259 return result;
260}
261
262static inline VkDeviceSize SafeDivision(VkDeviceSize dividend, VkDeviceSize divisor) {
263 VkDeviceSize result = 0;
264 if (divisor != 0) {
265 result = dividend / divisor;
266 }
267 return result;
268}
269
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -0600270extern "C" {
271#endif
272
Jon Ashburnd883d812016-03-24 08:32:09 -0600273#define VK_LAYER_API_VERSION VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION)
Mark Lobodzinskiadaac9d2016-01-08 11:07:56 -0700274
Mark Lobodzinskia9f33492016-01-11 14:17:05 -0700275typedef enum VkStringErrorFlagBits {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700276 VK_STRING_ERROR_NONE = 0x00000000,
277 VK_STRING_ERROR_LENGTH = 0x00000001,
278 VK_STRING_ERROR_BAD_DATA = 0x00000002,
Mark Lobodzinskia9f33492016-01-11 14:17:05 -0700279} VkStringErrorFlagBits;
280typedef VkFlags VkStringErrorFlags;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700281
Petr Kraus4ed81e32019-09-02 23:41:19 +0200282VK_LAYER_EXPORT void layer_debug_report_actions(debug_report_data *report_data, const VkAllocationCallbacks *pAllocator,
283 const char *layer_identifier);
Mark Young6ba8abe2017-11-09 10:37:04 -0700284
Petr Kraus4ed81e32019-09-02 23:41:19 +0200285VK_LAYER_EXPORT void layer_debug_messenger_actions(debug_report_data *report_data, const VkAllocationCallbacks *pAllocator,
286 const char *layer_identifier);
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600287
Mike Stroyana551bc02016-09-28 09:42:28 -0600288VK_LAYER_EXPORT VkStringErrorFlags vk_string_validate(const int max_length, const char *char_array);
Mark Lobodzinskia0555012018-08-15 16:43:49 -0600289VK_LAYER_EXPORT bool white_list(const char *item, const std::set<std::string> &whitelist);
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -0600290
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700291static inline int u_ffs(int val) {
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600292#ifdef WIN32
Mark Lobodzinski5ddf6c32015-12-16 17:47:28 -0700293 unsigned long bit_pos = 0;
Mike Stroyandebb9842016-01-07 10:05:21 -0700294 if (_BitScanForward(&bit_pos, val) != 0) {
Mark Lobodzinski5ddf6c32015-12-16 17:47:28 -0700295 bit_pos += 1;
296 }
297 return bit_pos;
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600298#else
Mark Lobodzinski5ddf6c32015-12-16 17:47:28 -0700299 return ffs(val);
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600300#endif
301}
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -0600302
303#ifdef __cplusplus
304}
305#endif
Jeff Bolz89b9a502019-08-20 08:58:51 -0500306
Jeremy Gebben32811462021-09-20 12:54:20 -0600307#ifdef __cplusplus
Jeremy Gebbena7c3a352021-09-20 12:54:20 -0600308// clang sets _MSC_VER to 1800 and _MSC_FULL_VER to 180000000, but we only want to clean up after MSVC.
309#if defined(_MSC_FULL_VER) && !defined(__clang__)
Bruce Dawson5fab7f82020-06-09 16:23:07 -0700310// Minimum Visual Studio 2015 Update 2, or libc++ with C++17
Jeremy Gebbena7c3a352021-09-20 12:54:20 -0600311// But, before Visual Studio 2017 version 15.7, __cplusplus is not set
312// correctly. See:
313// https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-160
314// Also, according to commit e2a6c442cb1e4, SDKs older than NTDDI_WIN10_RS2 do not
315// support shared_mutex.
316#if _MSC_FULL_VER >= 190023918 && NTDDI_VERSION > NTDDI_WIN10_RS2 && (!defined(_LIBCPP_VERSION) || __cplusplus >= 201703)
Jeremy Gebben32811462021-09-20 12:54:20 -0600317#define VVL_USE_SHARED_MUTEX 1
318#endif
319#elif __cplusplus >= 201703
320#define VVL_USE_SHARED_MUTEX 1
Jeremy Gebben7891d762021-10-11 17:17:58 -0600321#elif __cplusplus >= 201402
322#define VVL_USE_SHARED_TIMED_MUTEX 1
Jeremy Gebben32811462021-09-20 12:54:20 -0600323#endif
324
Jeremy Gebben7891d762021-10-11 17:17:58 -0600325#if defined(VVL_USE_SHARED_MUTEX) || defined(VVL_USE_SHARED_TIMED_MUTEX)
Jeff Bolz89b9a502019-08-20 08:58:51 -0500326#include <shared_mutex>
327#endif
328
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500329class ReadWriteLock {
330 private:
Jeremy Gebben32811462021-09-20 12:54:20 -0600331#if defined(VVL_USE_SHARED_MUTEX)
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600332 typedef std::shared_mutex Lock;
Jeremy Gebben7891d762021-10-11 17:17:58 -0600333#elif defined(VVL_USE_SHARED_TIMED_MUTEX)
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600334 typedef std::shared_timed_mutex Lock;
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500335#else
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600336 typedef std::mutex Lock;
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500337#endif
338
339 public:
340 void lock() { m_lock.lock(); }
341 bool try_lock() { return m_lock.try_lock(); }
342 void unlock() { m_lock.unlock(); }
Jeremy Gebben7891d762021-10-11 17:17:58 -0600343#if defined(VVL_USE_SHARED_MUTEX) || defined(VVL_USE_SHARED_TIMED_MUTEX)
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500344 void lock_shared() { m_lock.lock_shared(); }
345 bool try_lock_shared() { return m_lock.try_lock_shared(); }
346 void unlock_shared() { m_lock.unlock_shared(); }
347#else
348 void lock_shared() { lock(); }
349 bool try_lock_shared() { return try_lock(); }
350 void unlock_shared() { unlock(); }
351#endif
352 private:
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600353 Lock m_lock;
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500354};
355
Jeremy Gebben7891d762021-10-11 17:17:58 -0600356#if defined(VVL_USE_SHARED_MUTEX) || defined(VVL_USE_SHARED_TIMED_MUTEX)
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600357typedef std::shared_lock<ReadWriteLock> ReadLockGuard;
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500358#else
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600359typedef std::unique_lock<ReadWriteLock> ReadLockGuard;
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500360#endif
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600361typedef std::unique_lock<ReadWriteLock> WriteLockGuard;
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500362
Jeremy Gebben332d4dd2022-01-01 12:40:02 -0700363// helper class for the very common case of getting and then locking a command buffer (or other state object)
364template <typename T, typename Guard>
365class LockedSharedPtr : public std::shared_ptr<T> {
366 public:
367 LockedSharedPtr(std::shared_ptr<T> &&ptr, Guard &&guard) : std::shared_ptr<T>(std::move(ptr)), guard_(std::move(guard)) {}
368 LockedSharedPtr() : std::shared_ptr<T>(), guard_() {}
369
370 private:
371 Guard guard_;
372};
373
Jeff Bolz89b9a502019-08-20 08:58:51 -0500374// Limited concurrent_unordered_map that supports internally-synchronized
375// insert/erase/access. Splits locking across N buckets and uses shared_mutex
376// for read/write locking. Iterators are not supported. The following
377// operations are supported:
378//
379// insert_or_assign: Insert a new element or update an existing element.
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500380// insert: Insert a new element and return whether it was inserted.
Jeff Bolz89b9a502019-08-20 08:58:51 -0500381// erase: Remove an element.
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500382// contains: Returns true if the key is in the map.
Jeff Bolz89b9a502019-08-20 08:58:51 -0500383// find: Returns != end() if found, value is in ret->second.
384// pop: Erases and returns the erased value if found.
385//
386// find/end: find returns a vaguely iterator-like type that can be compared to
387// end and can use iter->second to retrieve the reference. This is to ease porting
388// for existing code that combines the existence check and lookup in a single
389// operation (and thus a single lock). i.e.:
390//
391// auto iter = map.find(key);
392// if (iter != map.end()) {
393// T t = iter->second;
394// ...
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500395//
396// snapshot: Return an array of elements (key, value pairs) that satisfy an optional
397// predicate. This can be used as a substitute for iterators in exceptional cases.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700398template <typename Key, typename T, int BUCKETSLOG2 = 2, typename Hash = layer_data::hash<Key>>
Jeff Bolz89b9a502019-08-20 08:58:51 -0500399class vl_concurrent_unordered_map {
Petr Kraus4ed81e32019-09-02 23:41:19 +0200400 public:
Jeff Bolz89b9a502019-08-20 08:58:51 -0500401 void insert_or_assign(const Key &key, const T &value) {
402 uint32_t h = ConcurrentMapHashObject(key);
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600403 WriteLockGuard lock(locks[h].lock);
Jeff Bolz89b9a502019-08-20 08:58:51 -0500404 maps[h][key] = value;
405 }
406
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500407 bool insert(const Key &key, const T &value) {
408 uint32_t h = ConcurrentMapHashObject(key);
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600409 WriteLockGuard lock(locks[h].lock);
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700410 auto ret = maps[h].emplace(key, value);
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500411 return ret.second;
412 }
413
Jeff Bolz89b9a502019-08-20 08:58:51 -0500414 // returns size_type
415 size_t erase(const Key &key) {
416 uint32_t h = ConcurrentMapHashObject(key);
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600417 WriteLockGuard lock(locks[h].lock);
Jeff Bolz89b9a502019-08-20 08:58:51 -0500418 return maps[h].erase(key);
419 }
420
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500421 bool contains(const Key &key) const {
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500422 uint32_t h = ConcurrentMapHashObject(key);
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600423 ReadLockGuard lock(locks[h].lock);
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500424 return maps[h].count(key) != 0;
425 }
426
Jeff Bolz89b9a502019-08-20 08:58:51 -0500427 // type returned by find() and end().
428 class FindResult {
Petr Kraus4ed81e32019-09-02 23:41:19 +0200429 public:
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500430 FindResult(bool a, T b) : result(a, std::move(b)) {}
Jeff Bolz89b9a502019-08-20 08:58:51 -0500431
432 // == and != only support comparing against end()
433 bool operator==(const FindResult &other) const {
434 if (result.first == false && other.result.first == false) {
435 return true;
436 }
437 return false;
438 }
439 bool operator!=(const FindResult &other) const { return !(*this == other); }
440
441 // Make -> act kind of like an iterator.
442 std::pair<bool, T> *operator->() { return &result; }
443 const std::pair<bool, T> *operator->() const { return &result; }
444
Petr Kraus4ed81e32019-09-02 23:41:19 +0200445 private:
Jeff Bolz89b9a502019-08-20 08:58:51 -0500446 // (found, reference to element)
447 std::pair<bool, T> result;
448 };
449
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500450 // find()/end() return a FindResult containing a copy of the value. For end(),
451 // return a default value.
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500452 FindResult end() const { return FindResult(false, T()); }
Jeremy Gebben51499ca2022-01-11 08:36:13 -0700453 FindResult cend() const { return end(); }
Jeff Bolz89b9a502019-08-20 08:58:51 -0500454
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500455 FindResult find(const Key &key) const {
Jeff Bolz89b9a502019-08-20 08:58:51 -0500456 uint32_t h = ConcurrentMapHashObject(key);
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600457 ReadLockGuard lock(locks[h].lock);
Jeff Bolz89b9a502019-08-20 08:58:51 -0500458
459 auto itr = maps[h].find(key);
460 bool found = itr != maps[h].end();
461
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500462 if (found) {
463 return FindResult(true, itr->second);
464 } else {
465 return end();
466 }
Jeff Bolz89b9a502019-08-20 08:58:51 -0500467 }
468
469 FindResult pop(const Key &key) {
470 uint32_t h = ConcurrentMapHashObject(key);
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600471 WriteLockGuard lock(locks[h].lock);
Jeff Bolz89b9a502019-08-20 08:58:51 -0500472
473 auto itr = maps[h].find(key);
474 bool found = itr != maps[h].end();
475
476 if (found) {
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500477 auto ret = std::move(FindResult(true, itr->second));
Jeff Bolz89b9a502019-08-20 08:58:51 -0500478 maps[h].erase(itr);
479 return ret;
480 } else {
481 return end();
482 }
483 }
484
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500485 std::vector<std::pair<const Key, T>> snapshot(std::function<bool(T)> f = nullptr) const {
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500486 std::vector<std::pair<const Key, T>> ret;
487 for (int h = 0; h < BUCKETS; ++h) {
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600488 ReadLockGuard lock(locks[h].lock);
John Zulauf79f06582021-02-27 18:38:39 -0700489 for (const auto &j : maps[h]) {
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500490 if (!f || f(j.second)) {
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700491 ret.emplace_back(j.first, j.second);
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500492 }
493 }
494 }
495 return ret;
496 }
497
Jeremy Gebben65975ed2021-10-29 11:16:10 -0600498 void clear() {
499 for (int h = 0; h < BUCKETS; ++h) {
Jeremy Gebbene406ff12022-01-07 10:34:40 -0700500 WriteLockGuard lock(locks[h].lock);
Jeremy Gebben65975ed2021-10-29 11:16:10 -0600501 maps[h].clear();
502 }
503 }
504
505 size_t size() const {
506 size_t result = 0;
507 for (int h = 0; h < BUCKETS; ++h) {
508 ReadLockGuard lock(locks[h].lock);
509 result += maps[h].size();
510 }
511 return result;
512 }
513
514 bool empty() const {
515 bool result = 0;
516 for (int h = 0; h < BUCKETS; ++h) {
517 ReadLockGuard lock(locks[h].lock);
518 result |= maps[h].empty();
519 }
520 return result;
521 }
522
Petr Kraus4ed81e32019-09-02 23:41:19 +0200523 private:
Jeff Bolz89b9a502019-08-20 08:58:51 -0500524 static const int BUCKETS = (1 << BUCKETSLOG2);
Jeff Bolz89b9a502019-08-20 08:58:51 -0500525
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700526 layer_data::unordered_map<Key, T, Hash> maps[BUCKETS];
Jeff Bolz89b9a502019-08-20 08:58:51 -0500527 struct {
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500528 mutable ReadWriteLock lock;
Jeff Bolz89b9a502019-08-20 08:58:51 -0500529 // Put each lock on its own cache line to avoid false cache line sharing.
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500530 char padding[(-int(sizeof(ReadWriteLock))) & 63];
Jeff Bolz89b9a502019-08-20 08:58:51 -0500531 } locks[BUCKETS];
532
533 uint32_t ConcurrentMapHashObject(const Key &object) const {
534 uint64_t u64 = (uint64_t)(uintptr_t)object;
535 uint32_t hash = (uint32_t)(u64 >> 32) + (uint32_t)u64;
536 hash ^= (hash >> BUCKETSLOG2) ^ (hash >> (2 * BUCKETSLOG2));
537 hash &= (BUCKETS - 1);
538 return hash;
539 }
540};
Jeremy Gebben32811462021-09-20 12:54:20 -0600541#endif