Skip to content

Commit 962cc19

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. Along the way, mark other constructors and operators as `noexcept`. Part of #110
1 parent e8c0a6c commit 962cc19

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/Utils/Mempool.hpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <cstddef>
3535
#include <cstdint>
3636
#include <cstring>
37+
#include <utility>
3738

3839
namespace tnt {
3940

@@ -114,7 +115,23 @@ class MempoolInstance : public MempoolStats<ENABLE_STATS> {
114115
static constexpr size_t BLOCK_ALIGN = BA;
115116
static constexpr size_t SLAB_ALIGN = SA;
116117

117-
MempoolInstance() = default;
118+
MempoolInstance() noexcept = default;
119+
MempoolInstance(const MempoolInstance& other) noexcept = default;
120+
MempoolInstance &operator=(const MempoolInstance& other) noexcept = default;
121+
MempoolInstance(MempoolInstance&& other) noexcept
122+
{
123+
/* Call move assignment operator. */
124+
*this = std::forward<MempoolInstance>(other);
125+
}
126+
MempoolInstance &operator=(MempoolInstance&& other) noexcept
127+
{
128+
/* Call copy assignment operator. */
129+
*this = static_cast<MempoolInstance &>(other);
130+
/* Use lvalue to call copy assignment operator. */
131+
MempoolInstance empty{};
132+
other = empty;
133+
return *this;
134+
}
118135
~MempoolInstance() noexcept
119136
{
120137
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)