summaryrefslogtreecommitdiff
path: root/libcxx/src/memory.cpp
AgeCommit message (Collapse)Author
2025-05-28Revert "[libc++] Introduce ABI sensitive areas to avoid requiring ↵James Y Knight
_LIBCPP_HIDE_FROM_ABI everywhere (#131156)" (#141756) This reverts commit c861fe8a71e64f3d2108c58147e7375cd9314521. Unfortunately, this use of hidden visibility attributes causes user-defined specializations of standard-library types to also be marked hidden by default, which is incorrect. See discussion thread on #131156. ...and also reverts the follow-up commits: Revert "[libc++] Add explicit ABI annotations to functions from the block runtime declared in <__functional/function.h> (#140592)" This reverts commit 3e4c9dc299c35155934688184319d391b298fff7. Revert "[libc++] Make ABI annotations explicit for windows-specific code (#140507)" This reverts commit f73287e623a6c2e4a3485832bc3e10860cd26eb5. Revert "[libc++][NFC] Replace a few "namespace std" with the correct macro (#140510)" This reverts commit 1d411f27c769a32cb22ce50b9dc4421e34fd40dd.
2025-05-18[libc++] Introduce ABI sensitive areas to avoid requiring ↵Nikolas Klauser
_LIBCPP_HIDE_FROM_ABI everywhere (#131156) This patch introduces `_LIBCPP_{BEGIN,END}_EXPLICIT_ABI_ANNOTATIONS`, which allow us to have implicit annotations for most functions, and just where it's not "hide_from_abi everything" we add explicit annotations. This allows us to drop the `_LIBCPP_HIDE_FROM_ABI` macro from most functions in libc++.
2025-05-12[libc++] Fix missing #includes (#130536)Matt
Adds missing includes that were detected when I tried to build libc++ as a module. Working towards #127012
2024-11-06[libc++] Refactor the configuration macros to being always defined (#112094)Nikolas Klauser
This is a follow-up to #89178. This updates the `<__config_site>` macros.
2023-12-18[libc++] Format the code base (#74334)Louis Dionne
This patch runs clang-format on all of libcxx/include and libcxx/src, in accordance with the RFC discussed at [1]. Follow-up patches will format the benchmarks, the test suite and remaining parts of the code. I'm splitting this one into its own patch so the diff is a bit easier to review. This patch was generated with: find libcxx/include libcxx/src -type f \ | grep -v 'module.modulemap.in' \ | grep -v 'CMakeLists.txt' \ | grep -v 'README.txt' \ | grep -v 'libcxx.imp' \ | grep -v '__config_site.in' \ | xargs clang-format -i A Git merge driver is available in libcxx/utils/clang-format-merge-driver.sh to help resolve merge and rebase issues across these formatting changes. [1]: https://discourse.llvm.org/t/rfc-clang-formatting-all-of-libc-once-and-for-all
2023-08-18[libc++] Change _LIBCPP_CONSTEXPR_SINCE_XXX to constexpr in the dylibLouis Dionne
Since we build the dylib with C++20, there's no need to use conditional macros anymore. Differential Revision: https://reviews.llvm.org/D157995
2023-04-19[libc++] Remove symbols for a std::allocator_arg & friends from the dylibLouis Dionne
This patch removes the symbols defined in the library for std::allocator_arg, std::defer_lock, std::try_to_lock, std::adopt_lock, and std::piecewise_construct. Those were defined in the library because we provided them in C++03 as an extension, and in C++03 it was impossible to define them as `constexpr` variables, like in the spec. This is technically an ABI break since we are removing symbols from the library. However, in practice, only programs compiled in C++03 mode who take the address of those objects (or pass them as a reference) will have an undefined ref to those symbols. In practice, this is expected to be rare. First, those are C++11 features that we happen to provide in C++03, and only the C++03 definition can potentially lead to code referencing the dylib definition. So any code that is using these objects but compiling in C++11 mode (as they should) is not at risk. Second, all uses of these types in the library is done by passing those types by value to a function that can get inlined. Since they are empty types, the compiler won't generate an undefined reference if passed by value, since there's nothing to pass anyway. Long story short, the risk for code actually containing an undefined reference to one of these types is rather small (but non-zero). I also couldn't find any app on the App Store that referenced these symbols, which supports my impression that this won't be an issue in practice. Differential Revision: https://reviews.llvm.org/D145587
2022-10-03[libc++] Avoid relying on non-portable behaviour in std::alignAlex Richardson
Round-tripping pointers via size_t is not portable, the C/C++ standards only require this to be valid when using (u)intptr_t. Originally committed to the CHERI fork of LLVM as https://github.com/CTSRD-CHERI/llvm-project/commit/dd01245185ab9e71b70b418bee8f11ea0199e1a3, but I forgot to upstream the change. I rediscovered this issue due to a compiler warning when building libc++ on a Arm Morello system. Reviewed By: #libc, ldionne, philnik Differential Revision: https://reviews.llvm.org/D134363
2022-09-02[libc++] Make the naming of private member variables consistent and enforce ↵Nikolas Klauser
it through readability-identifier-naming Reviewed By: ldionne, #libc Spies: aheejin, sstefan1, libcxx-commits Differential Revision: https://reviews.llvm.org/D129386
2022-06-13[libc++] Do not yield from __sp_mut::lock()Louis Dionne
Instead of trying to be clever and design our own locking primitive, simply rely on the OS-provided implementation to do the right thing. Indeed, manually yielding to the OS does not provide the necessary information for it to make good prioritization decisions. For example, if a thread with higher priority yields while waiting for a lock held by a thread with lower priority but the system is contended, it is possible for the thread with lower priority to not run until the higher priority thread has yielded 16 times and goes for __libcpp_mutex_lock(). Once that happens, the OS can bump the priority of the thread that currently holds the lock to unblock everyone. So instead, we might as well give the system all the information from the start so it can make appropriate decisions. As a fly-by change, also increase the number of locks in the table. The size increase is modest, but has the potential to half the amount of contention on those locks. rdar://93598606 Differential Revision: https://reviews.llvm.org/D126882
2022-05-26[libc++][NFC] Fix whitespaceLouis Dionne
2022-04-12[libc++] Define legacy symbols for inline functions at a finer-grained levelLouis Dionne
When we build the library with the stable ABI, we need to include some functions in the dylib that were made inline in later versions of the library (to avoid breaking code that might be relying on those symbols). However, those methods were made non-inline whenever we'd be building the library, which means that all translation units would end up using the old out-of-line definition of these methods, as opposed to the new inlined version. This patch makes it so that only the translation units that actually define the out-of-line methods use the old definition, opening up potential optimization opportunities in other translation units. This should solve some of the issues encountered in D65667. Differential Revision: https://reviews.llvm.org/D123519
2022-02-15[libc++] Replace `#include ""` with `<>` in libcxx/src/. NFCI.Arthur O'Dwyer
Our best guess is that the two syntaxes should have exactly equivalent effects, so, let's be consistent with what we do in libcxx/include/. I've left `#include "include/x.h"` and `#include "../y.h"` alone because I'm less sure that they're interchangeable, and they aren't inconsistent with libcxx/include/ because libcxx/include/ never does that kind of thing. Also, use the `_LIBCPP_PUSH_MACROS/POP_MACROS` dance for `<__undef_macros>`, even though it's technically unnecessary in a standalone .cpp file, just so we have consistently one way to do it. Differential Revision: https://reviews.llvm.org/D119561
2022-02-15[libc++] Rename *SAFE_STATIC to *CONSTINIT, and normalize its uses.Arthur O'Dwyer
In src/, most files can use `constinit` directly because they're always compiled with C++20. But some files, like "libcxxabi/src/fallback_malloc.cpp", can't, because they're `#include`d directly from test cases in libcxxabi/test/ and therefore must (currently) compile as C++03. We might consider refactoring those offending tests, or at least marking them `UNSUPPORTED: c++03`. Differential Revision: https://reviews.llvm.org/D119264
2021-11-17[libc++] Enable <atomic> when threads are disabledLouis Dionne
std::atomic is, for the most part, just a thin veneer on top of compiler builtins. Hence, it should be available even when threads are not available on the system, and in fact there has been requests for such support. This patch: - Moves __libcpp_thread_poll_with_backoff to its own header so it can be used in <atomic> when threads are disabled. - Adds a dummy backoff policy for atomic polling that doesn't know about threads. - Adjusts the <atomic> feature-test macros so they are provided even when threads are disabled. - Runs the <atomic> tests when threads are disabled. rdar://77873569 Differential Revision: https://reviews.llvm.org/D114109
2021-11-17[runtimes][NFC] Remove filenames at the top of the license noticeLouis Dionne
We've stopped doing it in libc++ for a while now because these names would end up rotting as we move things around and copy/paste stuff. This cleans up all the existing files so as to stop the spreading as people copy-paste headers around.
2021-11-11[libc++] Implement P2186R2 (Remove Garbage Collection)Nikolas Klauser
Reviewed By: Quuxplusone, #libc, ldionne Differential Revision: https://reviews.llvm.org/D112869
2021-05-03[libc++] Disentangle std::pointer_safetyLouis Dionne
This patch gets rid of technical debt around std::pointer_safety which, I claim, is entirely unnecessary. I don't think anybody has used std::pointer_safety in actual code because we do not implement the underlying garbage collection support. In fact, P2186 even proposes removing these facilities entirely from a future C++ version. As such, I think it's entirely fine to get rid of complex workarounds whose goals were to avoid breaking the ABI back in 2017. I'm putting this up both to get reviews and to discuss this proposal for a breaking change. I think we should be comfortable with making these tiny breaks if we are confident they won't hurt anyone, which I'm fairly confident is the case here. Differential Revision: https://reviews.llvm.org/D100410
2021-03-03[libc++/abi] Replace uses of _NOEXCEPT in src/ by noexceptLouis Dionne
We always build the libraries in a Standard mode that supports noexcept, so there's no need to use the _NOEXCEPT macro. Differential Revision: https://reviews.llvm.org/D97700
2020-10-20[libc++] Make __shared_weak_count vtable consistent across all build ↵Eric Fiselier
configurations This patch ensures that __shared_weak_count provides a consistent vtable regardless of if RTTI is enabled or if we are targeting a static or shared libc++ build. This patch is technically ABI breaking, but only for a very specific configuration that no vendor should be shipping. Note that _LIBCPP_BUILD_STATIC is not normally defined when building libc++.a, but instead it must be manually provided by the user or the __config_site. Differential Revision: https://reviews.llvm.org/D32838
2019-12-02[libcxx{,abi}] Emit deplibs only when detected by CMakeMichał Górny
This is a followup to 35bc5276ca3. It fixes the dependent libs usage in libcxx and libcxxabi to link pthread and rt libraries only if CMake detects them, rather than based on explicit platform blacklist. Differential Revision: https://reviews.llvm.org/D70888
2019-07-22[runtimes] Don't depend on libpthread on AndroidYi Kong
r362048 added support for ELF dependent libraries, but broke Android build since Android does not have libpthread. Remove the dependency on the Android build. Differential Revision: https://reviews.llvm.org/D65098 llvm-svn: 366734
2019-05-30[runtimes] Use _LIBCPP_HAS_COMMENT_LIB_PRAGMA in all relevant filesPetr Hosek
These two sources were omitted in r362055. llvm-svn: 362061
2019-05-30[runtimes] Support ELF dependent libraries featurePetr Hosek
As of r360984, LLD supports dependent libraries feature for ELF. libunwind, libc++abi and libc++ have library dependencies: libdl librt and libpthread, which means that when libunwind and libc++ are being statically linked (using -static-libstdc++ flag), user has to manually specify -ldl -lpthread which is onerous. This change includes the lib pragma to specify the library dependencies directly in the source that uses those libraries. This doesn't make any difference when using linkers that don't support dependent libraries. However, when using LLD that has dependent libraries feature, users no longer have to manually specifying library dependencies when using static linking, linker will pick the library automatically. Differential Revision: https://reviews.llvm.org/D62090 llvm-svn: 362048
2019-01-19Update more file headers across all of the LLVM projects in the monorepoChandler Carruth
to reflect the new license. These used slightly different spellings that defeated my regular expressions. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351648
2018-08-01[libc++] Remove _LIBCPP_BUILDING_XXX macros, which are redundant since ↵Louis Dionne
_LIBCPP_BUILDING_LIBRARY Summary: As suggested by Marshall in https://reviews.llvm.org/D49914 Reviewers: mclow.lists, EricWF Subscribers: christof, dexonsmith, cfe-commits Differential Revision: https://reviews.llvm.org/D50008 llvm-svn: 338475
2017-05-04Fix incorrect usage of __libcpp_mutex_trylock. Patch from Andrey KhalyavinEric Fiselier
llvm-svn: 302129
2017-05-04Use nullptr instead of the literal 0Eric Fiselier
llvm-svn: 302100
2017-01-17Add ABI option to remove recently inlined __shared_count functions from the ↵Eric Fiselier
library. In order to allow inlining of previously out-of-line functions without an ABI break libc++ provides legacy definitions in the dylib that old programs can continue to use. Unfortunatly Windows link.exe detects this hack and diagnoses the duplicate definitions. This patch disable the duplicate definitions on Windows by adding an ABI option which disables all "legacy out-of-line symbols" llvm-svn: 292190
2017-01-17[Test patch] Inline hot functions in libcxx shared_ptrKevin Hu
Moves hot functions such as atomic add into the memory header file so that they can be inlined, which brings performance benefits. Patch by Kevin Hu, Aditya Kumar, Sebastian Pop Differential Revision: https://reviews.llvm.org/D24991 llvm-svn: 292184
2017-01-05Fix std::pointer_safety type in ABI v2Eric Fiselier
In the C++ standard `std::pointer_safety` is defined as a C++11 strongly typed enum. However libc++ currently defines it as a class type which simulates a C++11 enumeration. This can be detected in valid C++ code. This patch introduces an the _LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE ABI option. When defined `std::pointer_safety` is implemented as an enum type. Unfortunatly this also means it can no longer be provided as an extension in C++03. Additionally this patch moves the definition for `get_pointer_safety()` out of the dylib, and into the headers. New usages of `get_pointer_safety()` will now use the inline version instead of the dylib version. However in order to keep the dylib ABI compatible the old definition is explicitly compiled into it. llvm-svn: 291046
2016-09-28Mark libc++ internal globals with _LIBCPP_SAFE_STATIC.Eric Fiselier
This patch applies the _LIBCPP_SAFE_STATIC attribute to internal globals, most of which are locking primitives, in order to ensure that they can safely be used during program startup. This patch also fixes an unsafe static init issue with the global locks used to implement atomic operations on shared pointers. Previously the locks were initialized using a dynamically initialized pointer, so it was possible that the pointer was uninitialized. llvm-svn: 282640
2016-08-02Fixing 'Aquire' typo and libcxx build.Ben Craig
llvm-svn: 277456
2016-08-01Improve shared_ptr dtor performanceBen Craig
If the last destruction is uncontended, skip the atomic store on __shared_weak_owners_. This shifts some costs from normal shared_ptr usage to weak_ptr uses. https://reviews.llvm.org/D22470 llvm-svn: 277357
2016-06-18Enable building and using atomic shared_ptr for GCC.Eric Fiselier
Summary: Currently the implementation of [util.smartptr.shared.atomic] is provided only when using Clang, and not with GCC. This is a relic of not having a GCC implementation of <atomic>, even though <atomic> isn't actually used in the implementation. This patch enables support for atomic shared_ptr functions when using GCC. Note that this is not a header only change. Previously only Clang builds of libc++.so would provide the required symbols. There is no reason for this restriction. After this change both Clang and GCC builds should be binary compatible with each other WRT these symbols. Reviewers: mclow.lists, rmaprath, EricWF Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D21407 llvm-svn: 273076
2016-05-06Refactor pthread usage of libcxx.Asiri Rathnayake
This patch extracts out all the pthread dependencies of libcxx into the new header __threading_support. The motivation is to make it easy to re-target libcxx into platforms that do not support pthread. Original patch from Fulvio Esposito (fulvio.esposito@outlook.com) - D11781 Applied with tweaks - D19412 Change-Id: I301111f0075de93dd8129416e06babc195aa936b llvm-svn: 268734
2015-08-19[libcxx] Allow use of <atomic> in C++03. Try 3.Eric Fiselier
Summary: After putting this question up on cfe-dev I have decided that it would be best to allow the use of `<atomic>` in C++03. Although static initialization is a concern the syntax required to get it is C++11 only. Meaning that C++11 constant static initialization cannot silently break in C++03, it will always cause a syntax error. Furthermore `ATOMIC_VAR_INIT` and `ATOMIC_FLAG_INIT` remain defined in C++03 even though they cannot be used because C++03 usages will cause better error messages. The main change in this patch is to replace `__has_feature(cxx_atomic)`, which only returns true when C++ >= 11, to `__has_extension(c_atomic)` which returns true whenever clang supports the required atomic builtins. This patch adds the following macros: * `_LIBCPP_HAS_C_ATOMIC_IMP` - Defined on clang versions which provide the C `_Atomic` keyword. * `_LIBCPP_HAS_GCC_ATOMIC_IMP` - Defined on GCC > 4.7. We must use the fallback atomic implementation. * `_LIBCPP_HAS_NO_ATOMIC_HEADER` - Defined when it is not safe to include `<atomic>`. `_LIBCPP_HAS_C_ATOMIC_IMP` and `_LIBCPP_HAS_GCC_ATOMIC_IMP` are mutually exclusive, only one should be defined. If neither is defined then `<atomic>` is not implemented and including `<atomic>` will issue an error. Reviewers: chandlerc, jroelofs, mclow.lists Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D11555 llvm-svn: 245463
2015-08-18Move atomic_support.h and config_elast.h into src/includeEric Fiselier
llvm-svn: 245354
2015-07-07[libcxx] Add atomic_support.h header to src that handles needed atomic ↵Eric Fiselier
operations. Summary: In some places in libc++ we need to use the `__atomic_*` builtins. This patch adds a header that provides access to those builtins in a uniform way from within the dylib source. If the compiler building the dylib does not support these builtins then a warning is issued. Only relaxed loads are needed within the headers. A singe function to do these relaxed loads has been added to `<memory>`. This patch applies the new atomic builtins to `__shared_count` and `call_once`. Reviewers: mclow.lists Subscribers: majnemer, jroelofs, cfe-commits Differential Revision: http://reviews.llvm.org/D10406 llvm-svn: 241532
2014-12-12Fix building and running tests when LIBCXX_ENABLE_EXCEPTIONS or ↵Eric Fiselier
LIBCXX_ENABLE_RTTI are turned off. llvm-svn: 224095
2014-09-05Address some post-commit review comments on r217261Jonathan Roelofs
llvm-svn: 217276
2014-09-05Allow libc++ to be built on systems without POSIX threadsJonathan Roelofs
If you're crazy enough to want this sort of thing, then add -D_LIBCPP_HAS_NO_THREADS to your CXXFLAGS and --param=additiona_features=libcpp-has-no-threads to your lit commnad line. http://reviews.llvm.org/D3969 llvm-svn: 217271
2014-01-04Switch to using C++ style casts.Joerg Sonnenberger
llvm-svn: 198505
2013-03-16This should be nothing but a load-time optimization. I'm trying to reduce ↵Howard Hinnant
load time initializers and this is a big one. No visible functionality change intended. llvm-svn: 177212
2012-10-30Rename uses of _ and __ because these are getting stepped on by macros from ↵Howard Hinnant
other system code. llvm-svn: 167038
2012-08-19Patch contributed by Dev Dude for mingw64 port.Howard Hinnant
llvm-svn: 162188
2012-07-30Despite my pathological distrust of spin locks, the number just don't lie. ↵Howard Hinnant
I've put a small spin in __sp_mut::lock() on std::mutex::try_lock(), which is testing quite well. In my experience, putting in a yield for every failed iteration is also a major performance booster. This change makes one of the performance tests I was using (a highly contended one) run about 20 times faster. llvm-svn: 160967
2012-07-30Implement [util.smartptr.shared.atomic]. This is the last unimplementedHoward Hinnant
section in libc++. This requires a recompiled dylib. Failure to rebuild the dylib will result in a link-time error if and only if the functions from [util.smartptr.shared.atomic] are used. The implementation is not lock free. After considerable thought, I know of no way to make the implementation lock free. Ideas welcome along that front. But changing the ABI of shared_ptr is not on the table at this point. The mutex used to lock these function is encapsulated by std::__sp_mut. The only thing the client knows about std::__sp_mut is that it has a void* data member, can't be constructed, and has lock and unlock members. Within the binary __sp_mut is currently implemented as a pointer to a std::mutex. That can change in the future without disturbing the ABI (as long as sizeof(__sp_mut) remains constant. I specifically did not make __sp_mut a spin lock as I have a pathological distrust of spin locks. Testing on OS X reveals that the use of std::mutex in this role is not a large performance penalty as long as the contention for the mutex is low (more likely to get the lock than to have to wait). In the future we can still make __sp_mut a spin lock if that is what is desired (without ABI damage). The dylib contains 16 __sp_mut's to be chosen based on the hash of the address of the shared_ptr. The constant 16 is a ball-park reasonable space/time tradeoff. std::hash<T*> was changed to call __murmur2_or_cityhash, instead of the identity function. I had thought we had already done this, but I was mistaken. All of this is under #if __has_feature(cxx_atomic) even though the implementation is not lock free, because the signatures require access to std::memory_order, which is currently available only under __has_feature(cxx_atomic). llvm-svn: 160940
2012-07-07Appy constexpr to <memory>. Picked up a few missing noexcepts as well.Howard Hinnant
llvm-svn: 159902
2011-12-27Fix memory leak in converting weak_ptr to shared_ptrHoward Hinnant
llvm-svn: 147298