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);
61inline void tsan_acquire([[maybe_unused]]
void *addr)
noexcept {
62#if defined(CONDY_DETAIL_HAS_TSAN)
67inline void tsan_release([[maybe_unused]]
void *addr)
noexcept {
68#if defined(CONDY_DETAIL_HAS_TSAN)
75#undef CONDY_DETAIL_HAS_TSAN
79template <
typename Func>
class [[nodiscard]] Defer {
81 Defer(Func func) : func_(std::move(func)) {}
87 CONDY_DELETE_COPY_MOVE(Defer);
90 void dismiss() noexcept { active_ =
false; }
103template <
typename Func>
auto defer(Func &&func) {
104 return Defer<std::decay_t<Func>>(std::forward<Func>(func));
107[[noreturn]]
inline void panic_on(std::string_view msg)
noexcept {
108 std::cerr << std::format(
"Panic: {}\n", msg);
113 std::exit(EXIT_FAILURE);
117template <
typename T>
class RawStorage {
119 template <
typename Factory>
120 void accept(Factory &&factory)
noexcept(
121 noexcept(T(std::forward<Factory>(factory)()))) {
122 new (&storage_) T(std::forward<Factory>(factory)());
125 template <
typename... Args>
126 void construct(Args &&...args)
noexcept(
127 std::is_nothrow_constructible_v<T, Args...>) {
128 accept([&]() {
return T(std::forward<Args>(args)...); });
131 T &get() noexcept {
return *std::launder(
reinterpret_cast<T *
>(storage_)); }
133 const T &get() const noexcept {
134 return *std::launder(
reinterpret_cast<const T *
>(storage_));
137 void destroy() noexcept { get().~T(); }
140 alignas(T)
unsigned char storage_[
sizeof(T)];
143template <
typename T,
size_t N>
class SmallArray {
145 SmallArray(
size_t capacity) : capacity_(capacity) {
147 large_ =
new T[capacity];
157 T &operator[](
size_t index)
noexcept {
158 return is_small_() ? small_[index] : large_[index];
161 const T &operator[](
size_t index)
const noexcept {
162 return is_small_() ? small_[index] : large_[index];
165 size_t capacity() const noexcept {
return capacity_; }
168 bool is_small_() const noexcept {
return capacity_ <= N; }
178inline auto make_system_error(std::string_view msg,
int ec) {
179 return std::system_error(ec, std::generic_category(), std::string(msg));
182inline auto make_system_error(std::string_view msg) {
183 return make_system_error(msg, errno);
186template <
typename M,
typename T>
187constexpr ptrdiff_t offset_of(M T::*member)
noexcept {
188 constexpr T *dummy =
nullptr;
189 return reinterpret_cast<ptrdiff_t
>(&(dummy->*member));
192template <
typename M,
typename T>
193T *container_of(M T::*member, M *ptr)
noexcept {
194 auto offset = offset_of(member);
196 return reinterpret_cast<T *
>(
reinterpret_cast<uintptr_t
>(ptr) - offset);
199template <typename T, T From = 0, T To = std::numeric_limits<T>::max()>
202 static_assert(From < To,
"Invalid ID range");
205 if (!recycled_ids_.empty()) {
206 T
id = recycled_ids_.top();
213 throw std::runtime_error(
"ID pool exhausted");
216 void recycle(T
id)
noexcept {
217 assert(From <=
id &&
id < next_id_ &&
id < To);
218 recycled_ids_.push(
id);
221 void reset() noexcept {
223 while (!recycled_ids_.empty()) {
230 std::stack<T> recycled_ids_;
233#if __cplusplus >= 202302L
234[[noreturn]]
inline void unreachable() { std::unreachable(); }
236[[noreturn]]
inline void unreachable() { __builtin_unreachable(); }
239template <
size_t Idx = 0,
typename... Ts>
240std::variant<Ts...> tuple_at(std::tuple<Ts...> &results,
size_t idx) {
241 if constexpr (Idx <
sizeof...(Ts)) {
243 return std::variant<Ts...>{std::in_place_index<Idx>,
244 std::move(std::get<Idx>(results))};
246 return tuple_at<Idx + 1, Ts...>(results, idx);
253 assert(
false &&
"Index out of bounds");
254 return std::variant<Ts...>{std::in_place_index<0>,
255 std::move(std::get<0>(results))};
257 panic_on(
"Index out of bounds in tuple_at");
262template <
typename T>
inline T align_up(T value, T alignment)
noexcept {
264 assert(alignment > 0 && (alignment & (alignment - 1)) == 0);
265 return (value + alignment - 1) & ~(alignment - 1);
The main namespace for the Condy library.
auto defer(Func &&func)
Defer the execution of a function until the current scope ends.