From 19fcec84d8d7d21e796c7624e521b60d28ee21ed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:45:59 +0200 Subject: Adding upstream version 16.2.11+ds. Signed-off-by: Daniel Baumann --- src/common/allocator.h | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/common/allocator.h (limited to 'src/common/allocator.h') diff --git a/src/common/allocator.h b/src/common/allocator.h new file mode 100644 index 000000000..b28572faa --- /dev/null +++ b/src/common/allocator.h @@ -0,0 +1,69 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#ifndef CEPH_COMMON_ALLOCATOR_H +#define CEPH_COMMON_ALLOCATOR_H + +#include "acconfig.h" + +#ifdef LIBTCMALLOC_MISSING_ALIGNED_ALLOC +#include +#include +#endif +#include + +namespace ceph { + +#ifdef LIBTCMALLOC_MISSING_ALIGNED_ALLOC + +// If libtcmalloc is missing 'aligned_alloc', provide a new allocator class that +// uses memalign which is what newer versions of tcmalloc do internally. C++17 +// will automatically use 'operator new(size_t, align_val_t)' for aligned +// structures, which will invoke the missing 'aligned_alloc' tcmalloc function. +// This method was added to tcmalloc (gperftools) in commit d406f228 after +// the 2.6.1 release was tagged. +template +struct allocator : public std::allocator { + using pointer = typename std::allocator::pointer; + using size_type = typename std::allocator::size_type; + + template + struct rebind { + typedef allocator other; + }; + + allocator() noexcept { + } + + allocator(const allocator& other) noexcept : std::allocator(other) { + } + + template + allocator(const allocator& other) noexcept : std::allocator(other) { + } + + pointer allocate(size_type n, const void* hint = nullptr) { + if (n > this->max_size()) { + throw std::bad_alloc(); + } + + if (alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { + return static_cast(memalign(alignof(T), n * sizeof(T))); + } + return std::allocator::allocate(n, hint); + } +}; + +#else // LIBTCMALLOC_MISSING_ALIGNED_ALLOC + +// re-use the full std::allocator implementation if tcmalloc is functional + +template +using allocator = std::allocator; + +#endif // LIBTCMALLOC_MISSING_ALIGNED_ALLOC + +} // namespace ceph + +#endif // CEPH_COMMON_ALLOCATOR_H + -- cgit v1.2.3