54 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___CXX03___MEMORY_ALIGNED_ALLOC_H10#define _LIBCPP___CXX03___MEMORY_ALIGNED_ALLOC_H11 12#include <__cxx03/__config>13#include <__cxx03/cstddef>14#include <__cxx03/cstdlib>15 16#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)17# pragma GCC system_header18#endif19 20_LIBCPP_BEGIN_NAMESPACE_STD21 22#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION23 24// Low-level helpers to call the aligned allocation and deallocation functions25// on the target platform. This is used to implement libc++'s own memory26// allocation routines -- if you need to allocate memory inside the library,27// chances are that you want to use `__libcpp_allocate` instead.28//29// Returns the allocated memory, or `nullptr` on failure.30inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {31# if defined(_LIBCPP_MSVCRT_LIKE)32 return ::_aligned_malloc(__size, __alignment);33# else34 void* __result = nullptr;35 (void)::posix_memalign(&__result, __alignment, __size);36 // If posix_memalign fails, __result is unmodified so we still return `nullptr`.37 return __result;38# endif39}40 41inline _LIBCPP_HIDE_FROM_ABI void __libcpp_aligned_free(void* __ptr) {42# if defined(_LIBCPP_MSVCRT_LIKE)43 ::_aligned_free(__ptr);44# else45 ::free(__ptr);46# endif47}48 49#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION50 51_LIBCPP_END_NAMESPACE_STD52 53#endif // _LIBCPP___CXX03___MEMORY_ALIGNED_ALLOC_H54