Get total memory currently allocated by an Arena allocator
[folly.git] / folly / test / ArenaTest.cpp
1 /*
2  * Copyright 2012 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "folly/Arena.h"
18 #include "folly/StlAllocator.h"
19
20 #include <set>
21 #include <vector>
22
23 #include <glog/logging.h>
24 #include <gtest/gtest.h>
25
26 using namespace folly;
27
28 TEST(Arena, SizeSanity) {
29   std::set<size_t*> allocatedItems;
30
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);
35
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() << " < "
44           << maximum_size;
45
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() << " < "
52           << maximum_size;
53
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);
58   }
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() << " < "
64           << maximum_size;
65
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() << " < "
74           << maximum_size;
75
76   // Nuke 'em all
77   for (const auto& item : allocatedItems) {
78     arena.deallocate(item);
79   }
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() << " < "
84           << maximum_size;
85 }
86
87 TEST(Arena, Vector) {
88   static const size_t requestedBlockSize = 64;
89   SysArena arena(requestedBlockSize);
90
91   EXPECT_EQ(arena.totalSize(), sizeof(SysArena));
92
93   std::vector<size_t, StlAllocator<SysArena, size_t>>
94     vec { {}, StlAllocator<SysArena, size_t>(&arena) };
95
96   for (size_t i = 0; i < 1000; i++) {
97     vec.push_back(i);
98   }
99
100   for (size_t i = 0; i < 1000; i++) {
101     EXPECT_EQ(i, vec[i]);
102   }
103 }
104
105 int main(int argc, char *argv[]) {
106   testing::InitGoogleTest(&argc, argv);
107   google::ParseCommandLineFlags(&argc, &argv, true);
108   auto ret = RUN_ALL_TESTS();
109   return ret;
110 }