DUNE PDELab (2.8)

alignedallocator.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_ALIGNED_ALLOCATOR_HH
4#define DUNE_ALIGNED_ALLOCATOR_HH
5
6#include "mallocallocator.hh"
7#include <cstdlib>
8
9
10namespace Dune
11{
12
20 template<class T, int Alignment = -1>
22
23 /*
24 * Check whether an explicit alignment was
25 * restricted or fall back to the default alignment of T.
26 */
27 static constexpr int fixAlignment(int align)
28 {
29 return (Alignment==-1) ? std::alignment_of<T>::value : Alignment;
30 }
31
32 public:
33 using pointer = typename MallocAllocator<T>::pointer;
34 using size_type = typename MallocAllocator<T>::size_type;
35 template <class U> struct rebind {
37 };
38
39 static constexpr int alignment = fixAlignment(sizeof(void*));
40
42 pointer allocate(size_type n, [[maybe_unused]] const void* hint = 0)
43 {
44 if (n > this->max_size())
45 throw std::bad_alloc();
46
47 /*
48 * Everybody else gets the standard treatment.
49 */
50 pointer ret = static_cast<pointer>(std::aligned_alloc(alignment, n * sizeof(T)));
51 if (!ret)
52 throw std::bad_alloc();
53
54 return ret;
55 }
56 };
57
58}
59
60#endif // DUNE_ALIGNED_ALLOCATOR_HH
Allocators which guarantee alignment of the memory.
Definition: alignedallocator.hh:21
pointer allocate(size_type n, const void *hint=0)
allocate n objects of type T
Definition: alignedallocator.hh:42
Allocators implementation which simply calls malloc/free.
Definition: mallocallocator.hh:22
size_type max_size() const noexcept
max size for allocate
Definition: mallocallocator.hh:72
Allocators that use malloc/free.
Dune namespace.
Definition: alignedallocator.hh:11
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 24, 22:29, 2024)