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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
//
// experimental/coro/partial.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2021-2023 Klemens D. Morgenstern
// (klemens dot morgenstern at gmx dot net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)
// Test that header file is self-contained.
#include "asio/experimental/coro.hpp"
#include <vector>
#include "asio/io_context.hpp"
#include "../../unit_test.hpp"
namespace exp = asio::experimental;
namespace coro {
template<typename Value = void>
struct tracked_allocator
{
using value_type = Value;
std::vector<std::pair<void*, std::size_t>> & allocs, &deallocs;
tracked_allocator(std::vector<std::pair<void*, std::size_t>> & allocs,
std::vector<std::pair<void*, std::size_t>> & deallocs) : allocs(allocs), deallocs(deallocs) {}
template<typename T>
tracked_allocator(const tracked_allocator<T> & a) : allocs(a.allocs), deallocs(a.deallocs) {}
value_type* allocate(std::size_t n)
{
auto p = new char[n * sizeof(Value)];
allocs.emplace_back(p, n);
return reinterpret_cast<value_type*>(p);
}
void deallocate(void* p, std::size_t n)
{
deallocs.emplace_back(p, n);
delete[] static_cast<char*>(p);
// ASIO_CHECK(allocs.back() == deallocs.back());
}
bool operator==(const tracked_allocator & rhs) const
{
return &allocs == &rhs.allocs
&& &deallocs == &rhs.deallocs;
}
};
exp::coro<void, void, asio::any_io_executor, tracked_allocator<void>>
alloc_test_impl(asio::io_context & ctx, int, std::allocator_arg_t, tracked_allocator<void> ta, double)
{
co_return ;
}
void alloc_test()
{
std::vector<std::pair<void*, std::size_t>> allocs, deallocs;
asio::io_context ctx;
bool ran = false;
{
auto pp = alloc_test_impl(ctx, 42, std::allocator_arg, {allocs, deallocs}, 42.);
ASIO_CHECK(allocs.size() == 1u);
ASIO_CHECK(deallocs.empty());
pp.async_resume([&](auto e){ran = true; ASIO_CHECK(!e);});
ctx.run();
ASIO_CHECK(deallocs.size() == 0u);
}
ctx.restart();
ctx.run();
ASIO_CHECK(deallocs.size() == 1u);
ASIO_CHECK(allocs == deallocs);
ASIO_CHECK(ran);
ran = false;
auto p = asio::experimental::detail::post_coroutine(
ctx,
asio::bind_allocator(tracked_allocator{allocs, deallocs}, [&]{ran = true;})).handle;
ASIO_CHECK(allocs.size() == 2u);
ASIO_CHECK(deallocs.size() == 1u);
p.resume();
ASIO_CHECK(allocs.size() == 3u);
ASIO_CHECK(deallocs.size() == 2u);
ctx.restart();
ctx.run();
ASIO_CHECK(allocs == deallocs);
}
} // namespace coro
ASIO_TEST_SUITE
(
"coro/allocate",
ASIO_TEST_CASE(::coro::alloc_test)
)
|