21 #include <type_traits> 24 #include <glog/logging.h> 32 using namespace folly;
36 std::size_t{1} << (8 *
sizeof(std::size_t) - 14));
45 auto trial = [](
size_t align) {
51 EXPECT_EQ(EINVAL, (trial(2), errno)) <<
"too small";
52 EXPECT_EQ(EINVAL, (trial(513), errno)) <<
"not power of two";
64 make_unique<string>(
"hello, world");
68 auto s = std::make_shared<int>(17);
84 auto ptr = allocate_unique<float>(alloc, 3.);
91 std::vector<float, Alloc> nums(alloc);
100 std::vector<float, Alloc> nums(alloc);
116 auto ptr = allocate_unique<float>(alloc, 3.);
124 std::vector<float, Alloc> nums(alloc);
128 EXPECT_EQ(0, std::uintptr_t(nums.data()) % 1024);
134 std::vector<float, Alloc> nums(alloc);
142 Alloc const a(1024),
b(1024),
c(512);
151 Alloc const alloc(1024);
152 auto ptr = allocate_unique<float>(alloc, 3.);
159 Alloc const alloc(1024);
160 std::vector<float, Alloc> nums(alloc);
164 EXPECT_EQ(0, std::uintptr_t(nums.data()) % 1024);
169 Alloc const alloc(1024);
170 std::vector<float, Alloc> nums(alloc);
179 Alloc1
const alloc1(1024);
180 Alloc2
const alloc2(alloc1);
189 size_t deallocates = 0;
192 template <
typename T>
201 std::allocator<T>::deallocate(p, n);
208 explicit CtorThrows(
bool cond) {
210 throw std::runtime_error(
"nope");
218 Alloc const alloc(stats);
220 std::unique_ptr<CtorThrows, Deleter>
ptr{
nullptr, Deleter{alloc}};
221 ptr = allocate_unique<CtorThrows>(alloc,
false);
230 Alloc const alloc(stats);
232 std::unique_ptr<CtorThrows, Deleter>
ptr{
nullptr, Deleter{alloc}};
234 ptr = allocate_unique<CtorThrows>(alloc,
true), std::runtime_error);
241 template <
typename T>
243 template <
typename U,
typename...
Args>
244 void construct(U* p,
Args&&... args) {
245 ::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
249 template <
typename T>
250 struct TestAlloc2 : TestAlloc1<T> {
251 template <
typename U>
257 template <
typename T>
258 struct TestAlloc3 : TestAlloc2<T> {
262 template <
typename T>
263 struct TestAlloc4 : TestAlloc3<T> {
267 template <
typename T>
274 TEST(AllocatorObjectLifecycleTraits, compiles) {
275 using A = std::allocator<int>;
340 template <
typename T>
345 : expectedSize_{expectedSize}, expectedCount_{expectedCount} {}
349 : expectedSize_{other.expectedSize_},
350 expectedCount_{other.expectedCount_} {}
356 return std::allocator<T>{}.allocate(n);
360 std::allocator<T>{}.deallocate(p, n);
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);
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);
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);
411 EXPECT_EQ((reinterpret_cast<uintptr_t>(p) % 64), 0);
ExpectingAlloc(std::size_t expectedSize, std::size_t expectedCount)
#define EXPECT_THROW(statement, expected_exception)
static constexpr std::size_t kTooBig
#define EXPECT_EQ(val1, val2)
void aligned_free(void *aligned_ptr)
void deallocate(T *p, std::size_t n)
CountedAllocator(CountedAllocatorStats &stats) noexcept
std::size_t expectedCount_
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
—— Concurrent Priority Queue Implementation ——
requires E e noexcept(noexcept(s.error(std::move(e))))
std::unique_ptr< T, allocator_delete< Alloc > > allocate_unique(Alloc const &alloc, Args &&...args)
T * allocate(std::size_t n)
bool_constant< true > true_type
CountedAllocatorStats * stats_
constexpr T constexpr_max(T a)
std::weak_ptr< T > to_weak_ptr(const std::shared_ptr< T > &ptr)
std::size_t expectedSize_
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
void * aligned_malloc(size_t size, size_t align)
constexpr bool kIsSanitize
std::allocator_traits< Alloc >::pointer allocateOverAligned(Alloc const &alloc, size_t n)
auto lock(Synchronized< D, M > &synchronized, Args &&...args)
std::array< char, 64 > raw_
void BENCHFUN() equality(size_t iters, size_t arg)
void * allocateBytes(size_t n)
size_t allocationBytesForOverAligned(size_t n)
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
#define EXPECT_TRUE(condition)
#define EXPECT_THAT(value, matcher)
#define EXPECT_NE(val1, val2)
SysBufferUniquePtr allocate_sys_buffer(std::size_t size)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
bool_constant< false > false_type
#define EXPECT_FALSE(condition)
void deallocateOverAligned(Alloc const &alloc, typename std::allocator_traits< Alloc >::pointer ptr, size_t n)
void deallocateBytes(void *p, size_t n)
ExpectingAlloc(ExpectingAlloc< U > const &other) noexcept
TEST(SequencedExecutor, CPUThreadPoolExecutor)
std::shared_ptr< T > to_shared_ptr(std::unique_ptr< T, D > &&ptr)
void deallocate(T *p, size_t n)