3#ifndef DUNE_ALIGNED_ALLOCATOR_HH
4#define DUNE_ALIGNED_ALLOCATOR_HH
9#if !(DUNE_HAVE_C_ALIGNED_ALLOC || (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600))
10 #error Need either aligned_alloc() or posix_memalign() to compile AlignedAllocator
23 template<
class T,
int Alignment = -1>
26#if !DUNE_HAVE_C_ALIGNED_ALLOC
38 static constexpr int fixAlignment(
int align)
40 return ((Alignment==-1) ? std::alignment_of<T>::value : Alignment) > align
41 ? fixAlignment(align << 1) : align;
50 static constexpr int fixAlignment(
int align)
52 return (Alignment==-1) ? std::alignment_of<T>::value : Alignment;
58 using pointer =
typename MallocAllocator<T>::pointer;
59 using size_type =
typename MallocAllocator<T>::size_type;
60 template <
class U>
struct rebind {
64 static constexpr int alignment = fixAlignment(
sizeof(
void*));
67 pointer
allocate(size_type n,
const void* hint = 0)
72 throw std::bad_alloc();
74#if !DUNE_HAVE_C_ALIGNED_ALLOC
81 if (posix_memalign(&ret, alignment, n *
sizeof(T)) != 0)
82 throw std::bad_alloc();
84 return static_cast<pointer
>(ret);
89 pointer ret =
static_cast<pointer
>(aligned_alloc(alignment, n *
sizeof(T)));
91 throw std::bad_alloc();
Allocators which guarantee alignment of the memory.
Definition: alignedallocator.hh:24
pointer allocate(size_type n, const void *hint=0)
allocate n objects of type T
Definition: alignedallocator.hh:67
Allocators implementation which simply calls malloc/free.
Definition: mallocallocator.hh:23
size_type max_size() const noexcept
max size for allocate
Definition: mallocallocator.hh:75
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:25
Allocators that use malloc/free.
Dune namespace.
Definition: alignedallocator.hh:14