2 * Copyright 2017 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.
18 * @author: Marcelo Juchem <marcelo@fb.com>
21 #include <folly/Memory.h>
22 #include <folly/Arena.h>
23 #include <folly/portability/GTest.h>
25 using namespace folly;
28 is_simple_allocator<int,SysArena>::value,
29 "SysArena should be a simple allocator"
32 struct global_counter {
33 global_counter(): count_(0) {}
35 void increase() { ++count_; }
41 unsigned count() const { return count_; }
48 explicit Foo(global_counter& counter):
59 global_counter& counter_;
62 template <typename Allocator>
63 void unique_ptr_test(Allocator& allocator) {
64 typedef typename AllocatorUniquePtr<Foo, Allocator>::type ptr_type;
66 global_counter counter;
67 EXPECT_EQ(counter.count(), 0);
72 auto p = folly::allocate_unique<Foo>(allocator, counter);
73 EXPECT_EQ(counter.count(), 1);
76 EXPECT_EQ(counter.count(), 0);
78 p = folly::allocate_unique<Foo>(allocator, counter);
79 EXPECT_EQ(counter.count(), 1);
82 EXPECT_EQ(counter.count(), 1);
84 EXPECT_EQ(counter.count(), 1);
87 auto p = folly::allocate_unique<Foo>(allocator, counter);
88 EXPECT_EQ(counter.count(), 2);
91 EXPECT_EQ(counter.count(), 2);
93 EXPECT_EQ(counter.count(), 1);
96 EXPECT_EQ(counter.count(), 1);
98 StlAllocator<Allocator, Foo>().destroy(foo);
99 EXPECT_EQ(counter.count(), 0);
102 TEST(ArenaSmartPtr, unique_ptr_SysArena) {
104 unique_ptr_test(arena);
107 TEST(ArenaSmartPtr, unique_ptr_StlAlloc_SysArena) {
109 StlAllocator<SysArena, Foo> alloc(&arena);
110 unique_ptr_test(alloc);
113 template <typename Allocator>
114 void shared_ptr_test(Allocator& allocator) {
115 typedef std::shared_ptr<Foo> ptr_type;
117 global_counter counter;
118 EXPECT_EQ(counter.count(), 0);
121 EXPECT_EQ(counter.count(), 0);
122 EXPECT_EQ(foo.use_count(), 0);
125 auto p = folly::allocate_shared<Foo>(allocator, counter);
126 EXPECT_EQ(counter.count(), 1);
127 EXPECT_EQ(p.use_count(), 1);
130 EXPECT_EQ(counter.count(), 0);
131 EXPECT_EQ(p.use_count(), 0);
133 p = folly::allocate_shared<Foo>(allocator, counter);
134 EXPECT_EQ(counter.count(), 1);
135 EXPECT_EQ(p.use_count(), 1);
138 EXPECT_EQ(p.use_count(), 2);
140 EXPECT_EQ(counter.count(), 1);
141 EXPECT_EQ(foo.use_count(), 1);
145 EXPECT_EQ(counter.count(), 1);
146 EXPECT_EQ(p.use_count(), 2);
149 EXPECT_EQ(counter.count(), 1);
150 EXPECT_EQ(p.use_count(), 3);
151 EXPECT_EQ(g.use_count(), 3);
153 EXPECT_EQ(counter.count(), 1);
154 EXPECT_EQ(p.use_count(), 2);
155 EXPECT_EQ(g.use_count(), 0);
157 EXPECT_EQ(counter.count(), 1);
158 EXPECT_EQ(p.use_count(), 2);
160 EXPECT_EQ(counter.count(), 1);
161 EXPECT_EQ(foo.use_count(), 1);
164 EXPECT_EQ(counter.count(), 0);
165 EXPECT_EQ(foo.use_count(), 0);
168 TEST(ArenaSmartPtr, shared_ptr_SysArena) {
170 shared_ptr_test(arena);
173 TEST(ArenaSmartPtr, shared_ptr_StlAlloc_SysArena) {
175 StlAllocator<SysArena, Foo> alloc(&arena);
176 shared_ptr_test(alloc);
179 int main(int argc, char *argv[]) {
180 testing::InitGoogleTest(&argc, argv);
181 return RUN_ALL_TESTS();