dune-common  2.7.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 #include <dune/common/unused.hh>
11 
16 namespace Dune
17 {
22  template <class T>
24  public:
25  typedef std::size_t size_type;
26  typedef std::ptrdiff_t difference_type;
27  typedef T* pointer;
28  typedef const T* const_pointer;
29  typedef T& reference;
30  typedef const T& const_reference;
31  typedef T value_type;
32  template <class U> struct rebind {
34  };
35 
37  MallocAllocator() noexcept {}
39  template <class U>
40  MallocAllocator(const MallocAllocator<U>&) noexcept {}
42  ~MallocAllocator() noexcept {}
43 
45  {
46  return &x;
47  }
49  {
50  return &x;
51  }
52 
55  const void* hint = 0)
56  {
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 
69  {
71  std::free(p);
72  }
73 
75  size_type max_size() const noexcept
76  {
77  return size_type(-1) / sizeof(T);
78  }
79 
81  void construct(pointer p, const T& val)
82  {
83  ::new((void*)p)T(val);
84  }
85 
87  template<typename ... Args>
88  void construct(pointer p, Args&&... args)
89  {
90  ::new((void *)p)T(std::forward<Args>(args) ...);
91  }
92 
94  void destroy(pointer p)
95  {
96  p->~T();
97  }
98  };
99 
101  template<class T>
102  constexpr bool
104  {
105  return true;
106  }
107 
109  template<class T>
110  constexpr bool
112  {
113  return false;
114  }
115 }
116 
117 #endif // DUNE_MALLOC_ALLOCATOR_HH
Dune::MallocAllocator::address
const_pointer address(const_reference x) const
Definition: mallocallocator.hh:48
Dune::MallocAllocator::difference_type
std::ptrdiff_t difference_type
Definition: mallocallocator.hh:26
Dune::MallocAllocator::value_type
T value_type
Definition: mallocallocator.hh:31
Dune::MallocAllocator::size_type
std::size_t size_type
Definition: mallocallocator.hh:25
Dune::const_reference
Get the 'const' version of a reference to a mutable object.
Definition: genericiterator.hh:84
Dune::MallocAllocator
Allocators implementation which simply calls malloc/free.
Definition: mallocallocator.hh:23
Dune::MallocAllocator::const_pointer
const typedef T * const_pointer
Definition: mallocallocator.hh:28
DUNE_UNUSED_PARAMETER
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:25
Dune::MallocAllocator::pointer
T * pointer
Definition: mallocallocator.hh:27
Dune::MallocAllocator::rebind::other
MallocAllocator< U > other
Definition: mallocallocator.hh:33
Dune::MallocAllocator::MallocAllocator
MallocAllocator(const MallocAllocator< U > &) noexcept
copy construct from an other MallocAllocator, possibly for a different result type
Definition: mallocallocator.hh:40
Dune::operator!=
constexpr bool operator!=(const DebugAllocator< T > &, const DebugAllocator< T > &)
check whether allocators are not equivalent
Definition: debugallocator.hh:318
Dune::MallocAllocator::deallocate
void deallocate(pointer p, size_type n)
deallocate n objects of type T at address p
Definition: mallocallocator.hh:68
Dune::MallocAllocator::allocate
pointer allocate(size_type n, const void *hint=0)
allocate n objects of type T
Definition: mallocallocator.hh:54
Dune::MallocAllocator::~MallocAllocator
~MallocAllocator() noexcept
cleanup this allocator
Definition: mallocallocator.hh:42
Dune::MallocAllocator::construct
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:81
Dune::MallocAllocator::reference
T & reference
Definition: mallocallocator.hh:29
Dune::MallocAllocator::rebind
Definition: mallocallocator.hh:32
unused.hh
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
Dune::MallocAllocator::destroy
void destroy(pointer p)
destroy an object of type T (i.e. call the destructor)
Definition: mallocallocator.hh:94
Dune::MallocAllocator::construct
void construct(pointer p, Args &&... args)
construct an object of type T from variadic parameters
Definition: mallocallocator.hh:88
Dune::MallocAllocator::MallocAllocator
MallocAllocator() noexcept
create a new MallocAllocator
Definition: mallocallocator.hh:37
Dune::operator==
constexpr bool operator==(const DebugAllocator< T > &, const DebugAllocator< T > &)
check whether allocators are equivalent
Definition: debugallocator.hh:310
Dune::MallocAllocator::address
pointer address(reference x) const
Definition: mallocallocator.hh:44
Dune::MallocAllocator::max_size
size_type max_size() const noexcept
max size for allocate
Definition: mallocallocator.hh:75
Dune
Dune namespace.
Definition: alignedallocator.hh:13
Dune::MallocAllocator::const_reference
const typedef T & const_reference
Definition: mallocallocator.hh:30