22#include <system_error>
29#define CONDY_DELETE_COPY(cls) \
30 cls(const cls &) = delete; \
31 cls &operator=(const cls &) = delete
33#define CONDY_DELETE_MOVE(cls) \
34 cls(cls &&) = delete; \
35 cls &operator=(cls &&) = delete
38#define CONDY_DELETE_COPY_MOVE(cls) \
39 CONDY_DELETE_COPY(cls); \
40 CONDY_DELETE_MOVE(cls)
42#if defined(__has_feature)
43#if __has_feature(thread_sanitizer)
44#define CONDY_DETAIL_HAS_TSAN
48#if defined(__SANITIZE_THREAD__)
49#define CONDY_DETAIL_HAS_TSAN
52#if defined(CONDY_DETAIL_HAS_TSAN)
54void __tsan_acquire(
void *addr);
55void __tsan_release(
void *addr);
62inline void tsan_acquire([[maybe_unused]]
void *addr)
noexcept {
63#if defined(CONDY_DETAIL_HAS_TSAN)
68inline void tsan_release([[maybe_unused]]
void *addr)
noexcept {
69#if defined(CONDY_DETAIL_HAS_TSAN)
74template <
typename Func>
auto defer(Func &&func) {
75 using F = std::decay_t<Func>;
76 class [[nodiscard]] Defer {
78 Defer(F func) : func_(std::move(func)) {}
81 CONDY_DELETE_COPY_MOVE(Defer);
86 return Defer(std::forward<Func>(func));
89template <typename T, T From = 0, T To = std::numeric_limits<T>::max()>
92 static_assert(From < To,
"Invalid ID range");
95 if (!recycled_ids_.empty()) {
96 T
id = recycled_ids_.top();
103 throw std::runtime_error(
"ID pool exhausted");
106 void recycle(T
id)
noexcept {
107 assert(From <=
id &&
id < next_id_ &&
id < To);
108 recycled_ids_.push(
id);
111 void reset() noexcept {
113 while (!recycled_ids_.empty()) {
120 std::stack<T> recycled_ids_;
123[[noreturn]]
inline void panic_on(std::string_view msg)
noexcept {
124 std::cerr << std::format(
"Panic: {}\n", msg);
129 std::exit(EXIT_FAILURE);
133template <
typename T>
class RawStorage {
135 template <
typename Factory>
136 void accept(Factory &&factory)
noexcept(
137 noexcept(T(std::forward<Factory>(factory)()))) {
138 new (&storage_) T(std::forward<Factory>(factory)());
141 template <
typename... Args>
142 void construct(Args &&...args)
noexcept(
143 std::is_nothrow_constructible_v<T, Args...>) {
144 accept([&]() {
return T(std::forward<Args>(args)...); });
147 T &get() noexcept {
return *std::launder(
reinterpret_cast<T *
>(storage_)); }
149 const T &get() const noexcept {
150 return *std::launder(
reinterpret_cast<const T *
>(storage_));
153 void destroy() noexcept { get().~T(); }
156 alignas(T)
unsigned char storage_[
sizeof(T)];
159template <
typename T,
size_t N>
class SmallArray {
161 SmallArray(
size_t capacity) : capacity_(capacity) {
163 large_ =
new T[capacity];
173 T &operator[](
size_t index)
noexcept {
174 return is_small_() ? small_[index] : large_[index];
177 const T &operator[](
size_t index)
const noexcept {
178 return is_small_() ? small_[index] : large_[index];
181 size_t capacity() const noexcept {
return capacity_; }
184 bool is_small_() const noexcept {
return capacity_ <= N; }
194inline auto make_system_error(std::string_view msg,
int ec) {
195 return std::system_error(ec, std::generic_category(), std::string(msg));
198inline auto make_system_error(std::string_view msg) {
199 return make_system_error(msg, errno);
202#if __cplusplus >= 202302L
203[[noreturn]]
inline void unreachable() { std::unreachable(); }
205[[noreturn]]
inline void unreachable() { __builtin_unreachable(); }
208template <
size_t Idx = 0,
typename... Ts>
209std::variant<Ts...> tuple_at(std::tuple<Ts...> &results,
size_t idx) {
210 if constexpr (Idx <
sizeof...(Ts)) {
212 return std::variant<Ts...>{std::in_place_index<Idx>,
213 std::move(std::get<Idx>(results))};
215 return tuple_at<Idx + 1, Ts...>(results, idx);
222 assert(
false &&
"Index out of bounds");
223 return std::variant<Ts...>{std::in_place_index<0>,
224 std::move(std::get<0>(results))};
226 panic_on(
"Index out of bounds in tuple_at");
231template <
typename T>
inline T align_up(T value, T alignment)
noexcept {
233 assert(alignment > 0 && (alignment & (alignment - 1)) == 0);
234 return (value + alignment - 1) & ~(alignment - 1);
240#undef CONDY_DETAIL_HAS_TSAN
The main namespace for the Condy library.