annotate src/fftw-3.3.3/kernel/kalloc.c @ 23:619f715526df sv_v2.1

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