Skip to content

Commit 4cd83c6

Browse files
committed
mempool: implement move semantics for MempoolInstance
Currently it uses default implementations that does not set pointers of moved objects to `NULL` and they are still by moved object. Let's manually implement move semantics because we will need to move MempoolInstance as a part of `tnt::Buffer` later. Part of #110
1 parent e8c0a6c commit 4cd83c6

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Utils/Mempool.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ class MempoolInstance : public MempoolStats<ENABLE_STATS> {
115115
static constexpr size_t SLAB_ALIGN = SA;
116116

117117
MempoolInstance() = default;
118+
MempoolInstance(const MempoolInstance& other) = default;
119+
MempoolInstance &operator=(const MempoolInstance& other) = default;
120+
MempoolInstance(MempoolInstance&& other) noexcept
121+
{
122+
memcpy(this, &other, sizeof(*this));
123+
memset(&other, 0, sizeof(*this));
124+
}
125+
MempoolInstance &operator=(MempoolInstance&& other) noexcept
126+
{
127+
memcpy(this, &other, sizeof(*this));
128+
memset(&other, 0, sizeof(*this));
129+
return *this;
130+
}
118131
~MempoolInstance() noexcept
119132
{
120133
while (m_SlabList != nullptr) {

test/MempoolUnitTest.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,35 @@ test_alignment()
247247
}
248248
}
249249

250+
template<size_t S>
251+
void
252+
test_move()
253+
{
254+
TEST_INIT(2, S, 0);
255+
constexpr size_t N = 1024;
256+
tnt::MempoolInstance<S> mp;
257+
258+
Allocations<S, N * 6> all;
259+
for (size_t i = 0; i < N; i++)
260+
all.add(mp.allocate());
261+
fail_unless(all.are_valid());
262+
263+
tnt::MempoolInstance<S> mp_move_constructed(std::move(mp));
264+
for (size_t i = 0; i < N; i++) {
265+
all.add(mp_move_constructed.allocate());
266+
all.add(mp.allocate());
267+
}
268+
fail_unless(all.are_valid());
269+
270+
tnt::MempoolInstance<S> mp_move_copied;
271+
mp_move_copied = std::move(mp);
272+
for (size_t i = 0; i < N; i++) {
273+
all.add(mp_move_copied.allocate());
274+
all.add(mp.allocate());
275+
}
276+
fail_unless(all.are_valid());
277+
}
278+
250279
int main()
251280
{
252281
test_default<8>();
@@ -275,4 +304,10 @@ int main()
275304
test_alignment<120, 2>();
276305
test_alignment<120, 13>();
277306
test_alignment<120, 64>();
307+
308+
test_move<8>();
309+
test_move<64>();
310+
test_move<14>();
311+
test_move<72>();
312+
test_move<80>();
278313
}

0 commit comments

Comments
 (0)