blob: 0ea32517aea8c73afa465ac6dcf97fa171b29641 [file] [log] [blame]
Anders Carlssonbfd47a32009-01-08 23:30:09 +00001/*===---- mm_malloc.h - Allocating and Freeing Aligned Memory Blocks -------===
2 *
Chandler Carruth4cf57432019-04-08 20:51:30 +00003 * 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
Anders Carlssonbfd47a32009-01-08 23:30:09 +00006 *
7 *===-----------------------------------------------------------------------===
8 */
9
10#ifndef __MM_MALLOC_H
11#define __MM_MALLOC_H
12
Chandler Carruth45c2fb12010-11-22 08:06:31 +000013#include <stdlib.h>
Anders Carlssonbfd47a32009-01-08 23:30:09 +000014
Eric Christopher8a8673e2010-10-18 23:38:51 +000015#ifdef _WIN32
16#include <malloc.h>
17#else
18#ifndef __cplusplus
David Blaikie3302f2b2013-01-16 23:08:36 +000019extern int posix_memalign(void **__memptr, size_t __alignment, size_t __size);
Eric Christopher8a8673e2010-10-18 23:38:51 +000020#else
Chandler Carruth45c2fb12010-11-22 08:06:31 +000021// 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.
David Blaikie3302f2b2013-01-16 23:08:36 +000025extern "C" int posix_memalign(void **__memptr, size_t __alignment, size_t __size);
Eric Christopher8a8673e2010-10-18 23:38:51 +000026#endif
27#endif
28
NAKAMURA Takumi1a780ee2011-03-15 02:32:43 +000029#if !(defined(_WIN32) && defined(_mm_malloc))
Eric Christopher8a8673e2010-10-18 23:38:51 +000030static __inline__ void *__attribute__((__always_inline__, __nodebug__,
31 __malloc__))
David Blaikie3302f2b2013-01-16 23:08:36 +000032_mm_malloc(size_t __size, size_t __align)
Anders Carlssonbfd47a32009-01-08 23:30:09 +000033{
David Blaikie3302f2b2013-01-16 23:08:36 +000034 if (__align == 1) {
35 return malloc(__size);
Anders Carlssonbfd47a32009-01-08 23:30:09 +000036 }
37
David Blaikie3302f2b2013-01-16 23:08:36 +000038 if (!(__align & (__align - 1)) && __align < sizeof(void *))
39 __align = sizeof(void *);
Eric Christopher8a8673e2010-10-18 23:38:51 +000040
David Blaikie3302f2b2013-01-16 23:08:36 +000041 void *__mallocedMemory;
NAKAMURA Takumia185c782011-07-18 11:13:50 +000042#if defined(__MINGW32__)
David Blaikie3302f2b2013-01-16 23:08:36 +000043 __mallocedMemory = __mingw_aligned_malloc(__size, __align);
NAKAMURA Takumia185c782011-07-18 11:13:50 +000044#elif defined(_WIN32)
David Blaikie3302f2b2013-01-16 23:08:36 +000045 __mallocedMemory = _aligned_malloc(__size, __align);
Eric Christopher8a8673e2010-10-18 23:38:51 +000046#else
David Blaikie3302f2b2013-01-16 23:08:36 +000047 if (posix_memalign(&__mallocedMemory, __align, __size))
Anders Carlssonbfd47a32009-01-08 23:30:09 +000048 return 0;
Eric Christopher8a8673e2010-10-18 23:38:51 +000049#endif
Anders Carlssonbfd47a32009-01-08 23:30:09 +000050
David Blaikie3302f2b2013-01-16 23:08:36 +000051 return __mallocedMemory;
Anders Carlssonbfd47a32009-01-08 23:30:09 +000052}
53
Daniel Dunbar6a6199d2010-03-26 20:17:17 +000054static __inline__ void __attribute__((__always_inline__, __nodebug__))
David Blaikie3302f2b2013-01-16 23:08:36 +000055_mm_free(void *__p)
Anders Carlssonbfd47a32009-01-08 23:30:09 +000056{
David Blaikie3302f2b2013-01-16 23:08:36 +000057 free(__p);
Anders Carlssonbfd47a32009-01-08 23:30:09 +000058}
NAKAMURA Takumi1a780ee2011-03-15 02:32:43 +000059#endif
Anders Carlssonbfd47a32009-01-08 23:30:09 +000060
61#endif /* __MM_MALLOC_H */