Skip to content
Open
19 changes: 19 additions & 0 deletions libcxx/include/__memory/align.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,33 @@

#include <__config>
#include <__cstddef/size_t.h>
#include <cstdint>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

// From >=v2 ABI, std::align is an inline function.
#if _LIBCPP_ABI_VERSION >= 2
inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
void* __r = nullptr;
if (__sz <= __space) {
char* __p1 = static_cast<char*>(__ptr);
char* __p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(__p1 + (__align - 1)) & -__align);
size_t __d = static_cast<size_t>(__p2 - __p1);
if (__d <= __space - __sz) {
__r = __p2;
__ptr = __r;
__space -= __d;
}
}
return __r;
}
#else
_LIBCPP_EXPORTED_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
#endif

_LIBCPP_END_NAMESPACE_STD

Expand Down
3 changes: 3 additions & 0 deletions libcxx/src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ __sp_mut& __get_sp_mut(const void* p) {

#endif // _LIBCPP_HAS_THREADS

// Remove std::align from >=v2 dylib ABI, make it an inline function.
#if _LIBCPP_ABI_VERSION == 1
void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
void* r = nullptr;
if (size <= space) {
Expand All @@ -146,5 +148,6 @@ void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
}
return r;
}
#endif

_LIBCPP_END_NAMESPACE_STD
Loading