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