Skip to content

Commit 65e53c3

Browse files
committed
remove duplicated code
1 parent 346c5c6 commit 65e53c3

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

libcxx/include/__memory/align.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@
1919

2020
_LIBCPP_BEGIN_NAMESPACE_STD
2121

22-
#ifdef _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
23-
24-
_LIBCPP_EXPORTED_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
25-
26-
#else
27-
28-
inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
22+
__attribute__((always_inline)) inline void* __align_impl(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
2923
void* __r = nullptr;
3024
if (__sz <= __space) {
3125
char* __p1 = static_cast<char*>(__ptr);
@@ -40,6 +34,16 @@ inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __p
4034
return __r;
4135
}
4236

37+
#ifdef _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
38+
39+
_LIBCPP_EXPORTED_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
40+
41+
#else
42+
43+
inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
44+
return __align_impl(__align, __sz, __ptr, __space);
45+
}
46+
4347
#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
4448

4549
_LIBCPP_END_NAMESPACE_STD

libcxx/src/memory.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,7 @@ __sp_mut& __get_sp_mut(const void* p) {
136136
#if defined(_LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS)
137137

138138
void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
139-
void* r = nullptr;
140-
if (size <= space) {
141-
char* p1 = static_cast<char*>(ptr);
142-
char* p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(p1 + (alignment - 1)) & -alignment);
143-
size_t d = static_cast<size_t>(p2 - p1);
144-
if (d <= space - size) {
145-
r = p2;
146-
ptr = r;
147-
space -= d;
148-
}
149-
}
150-
return r;
139+
return __align_impl(alignment, size, ptr, space);
151140
}
152141
#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
153142

0 commit comments

Comments
 (0)