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___MEMORY_ALIGN_H
10#define _LIBCPP___MEMORY_ALIGN_H
11
12#include <__config>
13#include <__cstddef/size_t.h>
14#include <cstdint>
15
16#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17# pragma GCC system_header
18#endif
19
20_LIBCPP_BEGIN_NAMESPACE_STD
21
22inline namespace __align_inline {
23_LIBCPP_HIDE_FROM_ABI inline void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
24 void* __r = nullptr;
25 if (__sz <= __space) {
26 char* __p1 = static_cast<char*>(__ptr);
27 char* __p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(__p1 + (__align - 1)) & -__align);
28 size_t __d = static_cast<size_t>(__p2 - __p1);
29 if (__d <= __space - __sz) {
30 __r = __p2;
31 __ptr = __r;
32 __space -= __d;
33 }
34 }
35 return __r;
36}
37
38} // namespace __align_inline
39
40_LIBCPP_END_NAMESPACE_STD
41
42#endif // _LIBCPP___MEMORY_ALIGN_H
43