summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2024-11-26 09:05:48 -0800
committerGitHub <noreply@github.com>2024-11-26 09:05:48 -0800
commitcc97853b5f03e8c8441159e68ecb2262beee166f (patch)
treeecf63827f726ff0401aa47a250539138b77a6a3c
parentf7afec956917c51071867f5b1b487111f1a1ef60 (diff)
downloadbinaryen-cc97853b5f03e8c8441159e68ecb2262beee166f.tar.gz
binaryen-cc97853b5f03e8c8441159e68ecb2262beee166f.tar.bz2
binaryen-cc97853b5f03e8c8441159e68ecb2262beee166f.zip
Fix ArenaVector::swap (#7115)
This was never right for over a decade, and just never used I suppose... it should have been called "take" since it grabbed data from the other item and then set that other item to empty. Fix it so it swaps properly.
-rw-r--r--src/mixed_arena.h9
-rw-r--r--test/gtest/CMakeLists.txt1
-rw-r--r--test/gtest/arena.cpp39
3 files changed, 43 insertions, 6 deletions
diff --git a/src/mixed_arena.h b/src/mixed_arena.h
index 530fa9daa..057887b82 100644
--- a/src/mixed_arena.h
+++ b/src/mixed_arena.h
@@ -263,12 +263,9 @@ public:
void operator=(SubType& other) { set(other); }
void swap(SubType& other) {
- data = other.data;
- usedElements = other.usedElements;
- allocatedElements = other.allocatedElements;
-
- other.data = nullptr;
- other.usedElements = other.allocatedElements = 0;
+ std::swap(data, other.data);
+ std::swap(usedElements, other.usedElements);
+ std::swap(allocatedElements, other.allocatedElements);
}
// iteration
diff --git a/test/gtest/CMakeLists.txt b/test/gtest/CMakeLists.txt
index 9b648fb65..c3d281f1c 100644
--- a/test/gtest/CMakeLists.txt
+++ b/test/gtest/CMakeLists.txt
@@ -2,6 +2,7 @@ include_directories(../../third_party/googletest/googletest/include)
include_directories(../../src/wasm)
set(unittest_SOURCES
+ arena.cpp
binary-reader.cpp
cfg.cpp
dfa_minimization.cpp
diff --git a/test/gtest/arena.cpp b/test/gtest/arena.cpp
new file mode 100644
index 000000000..7453ea9f6
--- /dev/null
+++ b/test/gtest/arena.cpp
@@ -0,0 +1,39 @@
+#include "mixed_arena.h"
+#include "gtest/gtest.h"
+
+using ArenaTest = ::testing::Test;
+
+TEST_F(ArenaTest, Swap) {
+ MixedArena arena;
+
+ ArenaVector<int> a(arena);
+ a.push_back(10);
+ a.push_back(20);
+
+ ArenaVector<int> b(arena);
+
+ EXPECT_EQ(a.size(), 2U);
+ EXPECT_EQ(b.size(), 0U);
+
+ a.swap(b);
+
+ EXPECT_EQ(a.size(), 0U);
+ EXPECT_EQ(b.size(), 2U);
+
+ a.swap(b);
+
+ EXPECT_EQ(a.size(), 2U);
+ EXPECT_EQ(b.size(), 0U);
+
+ // Now reverse a and b. The swap should be the same.
+
+ b.swap(a);
+
+ EXPECT_EQ(a.size(), 0U);
+ EXPECT_EQ(b.size(), 2U);
+
+ b.swap(a);
+
+ EXPECT_EQ(a.size(), 2U);
+ EXPECT_EQ(b.size(), 0U);
+}