cannam@95
|
1 /*
|
cannam@95
|
2 * Copyright (c) 2003, 2007-11 Matteo Frigo
|
cannam@95
|
3 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
|
cannam@95
|
4 *
|
cannam@95
|
5 * This program is free software; you can redistribute it and/or modify
|
cannam@95
|
6 * it under the terms of the GNU General Public License as published by
|
cannam@95
|
7 * the Free Software Foundation; either version 2 of the License, or
|
cannam@95
|
8 * (at your option) any later version.
|
cannam@95
|
9 *
|
cannam@95
|
10 * This program is distributed in the hope that it will be useful,
|
cannam@95
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
cannam@95
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
cannam@95
|
13 * GNU General Public License for more details.
|
cannam@95
|
14 *
|
cannam@95
|
15 * You should have received a copy of the GNU General Public License
|
cannam@95
|
16 * along with this program; if not, write to the Free Software
|
cannam@95
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
cannam@95
|
18 *
|
cannam@95
|
19 */
|
cannam@95
|
20
|
cannam@95
|
21
|
cannam@95
|
22 #include "ifftw.h"
|
cannam@95
|
23
|
cannam@95
|
24 /* ``kernel'' malloc(), with proper memory alignment */
|
cannam@95
|
25
|
cannam@95
|
26 #if defined(HAVE_DECL_MEMALIGN) && !HAVE_DECL_MEMALIGN
|
cannam@95
|
27 # if defined(HAVE_MALLOC_H)
|
cannam@95
|
28 # include <malloc.h>
|
cannam@95
|
29 # else
|
cannam@95
|
30 extern void *memalign(size_t, size_t);
|
cannam@95
|
31 # endif
|
cannam@95
|
32 #endif
|
cannam@95
|
33
|
cannam@95
|
34 #if defined(HAVE_DECL_POSIX_MEMALIGN) && !HAVE_DECL_POSIX_MEMALIGN
|
cannam@95
|
35 extern int posix_memalign(void **, size_t, size_t);
|
cannam@95
|
36 #endif
|
cannam@95
|
37
|
cannam@95
|
38 #if defined(macintosh) /* MacOS 9 */
|
cannam@95
|
39 # include <Multiprocessing.h>
|
cannam@95
|
40 #endif
|
cannam@95
|
41
|
cannam@95
|
42 #define real_free free /* memalign and malloc use ordinary free */
|
cannam@95
|
43
|
cannam@95
|
44 #define IS_POWER_OF_TWO(n) (((n) > 0) && (((n) & ((n) - 1)) == 0))
|
cannam@95
|
45 #if defined(WITH_OUR_MALLOC) && (MIN_ALIGNMENT >= 8) && IS_POWER_OF_TWO(MIN_ALIGNMENT)
|
cannam@95
|
46 /* Our own MIN_ALIGNMENT-aligned malloc/free. Assumes sizeof(void*) is a
|
cannam@95
|
47 power of two <= 8 and that malloc is at least sizeof(void*)-aligned.
|
cannam@95
|
48
|
cannam@95
|
49 The main reason for this routine is that, as of this writing,
|
cannam@95
|
50 Windows does not include any aligned allocation routines in its
|
cannam@95
|
51 system libraries, and instead provides an implementation with a
|
cannam@95
|
52 Visual C++ "Processor Pack" that you have to statically link into
|
cannam@95
|
53 your program. We do not want to require users to have VC++
|
cannam@95
|
54 (e.g. gcc/MinGW should be fine). Our code should be at least as good
|
cannam@95
|
55 as the MS _aligned_malloc, in any case, according to second-hand
|
cannam@95
|
56 reports of the algorithm it employs (also based on plain malloc). */
|
cannam@95
|
57 static void *our_malloc(size_t n)
|
cannam@95
|
58 {
|
cannam@95
|
59 void *p0, *p;
|
cannam@95
|
60 if (!(p0 = malloc(n + MIN_ALIGNMENT))) return (void *) 0;
|
cannam@95
|
61 p = (void *) (((uintptr_t) p0 + MIN_ALIGNMENT) & (~((uintptr_t) (MIN_ALIGNMENT - 1))));
|
cannam@95
|
62 *((void **) p - 1) = p0;
|
cannam@95
|
63 return p;
|
cannam@95
|
64 }
|
cannam@95
|
65 static void our_free(void *p)
|
cannam@95
|
66 {
|
cannam@95
|
67 if (p) free(*((void **) p - 1));
|
cannam@95
|
68 }
|
cannam@95
|
69 #endif
|
cannam@95
|
70
|
cannam@95
|
71 void *X(kernel_malloc)(size_t n)
|
cannam@95
|
72 {
|
cannam@95
|
73 void *p;
|
cannam@95
|
74
|
cannam@95
|
75 #if defined(MIN_ALIGNMENT)
|
cannam@95
|
76
|
cannam@95
|
77 # if defined(WITH_OUR_MALLOC)
|
cannam@95
|
78 p = our_malloc(n);
|
cannam@95
|
79 # undef real_free
|
cannam@95
|
80 # define real_free our_free
|
cannam@95
|
81
|
cannam@95
|
82 # elif defined(__FreeBSD__) && (MIN_ALIGNMENT <= 16)
|
cannam@95
|
83 /* FreeBSD does not have memalign, but its malloc is 16-byte aligned. */
|
cannam@95
|
84 p = malloc(n);
|
cannam@95
|
85
|
cannam@95
|
86 # elif (defined(__MACOSX__) || defined(__APPLE__)) && (MIN_ALIGNMENT <= 16)
|
cannam@95
|
87 /* MacOS X malloc is already 16-byte aligned */
|
cannam@95
|
88 p = malloc(n);
|
cannam@95
|
89
|
cannam@95
|
90 # elif defined(HAVE_MEMALIGN)
|
cannam@95
|
91 p = memalign(MIN_ALIGNMENT, n);
|
cannam@95
|
92
|
cannam@95
|
93 # elif defined(HAVE_POSIX_MEMALIGN)
|
cannam@95
|
94 /* note: posix_memalign is broken in glibc 2.2.5: it constrains
|
cannam@95
|
95 the size, not the alignment, to be (power of two) * sizeof(void*).
|
cannam@95
|
96 The bug seems to have been fixed as of glibc 2.3.1. */
|
cannam@95
|
97 if (posix_memalign(&p, MIN_ALIGNMENT, n))
|
cannam@95
|
98 p = (void*) 0;
|
cannam@95
|
99
|
cannam@95
|
100 # elif defined(__ICC) || defined(__INTEL_COMPILER) || defined(HAVE__MM_MALLOC)
|
cannam@95
|
101 /* Intel's C compiler defines _mm_malloc and _mm_free intrinsics */
|
cannam@95
|
102 p = (void *) _mm_malloc(n, MIN_ALIGNMENT);
|
cannam@95
|
103 # undef real_free
|
cannam@95
|
104 # define real_free _mm_free
|
cannam@95
|
105
|
cannam@95
|
106 # elif defined(_MSC_VER)
|
cannam@95
|
107 /* MS Visual C++ 6.0 with a "Processor Pack" supports SIMD
|
cannam@95
|
108 and _aligned_malloc/free (uses malloc.h) */
|
cannam@95
|
109 p = (void *) _aligned_malloc(n, MIN_ALIGNMENT);
|
cannam@95
|
110 # undef real_free
|
cannam@95
|
111 # define real_free _aligned_free
|
cannam@95
|
112
|
cannam@95
|
113 # elif defined(macintosh) /* MacOS 9 */
|
cannam@95
|
114 p = (void *) MPAllocateAligned(n,
|
cannam@95
|
115 # if MIN_ALIGNMENT == 8
|
cannam@95
|
116 kMPAllocate8ByteAligned,
|
cannam@95
|
117 # elif MIN_ALIGNMENT == 16
|
cannam@95
|
118 kMPAllocate16ByteAligned,
|
cannam@95
|
119 # elif MIN_ALIGNMENT == 32
|
cannam@95
|
120 kMPAllocate32ByteAligned,
|
cannam@95
|
121 # else
|
cannam@95
|
122 # error "Unknown alignment for MPAllocateAligned"
|
cannam@95
|
123 # endif
|
cannam@95
|
124 0);
|
cannam@95
|
125 # undef real_free
|
cannam@95
|
126 # define real_free MPFree
|
cannam@95
|
127
|
cannam@95
|
128 # else
|
cannam@95
|
129 /* Add your machine here and send a patch to fftw@fftw.org
|
cannam@95
|
130 or (e.g. for Windows) configure --with-our-malloc */
|
cannam@95
|
131 # error "Don't know how to malloc() aligned memory ... try configuring --with-our-malloc"
|
cannam@95
|
132 # endif
|
cannam@95
|
133
|
cannam@95
|
134 #else /* !defined(MIN_ALIGNMENT) */
|
cannam@95
|
135 p = malloc(n);
|
cannam@95
|
136 #endif
|
cannam@95
|
137
|
cannam@95
|
138 return p;
|
cannam@95
|
139 }
|
cannam@95
|
140
|
cannam@95
|
141 void X(kernel_free)(void *p)
|
cannam@95
|
142 {
|
cannam@95
|
143 real_free(p);
|
cannam@95
|
144 }
|