blob: f05fa40d262de81c424e3e1dc30e3d494aac1984 [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
ziga-lunargb65dbfb2022-03-19 18:45:09 +0100208static inline bool IsImageLayoutReadOnly(VkImageLayout layout) {
209 constexpr std::array<VkImageLayout, 7> read_only_layouts = {
210 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
211 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
212 VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL,
213 VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL,
214 VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL,
215 VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL,
216 VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL,
217 };
218 return std::any_of(read_only_layouts.begin(), read_only_layouts.end(),
219 [layout](const VkImageLayout read_only_layout) { return layout == read_only_layout; });
220}
221
ziga-lunarge23f82f2022-05-12 16:57:01 +0200222static inline bool IsImageLayoutDepthReadOnly(VkImageLayout layout) {
223 constexpr std::array<VkImageLayout, 7> read_only_layouts = {
224 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
225 VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL,
226 VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL,
227 VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL,
228 };
229 return std::any_of(read_only_layouts.begin(), read_only_layouts.end(),
230 [layout](const VkImageLayout read_only_layout) { return layout == read_only_layout; });
231}
232
233static inline bool IsImageLayoutStencilReadOnly(VkImageLayout layout) {
234 constexpr std::array<VkImageLayout, 7> read_only_layouts = {
235 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
236 VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL,
237 VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL,
238 VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL,
239 };
240 return std::any_of(read_only_layouts.begin(), read_only_layouts.end(),
241 [layout](const VkImageLayout read_only_layout) { return layout == read_only_layout; });
242}
243
sfricke-samsungbd0e8052020-06-06 01:36:39 -0700244static inline bool IsIdentitySwizzle(VkComponentMapping components) {
245 // clang-format off
246 return (
247 ((components.r == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.r == VK_COMPONENT_SWIZZLE_R)) &&
248 ((components.g == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.g == VK_COMPONENT_SWIZZLE_G)) &&
249 ((components.b == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.b == VK_COMPONENT_SWIZZLE_B)) &&
250 ((components.a == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.a == VK_COMPONENT_SWIZZLE_A))
251 );
252 // clang-format on
253}
254
sfricke-samsungdcb31412021-08-29 22:16:34 -0700255static inline VkDeviceSize GetIndexAlignment(VkIndexType indexType) {
256 switch (indexType) {
257 case VK_INDEX_TYPE_UINT16:
258 return 2;
259 case VK_INDEX_TYPE_UINT32:
260 return 4;
261 case VK_INDEX_TYPE_UINT8_EXT:
262 return 1;
263 default:
264 // Not a real index type. Express no alignment requirement here; we expect upper layer
265 // to have already picked up on the enum being nonsense.
266 return 1;
267 }
268}
269
sfricke-samsunge3086292021-11-18 23:02:35 -0800270static inline uint32_t GetPlaneIndex(VkImageAspectFlags aspect) {
271 // Returns an out of bounds index on error
272 switch (aspect) {
273 case VK_IMAGE_ASPECT_PLANE_0_BIT:
274 return 0;
275 break;
276 case VK_IMAGE_ASPECT_PLANE_1_BIT:
277 return 1;
278 break;
279 case VK_IMAGE_ASPECT_PLANE_2_BIT:
280 return 2;
281 break;
282 default:
283 // If more than one plane bit is set, return error condition
284 return FORMAT_MAX_PLANES;
285 break;
286 }
287}
288
sfricke-samsunged028b02021-09-06 23:14:51 -0700289// Perform a zero-tolerant modulo operation
290static inline VkDeviceSize SafeModulo(VkDeviceSize dividend, VkDeviceSize divisor) {
291 VkDeviceSize result = 0;
292 if (divisor != 0) {
293 result = dividend % divisor;
294 }
295 return result;
296}
297
298static inline VkDeviceSize SafeDivision(VkDeviceSize dividend, VkDeviceSize divisor) {
299 VkDeviceSize result = 0;
300 if (divisor != 0) {
301 result = dividend / divisor;
302 }
303 return result;
304}
305
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -0600306extern "C" {
307#endif
308
Jon Ashburnd883d812016-03-24 08:32:09 -0600309#define VK_LAYER_API_VERSION VK_MAKE_VERSION(1, 0, VK_HEADER_VERSION)
Mark Lobodzinskiadaac9d2016-01-08 11:07:56 -0700310
Mark Lobodzinskia9f33492016-01-11 14:17:05 -0700311typedef enum VkStringErrorFlagBits {
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700312 VK_STRING_ERROR_NONE = 0x00000000,
313 VK_STRING_ERROR_LENGTH = 0x00000001,
314 VK_STRING_ERROR_BAD_DATA = 0x00000002,
Mark Lobodzinskia9f33492016-01-11 14:17:05 -0700315} VkStringErrorFlagBits;
316typedef VkFlags VkStringErrorFlags;
Mark Lobodzinski1ed594e2016-02-03 09:57:14 -0700317
Petr Kraus4ed81e32019-09-02 23:41:19 +0200318VK_LAYER_EXPORT void layer_debug_report_actions(debug_report_data *report_data, const VkAllocationCallbacks *pAllocator,
319 const char *layer_identifier);
Mark Young6ba8abe2017-11-09 10:37:04 -0700320
Petr Kraus4ed81e32019-09-02 23:41:19 +0200321VK_LAYER_EXPORT void layer_debug_messenger_actions(debug_report_data *report_data, const VkAllocationCallbacks *pAllocator,
322 const char *layer_identifier);
Mark Lobodzinski1079e1b2016-03-15 14:21:59 -0600323
Mike Stroyana551bc02016-09-28 09:42:28 -0600324VK_LAYER_EXPORT VkStringErrorFlags vk_string_validate(const int max_length, const char *char_array);
Mark Lobodzinskia0555012018-08-15 16:43:49 -0600325VK_LAYER_EXPORT bool white_list(const char *item, const std::set<std::string> &whitelist);
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -0600326
Jon Ashburn5484e0c2016-03-08 17:48:44 -0700327static inline int u_ffs(int val) {
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600328#ifdef WIN32
Mark Lobodzinski5ddf6c32015-12-16 17:47:28 -0700329 unsigned long bit_pos = 0;
Mike Stroyandebb9842016-01-07 10:05:21 -0700330 if (_BitScanForward(&bit_pos, val) != 0) {
Mark Lobodzinski5ddf6c32015-12-16 17:47:28 -0700331 bit_pos += 1;
332 }
333 return bit_pos;
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600334#else
Mark Lobodzinski5ddf6c32015-12-16 17:47:28 -0700335 return ffs(val);
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600336#endif
337}
Mark Lobodzinski6f2274e2015-09-22 09:33:21 -0600338
339#ifdef __cplusplus
340}
341#endif
Jeff Bolz89b9a502019-08-20 08:58:51 -0500342
Jeremy Gebben32811462021-09-20 12:54:20 -0600343#ifdef __cplusplus
Jeremy Gebbena7c3a352021-09-20 12:54:20 -0600344// clang sets _MSC_VER to 1800 and _MSC_FULL_VER to 180000000, but we only want to clean up after MSVC.
345#if defined(_MSC_FULL_VER) && !defined(__clang__)
Bruce Dawson5fab7f82020-06-09 16:23:07 -0700346// Minimum Visual Studio 2015 Update 2, or libc++ with C++17
Jeremy Gebbena7c3a352021-09-20 12:54:20 -0600347// But, before Visual Studio 2017 version 15.7, __cplusplus is not set
348// correctly. See:
349// https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-160
350// Also, according to commit e2a6c442cb1e4, SDKs older than NTDDI_WIN10_RS2 do not
351// support shared_mutex.
352#if _MSC_FULL_VER >= 190023918 && NTDDI_VERSION > NTDDI_WIN10_RS2 && (!defined(_LIBCPP_VERSION) || __cplusplus >= 201703)
Jeremy Gebben32811462021-09-20 12:54:20 -0600353#define VVL_USE_SHARED_MUTEX 1
354#endif
355#elif __cplusplus >= 201703
356#define VVL_USE_SHARED_MUTEX 1
Jeremy Gebben7891d762021-10-11 17:17:58 -0600357#elif __cplusplus >= 201402
358#define VVL_USE_SHARED_TIMED_MUTEX 1
Jeremy Gebben32811462021-09-20 12:54:20 -0600359#endif
360
Jeremy Gebben7891d762021-10-11 17:17:58 -0600361#if defined(VVL_USE_SHARED_MUTEX) || defined(VVL_USE_SHARED_TIMED_MUTEX)
Jeff Bolz89b9a502019-08-20 08:58:51 -0500362#include <shared_mutex>
363#endif
364
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500365class ReadWriteLock {
366 private:
Jeremy Gebben32811462021-09-20 12:54:20 -0600367#if defined(VVL_USE_SHARED_MUTEX)
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600368 typedef std::shared_mutex Lock;
Jeremy Gebben7891d762021-10-11 17:17:58 -0600369#elif defined(VVL_USE_SHARED_TIMED_MUTEX)
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600370 typedef std::shared_timed_mutex Lock;
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500371#else
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600372 typedef std::mutex Lock;
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500373#endif
374
375 public:
376 void lock() { m_lock.lock(); }
377 bool try_lock() { return m_lock.try_lock(); }
378 void unlock() { m_lock.unlock(); }
Jeremy Gebben7891d762021-10-11 17:17:58 -0600379#if defined(VVL_USE_SHARED_MUTEX) || defined(VVL_USE_SHARED_TIMED_MUTEX)
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500380 void lock_shared() { m_lock.lock_shared(); }
381 bool try_lock_shared() { return m_lock.try_lock_shared(); }
382 void unlock_shared() { m_lock.unlock_shared(); }
383#else
384 void lock_shared() { lock(); }
385 bool try_lock_shared() { return try_lock(); }
386 void unlock_shared() { unlock(); }
387#endif
388 private:
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600389 Lock m_lock;
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500390};
391
Jeremy Gebben7891d762021-10-11 17:17:58 -0600392#if defined(VVL_USE_SHARED_MUTEX) || defined(VVL_USE_SHARED_TIMED_MUTEX)
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600393typedef std::shared_lock<ReadWriteLock> ReadLockGuard;
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500394#else
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600395typedef std::unique_lock<ReadWriteLock> ReadLockGuard;
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500396#endif
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600397typedef std::unique_lock<ReadWriteLock> WriteLockGuard;
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500398
Jeremy Gebben332d4dd2022-01-01 12:40:02 -0700399// helper class for the very common case of getting and then locking a command buffer (or other state object)
400template <typename T, typename Guard>
401class LockedSharedPtr : public std::shared_ptr<T> {
402 public:
403 LockedSharedPtr(std::shared_ptr<T> &&ptr, Guard &&guard) : std::shared_ptr<T>(std::move(ptr)), guard_(std::move(guard)) {}
404 LockedSharedPtr() : std::shared_ptr<T>(), guard_() {}
405
406 private:
407 Guard guard_;
408};
409
Jeff Bolz89b9a502019-08-20 08:58:51 -0500410// Limited concurrent_unordered_map that supports internally-synchronized
411// insert/erase/access. Splits locking across N buckets and uses shared_mutex
412// for read/write locking. Iterators are not supported. The following
413// operations are supported:
414//
415// insert_or_assign: Insert a new element or update an existing element.
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500416// insert: Insert a new element and return whether it was inserted.
Jeff Bolz89b9a502019-08-20 08:58:51 -0500417// erase: Remove an element.
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500418// contains: Returns true if the key is in the map.
Jeff Bolz89b9a502019-08-20 08:58:51 -0500419// find: Returns != end() if found, value is in ret->second.
420// pop: Erases and returns the erased value if found.
421//
422// find/end: find returns a vaguely iterator-like type that can be compared to
423// end and can use iter->second to retrieve the reference. This is to ease porting
424// for existing code that combines the existence check and lookup in a single
425// operation (and thus a single lock). i.e.:
426//
427// auto iter = map.find(key);
428// if (iter != map.end()) {
429// T t = iter->second;
430// ...
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500431//
432// snapshot: Return an array of elements (key, value pairs) that satisfy an optional
433// predicate. This can be used as a substitute for iterators in exceptional cases.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700434template <typename Key, typename T, int BUCKETSLOG2 = 2, typename Hash = layer_data::hash<Key>>
Jeff Bolz89b9a502019-08-20 08:58:51 -0500435class vl_concurrent_unordered_map {
Petr Kraus4ed81e32019-09-02 23:41:19 +0200436 public:
aitor-lunarg73e2de32022-03-23 20:44:00 +0100437 template <typename... Args>
438 void insert_or_assign(const Key &key, Args &&...args) {
Jeff Bolz89b9a502019-08-20 08:58:51 -0500439 uint32_t h = ConcurrentMapHashObject(key);
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600440 WriteLockGuard lock(locks[h].lock);
aitor-lunarg73e2de32022-03-23 20:44:00 +0100441 maps[h][key] = {std::forward<Args>(args)...};
Jeff Bolz89b9a502019-08-20 08:58:51 -0500442 }
443
aitor-lunarg73e2de32022-03-23 20:44:00 +0100444 template <typename... Args>
445 bool insert(const Key &key, Args &&...args) {
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500446 uint32_t h = ConcurrentMapHashObject(key);
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600447 WriteLockGuard lock(locks[h].lock);
aitor-lunarg73e2de32022-03-23 20:44:00 +0100448 auto ret = maps[h].emplace(key, std::forward<Args>(args)...);
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500449 return ret.second;
450 }
451
Jeff Bolz89b9a502019-08-20 08:58:51 -0500452 // returns size_type
453 size_t erase(const Key &key) {
454 uint32_t h = ConcurrentMapHashObject(key);
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600455 WriteLockGuard lock(locks[h].lock);
Jeff Bolz89b9a502019-08-20 08:58:51 -0500456 return maps[h].erase(key);
457 }
458
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500459 bool contains(const Key &key) const {
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500460 uint32_t h = ConcurrentMapHashObject(key);
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600461 ReadLockGuard lock(locks[h].lock);
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500462 return maps[h].count(key) != 0;
463 }
464
Jeff Bolz89b9a502019-08-20 08:58:51 -0500465 // type returned by find() and end().
466 class FindResult {
Petr Kraus4ed81e32019-09-02 23:41:19 +0200467 public:
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500468 FindResult(bool a, T b) : result(a, std::move(b)) {}
Jeff Bolz89b9a502019-08-20 08:58:51 -0500469
470 // == and != only support comparing against end()
471 bool operator==(const FindResult &other) const {
472 if (result.first == false && other.result.first == false) {
473 return true;
474 }
475 return false;
476 }
477 bool operator!=(const FindResult &other) const { return !(*this == other); }
478
479 // Make -> act kind of like an iterator.
480 std::pair<bool, T> *operator->() { return &result; }
481 const std::pair<bool, T> *operator->() const { return &result; }
482
Petr Kraus4ed81e32019-09-02 23:41:19 +0200483 private:
Jeff Bolz89b9a502019-08-20 08:58:51 -0500484 // (found, reference to element)
485 std::pair<bool, T> result;
486 };
487
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500488 // find()/end() return a FindResult containing a copy of the value. For end(),
489 // return a default value.
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500490 FindResult end() const { return FindResult(false, T()); }
Jeremy Gebben51499ca2022-01-11 08:36:13 -0700491 FindResult cend() const { return end(); }
Jeff Bolz89b9a502019-08-20 08:58:51 -0500492
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500493 FindResult find(const Key &key) const {
Jeff Bolz89b9a502019-08-20 08:58:51 -0500494 uint32_t h = ConcurrentMapHashObject(key);
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600495 ReadLockGuard lock(locks[h].lock);
Jeff Bolz89b9a502019-08-20 08:58:51 -0500496
497 auto itr = maps[h].find(key);
498 bool found = itr != maps[h].end();
499
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500500 if (found) {
501 return FindResult(true, itr->second);
502 } else {
503 return end();
504 }
Jeff Bolz89b9a502019-08-20 08:58:51 -0500505 }
506
507 FindResult pop(const Key &key) {
508 uint32_t h = ConcurrentMapHashObject(key);
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600509 WriteLockGuard lock(locks[h].lock);
Jeff Bolz89b9a502019-08-20 08:58:51 -0500510
511 auto itr = maps[h].find(key);
512 bool found = itr != maps[h].end();
513
514 if (found) {
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500515 auto ret = std::move(FindResult(true, itr->second));
Jeff Bolz89b9a502019-08-20 08:58:51 -0500516 maps[h].erase(itr);
517 return ret;
518 } else {
519 return end();
520 }
521 }
522
Jeff Bolz46c0ea02019-10-09 13:06:29 -0500523 std::vector<std::pair<const Key, T>> snapshot(std::function<bool(T)> f = nullptr) const {
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500524 std::vector<std::pair<const Key, T>> ret;
525 for (int h = 0; h < BUCKETS; ++h) {
Jeremy Gebben2e5b41b2021-10-11 16:41:49 -0600526 ReadLockGuard lock(locks[h].lock);
John Zulauf79f06582021-02-27 18:38:39 -0700527 for (const auto &j : maps[h]) {
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500528 if (!f || f(j.second)) {
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700529 ret.emplace_back(j.first, j.second);
Jeff Bolzfd3bb242019-08-22 06:10:49 -0500530 }
531 }
532 }
533 return ret;
534 }
535
Jeremy Gebben65975ed2021-10-29 11:16:10 -0600536 void clear() {
537 for (int h = 0; h < BUCKETS; ++h) {
Jeremy Gebbene406ff12022-01-07 10:34:40 -0700538 WriteLockGuard lock(locks[h].lock);
Jeremy Gebben65975ed2021-10-29 11:16:10 -0600539 maps[h].clear();
540 }
541 }
542
543 size_t size() const {
544 size_t result = 0;
545 for (int h = 0; h < BUCKETS; ++h) {
546 ReadLockGuard lock(locks[h].lock);
547 result += maps[h].size();
548 }
549 return result;
550 }
551
552 bool empty() const {
553 bool result = 0;
554 for (int h = 0; h < BUCKETS; ++h) {
555 ReadLockGuard lock(locks[h].lock);
556 result |= maps[h].empty();
557 }
558 return result;
559 }
560
Petr Kraus4ed81e32019-09-02 23:41:19 +0200561 private:
Jeff Bolz89b9a502019-08-20 08:58:51 -0500562 static const int BUCKETS = (1 << BUCKETSLOG2);
Jeff Bolz89b9a502019-08-20 08:58:51 -0500563
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700564 layer_data::unordered_map<Key, T, Hash> maps[BUCKETS];
Jeff Bolz89b9a502019-08-20 08:58:51 -0500565 struct {
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500566 mutable ReadWriteLock lock;
Jeff Bolz89b9a502019-08-20 08:58:51 -0500567 // Put each lock on its own cache line to avoid false cache line sharing.
Jeff Bolzcaeccc72019-10-15 15:35:26 -0500568 char padding[(-int(sizeof(ReadWriteLock))) & 63];
Jeff Bolz89b9a502019-08-20 08:58:51 -0500569 } locks[BUCKETS];
570
571 uint32_t ConcurrentMapHashObject(const Key &object) const {
572 uint64_t u64 = (uint64_t)(uintptr_t)object;
573 uint32_t hash = (uint32_t)(u64 >> 32) + (uint32_t)u64;
574 hash ^= (hash >> BUCKETSLOG2) ^ (hash >> (2 * BUCKETSLOG2));
575 hash &= (BUCKETS - 1);
576 return hash;
577 }
578};
Jeremy Gebben32811462021-09-20 12:54:20 -0600579#endif