DUNE PDELab (2.8)

mallocallocator.hh
Go to the documentation of this file.
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_MALLOC_ALLOCATOR_HH
4#define DUNE_MALLOC_ALLOCATOR_HH
5
6#include <exception>
7#include <cstdlib>
8#include <new>
9#include <utility>
10
15namespace Dune
16{
21 template <class T>
23 public:
24 typedef std::size_t size_type;
25 typedef std::ptrdiff_t difference_type;
26 typedef T* pointer;
27 typedef const T* const_pointer;
28 typedef T& reference;
29 typedef const T& const_reference;
30 typedef T value_type;
31 template <class U> struct rebind {
32 typedef MallocAllocator<U> other;
33 };
34
36 MallocAllocator() noexcept {}
38 template <class U>
41 ~MallocAllocator() noexcept {}
42
43 pointer address(reference x) const
44 {
45 return &x;
46 }
47 const_pointer address(const_reference x) const
48 {
49 return &x;
50 }
51
53 pointer allocate(size_type n,
54 [[maybe_unused]] const void* hint = 0)
55 {
56 if (n > this->max_size())
57 throw std::bad_alloc();
58
59 pointer ret = static_cast<pointer>(std::malloc(n * sizeof(T)));
60 if (!ret)
61 throw std::bad_alloc();
62 return ret;
63 }
64
66 void deallocate(pointer p, [[maybe_unused]] size_type n)
67 {
68 std::free(p);
69 }
70
72 size_type max_size() const noexcept
73 {
74 return size_type(-1) / sizeof(T);
75 }
76
78 void construct(pointer p, const T& val)
79 {
80 ::new((void*)p)T(val);
81 }
82
84 template<typename ... Args>
85 void construct(pointer p, Args&&... args)
86 {
87 ::new((void *)p)T(std::forward<Args>(args) ...);
88 }
89
91 void destroy(pointer p)
92 {
93 p->~T();
94 }
95 };
96
98 template<class T>
99 constexpr bool
101 {
102 return true;
103 }
104
106 template<class T>
107 constexpr bool
109 {
110 return false;
111 }
112}
113
114#endif // DUNE_MALLOC_ALLOCATOR_HH
Allocators implementation which simply calls malloc/free.
Definition: mallocallocator.hh:22
~MallocAllocator() noexcept
cleanup this allocator
Definition: mallocallocator.hh:41
void construct(pointer p, const T &val)
copy-construct an object of type T (i.e. make a placement new on p)
Definition: mallocallocator.hh:78
MallocAllocator() noexcept
create a new MallocAllocator
Definition: mallocallocator.hh:36
MallocAllocator(const MallocAllocator< U > &) noexcept
copy construct from an other MallocAllocator, possibly for a different result type
Definition: mallocallocator.hh:39
void deallocate(pointer p, size_type n)
deallocate n objects of type T at address p
Definition: mallocallocator.hh:66
void destroy(pointer p)
destroy an object of type T (i.e. call the destructor)
Definition: mallocallocator.hh:91
pointer allocate(size_type n, const void *hint=0)
allocate n objects of type T
Definition: mallocallocator.hh:53
void construct(pointer p, Args &&... args)
construct an object of type T from variadic parameters
Definition: mallocallocator.hh:85
size_type max_size() const noexcept
max size for allocate
Definition: mallocallocator.hh:72
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:235
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:257
Dune namespace.
Definition: alignedallocator.hh:11
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)