mm_malloc.h revision f0a6475763e0d194464302d954a9571fae0a6a99
1d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson/*===---- mm_malloc.h - Allocating and Freeing Aligned Memory Blocks -------=== 2d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * 3d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * Permission is hereby granted, free of charge, to any person obtaining a copy 4d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * of this software and associated documentation files (the "Software"), to deal 5d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * in the Software without restriction, including without limitation the rights 6d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * copies of the Software, and to permit persons to whom the Software is 8d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * furnished to do so, subject to the following conditions: 9d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * 10d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * The above copyright notice and this permission notice shall be included in 11d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * all copies or substantial portions of the Software. 12d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * 13d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * THE SOFTWARE. 20d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson * 21d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson *===-----------------------------------------------------------------------=== 22d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson */ 23d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson 24d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson#ifndef __MM_MALLOC_H 25d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson#define __MM_MALLOC_H 26d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson 277acb953031bed92c7420aaf1200d6732239e562eChandler Carruth#include <stdlib.h> 28d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson 2987a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher#ifdef _WIN32 3087a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher#include <malloc.h> 3187a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher#else 3287a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher#ifndef __cplusplus 3387a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopherextern int posix_memalign(void **memptr, size_t alignment, size_t size); 3487a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher#else 357acb953031bed92c7420aaf1200d6732239e562eChandler Carruth// Some systems (e.g. those with GNU libc) declare posix_memalign with an 367acb953031bed92c7420aaf1200d6732239e562eChandler Carruth// exception specifier. Via an "egregious workaround" in 377acb953031bed92c7420aaf1200d6732239e562eChandler Carruth// Sema::CheckEquivalentExceptionSpec, Clang accepts the following as a valid 387acb953031bed92c7420aaf1200d6732239e562eChandler Carruth// redeclaration of glibc's declaration. 3987a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopherextern "C" int posix_memalign(void **memptr, size_t alignment, size_t size); 4087a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher#endif 4187a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher#endif 4287a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher 43f0a6475763e0d194464302d954a9571fae0a6a99NAKAMURA Takumi#if !(defined(_WIN32) && defined(_mm_malloc)) 4487a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopherstatic __inline__ void *__attribute__((__always_inline__, __nodebug__, 4587a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher __malloc__)) 4682049c08da882602cdfa7de37079d6e3f7eb6893Daniel Dunbar_mm_malloc(size_t size, size_t align) 47d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson{ 4887a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher if (align == 1) { 4987a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher return malloc(size); 50d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson } 51d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson 5287a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher if (!(align & (align - 1)) && align < sizeof(void *)) 5387a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher align = sizeof(void *); 54d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson 5587a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher void *mallocedMemory; 5687a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher#ifdef _WIN32 5787a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher mallocedMemory = _aligned_malloc(size, align); 5887a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher#else 5987a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher if (posix_memalign(&mallocedMemory, align, size)) 60d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson return 0; 6187a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher#endif 62d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson 6387a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher return mallocedMemory; 64d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson} 65d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson 6682049c08da882602cdfa7de37079d6e3f7eb6893Daniel Dunbarstatic __inline__ void __attribute__((__always_inline__, __nodebug__)) 6782049c08da882602cdfa7de37079d6e3f7eb6893Daniel Dunbar_mm_free(void *p) 68d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson{ 6987a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher free(p); 70d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson} 71f0a6475763e0d194464302d954a9571fae0a6a99NAKAMURA Takumi#endif 72d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson 73d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson#endif /* __MM_MALLOC_H */ 74