proxygen
MemoryTest.cpp File Reference
#include <folly/Memory.h>
#include <limits>
#include <memory>
#include <type_traits>
#include <utility>
#include <glog/logging.h>
#include <folly/ConstexprMath.h>
#include <folly/String.h>
#include <folly/memory/Arena.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Classes

struct  CountedAllocatorStats
 
class  CountedAllocator< T >
 
struct  ExpectingAlloc< T >
 
struct  OverAlignedType
 

Functions

 TEST (allocateBytes, simple)
 
 TEST (aligned_malloc, examples)
 
 TEST (make_unique, compatible_with_std_make_unique)
 
 TEST (to_weak_ptr, example)
 
 TEST (SysAllocator, equality)
 
 TEST (SysAllocator, allocate_unique)
 
 TEST (SysAllocator, vector)
 
 TEST (SysAllocator, bad_alloc)
 
 TEST (AlignedSysAllocator, equality_fixed)
 
 TEST (AlignedSysAllocator, allocate_unique_fixed)
 
 TEST (AlignedSysAllocator, vector_fixed)
 
 TEST (AlignedSysAllocator, bad_alloc_fixed)
 
 TEST (AlignedSysAllocator, equality_default)
 
 TEST (AlignedSysAllocator, allocate_unique_default)
 
 TEST (AlignedSysAllocator, vector_default)
 
 TEST (AlignedSysAllocator, bad_alloc_default)
 
 TEST (AlignedSysAllocator, converting_constructor)
 
 TEST (allocate_sys_buffer, compiles)
 
 TEST (allocate_unique, ctor_failure)
 
 TEST (AllocatorObjectLifecycleTraits, compiles)
 
 TEST (allocateOverAligned, notActuallyOver)
 
 TEST (allocateOverAligned, manualOverStdAlloc)
 
 TEST (allocateOverAligned, manualOverCustomAlloc)
 
 TEST (allocateOverAligned, defaultOverCustomAlloc)
 

Variables

static constexpr std::size_t kTooBig
 

Function Documentation

TEST ( allocateBytes  ,
simple   
)

Definition at line 38 of file MemoryTest.cpp.

References folly::allocateBytes(), folly::deallocateBytes(), and EXPECT_TRUE.

38  {
39  auto p = allocateBytes(10);
40  EXPECT_TRUE(p != nullptr);
41  deallocateBytes(p, 10);
42 }
void * allocateBytes(size_t n)
Definition: Memory.h:43
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
void deallocateBytes(void *p, size_t n)
Definition: Memory.h:47
TEST ( aligned_malloc  ,
examples   
)

Definition at line 44 of file MemoryTest.cpp.

References folly::aligned_free(), folly::aligned_malloc(), EXPECT_EQ, folly::kIsSanitize, and ptr.

44  {
45  auto trial = [](size_t align) {
46  auto const ptr = aligned_malloc(1, align);
47  return (aligned_free(ptr), uintptr_t(ptr));
48  };
49 
50  if (!kIsSanitize) { // asan allocator raises SIGABRT instead
51  EXPECT_EQ(EINVAL, (trial(2), errno)) << "too small";
52  EXPECT_EQ(EINVAL, (trial(513), errno)) << "not power of two";
53  }
54 
55  EXPECT_EQ(0, trial(512) % 512);
56  EXPECT_EQ(0, trial(8192) % 8192);
57 }
void * ptr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void aligned_free(void *aligned_ptr)
Definition: Memory.h:89
void * aligned_malloc(size_t size, size_t align)
Definition: Memory.h:85
constexpr bool kIsSanitize
Definition: Portability.h:130
TEST ( make_unique  ,
compatible_with_std_make_unique   
)

Definition at line 59 of file MemoryTest.cpp.

References folly::to_shared_ptr().

59  {
60  // HACK: To enforce that `folly::` is imported here.
61  to_shared_ptr(std::unique_ptr<std::string>());
62 
63  using namespace std;
64  make_unique<string>("hello, world");
65 }
STL namespace.
std::shared_ptr< T > to_shared_ptr(std::unique_ptr< T, D > &&ptr)
Definition: Memory.h:323
TEST ( to_weak_ptr  ,
example   
)

Definition at line 67 of file MemoryTest.cpp.

References EXPECT_EQ, folly::lock(), s, and folly::to_weak_ptr().

