106c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
206c3fb27SDimitry Andric //
306c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
506c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606c3fb27SDimitry Andric //
706c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
806c3fb27SDimitry Andric
906c3fb27SDimitry Andric #ifndef _LIBCPP___MEMORY_ALIGNED_ALLOC_H
1006c3fb27SDimitry Andric #define _LIBCPP___MEMORY_ALIGNED_ALLOC_H
1106c3fb27SDimitry Andric
1206c3fb27SDimitry Andric #include <__config>
1306c3fb27SDimitry Andric #include <cstddef>
1406c3fb27SDimitry Andric #include <cstdlib>
1506c3fb27SDimitry Andric
1606c3fb27SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1706c3fb27SDimitry Andric # pragma GCC system_header
1806c3fb27SDimitry Andric #endif
1906c3fb27SDimitry Andric
2006c3fb27SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
2106c3fb27SDimitry Andric
2206c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
2306c3fb27SDimitry Andric
2406c3fb27SDimitry Andric // Low-level helpers to call the aligned allocation and deallocation functions
2506c3fb27SDimitry Andric // on the target platform. This is used to implement libc++'s own memory
2606c3fb27SDimitry Andric // allocation routines -- if you need to allocate memory inside the library,
2706c3fb27SDimitry Andric // chances are that you want to use `__libcpp_allocate` instead.
2806c3fb27SDimitry Andric //
2906c3fb27SDimitry Andric // Returns the allocated memory, or `nullptr` on failure.
__libcpp_aligned_alloc(std::size_t __alignment,std::size_t __size)30*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
3106c3fb27SDimitry Andric # if defined(_LIBCPP_MSVCRT_LIKE)
3206c3fb27SDimitry Andric return ::_aligned_malloc(__size, __alignment);
3306c3fb27SDimitry Andric # elif _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_C11_ALIGNED_ALLOC)
3406c3fb27SDimitry Andric // aligned_alloc() requires that __size is a multiple of __alignment,
3506c3fb27SDimitry Andric // but for C++ [new.delete.general], only states "if the value of an
3606c3fb27SDimitry Andric // alignment argument passed to any of these functions is not a valid
3706c3fb27SDimitry Andric // alignment value, the behavior is undefined".
3806c3fb27SDimitry Andric // To handle calls such as ::operator new(1, std::align_val_t(128)), we
3906c3fb27SDimitry Andric // round __size up to the next multiple of __alignment.
4006c3fb27SDimitry Andric size_t __rounded_size = (__size + __alignment - 1) & ~(__alignment - 1);
4106c3fb27SDimitry Andric // Rounding up could have wrapped around to zero, so we have to add another
4206c3fb27SDimitry Andric // max() ternary to the actual call site to avoid succeeded in that case.
4306c3fb27SDimitry Andric return ::aligned_alloc(__alignment, __size > __rounded_size ? __size : __rounded_size);
4406c3fb27SDimitry Andric # else
4506c3fb27SDimitry Andric void* __result = nullptr;
4606c3fb27SDimitry Andric (void)::posix_memalign(&__result, __alignment, __size);
4706c3fb27SDimitry Andric // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
4806c3fb27SDimitry Andric return __result;
4906c3fb27SDimitry Andric # endif
5006c3fb27SDimitry Andric }
5106c3fb27SDimitry Andric
__libcpp_aligned_free(void * __ptr)52*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void __libcpp_aligned_free(void* __ptr) {
5306c3fb27SDimitry Andric # if defined(_LIBCPP_MSVCRT_LIKE)
5406c3fb27SDimitry Andric ::_aligned_free(__ptr);
5506c3fb27SDimitry Andric # else
5606c3fb27SDimitry Andric ::free(__ptr);
5706c3fb27SDimitry Andric # endif
5806c3fb27SDimitry Andric }
5906c3fb27SDimitry Andric
6006c3fb27SDimitry Andric #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
6106c3fb27SDimitry Andric
6206c3fb27SDimitry Andric _LIBCPP_END_NAMESPACE_STD
6306c3fb27SDimitry Andric
6406c3fb27SDimitry Andric #endif // _LIBCPP___MEMORY_ALIGNED_ALLOC_H
65