mm_malloc.h revision 218893
1228753Smm/*===---- mm_malloc.h - Allocating and Freeing Aligned Memory Blocks -------=== 2228753Smm * 3228753Smm * Permission is hereby granted, free of charge, to any person obtaining a copy 4228753Smm * of this software and associated documentation files (the "Software"), to deal 5228753Smm * in the Software without restriction, including without limitation the rights 6228753Smm * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7228753Smm * copies of the Software, and to permit persons to whom the Software is 8228753Smm * furnished to do so, subject to the following conditions: 9228753Smm * 10228753Smm * The above copyright notice and this permission notice shall be included in 11228753Smm * all copies or substantial portions of the Software. 12228753Smm * 13228753Smm * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14228753Smm * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15228753Smm * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16228753Smm * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17228753Smm * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18228753Smm * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19228753Smm * THE SOFTWARE. 20228753Smm * 21228753Smm *===-----------------------------------------------------------------------=== 22228753Smm */ 23228753Smm 24228753Smm#ifndef __MM_MALLOC_H 25228753Smm#define __MM_MALLOC_H 26228753Smm 27228763Smm#include <stdlib.h> 28228753Smm 29228753Smm#ifdef _WIN32 30228753Smm#include <malloc.h> 31228753Smm#else 32228753Smm#ifndef __cplusplus 33228753Smmextern int posix_memalign(void **memptr, size_t alignment, size_t size); 34228753Smm#else 35228753Smm// Some systems (e.g. those with GNU libc) declare posix_memalign with an 36228753Smm// exception specifier. Via an "egregious workaround" in 37228753Smm// Sema::CheckEquivalentExceptionSpec, Clang accepts the following as a valid 38228753Smm// redeclaration of glibc's declaration. 39228753Smmextern "C" int posix_memalign(void **memptr, size_t alignment, size_t size); 40228753Smm#endif 41228753Smm#endif 42228753Smm 43228753Smmstatic __inline__ void *__attribute__((__always_inline__, __nodebug__, 44228753Smm __malloc__)) 45228753Smm_mm_malloc(size_t size, size_t align) 46228753Smm{ 47228753Smm if (align == 1) { 48228753Smm return malloc(size); 49228753Smm } 50228753Smm 51228753Smm if (!(align & (align - 1)) && align < sizeof(void *)) 52228753Smm align = sizeof(void *); 53228753Smm 54228753Smm void *mallocedMemory; 55228753Smm#ifdef _WIN32 56228753Smm mallocedMemory = _aligned_malloc(size, align); 57228753Smm#else 58228753Smm if (posix_memalign(&mallocedMemory, align, size)) 59228753Smm return 0; 60228753Smm#endif 61228753Smm 62228753Smm return mallocedMemory; 63228753Smm} 64228753Smm 65228753Smmstatic __inline__ void __attribute__((__always_inline__, __nodebug__)) 66228753Smm_mm_free(void *p) 67228753Smm{ 68228753Smm free(p); 69228753Smm} 70228753Smm 71228753Smm#endif /* __MM_MALLOC_H */ 72228753Smm