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
334f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikieextern 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.
394f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikieextern "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__))
464f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikie_mm_malloc(size_t __size, size_t __align)
47d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson{
484f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikie  if (__align == 1) {
494f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikie    return malloc(__size);
50d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson  }
51d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson
524f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikie  if (!(__align & (__align - 1)) && __align < sizeof(void *))
534f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikie    __align = sizeof(void *);
54d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson
554f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikie  void *__mallocedMemory;
562ac5321014492db781a73e14d877bddd904957fcNAKAMURA Takumi#if defined(__MINGW32__)
574f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikie  __mallocedMemory = __mingw_aligned_malloc(__size, __align);
582ac5321014492db781a73e14d877bddd904957fcNAKAMURA Takumi#elif defined(_WIN32)
594f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikie  __mallocedMemory = _aligned_malloc(__size, __align);
6087a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher#else
614f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikie  if (posix_memalign(&__mallocedMemory, __align, __size))
62d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson    return 0;
6387a34fd801b3f54d54de87a0255429fef3bbce92Eric Christopher#endif
64d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson
654f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikie  return __mallocedMemory;
66d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson}
67d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson
6882049c08da882602cdfa7de37079d6e3f7eb6893Daniel Dunbarstatic __inline__ void __attribute__((__always_inline__, __nodebug__))
694f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikie_mm_free(void *__p)
70d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson{
714f918aed75d4927e88365541c7200f0b5fe5014bDavid Blaikie  free(__p);
72d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson}
73f0a6475763e0d194464302d954a9571fae0a6a99NAKAMURA Takumi#endif
74d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson
75d4a5c9a3f8fe3512a0fbffb1b08c8048d1390cdfAnders Carlsson#endif /* __MM_MALLOC_H */
76