6071 Commits

Author SHA1 Message Date
Peng Xie
6285c46e16
[libc++] Refactor some code in monotonic_buffer_resource (#117271)
1. remove unused __default_buffer_alignment
2. two __try_allocate_from_chunk are same, put it together

This patch refactor some code in monotonic_buffer_resource.
2024-12-23 14:32:59 +08:00
Nikolas Klauser
b41d50aaa9 [libc++][NFC] Name unique_ptr function arguments __ptr and __deleter
This makes the code a bit more readable and users will see `ptr` and
`deleter` instead of `p` and `d` when using an IDE that shows the
argument names.
2024-12-22 03:03:42 +01:00
Nikolas Klauser
9b0c8ef37a
[libc++] Don't declare pmr::polymorphic_allocator before C++17 (#120850)
Fixes #120790
2024-12-22 00:56:06 +01:00
Nikolas Klauser
3b8faee6c6
[libc++] Switch _LIBCPP_NODEBUG to [[gnu::nodebug]] (#120720)
This makes the placement of the attribute more consistent. This also
avoids clang dropping the attribute silently (see #120722).
2024-12-22 00:55:35 +01:00
Nikolas Klauser
b9a2658a3e
[libc++][C++03] Use __cxx03/ headers in C++03 mode (#109002)
This patch implements the forwarding to frozen C++03 headers as
discussed in
https://discourse.llvm.org/t/rfc-freezing-c-03-headers-in-libc. In the
RFC, we initially proposed selecting the right headers from the Clang
driver, however consensus seemed to steer towards handling this in the
library itself. This patch implements that direction.

At a high level, the changes basically amount to making each public
header look like this:

```
// inside <vector>
#ifdef _LIBCPP_CXX03_LANG
#  include <__cxx03/vector>
#else
  // normal <vector> content
#endif
```

In most cases, public headers are simple umbrella headers so there isn't
much code in the #else branch. In other cases, the #else branch contains
the actual implementation of the header.
2024-12-21 13:01:48 +01:00
Louis Dionne
34e0f9cd36
[libc++] Remove the need for uselocale() (#120158)
Instead of requiring `uselocale()` as part of the base locale API,
define __locale_guard in the few places that need it directly, without
making __locale_guard part of the base API.

In practice, most mainstream platforms never used __locale_guard, so
they also didn't need to define uselocale(), and after this patch they
actually don't define it anymore.
2024-12-19 16:06:08 -05:00
Peng Liu
fafdf97047
[libc++] Simplify vector<bool>::flip() and add new tests (#119607)
This PR simplifies the internal bitwise logic of the `flip()` function
for `vector<bool>`, and creates new tests to validate the changes.
2024-12-19 11:48:51 -05:00
Nikolas Klauser
b905bcc509
[libc++] Remove some unused includes (#120219) 2024-12-18 21:10:27 +01:00
Louis Dionne
ce4ac99452
[libc++] Remove explicit mentions of __need_FOO macros (#119025)
This change has a long history. It was first attempted naively in
https://reviews.llvm.org/D131425, which didn't work because we broke the
ability for code to include e.g. <stdio.h> multiple times and get
different definitions based on the pre-defined macros.

However, in #86843 we managed to simplify <stddef.h> by including the
underlying system header outside of any include guards, which worked.

This patch applies the same simplification we did to <stddef.h> to the
other headers that currently mention __need_FOO macros explicitly.
2024-12-17 09:52:34 -05:00
Nikolas Klauser
59890c1334
[libc++] Granularize <new> includes (#119964) 2024-12-17 11:29:16 +01:00
Louis Dionne
084309a0ef
[libc++] Refactor the Windows and MinGW implementation of the locale base API (#115752)
This patch reimplements the locale base support for Windows flavors in a
way that is more modules-friendly and without defining non-internal
names.

Since this changes the name of some types and entry points in the built
library, this is effectively an ABI break on Windows (which is
acceptable after checking with the Windows/libc++ maintainers).
2024-12-16 17:46:05 -05:00
Nikolas Klauser
6157dbe48c
[libc++] Introduce __forward_as (#118168)
This allows forwarding an object as a specific type. This is usually
useful when using `deducing this` to avoid calling any functions in a
deriving class.
2024-12-14 15:09:16 +01:00
Louis Dionne
9474e09459
[libc++] Granularize the <new> header (#119270)
This disentangles the code which previously had a mix of many #ifdefs, a
non-versioned namespace and a versioned namespace. It also makes it
clearer which parts of <new> are implemented on Windows by including <new.h>.
2024-12-13 14:17:56 -05:00
Peng Liu
979e9361f0
[libc++] Fix improper static_cast in std::deque and __split_buffer (#119106)
This PR addresses the improper use of `static_cast` to `size_t` where
`size_type` is intended. Although the `size_type` member type of STL
containers is usually a synonym of `std::size_t`, there is no guarantee
that they are always equivalent. The C++ standard does not mandate this
equivalence.

In libc++'s implementations of `std::deque`, `std::vector`, and
`__split_buffer`, the `size_type` member type is defined as
`std::allocator_traits<allocator_type>::size_type`, which is either
`allocator_type::size_type` if available or
`std::make_unsigned<difference_type>::type`. While it is true for
`std::allocator` that the `size_type` member type is `std::size_t`, for
user-defined allocator types, they may mismatch. This justifies the need
to replace `static_cast<size_t>` with `static_cast<size_type>` in this
PR.
2024-12-13 09:28:30 -05:00
Nikolas Klauser
5fd385b3c1
[libc++][NFC] Simplify the implementation of string and string_views operator== (#117184) 2024-12-13 13:17:19 +01:00
Louis Dionne
4b8bf6aac8
[libc++] Properly guard flat_map includes based on C++ version (#119227)
That's what we (try to) do consistently for all other umbrella headers.

As a drive-by, remove the <__assert> header which is not mandated
anymore.
2024-12-11 16:53:33 -05:00
Nikolas Klauser
323bedd0d6
[libc++][C++03] Add #if 0 to the experimental/ and ext/ headers as well (#119541)
This has already been done for the most headers in
https://github.com/llvm/llvm-project/pull/119234, but I
forgot to also do it for the experimental/ and ext/ headers.

This is part of https://discourse.llvm.org/t/rfc-freezing-c-03-headers-in-libc.
2024-12-11 10:35:21 -05:00
Nikolas Klauser
c166a9c713
[libc++] Add #if 0 block to all the top-level headers (#119234)
Including The frozen C++03 headers results in a lot of formatting
changes in the main headers, so this splits these changes into a
separate commit instead.

This is part of
https://discourse.llvm.org/t/rfc-freezing-c-03-headers-in-libc.
2024-12-10 16:02:12 +01:00
Louis Dionne
4c4606a743
[libc++] Add missing assertion in std::span constructor (#118396)
The (iterator, size) constructor should ensure that it gets passed a
valid range when the size is not 0.

Fixes #107789
2024-12-09 11:51:07 -05:00
Nikolas Klauser
ce7771902d
[libc++][C++03] Update include guards (#109001)
This patch updates the includes guards of the C++ 03 headers.

This is part of https://discourse.llvm.org/t/rfc-freezing-c-03-headers-in-libc.
2024-12-09 17:34:37 +01:00
Hui
f22ecdd9b9
[libc++] Move out flat_map::iterator (for reusing it in flat_multimap) (#117445) 2024-12-09 09:22:27 -05:00
Louis Dionne
0e34f3f496
[libc++] Extract a clean base support API for std::atomic (#118129)
This patch documents the underlying API for implementing atomics on a
platform.

This doesn't change the operations that std::atomic is based on, but it
reorganizes the C11 / GCC implementation split to make it clearer what's
the base support layer and what's not.
2024-12-09 09:14:17 -05:00
cmtice
384e69a914
[libc++] Add _LIBCPP_NODEBUG on internal allocator trait aliases (#118835)
Put _LIBCPP_NODEBUG on the new allocator trait aliases introduced in
https://github.com/llvm/llvm-project/pull/115654. This prevents a large
increase in the gdb_index size that was introduced by that PR.
2024-12-06 08:53:56 -05:00
serge-sans-paille
f7ff3cde96
[libc++] Fix sub-overflow in std::gcd implementation (#117984)
Fix #117249
2024-12-06 07:57:49 +00:00
Louis Dionne
2393ab65ed
[libc++] Fix unintended ABI break in associative containers with reference comparators (#118685)
While reference comparators are a terrible idea and it's not entirely
clear whether they are supported, fixing the unintended ABI break is
straightforward so we should do it as a first step.

Fixes #118559
2024-12-05 15:56:00 -05:00
Louis Dionne
c8060423fe
[libc++] Drop dependency on __functional/operations.h from <atomic> (#117302)
This should reduce the preprocessed size of the atomic header and other
headers in the synchronization library.
2024-12-03 16:57:17 -05:00
Louis Dionne
3d437893c3
[libc++] Re-enable Clang-tidy checks in the CI (#110026)
Now that we've gained control of our CI docker image again,
we can re-enable the clang-tidy tests.
2024-12-02 14:26:28 -05:00
Nikolas Klauser
04cc492ec3 [libc++][NFC] Remove unused macros from <__configuration/availability.h> 2024-11-30 13:18:33 +01:00
Benjamin Kramer
59f57be94f Revert "[libc++] Simplify the implementation of reserve() and shrink_to_fit() (#113453)"
This reverts commit d648eed5899c4be10f1f7866eebef2bc171e673f. Breaks
anything that relies on sized deallocation, e.g. asan and tcmalloc.
2024-11-29 13:18:28 +01:00
Nikolas Klauser
d648eed589
[libc++] Simplify the implementation of reserve() and shrink_to_fit() (#113453)
Since we changed the implementation of `reserve(size_type)` to only ever
extend,
it doesn't make a ton of sense anymore to have `__shrink_or_extend`,
since the code
paths of `reserve` and `shrink_to_fit` are now almost completely
separate.

This patch splits up `__shrink_or_extend` so that the individual parts
are in `reserve`
and `shrink_to_fit` depending on where they are needed.
2024-11-28 23:07:45 +01:00
Louis Dionne
81c88135e4
[libc++] Use the __strtoNUM functions from __locale instead of the old API (#118029)
The commit where I switched from using the old locale base API to the
new functions defined inside the __locale namespace forgot to update
references to the strtoNUM functions. This patch fixes that.
2024-11-28 16:44:46 -05:00
Peng Liu
056153f36e
Optimize vector::assign for InputIterator-only pair inputs (#113852)
This PR optimizes the input iterator overload of `assign(_InputIterator,
_InputIterator)` in `std::vector<_Tp, _Allocator>` by directly assigning
to already initialized memory, rather than first destroying existing
elements and then constructing new ones. By eliminating unnecessary
destruction and construction, the proposed algorithm enhances the
performance by up to 2x for trivial element types (e.g.,
`std::vector<int>`), up to 2.6x for non-trivial element types like
`std::vector<std::string>`, and up to 3.4x for more complex non-trivial
types (e.g., `std::vector<std::vector<int>>`).

###  Google Benchmarks

Benchmark tests (`libcxx/test/benchmarks/vector_operations.bench.cpp`)
were conducted for the `assign()` implementations before and after this
patch. The tests focused on trivial element types like
`std::vector<int>`, and non-trivial element types such as
`std::vector<std::string>` and `std::vector<std::vector<int>>`.



#### Before
```
-------------------------------------------------------------------------------------------------
Benchmark                                                       Time             CPU   Iterations
-------------------------------------------------------------------------------------------------
BM_AssignInputIterIter/vector_int/1024/1024                  1157 ns         1169 ns       608188
BM_AssignInputIterIter<32>/vector_string/1024/1024          14559 ns        14710 ns        47277
BM_AssignInputIterIter<32>/vector_vector_int/1024/1024      26846 ns        27129 ns        25925
```


#### After
```
-------------------------------------------------------------------------------------------------
Benchmark                                                       Time             CPU   Iterations
-------------------------------------------------------------------------------------------------
BM_AssignInputIterIter/vector_int/1024/1024                   561 ns          566 ns      1242251
BM_AssignInputIterIter<32>/vector_string/1024/1024           5604 ns         5664 ns       128365
BM_AssignInputIterIter<32>/vector_vector_int/1024/1024       7927 ns         8012 ns        88579
```
2024-11-28 20:52:59 +01:00
Peng Liu
c5cd1e958c
[libc++] Add exception guard for vector<bool>::__init_with_sentinel (#115491)
As a drive-by, also improve the test coverage for throwing exceptions
in vector<bool> constructors.
2024-11-28 09:09:54 -05:00
Louis Dionne
d681e1030f
[libc++] Refactor atomic_wait using lambdas (#115746)
Now that we've dropped support for older C++ dialects in the
synchronization library, we can use lambdas to clarify some of the code
used to implement atomic_wait.
2024-11-27 14:49:57 -05:00
Nico Weber
a94cec5212
Revert "[libc++] Remove workaround which allows setting _LIBCPP_OVERRIDABLE_FUNC_VIS externally (#113139)" (#117779)
This reverts commit 2e686d6d17c4cc7608510a856055e6ca79fcb917.

See https://github.com/llvm/llvm-project/issues/117571
2024-11-26 18:45:03 -05:00
Nikolas Klauser
7ae61a36f9
[libc++] Add __detected_or_t and use it to implement some of the allocator traits aliases (#115654)
This simplifies the implementation a bit, since we don't need a lot of
the `__has_x` classes anymore. We just need two template aliases to
implement the `allocator_traits` aliases now.
2024-11-26 23:53:26 +01:00
Peng Liu
8458bbe594
[libc++] Fix capacity increase issue with shrink_to_fit for __split_buffer (#117720)
This PR fixes the issue where `__split_buffer::shrink_to_fit` may
unexpectedly increase the capacity, similar to the issue for
`std::vector` in #97895. The fix follows the same approach
used in #97895 for `std::vector`.
2024-11-26 16:38:47 -05:00
Peng Liu
5ce981e76d
[libc++] Refactor vector move constructor with allocator (#116449)
This PR simplifies the implementation of std::vector's move constructor
with an alternative allocator by invoking __init_with_size() instead of
calling assign(), which ultimately calls __assign_with_size(). The
advantage of using __init_with_size() lies in its internal use of
an exception guard, which simplifies the code. Furthermore, from a
semantic standpoint, it is more intuitive for a constructor to call
an initialization function than an assignment function.
2024-11-26 16:00:14 -05:00
Nikolas Klauser
99fd1c5536 [libc++][NFC] Don't add legacy transitive includes in <__chrono/duration.h> 2024-11-25 17:40:54 +01:00
Louis Dionne
afae1a5f32
[libc++] Remove _LIBCPP_DISABLE_AVAILABILITY macro (#112952)
This was slated for removal years ago, so now's a good time to remove it.
2024-11-23 20:02:15 -05:00
Nikolas Klauser
4a8329cd7d
[libc++] Granularize <mutex> includes (#117068) 2024-11-23 12:21:23 +01:00
Jay Foad
d6fc7d3ab1 Fix typo "intead" 2024-11-21 14:48:38 +00:00
Nikolas Klauser
9ebc6f5d6d
[libc++] Include headers in <thread> conditionally (#116539) 2024-11-20 23:07:20 +01:00
Louis Dionne
3a63407686
[libc++] Make __atomic_base into an implementation detail of std::atomic (#115764)
The __atomic_base base class is only useful to conditionalize the
operations we provide inside std::atomic. It shouldn't be used directly
from other places in the library which can use std::atomic directly
instead.

Since we've granularized our includes, using std::atomic directly should
not make much of a difference compile-time wise.

This patch starts using std::atomic directly from other classes like
std::barrier and std::latch. Changing this shouldn't be an ABI break
since both classes have the same size and layout.

The benefits of this patch are isolating other parts of the code base
from implementation details of std::atomic and simplifying the mental
model for std::atomic's layers of implementation by making it clear that
__atomic_base is only an implementation detail of std::atomic.
2024-11-20 00:35:14 +01:00
Louis Dionne
ec67ad594b [libc++][NFC] Format <string> 2024-11-18 23:23:46 +01:00
Nikolas Klauser
85ef9666c8
[libc++] Avoid including all of <thread> in <future> (#116541) 2024-11-18 20:04:05 +01:00
Nikolas Klauser
de5e4ebb5a
[libc++] Remove transitive includes from empty headers (#116295)
This removes transitive includes that are only in a header that is empty
in a given C++ version.
2024-11-18 19:59:47 +01:00
Louis Dionne
748a29f052 [libc++][NFC] Fix incorrect include guard 2024-11-18 14:29:14 +01:00
Louis Dionne
5a48162dc8
[libc++] Remove unnecessary std::vector accessors (#114423)
Now that we don't use __compressed_pair anymore inside std::vector, we
can remove some unnecessary accessors. This is a mechanical replacement
of the __alloc() and __end_cap() accessors, and similar for
std::vector<bool>.

Note that I consistently used this->__alloc_ instead of just __alloc_
since most of the code in <vector> uses that pattern to access members.
I don't think this is necessary anymore (and I'm even less certain I
like this), but I went for consistency with surrounding code. If we want
to change that, we can do a follow-up mechanical change.
2024-11-18 10:48:09 +01:00
A. Jiang
b5bc528c14
[libc++] Guard __pad_and_output with _LIBCPP_HAS_LOCALIZATION (#116580)
This fixes errors for no-localization builds (possibly introduced by
#116223).
2024-11-18 17:03:29 +08:00