67  {
68  auto s = std::make_shared<int>(17);
69  EXPECT_EQ(1, s.use_count());
70  EXPECT_EQ(2, (to_weak_ptr(s).lock(), s.use_count())) << "lvalue";
71  EXPECT_EQ(3, (to_weak_ptr(decltype(s)(s)).lock(), s.use_count())) << "rvalue";
72 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::weak_ptr< T > to_weak_ptr(const std::shared_ptr< T > &ptr)
Definition: Memory.h:346
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
Definition: Synchronized.h:871
static set< string > s
TEST ( SysAllocator  ,
equality   
)

Definition at line 74 of file MemoryTest.cpp.

References a, b, EXPECT_FALSE, and EXPECT_TRUE.

74  {
75  using Alloc = SysAllocator<float>;
76  Alloc const a, b;
77  EXPECT_TRUE(a == b);
78  EXPECT_FALSE(a != b);
79 }
char b
char a
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( SysAllocator  ,
allocate_unique   
)

Definition at line 81 of file MemoryTest.cpp.

References EXPECT_EQ, and ptr.

81  {
82  using Alloc = SysAllocator<float>;
83  Alloc const alloc;
84  auto ptr = allocate_unique<float>(alloc, 3.);
85  EXPECT_EQ(3., *ptr);
86 }
void * ptr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( SysAllocator  ,
vector   
)

Definition at line 88 of file MemoryTest.cpp.

References testing::ElementsAreArray(), and EXPECT_THAT.

88  {
89  using Alloc = SysAllocator<float>;
90  Alloc const alloc;
91  std::vector<float, Alloc> nums(alloc);
92  nums.push_back(3.);
93  nums.push_back(5.);
94  EXPECT_THAT(nums, testing::ElementsAreArray({3., 5.}));
95 }
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
#define EXPECT_THAT(value, matcher)
TEST ( SysAllocator  ,
bad_alloc   
)

Definition at line 97 of file MemoryTest.cpp.

References EXPECT_THROW, folly::kIsSanitize, and kTooBig.

97  {
98  using Alloc = SysAllocator<float>;
99  Alloc const alloc;
100  std::vector<float, Alloc> nums(alloc);
101  if (!kIsSanitize) {
102  EXPECT_THROW(nums.reserve(kTooBig), std::bad_alloc);
103  }
104 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
static constexpr std::size_t kTooBig
Definition: MemoryTest.cpp:34
constexpr bool kIsSanitize
Definition: Portability.h:130
TEST ( AlignedSysAllocator  ,
equality_fixed   
)

Definition at line 106 of file MemoryTest.cpp.

References a, b, EXPECT_FALSE, and EXPECT_TRUE.

106  {
108  Alloc const a, b;
109  EXPECT_TRUE(a == b);
110  EXPECT_FALSE(a != b);
111 }
char b
char a
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( AlignedSysAllocator  ,
allocate_unique_fixed   
)

Definition at line 113 of file MemoryTest.cpp.

References EXPECT_EQ, and ptr.

113  {
115  Alloc const alloc;
116  auto ptr = allocate_unique<float>(alloc, 3.);
117  EXPECT_EQ(3., *ptr);
118  EXPECT_EQ(0, std::uintptr_t(ptr.get()) % 1024);
119 }
void * ptr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( AlignedSysAllocator  ,
vector_fixed   
)

Definition at line 121 of file MemoryTest.cpp.

References testing::ElementsAreArray(), EXPECT_EQ, and EXPECT_THAT.

121  {
123  Alloc const alloc;
124  std::vector<float, Alloc> nums(alloc);
125  nums.push_back(3.);
126  nums.push_back(5.);
127  EXPECT_THAT(nums, testing::ElementsAreArray({3., 5.}));
128  EXPECT_EQ(0, std::uintptr_t(nums.data()) % 1024);
129 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
#define EXPECT_THAT(value, matcher)
TEST ( AlignedSysAllocator  ,
bad_alloc_fixed   
)

Definition at line 131 of file MemoryTest.cpp.

References EXPECT_THROW, folly::kIsSanitize, and kTooBig.

131  {
133  Alloc const alloc;
134  std::vector<float, Alloc> nums(alloc);
135  if (!kIsSanitize) {
136  EXPECT_THROW(nums.reserve(kTooBig), std::bad_alloc);
137  }
138 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
static constexpr std::size_t kTooBig
Definition: MemoryTest.cpp:34
constexpr bool kIsSanitize
Definition: Portability.h:130
TEST ( AlignedSysAllocator  ,
equality_default   
)

Definition at line 140 of file MemoryTest.cpp.

References a, b, c, EXPECT_FALSE, and EXPECT_TRUE.

140  {
142  Alloc const a(1024), b(1024), c(512);
143  EXPECT_TRUE(a == b);
144  EXPECT_FALSE(a != b);
145  EXPECT_FALSE(a == c);
146  EXPECT_TRUE(a != c);
147 }
char b
char a
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
char c
TEST ( AlignedSysAllocator  ,
allocate_unique_default   
)

Definition at line 149 of file MemoryTest.cpp.

References EXPECT_EQ, and ptr.

149  {
151  Alloc const alloc(1024);
152  auto ptr = allocate_unique<float>(alloc, 3.);
153  EXPECT_EQ(3., *ptr);
154  EXPECT_EQ(0, std::uintptr_t(ptr.get()) % 1024);
155 }
void * ptr
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( AlignedSysAllocator  ,
vector_default   
)

Definition at line 157 of file MemoryTest.cpp.

References testing::ElementsAreArray(), EXPECT_EQ, and EXPECT_THAT.

157  {
159  Alloc const alloc(1024);
160  std::vector<float, Alloc> nums(alloc);
161  nums.push_back(3.);
162  nums.push_back(5.);
163  EXPECT_THAT(nums, testing::ElementsAreArray({3., 5.}));
164  EXPECT_EQ(0, std::uintptr_t(nums.data()) % 1024);
165 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
#define EXPECT_THAT(value, matcher)
TEST ( AlignedSysAllocator  ,
bad_alloc_default   
)

Definition at line 167 of file MemoryTest.cpp.

References EXPECT_THROW, folly::kIsSanitize, and kTooBig.

167  {
169  Alloc const alloc(1024);
170  std::vector<float, Alloc> nums(alloc);
171  if (!kIsSanitize) {
172  EXPECT_THROW(nums.reserve(kTooBig), std::bad_alloc);
173  }
174 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
static constexpr std::size_t kTooBig
Definition: MemoryTest.cpp:34
constexpr bool kIsSanitize
Definition: Portability.h:130
TEST ( AlignedSysAllocator  ,
converting_constructor   
)

Definition at line 176 of file MemoryTest.cpp.

176  {
177  using Alloc1 = AlignedSysAllocator<float>;
178  using Alloc2 = AlignedSysAllocator<double>;
179  Alloc1 const alloc1(1024);
180  Alloc2 const alloc2(alloc1);
181 }
TEST ( allocate_sys_buffer  ,
compiles   
)

Definition at line 183 of file MemoryTest.cpp.

References folly::allocate_sys_buffer().

183  {
184  auto buf = allocate_sys_buffer(256);
185  // Freed at the end of the scope.
186 }
SysBufferUniquePtr allocate_sys_buffer(std::size_t size)
Definition: Memory.h:641
TEST ( allocate_unique  ,
ctor_failure   
)

Definition at line 206 of file MemoryTest.cpp.

References testing::Args(), CountedAllocatorStats::deallocates, destroy(), EXPECT_EQ, EXPECT_NE, EXPECT_THROW, and ptr.

206  {
207  struct CtorThrows {
208  explicit CtorThrows(bool cond) {
209  if (cond) {
210  throw std::runtime_error("nope");
211  }
212  }
213  };
216  {
217  CountedAllocatorStats stats;
218  Alloc const alloc(stats);
219  EXPECT_EQ(0, stats.deallocates);
220  std::unique_ptr<CtorThrows, Deleter> ptr{nullptr, Deleter{alloc}};
221  ptr = allocate_unique<CtorThrows>(alloc, false);
222  EXPECT_NE(nullptr, ptr);
223  EXPECT_EQ(0, stats.deallocates);
224  ptr = nullptr;
225  EXPECT_EQ(nullptr, ptr);
226  EXPECT_EQ(1, stats.deallocates);
227  }
228  {
229  CountedAllocatorStats stats;
230  Alloc const alloc(stats);
231  EXPECT_EQ(0, stats.deallocates);
232  std::unique_ptr<CtorThrows, Deleter> ptr{nullptr, Deleter{alloc}};
233  EXPECT_THROW(
234  ptr = allocate_unique<CtorThrows>(alloc, true), std::runtime_error);
235  EXPECT_EQ(nullptr, ptr);
236  EXPECT_EQ(1, stats.deallocates);
237  }
238 }
void * ptr
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST ( AllocatorObjectLifecycleTraits  ,
compiles   
)

Definition at line 274 of file MemoryTest.cpp.

References string, and folly::value().

274  {
275  using A = std::allocator<int>;
276  using S = std::string;
277 
278  static_assert(
281 
284 
285  static_assert(
288  int,
289  int>::value,
290  "");
291  static_assert(
294  S,
295  S>::value,
296  "");
297 
298  static_assert(
301  int>::value,
302  "");
303  static_assert(
306  S>::value,
307  "");
308 
309  static_assert(
311  "");
312  static_assert(
313  folly::AllocatorHasDefaultObjectDestroy<TestAlloc1<S>, S>::value, "");
314 
315  static_assert(
317  "");
318  static_assert(
319  !folly::AllocatorHasDefaultObjectDestroy<TestAlloc2<S>, S>::value, "");
320 
321  static_assert(
323  "");
324  static_assert(
325  !folly::AllocatorHasDefaultObjectDestroy<TestAlloc3<S>, S>::value, "");
326 
327  static_assert(
329  "");
330  static_assert(
331  folly::AllocatorHasDefaultObjectDestroy<TestAlloc4<S>, S>::value, "");
332 
333  static_assert(
335  "");
336  static_assert(
337  !folly::AllocatorHasDefaultObjectDestroy<TestAlloc5<S>, S>::value, "");
338 }
std::unique_ptr< int > A
static const char *const value
Definition: Conv.cpp:50
const char * string
Definition: Conv.cpp:212
TEST ( allocateOverAligned  ,
notActuallyOver   
)

Definition at line 371 of file MemoryTest.cpp.

References a, folly::allocationBytesForOverAligned(), and EXPECT_EQ.

371  {
372  // allocates 6 bytes with alignment 4, should get turned into an
373  // allocation of 2 elements of something of size and alignment 4
374  ExpectingAlloc<char> a(4, 2);
375  auto p = folly::allocateOverAligned<decltype(a), 4>(a, 6);
376  EXPECT_EQ((reinterpret_cast<uintptr_t>(p) % 4), 0);
377  folly::deallocateOverAligned<decltype(a), 4>(a, p, 6);
378  EXPECT_EQ((folly::allocationBytesForOverAligned<decltype(a), 4>(6)), 8);
379 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
size_t allocationBytesForOverAligned(size_t n)
Definition: Memory.h:223
TEST ( allocateOverAligned  ,
manualOverStdAlloc   
)

Definition at line 381 of file MemoryTest.cpp.

References a, folly::aligned_free(), folly::allocationBytesForOverAligned(), and EXPECT_EQ.

381  {
382  // allocates 6 bytes with alignment 64 using std::allocator, which will
383  // result in a call to aligned_malloc underneath. We free one directly
384  // to check that this is not the padding path
385  std::allocator<short> a;
386  auto p = folly::allocateOverAligned<decltype(a), 64>(a, 3);
387  auto p2 = folly::allocateOverAligned<decltype(a), 64>(a, 3);
388  EXPECT_EQ((reinterpret_cast<uintptr_t>(p) % 64), 0);
389  folly::deallocateOverAligned<decltype(a), 64>(a, p, 3);
390  aligned_free(p2);
391  EXPECT_EQ((folly::allocationBytesForOverAligned<decltype(a), 64>(3)), 6);
392 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void aligned_free(void *aligned_ptr)
Definition: Memory.h:89
char a
size_t allocationBytesForOverAligned(size_t n)
Definition: Memory.h:223
TEST ( allocateOverAligned  ,
manualOverCustomAlloc   
)

Definition at line 394 of file MemoryTest.cpp.

References a, folly::allocationBytesForOverAligned(), and EXPECT_EQ.

394  {
395  // allocates 6 byte with alignment 64 using non-standard allocator, which
396  // will result in an allocation of 64 + alignof(max_align_t) underneath.
398  alignof(folly::max_align_t), 64 / alignof(folly::max_align_t) + 1);
399  auto p = folly::allocateOverAligned<decltype(a), 64>(a, 3);
400  EXPECT_EQ((reinterpret_cast<uintptr_t>(p) % 64), 0);
401  folly::deallocateOverAligned<decltype(a), 64>(a, p, 3);
402  EXPECT_EQ(
403  (folly::allocationBytesForOverAligned<decltype(a), 64>(3)),
404  64 + alignof(folly::max_align_t));
405 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
char a
size_t allocationBytesForOverAligned(size_t n)
Definition: Memory.h:223
TEST ( allocateOverAligned  ,
defaultOverCustomAlloc   
)

Definition at line 407 of file MemoryTest.cpp.

References a, folly::allocateOverAligned(), folly::allocationBytesForOverAligned(), folly::deallocateOverAligned(), and EXPECT_EQ.

407  {
409  alignof(folly::max_align_t), 128 / alignof(folly::max_align_t));
410  auto p = folly::allocateOverAligned(a, 1);
411  EXPECT_EQ((reinterpret_cast<uintptr_t>(p) % 64), 0);
414 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::allocator_traits< Alloc >::pointer allocateOverAligned(Alloc const &alloc, size_t n)
Definition: Memory.h:198
char a
size_t allocationBytesForOverAligned(size_t n)
Definition: Memory.h:223
void deallocateOverAligned(Alloc const &alloc, typename std::allocator_traits< Alloc >::pointer ptr, size_t n)
Definition: Memory.h:212

Variable Documentation

constexpr std::size_t kTooBig
static
Initial value:
std::size_t{1} << (8 * sizeof(std::size_t) - 14))
LogLevel max
Definition: LogLevel.cpp:31
constexpr T constexpr_max(T a)
Definition: ConstexprMath.h:68

Definition at line 34 of file MemoryTest.cpp.

Referenced by TEST().