Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/condy/coro.inl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <coroutine>
#include <exception>
#include <mutex>
#include <new>
#include <optional>

namespace condy {
Expand Down Expand Up @@ -64,8 +65,8 @@ public:
(size + alignof(Allocator) - 1) & ~(alignof(Allocator) - 1);
size_t total_size = allocator_offset + sizeof(Allocator);
Pointer mem = static_cast<Pointer>(ptr);
Allocator &alloc =
*reinterpret_cast<Allocator *>(mem + allocator_offset);
Allocator &alloc = *std::launder(
reinterpret_cast<Allocator *>(mem + allocator_offset));
Allocator alloc_copy = std::move(alloc);
alloc.~Allocator();
alloc_copy.deallocate(mem, total_size);
Expand Down
13 changes: 10 additions & 3 deletions include/condy/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <format>
#include <iostream>
#include <limits>
#include <new>
#include <stack>
#include <stdexcept>
#include <string_view>
Expand Down Expand Up @@ -82,16 +83,22 @@ template <typename Func> auto defer(Func &&func) {

template <typename T> class RawStorage {
public:
template <typename Factory>
void accept(Factory &&factory) noexcept(
noexcept(T(std::forward<Factory>(factory)()))) {
new (&storage_) T(std::forward<Factory>(factory)());
}

template <typename... Args>
void construct(Args &&...args) noexcept(
std::is_nothrow_constructible_v<T, Args...>) {
new (&storage_) T(std::forward<Args>(args)...);
accept([&]() { return T(std::forward<Args>(args)...); });
}

T &get() noexcept { return *reinterpret_cast<T *>(&storage_); }
T &get() noexcept { return *std::launder(reinterpret_cast<T *>(storage_)); }

const T &get() const noexcept {
return *reinterpret_cast<const T *>(&storage_);
return *std::launder(reinterpret_cast<const T *>(storage_));
}
Comment thread
wokron marked this conversation as resolved.

void destroy() noexcept { get().~T(); }
Expand Down
22 changes: 21 additions & 1 deletion tests/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct int_deleter {

TEST_CASE("test raw_storage - int") {
condy::RawStorage<int> storage;
new (&storage) int(77);
storage.construct(77);
REQUIRE(storage.get() == 77);
storage.destroy();
}
Expand All @@ -42,6 +42,26 @@ TEST_CASE("test raw_storage - std::unique_ptr") {
REQUIRE(int_deleter::called);
}

TEST_CASE("test raw_storage - guaranteed return value optimization") {
struct Fixed {
Fixed(int v) : value(v) {}
Fixed(const Fixed &) = delete;
Fixed &operator=(const Fixed &) = delete;
Fixed(Fixed &&) = delete;
Fixed &operator=(Fixed &&) = delete;
int value;
};

auto f1 = [](int v) { return Fixed(v); };

auto f2 = [&]() { return f1(42); };

condy::RawStorage<Fixed> storage;
storage.accept(f2);
REQUIRE(storage.get().value == 42);
storage.destroy();
}

TEST_CASE("test small_array - small") {
condy::SmallArray<int, 4> arr(3);
arr[0] = 10;
Expand Down
Loading