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