Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:34

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___CXX03___MEMORY_ALIGNED_ALLOC_H
0010 #define _LIBCPP___CXX03___MEMORY_ALIGNED_ALLOC_H
0011 
0012 #include <__cxx03/__config>
0013 #include <__cxx03/cstddef>
0014 #include <__cxx03/cstdlib>
0015 
0016 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0017 #  pragma GCC system_header
0018 #endif
0019 
0020 _LIBCPP_BEGIN_NAMESPACE_STD
0021 
0022 #ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
0023 
0024 // Low-level helpers to call the aligned allocation and deallocation functions
0025 // on the target platform. This is used to implement libc++'s own memory
0026 // allocation routines -- if you need to allocate memory inside the library,
0027 // chances are that you want to use `__libcpp_allocate` instead.
0028 //
0029 // Returns the allocated memory, or `nullptr` on failure.
0030 inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
0031 #  if defined(_LIBCPP_MSVCRT_LIKE)
0032   return ::_aligned_malloc(__size, __alignment);
0033 #  elif _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_C11_ALIGNED_ALLOC)
0034   // aligned_alloc() requires that __size is a multiple of __alignment,
0035   // but for C++ [new.delete.general], only states "if the value of an
0036   // alignment argument passed to any of these functions is not a valid
0037   // alignment value, the behavior is undefined".
0038   // To handle calls such as ::operator new(1, std::align_val_t(128)), we
0039   // round __size up to the next multiple of __alignment.
0040   size_t __rounded_size = (__size + __alignment - 1) & ~(__alignment - 1);
0041   // Rounding up could have wrapped around to zero, so we have to add another
0042   // max() ternary to the actual call site to avoid succeeded in that case.
0043   return ::aligned_alloc(__alignment, __size > __rounded_size ? __size : __rounded_size);
0044 #  else
0045   void* __result = nullptr;
0046   (void)::posix_memalign(&__result, __alignment, __size);
0047   // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
0048   return __result;
0049 #  endif
0050 }
0051 
0052 inline _LIBCPP_HIDE_FROM_ABI void __libcpp_aligned_free(void* __ptr) {
0053 #  if defined(_LIBCPP_MSVCRT_LIKE)
0054   ::_aligned_free(__ptr);
0055 #  else
0056   ::free(__ptr);
0057 #  endif
0058 }
0059 
0060 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
0061 
0062 _LIBCPP_END_NAMESPACE_STD
0063 
0064 #endif // _LIBCPP___CXX03___MEMORY_ALIGNED_ALLOC_H