Commit e124ab8e authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

fix Arena bytes-used accounting across merge

Summary: Fix `Arena` bytes-used accounting across calls to member `merge`.

Reviewed By: luciang

Differential Revision: D25656949

fbshipit-source-id: b230b0cdbf51b146fd566cbf688f49817bdf874d
parent 37f3868a
......@@ -77,5 +77,7 @@ void Arena<Alloc>::merge(Arena<Alloc>&& other) {
other.ptr_ = other.end_ = nullptr;
totalAllocatedSize_ += other.totalAllocatedSize_;
other.totalAllocatedSize_ = 0;
bytesUsed_ += other.bytesUsed_;
other.bytesUsed_ = 0;
}
} // namespace folly
......@@ -15,15 +15,17 @@
*/
#include <folly/memory/Arena.h>
#include <folly/Memory.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>
#include <set>
#include <vector>
#include <glog/logging.h>
#include <folly/Memory.h>
#include <folly/memory/Malloc.h>
#include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h>
using namespace folly;
static_assert(AllocatorHasTrivialDeallocate<SysArena>::value, "");
......@@ -233,6 +235,29 @@ TEST(Arena, ClearAfterLarge) {
EXPECT_EQ(0, arena.bytesUsed());
}
TEST(Arena, Merge) {
constexpr size_t blockSize = 1024;
size_t blockAllocSize = goodMallocSize(blockSize + SysArena::kBlockOverhead);
SysArena arena1(blockSize);
SysArena arena2(blockSize);
arena1.allocate(16);
arena2.allocate(32);
EXPECT_EQ(blockAllocSize + sizeof(SysArena), arena1.totalSize());
EXPECT_EQ(blockAllocSize + sizeof(SysArena), arena2.totalSize());
EXPECT_EQ(16, arena1.bytesUsed());
EXPECT_EQ(32, arena2.bytesUsed());
arena1.merge(std::move(arena2));
EXPECT_EQ(blockAllocSize * 2 + sizeof(SysArena), arena1.totalSize());
EXPECT_EQ(blockAllocSize * 0 + sizeof(SysArena), arena2.totalSize());
EXPECT_EQ(48, arena1.bytesUsed());
EXPECT_EQ(0, arena2.bytesUsed());
}
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment