xref: /src/contrib/llvm-project/libcxx/include/__memory/aligned_alloc.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
17fa27ce4SDimitry Andric //===----------------------------------------------------------------------===//
27fa27ce4SDimitry Andric //
37fa27ce4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47fa27ce4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
57fa27ce4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67fa27ce4SDimitry Andric //
77fa27ce4SDimitry Andric //===----------------------------------------------------------------------===//
87fa27ce4SDimitry Andric 
97fa27ce4SDimitry Andric #ifndef _LIBCPP___MEMORY_ALIGNED_ALLOC_H
107fa27ce4SDimitry Andric #define _LIBCPP___MEMORY_ALIGNED_ALLOC_H
117fa27ce4SDimitry Andric 
127fa27ce4SDimitry Andric #include <__config>
137fa27ce4SDimitry Andric #include <cstddef>
147fa27ce4SDimitry Andric #include <cstdlib>
157fa27ce4SDimitry Andric 
167fa27ce4SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
177fa27ce4SDimitry Andric #  pragma GCC system_header
187fa27ce4SDimitry Andric #endif
197fa27ce4SDimitry Andric 
207fa27ce4SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
217fa27ce4SDimitry Andric 
227fa27ce4SDimitry Andric #ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
237fa27ce4SDimitry Andric 
247fa27ce4SDimitry Andric // Low-level helpers to call the aligned allocation and deallocation functions
257fa27ce4SDimitry Andric // on the target platform. This is used to implement libc++'s own memory
267fa27ce4SDimitry Andric // allocation routines -- if you need to allocate memory inside the library,
277fa27ce4SDimitry Andric // chances are that you want to use `__libcpp_allocate` instead.
287fa27ce4SDimitry Andric //
297fa27ce4SDimitry Andric // Returns the allocated memory, or `nullptr` on failure.
__libcpp_aligned_alloc(std::size_t __alignment,std::size_t __size)3099aabd70SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
317fa27ce4SDimitry Andric #  if defined(_LIBCPP_MSVCRT_LIKE)
327fa27ce4SDimitry Andric   return ::_aligned_malloc(__size, __alignment);
337fa27ce4SDimitry Andric #  elif _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_C11_ALIGNED_ALLOC)
347fa27ce4SDimitry Andric   // aligned_alloc() requires that __size is a multiple of __alignment,
357fa27ce4SDimitry Andric   // but for C++ [new.delete.general], only states "if the value of an
367fa27ce4SDimitry Andric   // alignment argument passed to any of these functions is not a valid
377fa27ce4SDimitry Andric   // alignment value, the behavior is undefined".
387fa27ce4SDimitry Andric   // To handle calls such as ::operator new(1, std::align_val_t(128)), we
397fa27ce4SDimitry Andric   // round __size up to the next multiple of __alignment.
407fa27ce4SDimitry Andric   size_t __rounded_size = (__size + __alignment - 1) & ~(__alignment - 1);
417fa27ce4SDimitry Andric   // Rounding up could have wrapped around to zero, so we have to add another
427fa27ce4SDimitry Andric   // max() ternary to the actual call site to avoid succeeded in that case.
437fa27ce4SDimitry Andric   return ::aligned_alloc(__alignment, __size > __rounded_size ? __size : __rounded_size);
447fa27ce4SDimitry Andric #  else
457fa27ce4SDimitry Andric   void* __result = nullptr;
467fa27ce4SDimitry Andric   (void)::posix_memalign(&__result, __alignment, __size);
477fa27ce4SDimitry Andric   // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
487fa27ce4SDimitry Andric   return __result;
497fa27ce4SDimitry Andric #  endif
507fa27ce4SDimitry Andric }
517fa27ce4SDimitry Andric 
__libcpp_aligned_free(void * __ptr)5299aabd70SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void __libcpp_aligned_free(void* __ptr) {
537fa27ce4SDimitry Andric #  if defined(_LIBCPP_MSVCRT_LIKE)
547fa27ce4SDimitry Andric   ::_aligned_free(__ptr);
557fa27ce4SDimitry Andric #  else
567fa27ce4SDimitry Andric   ::free(__ptr);
577fa27ce4SDimitry Andric #  endif
587fa27ce4SDimitry Andric }
597fa27ce4SDimitry Andric 
607fa27ce4SDimitry Andric #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
617fa27ce4SDimitry Andric 
627fa27ce4SDimitry Andric _LIBCPP_END_NAMESPACE_STD
637fa27ce4SDimitry Andric 
647fa27ce4SDimitry Andric #endif // _LIBCPP___MEMORY_ALIGNED_ALLOC_H
65