Skip to content

Commit 12378b0

Browse files
committed
remove duplicated code
1 parent ba126e9 commit 12378b0

File tree

3 files changed

+10
-23
lines changed

3 files changed

+10
-23
lines changed

libcxx/include/__memory/align.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
_LIBCPP_BEGIN_NAMESPACE_STD
2121

2222
#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-
23+
# define _LIBCPP_ALIGN_ABI_VISIBILITY _LIBCPP_EXPORTED_FROM_ABI
2624
#else
25+
# define _LIBCPP_ALIGN_ABI_VISIBILITY _LIBCPP_HIDE_FROM_ABI
26+
#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
2727

28-
inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
28+
inline _LIBCPP_ALIGN_ABI_VISIBILITY void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
2929
void* __r = nullptr;
3030
if (__sz <= __space) {
3131
char* __p1 = static_cast<char*>(__ptr);
@@ -40,7 +40,7 @@ inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __p
4040
return __r;
4141
}
4242

43-
#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
43+
#undef _LIBCPP_ALIGN_ABI_VISIBILITY
4444

4545
_LIBCPP_END_NAMESPACE_STD
4646

libcxx/src/memory.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,8 @@ __sp_mut& __get_sp_mut(const void* p) {
135135

136136
#if defined(_LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS)
137137

138-
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;
151-
}
138+
constinit auto* __align__abi__keeper = align;
139+
152140
#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
153141

154142
_LIBCPP_END_NAMESPACE_STD

libcxx/test/benchmarks/memory/align.bench.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515

1616
static void BM_align(benchmark::State& state) {
1717
char buffer[1024];
18-
char* data = buffer + 123;
19-
std::size_t sz{1024 - 123};
20-
void* p;
18+
void* data = buffer + 123;
19+
std::size_t sz{sizeof(buffer) - 123};
2120

2221
for (auto _ : state) {
23-
benchmark::DoNotOptimize(std::align(state.range(), state.range(), p, sz))
22+
benchmark::DoNotOptimize(std::align(state.range(), state.range(), data, sz));
2423
}
2524
}
2625
BENCHMARK(BM_align)->Range(1, 256);

0 commit comments

Comments
 (0)