aligned_alloc.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //===----------------------------------------------------------------------===//
  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. #ifndef _LIBCPP___MEMORY_ALIGNED_ALLOC_H
  9. #define _LIBCPP___MEMORY_ALIGNED_ALLOC_H
  10. #include <__config>
  11. #include <cstddef>
  12. #include <cstdlib>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. _LIBCPP_BEGIN_NAMESPACE_STD
  17. #ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
  18. // Low-level helpers to call the aligned allocation and deallocation functions
  19. // on the target platform. This is used to implement libc++'s own memory
  20. // allocation routines -- if you need to allocate memory inside the library,
  21. // chances are that you want to use `__libcpp_allocate` instead.
  22. //
  23. // Returns the allocated memory, or `nullptr` on failure.
  24. inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
  25. # if defined(_LIBCPP_MSVCRT_LIKE)
  26. return ::_aligned_malloc(__size, __alignment);
  27. // Use posix_memalign instead of ::aligned_alloc to fix the musl and some of the tests
  28. # elif _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_C11_ALIGNED_ALLOC) && false
  29. // aligned_alloc() requires that __size is a multiple of __alignment,
  30. // but for C++ [new.delete.general], only states "if the value of an
  31. // alignment argument passed to any of these functions is not a valid
  32. // alignment value, the behavior is undefined".
  33. // To handle calls such as ::operator new(1, std::align_val_t(128)), we
  34. // round __size up to the next multiple of __alignment.
  35. size_t __rounded_size = (__size + __alignment - 1) & ~(__alignment - 1);
  36. // Rounding up could have wrapped around to zero, so we have to add another
  37. // max() ternary to the actual call site to avoid succeeded in that case.
  38. return ::aligned_alloc(__alignment, __size > __rounded_size ? __size : __rounded_size);
  39. # else
  40. void* __result = nullptr;
  41. (void)::posix_memalign(&__result, __alignment, __size);
  42. // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
  43. return __result;
  44. # endif
  45. }
  46. inline _LIBCPP_HIDE_FROM_ABI void __libcpp_aligned_free(void* __ptr) {
  47. # if defined(_LIBCPP_MSVCRT_LIKE)
  48. ::_aligned_free(__ptr);
  49. # else
  50. ::free(__ptr);
  51. # endif
  52. }
  53. #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
  54. _LIBCPP_END_NAMESPACE_STD
  55. #endif // _LIBCPP___MEMORY_ALIGNED_ALLOC_H