1*bed243d3SAndroid Build Coastguard Worker /*===---- mm_malloc.h - Allocating and Freeing Aligned Memory Blocks -------===
2*bed243d3SAndroid Build Coastguard Worker *
3*bed243d3SAndroid Build Coastguard Worker * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*bed243d3SAndroid Build Coastguard Worker * See https://llvm.org/LICENSE.txt for license information.
5*bed243d3SAndroid Build Coastguard Worker * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*bed243d3SAndroid Build Coastguard Worker *
7*bed243d3SAndroid Build Coastguard Worker *===-----------------------------------------------------------------------===
8*bed243d3SAndroid Build Coastguard Worker */
9*bed243d3SAndroid Build Coastguard Worker
10*bed243d3SAndroid Build Coastguard Worker #ifndef __MM_MALLOC_H
11*bed243d3SAndroid Build Coastguard Worker #define __MM_MALLOC_H
12*bed243d3SAndroid Build Coastguard Worker
13*bed243d3SAndroid Build Coastguard Worker #include <stdlib.h>
14*bed243d3SAndroid Build Coastguard Worker
15*bed243d3SAndroid Build Coastguard Worker #ifdef _WIN32
16*bed243d3SAndroid Build Coastguard Worker #include <malloc.h>
17*bed243d3SAndroid Build Coastguard Worker #else
18*bed243d3SAndroid Build Coastguard Worker #ifndef __cplusplus
19*bed243d3SAndroid Build Coastguard Worker extern int posix_memalign(void **__memptr, size_t __alignment, size_t __size);
20*bed243d3SAndroid Build Coastguard Worker #else
21*bed243d3SAndroid Build Coastguard Worker // Some systems (e.g. those with GNU libc) declare posix_memalign with an
22*bed243d3SAndroid Build Coastguard Worker // exception specifier. Via an "egregious workaround" in
23*bed243d3SAndroid Build Coastguard Worker // Sema::CheckEquivalentExceptionSpec, Clang accepts the following as a valid
24*bed243d3SAndroid Build Coastguard Worker // redeclaration of glibc's declaration.
25*bed243d3SAndroid Build Coastguard Worker extern "C" int posix_memalign(void **__memptr, size_t __alignment, size_t __size);
26*bed243d3SAndroid Build Coastguard Worker #endif
27*bed243d3SAndroid Build Coastguard Worker #endif
28*bed243d3SAndroid Build Coastguard Worker
29*bed243d3SAndroid Build Coastguard Worker #if !(defined(_WIN32) && defined(_mm_malloc))
30*bed243d3SAndroid Build Coastguard Worker static __inline__ void *__attribute__((__always_inline__, __nodebug__,
31*bed243d3SAndroid Build Coastguard Worker __malloc__, __alloc_size__(1),
32*bed243d3SAndroid Build Coastguard Worker __alloc_align__(2)))
_mm_malloc(size_t __size,size_t __align)33*bed243d3SAndroid Build Coastguard Worker _mm_malloc(size_t __size, size_t __align) {
34*bed243d3SAndroid Build Coastguard Worker if (__align == 1) {
35*bed243d3SAndroid Build Coastguard Worker return malloc(__size);
36*bed243d3SAndroid Build Coastguard Worker }
37*bed243d3SAndroid Build Coastguard Worker
38*bed243d3SAndroid Build Coastguard Worker if (!(__align & (__align - 1)) && __align < sizeof(void *))
39*bed243d3SAndroid Build Coastguard Worker __align = sizeof(void *);
40*bed243d3SAndroid Build Coastguard Worker
41*bed243d3SAndroid Build Coastguard Worker void *__mallocedMemory;
42*bed243d3SAndroid Build Coastguard Worker #if defined(__MINGW32__)
43*bed243d3SAndroid Build Coastguard Worker __mallocedMemory = __mingw_aligned_malloc(__size, __align);
44*bed243d3SAndroid Build Coastguard Worker #elif defined(_WIN32)
45*bed243d3SAndroid Build Coastguard Worker __mallocedMemory = _aligned_malloc(__size, __align);
46*bed243d3SAndroid Build Coastguard Worker #else
47*bed243d3SAndroid Build Coastguard Worker if (posix_memalign(&__mallocedMemory, __align, __size))
48*bed243d3SAndroid Build Coastguard Worker return 0;
49*bed243d3SAndroid Build Coastguard Worker #endif
50*bed243d3SAndroid Build Coastguard Worker
51*bed243d3SAndroid Build Coastguard Worker return __mallocedMemory;
52*bed243d3SAndroid Build Coastguard Worker }
53*bed243d3SAndroid Build Coastguard Worker
54*bed243d3SAndroid Build Coastguard Worker static __inline__ void __attribute__((__always_inline__, __nodebug__))
_mm_free(void * __p)55*bed243d3SAndroid Build Coastguard Worker _mm_free(void *__p)
56*bed243d3SAndroid Build Coastguard Worker {
57*bed243d3SAndroid Build Coastguard Worker #if defined(__MINGW32__)
58*bed243d3SAndroid Build Coastguard Worker __mingw_aligned_free(__p);
59*bed243d3SAndroid Build Coastguard Worker #elif defined(_WIN32)
60*bed243d3SAndroid Build Coastguard Worker _aligned_free(__p);
61*bed243d3SAndroid Build Coastguard Worker #else
62*bed243d3SAndroid Build Coastguard Worker free(__p);
63*bed243d3SAndroid Build Coastguard Worker #endif
64*bed243d3SAndroid Build Coastguard Worker }
65*bed243d3SAndroid Build Coastguard Worker #endif
66*bed243d3SAndroid Build Coastguard Worker
67*bed243d3SAndroid Build Coastguard Worker #endif /* __MM_MALLOC_H */
68