1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
///////////////////////////////////////////////////////////////////////////////
// Reference implementation of std::generator proposal P2168.
//
// See https://wg21.link/P2168 for details.
//
///////////////////////////////////////////////////////////////////////////////
// Copyright Lewis Baker, Corentin Jabot
//
// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0.
// (See accompanying file LICENSE or http://www.boost.org/LICENSE_1_0.txt)
///////////////////////////////////////////////////////////////////////////////
export module nihil.generator:promise_base_alloc;
import nihil.std;
import :util;
namespace nihil {
template<typename Alloc>
struct promise_base_alloc
{
template<typename... Args>
static void* operator new(std::size_t frame_size, std::allocator_arg_t, Alloc alloc, Args &...)
{
void* frame = alloc.allocate(padded_frame_size(frame_size));
// Store allocator at end of the coroutine frame. Assuming the
// allocator's move constructor is non-throwing (a requirement
// for allocators)
auto *alloc_address = static_cast<void*>(std::addressof(get_allocator(frame, frame_size)));
::new (alloc_address) Alloc(std::move(alloc));
return frame;
}
template<typename This, typename... Args>
static void* operator new(std::size_t frame_size, This &, std::allocator_arg_t, Alloc alloc, Args &...)
{
return promise_base_alloc::operator new(frame_size, std::allocator_arg, std::move(alloc));
}
static void operator delete(void* ptr, std::size_t frame_size) noexcept
{
auto &alloc = get_allocator(ptr, frame_size);
auto local_alloc = Alloc(std::move(alloc));
alloc.~Alloc();
local_alloc.deallocate(static_cast<std::byte*>(ptr), padded_frame_size(frame_size));
}
private:
[[nodiscard]] static constexpr auto offset_of_allocator(std::size_t frame_size) noexcept -> std::size_t
{
return aligned_allocation_size(frame_size, alignof(Alloc));
}
[[nodiscard]] static constexpr auto padded_frame_size(std::size_t frame_size) noexcept -> std::size_t
{
return offset_of_allocator(frame_size) + sizeof(Alloc);
}
[[nodiscard]] static auto get_allocator(void* frame, std::size_t frame_size) noexcept -> Alloc &
{
return *reinterpret_cast<Alloc*>(
static_cast<char*>(frame) + offset_of_allocator(frame_size));
}
};
template<typename Alloc>
requires (!allocator_needs_to_be_stored<Alloc>)
struct promise_base_alloc<Alloc>
{
static void* operator new(std::size_t size)
{
auto alloc = Alloc();
return alloc.allocate(size);
}
static void operator delete(void *ptr, std::size_t size) noexcept
{
auto alloc = Alloc();
alloc.deallocate(static_cast<std::byte *>(ptr), size);
}
};
} // namespace nihil
|