Fixes RCU test cases error (loads should use Consume ordering)
[folly.git] / folly / test / MemoryTest.cpp
1 /*
2  * Copyright 2013-present 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/Memory.h>
18
19 #include <type_traits>
20 #include <utility>
21
22 #include <glog/logging.h>
23
24 #include <folly/String.h>
25 #include <folly/memory/Arena.h>
26 #include <folly/portability/GTest.h>
27
28 using namespace folly;
29
30 TEST(aligned_malloc, examples) {
31   auto trial = [](size_t align) {
32     auto const ptr = aligned_malloc(1, align);
33     return (aligned_free(ptr), uintptr_t(ptr));
34   };
35
36   if (!kIsSanitize) { // asan allocator raises SIGABRT instead
37     EXPECT_EQ(EINVAL, (trial(2), errno)) << "too small";
38     EXPECT_EQ(EINVAL, (trial(513), errno)) << "not power of two";
39   }
40
41   EXPECT_EQ(0, trial(512) % 512);
42   EXPECT_EQ(0, trial(8192) % 8192);
43 }
44
45 TEST(make_unique, compatible_with_std_make_unique) {
46   //  HACK: To enforce that `folly::` is imported here.
47   to_shared_ptr(std::unique_ptr<std::string>());
48
49   using namespace std;
50   make_unique<string>("hello, world");
51 }
52
53 TEST(to_weak_ptr, example) {
54   auto s = std::make_shared<int>(17);
55   EXPECT_EQ(1, s.use_count());
56   EXPECT_EQ(2, (to_weak_ptr(s).lock(), s.use_count())) << "lvalue";
57   EXPECT_EQ(3, (to_weak_ptr(decltype(s)(s)).lock(), s.use_count())) << "rvalue";
58 }
59
60 TEST(allocate_sys_buffer, compiles) {
61   auto buf = allocate_sys_buffer(256);
62   //  Freed at the end of the scope.
63 }
64
65 template <std::size_t> struct T {};
66 template <std::size_t> struct S {};
67 template <std::size_t> struct P {};
68
69 TEST(as_stl_allocator, sanity_check) {
70   typedef StlAllocator<SysArena, int> stl_arena_alloc;
71
72   EXPECT_TRUE((std::is_same<
73     as_stl_allocator<int, SysArena>::type,
74     stl_arena_alloc
75   >::value));
76
77   EXPECT_TRUE((std::is_same<
78     as_stl_allocator<int, stl_arena_alloc>::type,
79     stl_arena_alloc
80   >::value));
81 }
82
83 TEST(StlAllocator, void_allocator) {
84   typedef StlAllocator<SysArena, void> void_allocator;
85   SysArena arena;
86   void_allocator valloc(&arena);
87
88   typedef void_allocator::rebind<int>::other int_allocator;
89   int_allocator ialloc(valloc);
90
91   auto i = std::allocate_shared<int>(ialloc, 10);
92   ASSERT_NE(nullptr, i.get());
93   EXPECT_EQ(10, *i);
94   i.reset();
95   ASSERT_EQ(nullptr, i.get());
96 }
97
98 TEST(rebind_allocator, sanity_check) {
99   std::allocator<long> alloc;
100
101   auto i = std::allocate_shared<int>(
102     rebind_allocator<int, decltype(alloc)>(alloc), 10
103   );
104   ASSERT_NE(nullptr, i.get());
105   EXPECT_EQ(10, *i);
106   i.reset();
107   ASSERT_EQ(nullptr, i.get());
108
109   auto d = std::allocate_shared<double>(
110     rebind_allocator<double>(alloc), 5.6
111   );
112   ASSERT_NE(nullptr, d.get());
113   EXPECT_EQ(5.6, *d);
114   d.reset();
115   ASSERT_EQ(nullptr, d.get());
116
117   auto s = std::allocate_shared<std::string>(
118     rebind_allocator<std::string>(alloc), "HELLO, WORLD"
119   );
120   ASSERT_NE(nullptr, s.get());
121   EXPECT_EQ("HELLO, WORLD", *s);
122   s.reset();
123   ASSERT_EQ(nullptr, s.get());
124 }
125
126 template <typename C>
127 static void test_enable_shared_from_this(std::shared_ptr<C> sp) {
128   ASSERT_EQ(1l, sp.use_count());
129
130   // Test shared_from_this().
131   std::shared_ptr<C> sp2 = sp->shared_from_this();
132   ASSERT_EQ(sp, sp2);
133
134   // Test weak_from_this().
135   std::weak_ptr<C> wp = sp->weak_from_this();
136   ASSERT_EQ(sp, wp.lock());
137   sp.reset();
138   sp2.reset();
139   ASSERT_EQ(nullptr, wp.lock());
140
141   // Test shared_from_this() and weak_from_this() on object not owned by a
142   // shared_ptr. Undefined in C++14 but well-defined in C++17. Also known to
143   // work with libstdc++ >= 20150123. Feel free to add other standard library
144   // versions where the behavior is known.
145 #if __cplusplus >= 201700L || \
146     __GLIBCXX__ >= 20150123L
147   C stack_resident;
148   ASSERT_THROW(stack_resident.shared_from_this(), std::bad_weak_ptr);
149   ASSERT_TRUE(stack_resident.weak_from_this().expired());
150 #endif
151 }
152
153 TEST(enable_shared_from_this, compatible_with_std_enable_shared_from_this) {
154   // Compile-time compatibility.
155   class C_std : public std::enable_shared_from_this<C_std> {};
156   class C_folly : public folly::enable_shared_from_this<C_folly> {};
157   static_assert(
158     noexcept(std::declval<C_std>().shared_from_this()) ==
159     noexcept(std::declval<C_folly>().shared_from_this()), "");
160   static_assert(
161     noexcept(std::declval<C_std const>().shared_from_this()) ==
162     noexcept(std::declval<C_folly const>().shared_from_this()), "");
163   static_assert(noexcept(std::declval<C_folly>().weak_from_this()), "");
164   static_assert(noexcept(std::declval<C_folly const>().weak_from_this()), "");
165
166   // Runtime compatibility.
167   test_enable_shared_from_this(std::make_shared<C_folly>());
168   test_enable_shared_from_this(std::make_shared<C_folly const>());
169 }