cannam@167: /* cannam@167: * Copyright (c) 2003, 2007-14 Matteo Frigo cannam@167: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology cannam@167: * cannam@167: * This program is free software; you can redistribute it and/or modify cannam@167: * it under the terms of the GNU General Public License as published by cannam@167: * the Free Software Foundation; either version 2 of the License, or cannam@167: * (at your option) any later version. cannam@167: * cannam@167: * This program is distributed in the hope that it will be useful, cannam@167: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@167: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@167: * GNU General Public License for more details. cannam@167: * cannam@167: * You should have received a copy of the GNU General Public License cannam@167: * along with this program; if not, write to the Free Software cannam@167: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@167: * cannam@167: */ cannam@167: cannam@167: cannam@167: #include "kernel/ifftw.h" cannam@167: cannam@167: #if defined(HAVE_MALLOC_H) cannam@167: # include cannam@167: #endif cannam@167: cannam@167: /* ``kernel'' malloc(), with proper memory alignment */ cannam@167: cannam@167: #if defined(HAVE_DECL_MEMALIGN) && !HAVE_DECL_MEMALIGN cannam@167: extern void *memalign(size_t, size_t); cannam@167: #endif cannam@167: cannam@167: #if defined(HAVE_DECL_POSIX_MEMALIGN) && !HAVE_DECL_POSIX_MEMALIGN cannam@167: extern int posix_memalign(void **, size_t, size_t); cannam@167: #endif cannam@167: cannam@167: #if defined(macintosh) /* MacOS 9 */ cannam@167: # include cannam@167: #endif cannam@167: cannam@167: #define real_free free /* memalign and malloc use ordinary free */ cannam@167: cannam@167: #define IS_POWER_OF_TWO(n) (((n) > 0) && (((n) & ((n) - 1)) == 0)) cannam@167: #if defined(WITH_OUR_MALLOC) && (MIN_ALIGNMENT >= 8) && IS_POWER_OF_TWO(MIN_ALIGNMENT) cannam@167: /* Our own MIN_ALIGNMENT-aligned malloc/free. Assumes sizeof(void*) is a cannam@167: power of two <= 8 and that malloc is at least sizeof(void*)-aligned. cannam@167: cannam@167: The main reason for this routine is that, as of this writing, cannam@167: Windows does not include any aligned allocation routines in its cannam@167: system libraries, and instead provides an implementation with a cannam@167: Visual C++ "Processor Pack" that you have to statically link into cannam@167: your program. We do not want to require users to have VC++ cannam@167: (e.g. gcc/MinGW should be fine). Our code should be at least as good cannam@167: as the MS _aligned_malloc, in any case, according to second-hand cannam@167: reports of the algorithm it employs (also based on plain malloc). */ cannam@167: static void *our_malloc(size_t n) cannam@167: { cannam@167: void *p0, *p; cannam@167: if (!(p0 = malloc(n + MIN_ALIGNMENT))) return (void *) 0; cannam@167: p = (void *) (((uintptr_t) p0 + MIN_ALIGNMENT) & (~((uintptr_t) (MIN_ALIGNMENT - 1)))); cannam@167: *((void **) p - 1) = p0; cannam@167: return p; cannam@167: } cannam@167: static void our_free(void *p) cannam@167: { cannam@167: if (p) free(*((void **) p - 1)); cannam@167: } cannam@167: #endif cannam@167: cannam@167: void *X(kernel_malloc)(size_t n) cannam@167: { cannam@167: void *p; cannam@167: cannam@167: #if defined(MIN_ALIGNMENT) cannam@167: cannam@167: # if defined(WITH_OUR_MALLOC) cannam@167: p = our_malloc(n); cannam@167: # undef real_free cannam@167: # define real_free our_free cannam@167: cannam@167: # elif defined(__FreeBSD__) && (MIN_ALIGNMENT <= 16) cannam@167: /* FreeBSD does not have memalign, but its malloc is 16-byte aligned. */ cannam@167: p = malloc(n); cannam@167: cannam@167: # elif (defined(__MACOSX__) || defined(__APPLE__)) && (MIN_ALIGNMENT <= 16) cannam@167: /* MacOS X malloc is already 16-byte aligned */ cannam@167: p = malloc(n); cannam@167: cannam@167: # elif defined(HAVE_MEMALIGN) cannam@167: p = memalign(MIN_ALIGNMENT, n); cannam@167: cannam@167: # elif defined(HAVE_POSIX_MEMALIGN) cannam@167: /* note: posix_memalign is broken in glibc 2.2.5: it constrains cannam@167: the size, not the alignment, to be (power of two) * sizeof(void*). cannam@167: The bug seems to have been fixed as of glibc 2.3.1. */ cannam@167: if (posix_memalign(&p, MIN_ALIGNMENT, n)) cannam@167: p = (void*) 0; cannam@167: cannam@167: # elif defined(__ICC) || defined(__INTEL_COMPILER) || defined(HAVE__MM_MALLOC) cannam@167: /* Intel's C compiler defines _mm_malloc and _mm_free intrinsics */ cannam@167: p = (void *) _mm_malloc(n, MIN_ALIGNMENT); cannam@167: # undef real_free cannam@167: # define real_free _mm_free cannam@167: cannam@167: # elif defined(_MSC_VER) cannam@167: /* MS Visual C++ 6.0 with a "Processor Pack" supports SIMD cannam@167: and _aligned_malloc/free (uses malloc.h) */ cannam@167: p = (void *) _aligned_malloc(n, MIN_ALIGNMENT); cannam@167: # undef real_free cannam@167: # define real_free _aligned_free cannam@167: cannam@167: # elif defined(macintosh) /* MacOS 9 */ cannam@167: p = (void *) MPAllocateAligned(n, cannam@167: # if MIN_ALIGNMENT == 8 cannam@167: kMPAllocate8ByteAligned, cannam@167: # elif MIN_ALIGNMENT == 16 cannam@167: kMPAllocate16ByteAligned, cannam@167: # elif MIN_ALIGNMENT == 32 cannam@167: kMPAllocate32ByteAligned, cannam@167: # else cannam@167: # error "Unknown alignment for MPAllocateAligned" cannam@167: # endif cannam@167: 0); cannam@167: # undef real_free cannam@167: # define real_free MPFree cannam@167: cannam@167: # else cannam@167: /* Add your machine here and send a patch to fftw@fftw.org cannam@167: or (e.g. for Windows) configure --with-our-malloc */ cannam@167: # error "Don't know how to malloc() aligned memory ... try configuring --with-our-malloc" cannam@167: # endif cannam@167: cannam@167: #else /* !defined(MIN_ALIGNMENT) */ cannam@167: p = malloc(n); cannam@167: #endif cannam@167: cannam@167: return p; cannam@167: } cannam@167: cannam@167: void X(kernel_free)(void *p) cannam@167: { cannam@167: real_free(p); cannam@167: }