Vc 1.4.5
SIMD Vector Classes for C++
 
Loading...
Searching...
No Matches
malloc.h
1/* This file is part of the Vc library. {{{
2Copyright © 2013-2015 Matthias Kretz <kretz@kde.org>
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are met:
6 * Redistributions of source code must retain the above copyright
7 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 * Neither the names of contributing organizations nor the
12 names of its contributors may be used to endorse or promote products
13 derived from this software without specific prior written permission.
14
15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26}}}*/
27
28#ifndef VC_COMMON_MALLOC_H_
29#define VC_COMMON_MALLOC_H_
30
31#ifndef Vc_VECTOR_DECLARED_
32#error "Incorrect inclusion order. This header must be included from Vc/vector.h only."
33#endif
34
35#if defined _WIN32 || defined _WIN64
36#include <malloc.h>
37#else
38#include <cstdlib>
39#endif
40
41#include "macros.h"
42
43namespace Vc_VERSIONED_NAMESPACE
44{
45namespace Common
46{
47
48template <size_t X> static constexpr size_t nextMultipleOf(size_t value)
49{
50 return (value % X) > 0 ? value + X - (value % X) : value;
51}
52
53template <std::size_t alignment> Vc_INTRINSIC void *aligned_malloc(std::size_t n)
54{
55#ifdef __MIC__
56 return _mm_malloc(nextMultipleOf<alignment>(n), alignment);
57#elif defined(_WIN32)
58# ifdef __GNUC__
59 return __mingw_aligned_malloc(nextMultipleOf<alignment>(n), alignment);
60# else
61 return _aligned_malloc(nextMultipleOf<alignment>(n), alignment);
62# endif
63#else
64 void *ptr = nullptr;
65 if (0 == posix_memalign(&ptr, alignment < sizeof(void *) ? sizeof(void *) : alignment,
66 nextMultipleOf<alignment>(n))) {
67 return ptr;
68 }
69 return ptr;
70#endif
71}
72
73template <Vc::MallocAlignment A> Vc_ALWAYS_INLINE void *malloc(size_t n)
74{
75 switch (A) {
77 return aligned_malloc<Vc::VectorAlignment>(n);
79 // TODO: hardcoding 64 is not such a great idea
80 return aligned_malloc<64>(n);
81 case Vc::AlignOnPage:
82 // TODO: hardcoding 4096 is not such a great idea
83 return aligned_malloc<4096>(n);
84 }
85 return nullptr;
86}
87
88Vc_ALWAYS_INLINE void free(void *p)
89{
90#ifdef __MIC__
91 _mm_free(p);
92#elif defined(_WIN32)
93# ifdef __GNUC__
94 return __mingw_aligned_free(p);
95# else
96 return _aligned_free(p);
97# endif
98#else
99 std::free(p);
100#endif
101}
102} // namespace Common
103
135template<typename T, Vc::MallocAlignment A>
136Vc_ALWAYS_INLINE T *malloc(size_t n)
137{
138 return static_cast<T *>(Common::malloc<A>(n * sizeof(T)));
139}
140
162template<typename T>
163Vc_ALWAYS_INLINE void free(T *p)
164{
165 Common::free(p);
166}
167} // namespace Vc
168
169#endif // VC_COMMON_MALLOC_H_
@ AlignOnPage
Align on boundary of page sizes (e.g.
Definition global.h:470
@ AlignOnCacheline
Align on boundary of cache line sizes (e.g.
Definition global.h:464
@ AlignOnVector
Align on boundary of vector sizes (e.g.
Definition global.h:458