1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
Consult the COPYING file in the top-level source directory of this
module for the precise wording of the license and the list of
copyright holders.
*/
/*!
* \file
* \brief This is a stand-alone version of boost::alignment::aligned_allocator from Boost
* 1.58
*
* The file has been modified to assume a C++-2011 compatible compiler on a POSIX
* operating system to remove the boost dependencies which the original version
* contained. The original copyright notice for this file is:
*
<pre>
(c) 2014 Glen Joseph Fernandes
glenjofe at gmail dot com
Distributed under the Boost Software
License, Version 1.0.
http://boost.org/LICENSE_1_0.txt
</pre>
*/
#ifndef EWOMS_ALIGNED_ALLOCATOR_HH
#define EWOMS_ALIGNED_ALLOCATOR_HH
#include <utility>
#include <memory>
#include <type_traits>
#include <cassert>
namespace Opm {
namespace detail {
constexpr inline bool is_alignment(std::size_t value) noexcept
{
return (value > 0) && ((value & (value - 1)) == 0);
}
template<std::size_t N>
struct is_alignment_constant
: std::integral_constant<bool, (N > 0) && ((N & (N - 1)) == 0)>
{};
template<std::size_t A, std::size_t B>
struct min_size
: std::integral_constant<std::size_t, (A < B) ? A : B>
{ };
template<class T>
struct offset_object
{
char offset;
T object;
};
template<class T>
struct alignment_of
: min_size<sizeof(T), sizeof(offset_object<T>) - sizeof(T)>::type
{};
template<std::size_t A, std::size_t B>
struct max_align
: std::integral_constant<std::size_t,(A > B) ? A : B>
{};
template<class T>
struct max_count_of
: std::integral_constant<std::size_t, ~static_cast<std::size_t>(0) / sizeof(T)>
{};
using std::addressof;
}
inline void* aligned_alloc(std::size_t alignment,
std::size_t size) noexcept
{
assert(detail::is_alignment(alignment));
if (alignment < sizeof(void*)) {
alignment = sizeof(void*);
}
void* p;
if (::posix_memalign(&p, alignment, size) != 0) {
p = 0;
}
return p;
}
inline void aligned_free(void* ptr)
noexcept
{
::free(ptr);
}
template<class T, std::size_t Alignment>
class aligned_allocator {
static_assert(detail::is_alignment_constant<Alignment>::value, "Alignment must be powers of two!");
public:
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using void_pointer = void*;
using const_void_pointer = const void*;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = T&;
using const_reference = const T&;
private:
using MaxAlign = detail::max_align<Alignment, detail::alignment_of<value_type>::value>;
public:
template<class U>
struct rebind {
using other = aligned_allocator<U, Alignment>;
};
aligned_allocator()
noexcept = default;
template<class U>
aligned_allocator(const aligned_allocator<U,
Alignment>&) noexcept {
}
pointer address(reference value) const
noexcept {
return detail::addressof(value);
}
const_pointer address(const_reference value) const
noexcept {
return detail::addressof(value);
}
pointer allocate(size_type size,
const_void_pointer = 0) {
void* p = aligned_alloc(MaxAlign::value,
sizeof(T) * size);
if (!p && size > 0) {
throw std::bad_alloc();
}
return static_cast<T*>(p);
}
void deallocate(pointer ptr, size_type) {
aligned_free(ptr);
}
constexpr size_type max_size() const
noexcept {
return detail::max_count_of<T>::value;
}
template<class U, class... Args>
void construct(U* ptr, Args&&... args) {
void* p = ptr;
::new(p) U(std::forward<Args>(args)...);
}
template<class U>
void construct(U* ptr) {
void* p = ptr;
::new(p) U();
}
template<class U>
void destroy(U* ptr) {
(void)ptr;
ptr->~U();
}
};
template<std::size_t Alignment>
class aligned_allocator<void, Alignment> {
static_assert(detail::is_alignment_constant<Alignment>::value,
"The specified alignment is not a power of two!");
public:
using value_type = void;
using pointer = void*;
using const_pointer = const void*;
template<class U>
struct rebind {
using other = aligned_allocator<U, Alignment>;
};
};
template<class T1, class T2, std::size_t Alignment>
inline bool operator==(const aligned_allocator<T1,
Alignment>&, const aligned_allocator<T2,
Alignment>&) noexcept
{
return true;
}
template<class T1, class T2, std::size_t Alignment>
inline bool operator!=(const aligned_allocator<T1,
Alignment>&, const aligned_allocator<T2,
Alignment>&) noexcept
{
return false;
}
}
#endif
|