2 * Copyright 2016 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>
24 #include <gtest/gtest.h>
26 using namespace folly;
29 is_simple_allocator<int,SysArena>::value,
30 "SysArena should be a simple allocator"
33 struct global_counter {
34 global_counter(): count_(0) {}
36 void increase() { ++count_; }
42 unsigned count() const { return count_; }
49 explicit Foo(global_counter& counter):
60 global_counter& counter_;
63 template <typename Allocator>
64 void unique_ptr_test(Allocator& allocator) {
65 typedef typename AllocatorUniquePtr<Foo, Allocator>::type ptr_type;
67 global_counter counter;
68 EXPECT_EQ(counter.count(), 0);
73 auto p = folly::allocate_unique<Foo>(allocator, counter);
74 EXPECT_EQ(counter.count(), 1);
77 EXPECT_EQ(counter.count(), 0);
79 p = folly::allocate_unique<Foo>(allocator, counter);
80 EXPECT_EQ(counter.count(), 1);
83 EXPECT_EQ(counter.count(), 1);
85 EXPECT_EQ(counter.count(), 1);
88 auto p = folly::allocate_unique<Foo>(allocator, counter);
89 EXPECT_EQ(counter.count(), 2);
92 EXPECT_EQ(counter.count(), 2);
94 EXPECT_EQ(counter.count(), 1);
97 EXPECT_EQ(counter.count(), 1);
99 StlAllocator<Allocator, Foo>().destroy(foo);
100 EXPECT_EQ(counter.count(), 0);
103 TEST(ArenaSmartPtr, unique_ptr_SysArena) {
105 unique_ptr_test(arena);
108 TEST(ArenaSmartPtr, unique_ptr_StlAlloc_SysArena) {
110 StlAllocator<SysArena, Foo> alloc(&arena);
111 unique_ptr_test(alloc);
114 template <typename Allocator>
115 void shared_ptr_test(Allocator& allocator) {
116 typedef std::shared_ptr<Foo> ptr_type;
118 global_counter counter;
119 EXPECT_EQ(counter.count(), 0);
122 EXPECT_EQ(counter.count(), 0);
123 EXPECT_EQ(foo.use_count(), 0);
126 auto p = folly::allocate_shared<Foo>(allocator, counter);
127 EXPECT_EQ(counter.count(), 1);
128 EXPECT_EQ(p.use_count(), 1);
131 EXPECT_EQ(counter.count(), 0);
132 EXPECT_EQ(p.use_count(), 0);
134 p = folly::allocate_shared<Foo>(allocator, counter);
135 EXPECT_EQ(counter.count(), 1);
136 EXPECT_EQ(p.use_count(), 1);
139 EXPECT_EQ(p.use_count(), 2);
141 EXPECT_EQ(counter.count(), 1);
142 EXPECT_EQ(foo.use_count(), 1);
146 EXPECT_EQ(counter.count(), 1);
147 EXPECT_EQ(p.use_count(), 2);
150 EXPECT_EQ(counter.count(), 1);
151 EXPECT_EQ(p.use_count(), 3);
152 EXPECT_EQ(g.use_count(), 3);
154 EXPECT_EQ(counter.count(), 1);
155 EXPECT_EQ(p.use_count(), 2);
156 EXPECT_EQ(g.use_count(), 0);
158 EXPECT_EQ(counter.count(), 1);
159 EXPECT_EQ(p.use_count(), 2);
161 EXPECT_EQ(counter.count(), 1);
162 EXPECT_EQ(foo.use_count(), 1);
165 EXPECT_EQ(counter.count(), 0);
166 EXPECT_EQ(foo.use_count(), 0);
169 TEST(ArenaSmartPtr, shared_ptr_SysArena) {
171 shared_ptr_test(arena);
174 TEST(ArenaSmartPtr, shared_ptr_StlAlloc_SysArena) {
176 StlAllocator<SysArena, Foo> alloc(&arena);
177 shared_ptr_test(alloc);
180 int main(int argc, char *argv[]) {
181 testing::InitGoogleTest(&argc, argv);
182 return RUN_ALL_TESTS();