The open source OpenXR runtime

u/limited_unique_id: Add process unique id generator

+68
+2
src/xrt/auxiliary/util/CMakeLists.txt
··· 59 59 u_json.c 60 60 u_json.h 61 61 u_json.hpp 62 + u_limited_unique_id.cpp 63 + u_limited_unique_id.h 62 64 u_logging.c 63 65 u_logging.h 64 66 u_metrics.c
+26
src/xrt/auxiliary/util/u_limited_unique_id.cpp
··· 1 + // Copyright 2023, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief A very simple generator to create process unique ids. 6 + * @author Jakob Bornecrantz <jakob@collabora.com> 7 + * @ingroup aux_util 8 + */ 9 + 10 + #include "u_limited_unique_id.h" 11 + 12 + #include <atomic> 13 + 14 + 15 + /* 16 + * This code is in C++ and not C because MSVC doesn't implement C atomics yet, 17 + * but to make things even more fun the C++11 standard defines atomic_uint64_t 18 + * as optional, but atomic_uint_fast64_t is listed as required, so here we are. 19 + */ 20 + static std::atomic_uint_fast64_t generator; 21 + 22 + extern "C" xrt_limited_unique_id_t 23 + u_limited_unique_id_get(void) 24 + { 25 + return xrt_limited_unique_id_t{static_cast<uint64_t>(++generator)}; 26 + }
+40
src/xrt/auxiliary/util/u_limited_unique_id.h
··· 1 + // Copyright 2023, Collabora, Ltd. 2 + // SPDX-License-Identifier: BSL-1.0 3 + /*! 4 + * @file 5 + * @brief A very simple generator to create process unique ids. 6 + * @author Jakob Bornecrantz <jakob@collabora.com> 7 + * @ingroup aux_util 8 + */ 9 + 10 + 11 + #pragma once 12 + 13 + #include "xrt/xrt_defines.h" 14 + 15 + 16 + #ifdef __cplusplus 17 + extern "C" { 18 + #endif 19 + 20 + 21 + /*! 22 + * This function returns a unsigned 64 bit value that is guaranteed to be unique 23 + * within the current running process, and not zero. There is of course the 24 + * limit of running out of those ID once all values has been returned, but the 25 + * value is 64 bit so that should not be a practical limit. The value is useful 26 + * when needing to implement caching of a complex object, this lets us not use 27 + * memory addresses as keys which may be reused by underlying alloc 28 + * implementation and could lead to false hits. 29 + * 30 + * The current implementation is naive and is a simple monotonic counter. 31 + * 32 + * @ingroup aux_util 33 + */ 34 + xrt_limited_unique_id_t 35 + u_limited_unique_id_get(void); 36 + 37 + 38 + #ifdef __cplusplus 39 + } 40 + #endif