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