Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:40

0001 /*  This file is part of the Vc library. {{{
0002 Copyright © 2013-2015 Matthias Kretz <kretz@kde.org>
0003 
0004 Redistribution and use in source and binary forms, with or without
0005 modification, are permitted provided that the following conditions are met:
0006     * Redistributions of source code must retain the above copyright
0007       notice, this list of conditions and the following disclaimer.
0008     * Redistributions in binary form must reproduce the above copyright
0009       notice, this list of conditions and the following disclaimer in the
0010       documentation and/or other materials provided with the distribution.
0011     * Neither the names of contributing organizations nor the
0012       names of its contributors may be used to endorse or promote products
0013       derived from this software without specific prior written permission.
0014 
0015 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
0016 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
0017 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0018 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
0019 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
0020 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0021 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0022 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0024 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 
0026 }}}*/
0027 
0028 #ifndef VC_COMMON_MALLOC_H_
0029 #define VC_COMMON_MALLOC_H_
0030 
0031 #ifndef Vc_VECTOR_DECLARED_
0032 #error "Incorrect inclusion order. This header must be included from Vc/vector.h only."
0033 #endif
0034 
0035 #if defined _WIN32 || defined _WIN64
0036 #include <malloc.h>
0037 #else
0038 #include <cstdlib>
0039 #endif
0040 
0041 #include "macros.h"
0042 
0043 namespace Vc_VERSIONED_NAMESPACE
0044 {
0045 namespace Common
0046 {
0047 
0048 template <size_t X> static constexpr size_t nextMultipleOf(size_t value)
0049 {
0050     return (value % X) > 0 ? value + X - (value % X) : value;
0051 }
0052 
0053 template <std::size_t alignment> Vc_INTRINSIC void *aligned_malloc(std::size_t n)
0054 {
0055 #ifdef __MIC__
0056     return _mm_malloc(nextMultipleOf<alignment>(n), alignment);
0057 #elif defined(_WIN32)
0058 # ifdef __GNUC__
0059     return __mingw_aligned_malloc(nextMultipleOf<alignment>(n), alignment);
0060 # else
0061     return _aligned_malloc(nextMultipleOf<alignment>(n), alignment);
0062 # endif
0063 #else
0064     void *ptr = nullptr;
0065     if (0 == posix_memalign(&ptr, alignment < sizeof(void *) ? sizeof(void *) : alignment,
0066                             nextMultipleOf<alignment>(n))) {
0067         return ptr;
0068     }
0069     return ptr;
0070 #endif
0071 }
0072 
0073 template <Vc::MallocAlignment A> Vc_ALWAYS_INLINE void *malloc(size_t n)
0074 {
0075     switch (A) {
0076     case Vc::AlignOnVector:
0077         return aligned_malloc<Vc::VectorAlignment>(n);
0078     case Vc::AlignOnCacheline:
0079         // TODO: hardcoding 64 is not such a great idea
0080         return aligned_malloc<64>(n);
0081     case Vc::AlignOnPage:
0082         // TODO: hardcoding 4096 is not such a great idea
0083         return aligned_malloc<4096>(n);
0084     }
0085     return nullptr;
0086 }
0087 
0088 Vc_ALWAYS_INLINE void free(void *p)
0089 {
0090 #ifdef __MIC__
0091     _mm_free(p);
0092 #elif defined(_WIN32)
0093 # ifdef __GNUC__
0094     return __mingw_aligned_free(p);
0095 # else
0096     return _aligned_free(p);
0097 # endif
0098 #else
0099     std::free(p);
0100 #endif
0101 }
0102 }  // namespace Common
0103 
0104 /**
0105  * Allocates memory on the Heap with alignment and padding suitable for vectorized access.
0106  *
0107  * Memory that was allocated with this function must be released with Vc::free! Other methods might
0108  * work but are not portable.
0109  *
0110  * \param n Specifies the number of objects the allocated memory must be able to store.
0111  * \tparam T The type of the allocated memory. Note, that the constructor is not called.
0112  * \tparam A Determines the alignment of the memory. See \ref Vc::MallocAlignment.
0113  *
0114  * \return Pointer to memory of the requested type, or 0 on error. The allocated memory is padded at
0115  * the end to be a multiple of the requested alignment \p A. Thus if you request memory for 21
0116  * int objects, aligned via Vc::AlignOnCacheline, you can safely read a full cacheline until the
0117  * end of the array, without generating an out-of-bounds access. For a cacheline size of 64 Bytes
0118  * and an int size of 4 Bytes you would thus get an array of 128 Bytes to work with.
0119  *
0120  * \warning
0121  * \li The standard malloc function specifies the number of Bytes to allocate whereas this
0122  *     function specifies the number of values, thus differing in a factor of sizeof(T).
0123  * \li This function is mainly meant for use with builtin types. If you use a custom
0124  *     type with a sizeof that is not a multiple of 2 the results might not be what you expect.
0125  * \li The constructor of T is not called. You can make up for this:
0126  * \code
0127  * SomeType *array = new(Vc::malloc<SomeType, Vc::AlignOnCacheline>(N)) SomeType[N];
0128  * \endcode
0129  *
0130  * \see Vc::free
0131  *
0132  * \ingroup Utilities
0133  * \headerfile memory.h <Vc/Memory>
0134  */
0135 template<typename T, Vc::MallocAlignment A>
0136 Vc_ALWAYS_INLINE T *malloc(size_t n)
0137 {
0138     return static_cast<T *>(Common::malloc<A>(n * sizeof(T)));
0139 }
0140 
0141 /**
0142  * Frees memory that was allocated with Vc::malloc.
0143  *
0144  * \param p The pointer to the memory to be freed.
0145  *
0146  * \tparam T The type of the allocated memory.
0147  *
0148  * \warning The destructor of T is not called. If needed, you can call the destructor before calling
0149  * free:
0150  * \code
0151  * for (int i = 0; i < N; ++i) {
0152  *   p[i].~T();
0153  * }
0154  * Vc::free(p);
0155  * \endcode
0156  *
0157  * \ingroup Utilities
0158  * \headerfile memory.h <Vc/Memory>
0159  *
0160  * \see Vc::malloc
0161  */
0162 template<typename T>
0163 Vc_ALWAYS_INLINE void free(T *p)
0164 {
0165     Common::free(p);
0166 }
0167 }  // namespace Vc
0168 
0169 #endif // VC_COMMON_MALLOC_H_