annotate src/fftw-3.3.8/kernel/kalloc.c @ 169:223a55898ab9 tip default

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