Skip to content

Commit d8df2d4

Browse files
committed
refactor: simplify code
1 parent 54dbd25 commit d8df2d4

3 files changed

Lines changed: 44 additions & 38 deletions

File tree

include/coio/utils/allocator_resource.h

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <memory_resource>
55
#include <cstddef>
66
#include <coio/utils/utility.h>
7+
#include <coio/utils/new_object.h>
78
#include <coio/detail/concepts.h>
89
#include <coio/detail/suppress_push.h> // IWYU pragma: keep
910

@@ -99,11 +100,7 @@ namespace coio {
99100
std::destroy_at(this);
100101
}
101102
else {
102-
using alloc_t = typename std::allocator_traits<Alloc>::template rebind_alloc<proxied>;
103-
using traits_t = std::allocator_traits<alloc_t>;
104-
alloc_t alloc(alloc_);
105-
traits_t::destroy(alloc, this);
106-
traits_t::deallocate(alloc, this, 1);
103+
coio::delete_object(alloc_, this);
107104
}
108105
}
109106

@@ -113,25 +110,13 @@ namespace coio {
113110
public:
114111
// ReSharper disable once CppPossiblyUninitializedMember
115112
template<simple_allocator Alloc>
116-
explicit allocator_resource(Alloc alloc) { // NOLINT(*-pro-type-member-init)
117-
proxied<Alloc>* location = nullptr;
113+
explicit allocator_resource(const Alloc& alloc) { // NOLINT(*-pro-type-member-init)
118114
if constexpr (is_small_object<proxied<Alloc>>) {
119-
location = std::construct_at(reinterpret_cast<proxied<Alloc>*>(storage_), alloc);
115+
impl_ = std::construct_at(reinterpret_cast<proxied<Alloc>*>(storage_), alloc);
120116
}
121117
else {
122-
using alloc_t = typename std::allocator_traits<Alloc>::template rebind_alloc<proxied<Alloc>>;
123-
using traits_t = std::allocator_traits<alloc_t>;
124-
alloc_t pro_alloc(alloc);
125-
location = traits_t::allocate(pro_alloc, 1);
126-
try {
127-
traits_t::construct(pro_alloc, location, alloc);
128-
}
129-
catch (...) {
130-
traits_t::deallocate(pro_alloc, location, 1);
131-
throw;
132-
}
118+
impl_ = coio::new_object<proxied<Alloc>>(alloc, alloc);
133119
}
134-
impl_ = location;
135120
}
136121

137122
allocator_resource(const allocator_resource&) = delete;

include/coio/utils/new_object.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
#include <memory>
3+
#include <utility>
4+
#include <coio/detail/concepts.h>
5+
6+
namespace coio {
7+
template<unqualified_object T, simple_allocator Allocator, typename... Args> requires std::constructible_from<T, Args...>
8+
[[nodiscard]]
9+
COIO_ALWAYS_INLINE auto new_object(const Allocator& allocator, Args&&... args) -> T* {
10+
using alloc_t = typename std::allocator_traits<Allocator>::template rebind_alloc<T>;
11+
using alloc_traits = std::allocator_traits<alloc_t>;
12+
alloc_t alloc(allocator);
13+
auto ptr = alloc_traits::allocate(alloc, 1);
14+
try {
15+
alloc_traits::construct(alloc, ptr, std::forward<Args>(args)...);
16+
return ptr;
17+
}
18+
catch (...) {
19+
alloc_traits::deallocate(alloc, ptr, 1);
20+
throw;
21+
}
22+
}
23+
24+
template<simple_allocator Allocator, typename T>
25+
COIO_ALWAYS_INLINE auto delete_object(const Allocator& allocator, T* ptr) noexcept -> void {
26+
using alloc_t = typename std::allocator_traits<Allocator>::template rebind_alloc<T>;
27+
using alloc_traits = std::allocator_traits<alloc_t>;
28+
alloc_t alloc(allocator);
29+
alloc_traits::destroy(alloc, ptr);
30+
alloc_traits::deallocate(alloc, ptr, 1);
31+
}
32+
}

include/coio/utils/polymorphic_scheduler.h

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <optional>
44
#include <coio/detail/concepts.h>
55
#include <coio/detail/execution.h>
6+
#include <coio/utils/new_object.h>
67
#include <coio/utils/retain_ptr.h>
78
#include <coio/utils/scope_exit.h>
89

@@ -66,10 +67,7 @@ namespace coio {
6667
}
6768

6869
auto delete_self() noexcept -> void override {
69-
using alloc_t = std::allocator_traits<allocator_t>::template rebind_alloc<state_proxy>;
70-
alloc_t al(std::move(alloc));
71-
std::allocator_traits<alloc_t>::destroy(al, this);
72-
std::allocator_traits<alloc_t>::deallocate(al, this, 1);
70+
coio::delete_object(alloc, this);
7371
}
7472

7573
allocator_t alloc;
@@ -99,11 +97,7 @@ namespace coio {
9997
}
10098
else {
10199
auto alloc = detail::get_suitable_allocator(execution::get_env(sndr));
102-
using alloc_t = std::allocator_traits<decltype(alloc)>::template rebind_alloc<state_proxy<Sndr, false>>;
103-
alloc_t al(std::move(alloc));
104-
auto ptr = std::allocator_traits<alloc_t>::allocate(al, 1);
105-
std::allocator_traits<alloc_t>::construct(al, ptr, std::move(sndr), std::move(rcvr));
106-
proxy = ptr;
100+
proxy = coio::new_object<state_proxy<Sndr, false>>(alloc, std::move(sndr), std::move(rcvr));
107101
}
108102
}
109103

@@ -251,22 +245,17 @@ namespace coio {
251245
explicit backend_for(Sched sched, Alloc alloc) noexcept : base(std::move(sched)), alloc(std::move(alloc)) {}
252246

253247
auto do_lose() noexcept -> void override {
254-
using alloc_t = std::allocator_traits<Alloc>::template rebind_alloc<backend_for>;
255-
alloc_t al(std::move(alloc));
256-
std::allocator_traits<alloc_t>::destroy(al, this);
257-
std::allocator_traits<alloc_t>::deallocate(al, this, 1);
248+
coio::delete_object(alloc, this);
258249
}
259250

260251
COIO_NO_UNIQUE_ADDRESS Alloc alloc;
261252
};
262253

263254
template<typename Sched, typename Alloc>
264255
static auto create_backend(Sched sched, Alloc alloc) -> retain_ptr<backend> {
265-
using alloc_t = std::allocator_traits<Alloc>::template rebind_alloc<backend_for<Sched, Alloc>>;
266-
alloc_t al(alloc);
267-
auto ptr = std::allocator_traits<alloc_t>::allocate(al, 1);
268-
std::allocator_traits<alloc_t>::construct(al, ptr, std::move(sched), std::move(alloc));
269-
return retain_ptr<backend>(ptr);
256+
return retain_ptr<backend>{
257+
coio::new_object<backend_for<Sched, Alloc>>(alloc, std::move(sched), std::move(alloc))
258+
};
270259
}
271260

272261
public:

0 commit comments

Comments
 (0)