1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #ifndef CEPH_COMMON_ALLOCATOR_H
5 #define CEPH_COMMON_ALLOCATOR_H
9 #ifdef LIBTCMALLOC_MISSING_ALIGNED_ALLOC
17 #ifdef LIBTCMALLOC_MISSING_ALIGNED_ALLOC
19 // If libtcmalloc is missing 'aligned_alloc', provide a new allocator class that
20 // uses memalign which is what newer versions of tcmalloc do internally. C++17
21 // will automatically use 'operator new(size_t, align_val_t)' for aligned
22 // structures, which will invoke the missing 'aligned_alloc' tcmalloc function.
23 // This method was added to tcmalloc (gperftools) in commit d406f228 after
24 // the 2.6.1 release was tagged.
26 struct allocator
: public std::allocator
<T
> {
27 using pointer
= typename
std::allocator
<T
>::pointer
;
28 using size_type
= typename
std::allocator
<T
>::size_type
;
32 typedef allocator
<U
> other
;
35 allocator() noexcept
{
38 allocator(const allocator
& other
) noexcept
: std::allocator
<T
>(other
) {
42 allocator(const allocator
<U
>& other
) noexcept
: std::allocator
<T
>(other
) {
45 pointer
allocate(size_type n
, const void* hint
= nullptr) {
46 if (n
> this->max_size()) {
47 throw std::bad_alloc();
50 if (alignof(T
) > __STDCPP_DEFAULT_NEW_ALIGNMENT__
) {
51 return static_cast<T
*>(memalign(alignof(T
), n
* sizeof(T
)));
53 return std::allocator
<T
>::allocate(n
, hint
);
57 #else // LIBTCMALLOC_MISSING_ALIGNED_ALLOC
59 // re-use the full std::allocator implementation if tcmalloc is functional
62 using allocator
= std::allocator
<T
>;
64 #endif // LIBTCMALLOC_MISSING_ALIGNED_ALLOC
68 #endif // CEPH_COMMON_ALLOCATOR_H