piranha  0.10
dynamic_aligning_allocator.hpp
1 /* Copyright 2009-2017 Francesco Biscani (bluescarni@gmail.com)
2 
3 This file is part of the Piranha library.
4 
5 The Piranha library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8  * the GNU Lesser General Public License as published by the Free
9  Software Foundation; either version 3 of the License, or (at your
10  option) any later version.
11 
12 or
13 
14  * the GNU General Public License as published by the Free Software
15  Foundation; either version 3 of the License, or (at your option) any
16  later version.
17 
18 or both in parallel, as here.
19 
20 The Piranha library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the Piranha library. If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #ifndef PIRANHA_DYNAMIC_ALIGNING_ALLOCATOR_HPP
30 #define PIRANHA_DYNAMIC_ALIGNING_ALLOCATOR_HPP
31 
32 #include <cstddef>
33 #include <limits>
34 #include <new>
35 #include <type_traits>
36 
37 #include <piranha/config.hpp>
38 #include <piranha/memory.hpp>
39 
40 namespace piranha
41 {
42 
44 
49 template <typename T>
51 {
52 public:
54 
57  using value_type = T;
59  using size_type = std::size_t;
61 
66 
69  dynamic_aligning_allocator() : m_alignment(0)
70  {
71  }
77 
80  explicit dynamic_aligning_allocator(const std::size_t &alignment) : m_alignment(alignment)
81  {
82  }
84 
89  template <typename U>
90  explicit dynamic_aligning_allocator(const dynamic_aligning_allocator<U> &other) : m_alignment(other.alignment())
91  {
92  }
94 
99  template <typename U>
100  explicit dynamic_aligning_allocator(dynamic_aligning_allocator<U> &&other) : m_alignment(other.alignment())
101  {
102  }
104  ~dynamic_aligning_allocator() = default;
106 
113 
120 
124  constexpr size_type max_size() const
125  {
126  return std::numeric_limits<size_type>::max() / sizeof(value_type);
127  }
129 
139  value_type *allocate(const size_type &size) const
140  {
141  if (unlikely(size > max_size())) {
142  piranha_throw(std::bad_alloc, );
143  }
144  return static_cast<value_type *>(
145  aligned_palloc(m_alignment, static_cast<size_type>(size * sizeof(value_type))));
146  }
148 
155  void deallocate(value_type *ptr, const size_type &) const
156  {
157  aligned_pfree(m_alignment, ptr);
158  }
160 
165  bool operator==(const dynamic_aligning_allocator &other) const
166  {
167  return m_alignment == other.m_alignment;
168  }
170 
175  bool operator!=(const dynamic_aligning_allocator &other) const
176  {
177  return !(this->operator==(other));
178  }
180 
183  std::size_t alignment() const
184  {
185  return m_alignment;
186  }
187 
188 private:
189  std::size_t m_alignment;
190 };
191 }
192 
193 #endif
void * aligned_palloc(const std::size_t &alignment, const std::size_t &size)
Allocate memory aligned to a specific value.
Definition: memory.hpp:134
void deallocate(value_type *ptr, const size_type &) const
Deallocation function.
dynamic_aligning_allocator & operator=(const dynamic_aligning_allocator &other)=default
Copy assignment operator.
constexpr size_type max_size() const
Maximum allocatable size.
std::size_t alignment() const
Alignment getter.
bool operator!=(const dynamic_aligning_allocator &other) const
Inequality operator.
value_type * allocate(const size_type &size) const
Allocation function.
dynamic_aligning_allocator(dynamic_aligning_allocator< U > &&other)
Converting move constructor.
Memory allocator with runtime alignment support.
std::true_type propagate_on_container_move_assignment
Move assignment propagation.
bool operator==(const dynamic_aligning_allocator &other) const
Equality operator.
#define piranha_throw(exception_type,...)
Exception-throwing macro.
Definition: exceptions.hpp:118
Root piranha namespace.
Definition: array_key.hpp:52
void aligned_pfree(const std::size_t &alignment, void *ptr)
Free memory allocated via piranha::aligned_alloc.
Definition: memory.hpp:182
dynamic_aligning_allocator(const dynamic_aligning_allocator< U > &other)
Converting copy constructor.
~dynamic_aligning_allocator()=default
Defaulted destructor.
Low-level memory management functions.
dynamic_aligning_allocator(const std::size_t &alignment)
Constructor from alignment value.