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 227
|
/*
* Simd Library (http://ermig1979.github.io/Simd).
*
* Copyright (c) 2011-2020 Yermalayeu Ihar.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __SimdAllocator_hpp__
#define __SimdAllocator_hpp__
#include "Simd/SimdLib.h"
#include <memory>
namespace Simd
{
/*! @ingroup cpp_allocator
\short Aligned memory allocator.
Performs allocation and deletion of aligned memory.
\note Also it can be used as an allocator for STL containers.
*/
template <class T> struct Allocator
{
/*!
\fn void * Allocate(size_t size, size_t align);
\short Allocates aligned memory block.
\note The memory allocated by this function is must be deleted by function Simd::Allocator::Free.
\param [in] size - a size of required memory block.
\param [in] align - an align of allocated memory address.
\return a pointer to allocated memory.
*/
static SIMD_INLINE void * Allocate(size_t size, size_t align)
{
#ifdef __SimdMemory_h__
return Simd::Allocate(size, align);
#else
return SimdAllocate(size, align);
#endif
}
/*!
\fn void Free(void * ptr);
\short Frees aligned memory block.
\note This function frees a memory allocated by function Simd::Allocator::Allocate.
\param [in] ptr - a pointer to the memory to be deleted.
*/
static SIMD_INLINE void Free(void * ptr)
{
#ifdef __SimdMemory_h__
Simd::Free(ptr);
#else
SimdFree(ptr);
#endif
}
/*!
\fn size_t Align(size_t size, size_t align);
\short Gets aligned size.
\param [in] size - an original size.
\param [in] align - a required alignment.
\return an aligned size.
*/
static SIMD_INLINE size_t Align(size_t size, size_t align)
{
#ifdef __SimdMemory_h__
return Simd::AlignHi(size, align);
#else
return SimdAlign(size, align);
#endif
}
/*!
\fn void * Align(void * ptr, size_t align);
\short Gets aligned address.
\param [in] ptr - an original pointer.
\param [in] align - a required alignment.
\return an aligned address.
*/
static SIMD_INLINE void * Align(void * ptr, size_t align)
{
#ifdef __SimdMemory_h__
return Simd::AlignHi(ptr, align);
#else
return (void *)SimdAlign((size_t)ptr, align);
#endif
}
/*!
\fn size_t Alignment();
\short Gets memory alignment required for the most productive work.
\return a required memory alignment.
*/
static SIMD_INLINE size_t Alignment()
{
#if defined(__SimdAlignment_h__) && defined(_WIN32)
return Simd::Alignment();
#else
return SimdAlignment();
#endif
}
//---------------------------------------------------------------------
// STL allocator interface implementation:
typedef T value_type;
typedef T * pointer;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T & reference;
typedef const T & const_reference;
typedef const T * const_pointer;
template <typename U>
struct rebind
{
typedef Allocator<U> other;
};
SIMD_INLINE Allocator()
{
}
template <typename U> SIMD_INLINE Allocator(const Allocator<U> & /*a*/)
{
}
SIMD_INLINE const_pointer address(const_reference value) const
{
#if defined(SIMD_CPP_2011_ENABLE)
return std::addressof(value);
#else
return (reinterpret_cast<const_pointer>(&const_cast<char&>(reinterpret_cast<const volatile char&>(value))));
#endif
}
SIMD_INLINE pointer address(reference value) const
{
#if defined(SIMD_CPP_2011_ENABLE)
return std::addressof(value);
#else
return (reinterpret_cast<pointer>(&const_cast<char&>(reinterpret_cast<const volatile char&>(value))));
#endif
}
SIMD_INLINE pointer allocate(size_type size, const void * /*ptr*/ = NULL)
{
return static_cast<pointer>(Allocate(size * sizeof(T), Alignment()));
}
SIMD_INLINE size_type max_size() const
{
return ~static_cast<std::size_t>(0) / sizeof(T);
}
SIMD_INLINE void deallocate(pointer ptr, size_type /*size*/)
{
Free(ptr);
}
template<class U, class V> SIMD_INLINE void construct(U * ptr, const V & value)
{
::new((void*)ptr) U(value);
}
#if defined(SIMD_CPP_2011_ENABLE)
template<class U, class... Args> SIMD_INLINE void construct(U * ptr, Args &&... args)
{
::new((void*)ptr) U(std::forward<Args>(args)...);
}
#endif
template<class U> SIMD_INLINE void construct(U * ptr)
{
::new((void*)ptr) U();
}
template<class U> SIMD_INLINE void destroy(U * ptr)
{
ptr->~U();
}
};
template<typename T1, typename T2> SIMD_INLINE bool operator == (const Allocator<T1> & /*a1*/, const Allocator<T2> & /*a2*/)
{
return true;
}
template<typename T1, typename T2> SIMD_INLINE bool operator != (const Allocator<T1> & /*a1*/, const Allocator<T2> & /*a2*/)
{
return false;
}
}
#endif//__SimdAllocator_hpp__
|