···11+// Copyright 2023, Collabora, Ltd.
22+// SPDX-License-Identifier: BSL-1.0
33+/*!
44+ * @file
55+ * @brief A very simple generator to create process unique ids.
66+ * @author Jakob Bornecrantz <jakob@collabora.com>
77+ * @ingroup aux_util
88+ */
99+1010+#include "u_limited_unique_id.h"
1111+1212+#include <atomic>
1313+1414+1515+/*
1616+ * This code is in C++ and not C because MSVC doesn't implement C atomics yet,
1717+ * but to make things even more fun the C++11 standard defines atomic_uint64_t
1818+ * as optional, but atomic_uint_fast64_t is listed as required, so here we are.
1919+ */
2020+static std::atomic_uint_fast64_t generator;
2121+2222+extern "C" xrt_limited_unique_id_t
2323+u_limited_unique_id_get(void)
2424+{
2525+ return xrt_limited_unique_id_t{static_cast<uint64_t>(++generator)};
2626+}
+40
src/xrt/auxiliary/util/u_limited_unique_id.h
···11+// Copyright 2023, Collabora, Ltd.
22+// SPDX-License-Identifier: BSL-1.0
33+/*!
44+ * @file
55+ * @brief A very simple generator to create process unique ids.
66+ * @author Jakob Bornecrantz <jakob@collabora.com>
77+ * @ingroup aux_util
88+ */
99+1010+1111+#pragma once
1212+1313+#include "xrt/xrt_defines.h"
1414+1515+1616+#ifdef __cplusplus
1717+extern "C" {
1818+#endif
1919+2020+2121+/*!
2222+ * This function returns a unsigned 64 bit value that is guaranteed to be unique
2323+ * within the current running process, and not zero. There is of course the
2424+ * limit of running out of those ID once all values has been returned, but the
2525+ * value is 64 bit so that should not be a practical limit. The value is useful
2626+ * when needing to implement caching of a complex object, this lets us not use
2727+ * memory addresses as keys which may be reused by underlying alloc
2828+ * implementation and could lead to false hits.
2929+ *
3030+ * The current implementation is naive and is a simple monotonic counter.
3131+ *
3232+ * @ingroup aux_util
3333+ */
3434+xrt_limited_unique_id_t
3535+u_limited_unique_id_get(void);
3636+3737+3838+#ifdef __cplusplus
3939+}
4040+#endif