DUNE PDELab (git)

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// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_MALLOC_ALLOCATOR_HH
6#define DUNE_MALLOC_ALLOCATOR_HH
7
8#include <exception>
9#include <cstdlib>
10#include <new>
11#include <utility>
12
17namespace Dune
18{
23 template <class T>
25 public:
26 typedef std::size_t size_type;
27 typedef std::ptrdiff_t difference_type;
28 typedef T* pointer;
29 typedef const T* const_pointer;
30 typedef T& reference;
31 typedef const T& const_reference;
32 typedef T value_type;
33 template <class U> struct rebind {
34 typedef MallocAllocator<U> other;
35 };
36
38 MallocAllocator() noexcept {}
40 template <class U>
43 ~MallocAllocator() noexcept {}
44
45 pointer address(reference x) const
46 {
47 return &x;
48 }
49 const_pointer address(const_reference x) const
50 {
51 return &x;
52 }
53
55 pointer allocate(size_type n,
56 [[maybe_unused]] const void* hint = 0)
57 {
58 if (n > this->max_size())
59 throw std::bad_alloc();
60
61 pointer ret = static_cast<pointer>(std::malloc(n * sizeof(T)));
62 if (!ret)
63 throw std::bad_alloc();
64 return ret;
65 }
66
68 void deallocate(pointer p, [[maybe_unused]] size_type n)
69 {
70 std::free(p);
71 }
72
74 size_type max_size() const noexcept
75 {
76 return size_type(-1) / sizeof(T);
77 }
78
80 void construct(pointer p, const T& val)
81 {
82 ::new((void*)p)T(val);
83 }
84
86 template<typename ... Args>
87 void construct(pointer p, Args&&... args)
88 {
89 ::new((void *)p)T(std::forward<Args>(args) ...);
90 }
91
93 void destroy(pointer p)
94 {
95 p->~T();
96 }
97 };
98
100 template<class T>
101 constexpr bool
103 {
104 return true;
105 }
106
108 template<class T>
109 constexpr bool
111 {
112 return false;
113 }
114}
115
116#endif // DUNE_MALLOC_ALLOCATOR_HH
Allocators implementation which simply calls malloc/free.
Definition: mallocallocator.hh:24
~MallocAllocator() noexcept
cleanup this allocator
Definition: mallocallocator.hh:43
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:80
MallocAllocator() noexcept
create a new MallocAllocator
Definition: mallocallocator.hh:38
MallocAllocator(const MallocAllocator< U > &) noexcept
copy construct from an other MallocAllocator, possibly for a different result type
Definition: mallocallocator.hh:41
void deallocate(pointer p, size_type n)
deallocate n objects of type T at address p
Definition: mallocallocator.hh:68
void destroy(pointer p)
destroy an object of type T (i.e. call the destructor)
Definition: mallocallocator.hh:93
pointer allocate(size_type n, const void *hint=0)
allocate n objects of type T
Definition: mallocallocator.hh:55
void construct(pointer p, Args &&... args)
construct an object of type T from variadic parameters
Definition: mallocallocator.hh:87
size_type max_size() const noexcept
max size for allocate
Definition: mallocallocator.hh:74
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:238
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:260
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)