Skip to content
Open
6 changes: 6 additions & 0 deletions libcxx/docs/ReleaseNotes/22.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Improvements and New Features
iterators, resulting in a performance improvement for ``std::deque<short>`` and
``std::join_view<vector<vector<short>>>`` iterators.

- The performance of ``align`` has been improved about 3x by making it an inline function.

Deprecations and Removals
-------------------------

Expand All @@ -99,6 +101,8 @@ Potentially breaking changes
``_LIBCPP_ABI_NO_ITERATOR_BASES``. If you are using this flag and care about ABI stability, you should set
``_LIBCPP_ABI_NO_REVERSE_ITERATOR_SECOND_MEMBER`` as well.

- ``align`` has been removed from libc++ dynamic library in ABI v2. You could use the ABI flag ``_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS`` to reserve it.

Announcements About Future Releases
-----------------------------------

Expand All @@ -114,5 +118,7 @@ ABI Affecting Changes
potentially inheriting from the types they wrap. At this point in time we are not aware of any ABI changes caused by
this.

- ``align`` has been removed from libc++ dynamic library in ABI v2. You could use the ABI flag ``_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS`` to reserve it.

Build System Changes
--------------------
26 changes: 26 additions & 0 deletions libcxx/include/__memory/align.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,41 @@

#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

__attribute__((always_inline)) inline void* __align_impl(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;
}

#ifdef _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS

_LIBCPP_EXPORTED_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);

#else

inline _LIBCPP_HIDE_FROM_ABI void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space) {
return __align_impl(__align, __sz, __ptr, __space);
}

#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___MEMORY_ALIGN_H
17 changes: 5 additions & 12 deletions libcxx/src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <__config>
#ifdef _LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS
# define _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS
# define _LIBCPP_SHARED_PTR_DEFINE_LEGACY_INLINE_FUNCTIONS
#endif

Expand Down Expand Up @@ -132,19 +133,11 @@ __sp_mut& __get_sp_mut(const void* p) {

#endif // _LIBCPP_HAS_THREADS

#if defined(_LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS)

void* align(size_t alignment, size_t size, void*& ptr, size_t& space) {
void* r = nullptr;
if (size <= space) {
char* p1 = static_cast<char*>(ptr);
char* p2 = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(p1 + (alignment - 1)) & -alignment);
size_t d = static_cast<size_t>(p2 - p1);
if (d <= space - size) {
r = p2;
ptr = r;
space -= d;
}
}
return r;
return __align_impl(alignment, size, ptr, space);
}
#endif // _LIBCPP_ALIGN_DEFINE_LEGACY_INLINE_FUNCTIONS

_LIBCPP_END_NAMESPACE_STD
27 changes: 27 additions & 0 deletions libcxx/test/benchmarks/memory/align.bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03

#include <memory>

#include "benchmark/benchmark.h"
#include "test_macros.h"

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

for (auto _ : state) {
benchmark::DoNotOptimize(std::align(state.range(), state.range(), data, sz));
}
}
BENCHMARK(BM_align)->Range(1, 256);

BENCHMARK_MAIN();
Loading