xref: /openbsd-src/gnu/llvm/libcxx/src/include/refstring.h (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1*4bdff4beSrobert //===----------------------------------------------------------------------===//
246035553Spatrick //
346035553Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
446035553Spatrick // See https://llvm.org/LICENSE.txt for license information.
546035553Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
646035553Spatrick //
746035553Spatrick //===----------------------------------------------------------------------===//
846035553Spatrick 
946035553Spatrick #ifndef _LIBCPP_REFSTRING_H
1046035553Spatrick #define _LIBCPP_REFSTRING_H
1146035553Spatrick 
1246035553Spatrick #include <__config>
1346035553Spatrick #include <stdexcept>
1446035553Spatrick #include <cstddef>
1546035553Spatrick #include <cstring>
1676d0caaeSpatrick #include "atomic_support.h"
1776d0caaeSpatrick 
1876d0caaeSpatrick // MacOS and iOS used to ship with libstdc++, and still support old applications
1976d0caaeSpatrick // linking against libstdc++. The libc++ and libstdc++ exceptions are supposed
2076d0caaeSpatrick // to be ABI compatible, such that they can be thrown from one library and caught
2176d0caaeSpatrick // in the other.
2276d0caaeSpatrick //
2376d0caaeSpatrick // For that reason, we must look for libstdc++ in the same process and if found,
2476d0caaeSpatrick // check the string stored in the exception object to see if it is the GCC empty
2576d0caaeSpatrick // string singleton before manipulating the reference count. This is done so that
2676d0caaeSpatrick // if an exception is created with a zero-length string in libstdc++, libc++abi
2776d0caaeSpatrick // won't try to delete the memory.
2876d0caaeSpatrick #if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \
2976d0caaeSpatrick     defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
3076d0caaeSpatrick #   define _LIBCPP_CHECK_FOR_GCC_EMPTY_STRING_STORAGE
3146035553Spatrick #   include <dlfcn.h>
3246035553Spatrick #   include <mach-o/dyld.h>
3346035553Spatrick #endif
3446035553Spatrick 
3546035553Spatrick _LIBCPP_BEGIN_NAMESPACE_STD
3646035553Spatrick 
3746035553Spatrick namespace __refstring_imp { namespace {
3846035553Spatrick typedef int count_t;
3946035553Spatrick 
4046035553Spatrick struct _Rep_base {
4146035553Spatrick     std::size_t len;
4246035553Spatrick     std::size_t cap;
4346035553Spatrick     count_t     count;
4446035553Spatrick };
4546035553Spatrick 
rep_from_data(const char * data_)4646035553Spatrick inline _Rep_base* rep_from_data(const char *data_) noexcept {
4746035553Spatrick     char *data = const_cast<char *>(data_);
4846035553Spatrick     return reinterpret_cast<_Rep_base *>(data - sizeof(_Rep_base));
4946035553Spatrick }
5046035553Spatrick 
data_from_rep(_Rep_base * rep)5146035553Spatrick inline char * data_from_rep(_Rep_base *rep) noexcept {
5246035553Spatrick     char *data = reinterpret_cast<char *>(rep);
5346035553Spatrick     return data + sizeof(*rep);
5446035553Spatrick }
5546035553Spatrick 
5676d0caaeSpatrick #if defined(_LIBCPP_CHECK_FOR_GCC_EMPTY_STRING_STORAGE)
5746035553Spatrick inline
compute_gcc_empty_string_storage()5876d0caaeSpatrick const char* compute_gcc_empty_string_storage() noexcept
5946035553Spatrick {
6046035553Spatrick     void* handle = dlopen("/usr/lib/libstdc++.6.dylib", RTLD_NOLOAD);
6146035553Spatrick     if (handle == nullptr)
6246035553Spatrick         return nullptr;
6346035553Spatrick     void* sym = dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE");
6446035553Spatrick     if (sym == nullptr)
6546035553Spatrick         return nullptr;
6646035553Spatrick     return data_from_rep(reinterpret_cast<_Rep_base *>(sym));
6746035553Spatrick }
6846035553Spatrick 
6946035553Spatrick inline
7046035553Spatrick const char*
get_gcc_empty_string_storage()7176d0caaeSpatrick get_gcc_empty_string_storage() noexcept
7246035553Spatrick {
7346035553Spatrick     static const char* p = compute_gcc_empty_string_storage();
7446035553Spatrick     return p;
7546035553Spatrick }
7646035553Spatrick #endif
7746035553Spatrick 
7846035553Spatrick }} // namespace __refstring_imp
7946035553Spatrick 
8046035553Spatrick using namespace __refstring_imp;
8146035553Spatrick 
8246035553Spatrick inline
__libcpp_refstring(const char * msg)8346035553Spatrick __libcpp_refstring::__libcpp_refstring(const char* msg) {
8446035553Spatrick     std::size_t len = strlen(msg);
8546035553Spatrick     _Rep_base* rep = static_cast<_Rep_base *>(::operator new(sizeof(*rep) + len + 1));
8646035553Spatrick     rep->len = len;
8746035553Spatrick     rep->cap = len;
8846035553Spatrick     rep->count = 0;
8946035553Spatrick     char *data = data_from_rep(rep);
9046035553Spatrick     std::memcpy(data, msg, len + 1);
9146035553Spatrick     __imp_ = data;
9246035553Spatrick }
9346035553Spatrick 
9446035553Spatrick inline
__libcpp_refstring(const __libcpp_refstring & s)9576d0caaeSpatrick __libcpp_refstring::__libcpp_refstring(const __libcpp_refstring &s) noexcept
9646035553Spatrick     : __imp_(s.__imp_)
9746035553Spatrick {
9846035553Spatrick     if (__uses_refcount())
9946035553Spatrick         __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1);
10046035553Spatrick }
10146035553Spatrick 
10246035553Spatrick inline
10376d0caaeSpatrick __libcpp_refstring& __libcpp_refstring::operator=(__libcpp_refstring const& s) noexcept {
10446035553Spatrick     bool adjust_old_count = __uses_refcount();
10546035553Spatrick     struct _Rep_base *old_rep = rep_from_data(__imp_);
10646035553Spatrick     __imp_ = s.__imp_;
10746035553Spatrick     if (__uses_refcount())
10846035553Spatrick         __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1);
10946035553Spatrick     if (adjust_old_count)
11046035553Spatrick     {
11146035553Spatrick         if (__libcpp_atomic_add(&old_rep->count, count_t(-1)) < 0)
11246035553Spatrick         {
11346035553Spatrick             ::operator delete(old_rep);
11446035553Spatrick         }
11546035553Spatrick     }
11646035553Spatrick     return *this;
11746035553Spatrick }
11846035553Spatrick 
11946035553Spatrick inline
~__libcpp_refstring()12046035553Spatrick __libcpp_refstring::~__libcpp_refstring() {
12146035553Spatrick     if (__uses_refcount()) {
12246035553Spatrick         _Rep_base* rep = rep_from_data(__imp_);
12346035553Spatrick         if (__libcpp_atomic_add(&rep->count, count_t(-1)) < 0) {
12446035553Spatrick             ::operator delete(rep);
12546035553Spatrick         }
12646035553Spatrick     }
12746035553Spatrick }
12846035553Spatrick 
12946035553Spatrick inline
__uses_refcount()13046035553Spatrick bool __libcpp_refstring::__uses_refcount() const {
13176d0caaeSpatrick #if defined(_LIBCPP_CHECK_FOR_GCC_EMPTY_STRING_STORAGE)
13246035553Spatrick     return __imp_ != get_gcc_empty_string_storage();
13346035553Spatrick #else
13446035553Spatrick     return true;
13546035553Spatrick #endif
13646035553Spatrick }
13746035553Spatrick 
13846035553Spatrick _LIBCPP_END_NAMESPACE_STD
13946035553Spatrick 
14046035553Spatrick #endif //_LIBCPP_REFSTRING_H
141