1193326Sed/*===---- mm_malloc.h - Allocating and Freeing Aligned Memory Blocks -------=== 2193326Sed * 3193326Sed * Permission is hereby granted, free of charge, to any person obtaining a copy 4193326Sed * of this software and associated documentation files (the "Software"), to deal 5193326Sed * in the Software without restriction, including without limitation the rights 6193326Sed * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7193326Sed * copies of the Software, and to permit persons to whom the Software is 8193326Sed * furnished to do so, subject to the following conditions: 9193326Sed * 10193326Sed * The above copyright notice and this permission notice shall be included in 11193326Sed * all copies or substantial portions of the Software. 12193326Sed * 13193326Sed * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14193326Sed * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15193326Sed * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16193326Sed * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17193326Sed * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18193326Sed * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19193326Sed * THE SOFTWARE. 20193326Sed * 21193326Sed *===-----------------------------------------------------------------------=== 22193326Sed */ 23193326Sed 24193326Sed#ifndef __MM_MALLOC_H 25193326Sed#define __MM_MALLOC_H 26193326Sed 27193326Sed#include <stdlib.h> 28193326Sed 29218893Sdim#ifdef _WIN32 30218893Sdim#include <malloc.h> 31218893Sdim#else 32218893Sdim#ifndef __cplusplus 33249423Sdimextern int posix_memalign(void **__memptr, size_t __alignment, size_t __size); 34218893Sdim#else 35218893Sdim// Some systems (e.g. those with GNU libc) declare posix_memalign with an 36218893Sdim// exception specifier. Via an "egregious workaround" in 37218893Sdim// Sema::CheckEquivalentExceptionSpec, Clang accepts the following as a valid 38218893Sdim// redeclaration of glibc's declaration. 39249423Sdimextern "C" int posix_memalign(void **__memptr, size_t __alignment, size_t __size); 40218893Sdim#endif 41218893Sdim#endif 42218893Sdim 43221345Sdim#if !(defined(_WIN32) && defined(_mm_malloc)) 44218893Sdimstatic __inline__ void *__attribute__((__always_inline__, __nodebug__, 45218893Sdim __malloc__)) 46249423Sdim_mm_malloc(size_t __size, size_t __align) 47193326Sed{ 48249423Sdim if (__align == 1) { 49249423Sdim return malloc(__size); 50193326Sed } 51193326Sed 52249423Sdim if (!(__align & (__align - 1)) && __align < sizeof(void *)) 53249423Sdim __align = sizeof(void *); 54193326Sed 55249423Sdim void *__mallocedMemory; 56226633Sdim#if defined(__MINGW32__) 57249423Sdim __mallocedMemory = __mingw_aligned_malloc(__size, __align); 58226633Sdim#elif defined(_WIN32) 59249423Sdim __mallocedMemory = _aligned_malloc(__size, __align); 60218893Sdim#else 61249423Sdim if (posix_memalign(&__mallocedMemory, __align, __size)) 62193326Sed return 0; 63218893Sdim#endif 64193326Sed 65249423Sdim return __mallocedMemory; 66193326Sed} 67193326Sed 68206084Srdivackystatic __inline__ void __attribute__((__always_inline__, __nodebug__)) 69249423Sdim_mm_free(void *__p) 70193326Sed{ 71249423Sdim free(__p); 72193326Sed} 73221345Sdim#endif 74193326Sed 75193326Sed#endif /* __MM_MALLOC_H */ 76