piranha  0.10
cache_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_CACHE_ALIGNING_ALLOCATOR_HPP
30 #define PIRANHA_CACHE_ALIGNING_ALLOCATOR_HPP
31 
32 #include <cstddef>
33 #include <utility>
34 
35 #include <piranha/dynamic_aligning_allocator.hpp>
36 #include <piranha/memory.hpp>
37 #include <piranha/safe_cast.hpp>
38 #include <piranha/settings.hpp>
39 
40 namespace piranha
41 {
42 
44 
49 template <typename T>
51 {
53  static std::size_t determine_alignment()
54  {
55 #if !defined(PIRANHA_HAVE_MEMORY_ALIGNMENT_PRIMITIVES)
56  return 0u;
57 #endif
58  try {
59  const std::size_t alignment = safe_cast<std::size_t>(settings::get_cache_line_size());
60  if (!alignment_check<T>(alignment)) {
61  return 0u;
62  }
63  return alignment;
64  } catch (...) {
65  return 0u;
66  }
67  }
68 
69 public:
70  // NOTE: these members (down to the default ctor) are not needed according
71  // to the C++11 standard, but GCC's stdlib does not support allocator aware
72  // containers yet.
74  template <typename U>
75  struct rebind {
78  };
80  using pointer = T *;
82  using const_pointer = T const *;
84  using reference = T &;
86  using const_reference = T const &;
88 
91  void destroy(pointer p)
92  {
93  p->~T();
94  }
96 
100  // NOTE: here, according to the standard, the allocator must be
101  // able to construct objects of arbitrary type:
102  // http://en.cppreference.com/w/cpp/concept/Allocator
103  template <typename U, typename... Args>
104  void construct(U *p, Args &&... args)
105  {
106  ::new (static_cast<void *>(p)) U(std::forward<Args>(args)...);
107  }
109 
116  cache_aligning_allocator() : base(determine_alignment())
117  {
118  }
124 
129  template <typename U>
131  {
132  }
134 
139  template <typename U>
141  {
142  }
144  ~cache_aligning_allocator() = default;
146 
153 
159 };
160 }
161 
162 #endif
T const & const_reference
Const reference type.
std::size_t alignment() const
Alignment getter.
static unsigned get_cache_line_size()
Get the cache line size.
Definition: settings.hpp:166
~cache_aligning_allocator()=default
Defaulted destructor.
STL namespace.
Memory allocator with runtime alignment support.
cache_aligning_allocator(const cache_aligning_allocator< U > &other)
Copy-constructor from different instance.
cache_aligning_allocator(cache_aligning_allocator< U > &&other)
Move-constructor from different instance.
cache_aligning_allocator & operator=(const cache_aligning_allocator &other)=default
Copy assignment operator.
void destroy(pointer p)
Destructor method.
Root piranha namespace.
Definition: array_key.hpp:52
void construct(U *p, Args &&... args)
Variadic construction method.
T const * const_pointer
Const pointer type.
Allocator that tries to align memory to the cache line size.
Low-level memory management functions.
safe_cast_type< To, From > safe_cast(const From &x)
Safe cast.
Definition: safe_cast.hpp:219