Debian Codegraph @

libcxx/include/__cxx03/__memory/aligned_alloc.h

raw
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
9 #ifndef _LIBCPP___CXX03___MEMORY_ALIGNED_ALLOC_H
10 #define _LIBCPP___CXX03___MEMORY_ALIGNED_ALLOC_H
11
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_header
18 #endif
19
20 _LIBCPP_BEGIN_NAMESPACE_STD
21
22 #ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
23
24 // Low-level helpers to call the aligned allocation and deallocation functions
25 // on the target platform. This is used to implement libc++'s own memory
26 // 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.
30 inline _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 # else
34 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 # endif
39 }
40
41 inline _LIBCPP_HIDE_FROM_ABI void __libcpp_aligned_free(void* __ptr) {
42 # if defined(_LIBCPP_MSVCRT_LIKE)
43 ::_aligned_free(__ptr);
44 # else
45 ::free(__ptr);
46 # endif
47 }
48
49 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
50
51 _LIBCPP_END_NAMESPACE_STD
52
53 #endif // _LIBCPP___CXX03___MEMORY_ALIGNED_ALLOC_H