Fix allocator move/copy assignment in flat_buffer and multi_buffer

Fixes #3034
This commit is contained in:
Mohammad Nejati
2025-10-02 12:50:15 +00:00
committed by Mohammad Nejati
parent 164db4bc57
commit 97d6ddb730
5 changed files with 48 additions and 5 deletions
+15 -3
View File
@@ -452,7 +452,19 @@ move_assign(basic_flat_buffer& other, std::false_type)
}
else
{
move_assign(other, std::true_type{});
clear();
shrink_to_fit();
begin_ = other.begin_;
in_ = other.in_;
out_ = other.out_;
last_ = out_;
end_ = other.end_;
max_ = other.max_;
other.begin_ = nullptr;
other.in_ = nullptr;
other.out_ = nullptr;
other.last_ = nullptr;
other.end_ = nullptr;
}
}
@@ -461,6 +473,8 @@ void
basic_flat_buffer<Allocator>::
copy_assign(basic_flat_buffer const& other, std::true_type)
{
clear();
shrink_to_fit();
max_ = other.max_;
this->get() = other.get();
copy_from(other);
@@ -471,8 +485,6 @@ void
basic_flat_buffer<Allocator>::
copy_assign(basic_flat_buffer const& other, std::false_type)
{
clear();
shrink_to_fit();
max_ = other.max_;
copy_from(other);
}
+21 -2
View File
@@ -610,7 +610,6 @@ operator=(basic_multi_buffer&& other) ->
{
if(this == &other)
return *this;
clear();
max_ = other.max_;
move_assign(other, pocma{});
return *this;
@@ -1073,6 +1072,8 @@ void
basic_multi_buffer<Allocator>::
move_assign(basic_multi_buffer& other, std::true_type) noexcept
{
clear();
shrink_to_fit();
this->get() = std::move(other.get());
auto const at_end =
other.out_ == other.list_.end();
@@ -1103,7 +1104,24 @@ move_assign(basic_multi_buffer& other, std::false_type)
}
else
{
move_assign(other, std::true_type{});
clear();
shrink_to_fit();
auto const at_end =
other.out_ == other.list_.end();
list_ = std::move(other.list_);
out_ = at_end ? list_.end() : other.out_;
in_size_ = other.in_size_;
in_pos_ = other.in_pos_;
out_pos_ = other.out_pos_;
out_end_ = other.out_end_;
max_ = other.max_;
other.in_size_ = 0;
other.out_ = other.list_.end();
other.in_pos_ = 0;
other.out_pos_ = 0;
other.out_end_ = 0;
}
}
@@ -1123,6 +1141,7 @@ copy_assign(
basic_multi_buffer const& other, std::true_type)
{
clear();
shrink_to_fit();
this->get() = other.get();
copy_from(other);
}
+4
View File
@@ -198,6 +198,7 @@ public:
ostream(b1) << "Hello";
basic_flat_buffer<pocma_t> b2;
b2 = std::move(b1);
BEAST_EXPECT(b1.get_allocator()->nmassign == 1);
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
}
@@ -209,6 +210,7 @@ public:
ostream(b1) << "Hello";
basic_flat_buffer<pocma_t> b2;
b2 = std::move(b1);
BEAST_EXPECT(b1.get_allocator()->nmassign == 0);
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
}
@@ -235,6 +237,7 @@ public:
ostream(b1) << "Hello";
basic_flat_buffer<pocca_t> b2;
b2 = b1;
BEAST_EXPECT(b1.get_allocator()->ncpassign == 1);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
}
{
@@ -245,6 +248,7 @@ public:
ostream(b1) << "Hello";
basic_flat_buffer<pocca_t> b2;
b2 = b1;
BEAST_EXPECT(b1.get_allocator()->ncpassign == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
}
}
+4
View File
@@ -312,6 +312,7 @@ public:
ostream(b1) << "Hello";
basic_multi_buffer<pocma_t> b2;
b2 = std::move(b1);
BEAST_EXPECT(b1.get_allocator()->nmassign == 1);
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
}
@@ -323,6 +324,7 @@ public:
ostream(b1) << "Hello";
basic_multi_buffer<pocma_t> b2;
b2 = std::move(b1);
BEAST_EXPECT(b1.get_allocator()->nmassign == 0);
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
}
@@ -349,6 +351,7 @@ public:
ostream(b1) << "Hello";
basic_multi_buffer<pocca_t> b2;
b2 = b1;
BEAST_EXPECT(b1.get_allocator()->ncpassign == 1);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
}
{
@@ -359,6 +362,7 @@ public:
ostream(b1) << "Hello";
basic_multi_buffer<pocca_t> b2;
b2 = b1;
BEAST_EXPECT(b1.get_allocator()->ncpassign == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
}
}
+4
View File
@@ -188,6 +188,7 @@ public:
f1.insert("1", "1");
basic_fields<pocma_t> f2;
f2 = std::move(f1);
BEAST_EXPECT(f1.get_allocator()->nmassign == 1);
BEAST_EXPECT(f1.begin() == f1.end());
BEAST_EXPECT(f2["1"] == "1");
}
@@ -199,6 +200,7 @@ public:
f1.insert("1", "1");
basic_fields<pocma_t> f2;
f2 = std::move(f1);
BEAST_EXPECT(f1.get_allocator()->nmassign == 0);
BEAST_EXPECT(f1.begin() == f1.end());
BEAST_EXPECT(f2["1"] == "1");
}
@@ -225,6 +227,7 @@ public:
f1.insert("1", "1");
basic_fields<pocca_t> f2;
f2 = f1;
BEAST_EXPECT(f1.get_allocator()->ncpassign == 1);
BEAST_EXPECT(f2["1"] == "1");
}
{
@@ -235,6 +238,7 @@ public:
f1.insert("1", "1");
basic_fields<pocca_t> f2;
f2 = f1;
BEAST_EXPECT(f1.get_allocator()->ncpassign == 0);
BEAST_EXPECT(f2["1"] == "1");
}
}