dune-common  2.8.0
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 
15 namespace 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 {
33  };
34 
36  MallocAllocator() noexcept {}
38  template <class U>
39  MallocAllocator(const MallocAllocator<U>&) noexcept {}
41  ~MallocAllocator() noexcept {}
42 
44  {
45  return &x;
46  }
48  {
49  return &x;
50  }
51 
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
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
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
Dune namespace.
Definition: alignedallocator.hh:11
Allocators implementation which simply calls malloc/free.
Definition: mallocallocator.hh:22
~MallocAllocator() noexcept
cleanup this allocator
Definition: mallocallocator.hh:41
T * pointer
Definition: mallocallocator.hh:26
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
std::size_t size_type
Definition: mallocallocator.hh:24
std::ptrdiff_t difference_type
Definition: mallocallocator.hh:25
T value_type
Definition: mallocallocator.hh:30
const_pointer address(const_reference x) const
Definition: mallocallocator.hh:47
const T & const_reference
Definition: mallocallocator.hh:29
void destroy(pointer p)
destroy an object of type T (i.e. call the destructor)
Definition: mallocallocator.hh:91
void deallocate(pointer p, [[maybe_unused]] size_type n)
deallocate n objects of type T at address p
Definition: mallocallocator.hh:66
pointer address(reference x) const
Definition: mallocallocator.hh:43
void construct(pointer p, Args &&... args)
construct an object of type T from variadic parameters
Definition: mallocallocator.hh:85
T & reference
Definition: mallocallocator.hh:28
pointer allocate(size_type n, [[maybe_unused]] const void *hint=0)
allocate n objects of type T
Definition: mallocallocator.hh:53
size_type max_size() const noexcept
max size for allocate
Definition: mallocallocator.hh:72
const T * const_pointer
Definition: mallocallocator.hh:27
Definition: mallocallocator.hh:31
MallocAllocator< U > other
Definition: mallocallocator.hh:32