2 * Copyright 2012 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "folly/Arena.h"
18 #include "folly/StlAllocator.h"
23 #include <glog/logging.h>
24 #include <gtest/gtest.h>
26 using namespace folly;
28 TEST(Arena, SizeSanity) {
29 std::set<size_t*> allocatedItems;
31 static const size_t requestedBlockSize = 64;
32 SysArena arena(requestedBlockSize);
33 size_t minimum_size = sizeof(SysArena), maximum_size = minimum_size;
34 EXPECT_EQ(arena.totalSize(), minimum_size);
36 // Insert a single small element to get a new block
37 size_t* ptr = static_cast<size_t*>(arena.allocate(sizeof(long)));
38 allocatedItems.insert(ptr);
39 minimum_size += requestedBlockSize;
40 maximum_size += goodMallocSize(requestedBlockSize + 1);
41 EXPECT_TRUE(arena.totalSize() >= minimum_size);
42 EXPECT_TRUE(arena.totalSize() <= maximum_size);
43 VLOG(4) << minimum_size << " < " << arena.totalSize() << " < "
46 // Insert a larger element, size should be the same
47 ptr = static_cast<size_t*>(arena.allocate(requestedBlockSize / 2));
48 allocatedItems.insert(ptr);
49 EXPECT_TRUE(arena.totalSize() >= minimum_size);
50 EXPECT_TRUE(arena.totalSize() <= maximum_size);
51 VLOG(4) << minimum_size << " < " << arena.totalSize() << " < "
54 // Insert 10 full block sizes to get 10 new blocks
55 for (int i = 0; i < 10; i++) {
56 ptr = static_cast<size_t*>(arena.allocate(requestedBlockSize));
57 allocatedItems.insert(ptr);
59 minimum_size += 10 * requestedBlockSize;
60 maximum_size += 10 * goodMallocSize(requestedBlockSize + 1);
61 EXPECT_TRUE(arena.totalSize() >= minimum_size);
62 EXPECT_TRUE(arena.totalSize() <= maximum_size);
63 VLOG(4) << minimum_size << " < " << arena.totalSize() << " < "
66 // Insert something huge
67 ptr = static_cast<size_t*>(arena.allocate(10 * requestedBlockSize));
68 allocatedItems.insert(ptr);
69 minimum_size += 10 * requestedBlockSize;
70 maximum_size += goodMallocSize(10 * requestedBlockSize + 1);
71 EXPECT_TRUE(arena.totalSize() >= minimum_size);
72 EXPECT_TRUE(arena.totalSize() <= maximum_size);
73 VLOG(4) << minimum_size << " < " << arena.totalSize() << " < "
77 for (const auto& item : allocatedItems) {
78 arena.deallocate(item);
80 //The total size should be the same
81 EXPECT_TRUE(arena.totalSize() >= minimum_size);
82 EXPECT_TRUE(arena.totalSize() <= maximum_size);
83 VLOG(4) << minimum_size << " < " << arena.totalSize() << " < "
88 static const size_t requestedBlockSize = 64;
89 SysArena arena(requestedBlockSize);
91 EXPECT_EQ(arena.totalSize(), sizeof(SysArena));
93 std::vector<size_t, StlAllocator<SysArena, size_t>>
94 vec { {}, StlAllocator<SysArena, size_t>(&arena) };
96 for (size_t i = 0; i < 1000; i++) {
100 for (size_t i = 0; i < 1000; i++) {
101 EXPECT_EQ(i, vec[i]);
105 int main(int argc, char *argv[]) {
106 testing::InitGoogleTest(&argc, argv);
107 google::ParseCommandLineFlags(&argc, &argv, true);
108 auto ret = RUN_ALL_TESTS();