Skip to content

Commit eb8799e

Browse files
committed
[libc++] Make std::align an inline function from v2 ABI
1 parent db8c38d commit eb8799e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

libcxx/include/__memory/align.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,33 @@
1111

1212
#include <__config>
1313
#include <__cstddef/size_t.h>
14+
#include <cstdint>
1415

1516
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1617
# pragma GCC system_header
1718
#endif
1819

1920
_LIBCPP_BEGIN_NAMESPACE_STD
2021

22+
// From >=v2 ABI, std::align is an inline function.
23+
#if _LIBCPP_ABI_VERSION >= 2
24+
inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
25+
void* r = nullptr;
26+
if (__sz <= __space) {
27+
char* p1 = static_cast<char*>(__ptr);
28+
char* p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(p1 + (__align - 1)) & -__align);
29+
size_t d = static_cast<size_t>(p2 - p1);
30+
if (d <= __space - __sz) {
31+
r = p2;
32+
__ptr = r;
33+
__space -= d;
34+
}
35+
}
36+
return r;
37+
}
38+
#else
2139
_LIBCPP_EXPORTED_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
40+
#endif
2241

2342
_LIBCPP_END_NAMESPACE_STD
2443

libcxx/src/memory.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ __sp_mut& __get_sp_mut(const void* p) {
132132

133133
#endif // _LIBCPP_HAS_THREADS
134134

135+
// Remove std::align from >=v2 dylib ABI, make it an inline function.
136+
#if _LIBCPP_ABI_VERSION == 1
135137
void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
136138
void* r = nullptr;
137139
if (size <= space) {
@@ -146,5 +148,6 @@ void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
146148
}
147149
return r;
148150
}
151+
#endif
149152

150153
_LIBCPP_END_NAMESPACE_STD

0 commit comments

Comments
 (0)