1*4d6fc14bSjoerg //===------------------------ __refstring ---------------------------------===//
2*4d6fc14bSjoerg //
3*4d6fc14bSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*4d6fc14bSjoerg // See https://llvm.org/LICENSE.txt for license information.
5*4d6fc14bSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*4d6fc14bSjoerg //
7*4d6fc14bSjoerg //===----------------------------------------------------------------------===//
8*4d6fc14bSjoerg
9*4d6fc14bSjoerg #ifndef _LIBCPP_REFSTRING_H
10*4d6fc14bSjoerg #define _LIBCPP_REFSTRING_H
11*4d6fc14bSjoerg
12*4d6fc14bSjoerg #include <__config>
13*4d6fc14bSjoerg #include <stdexcept>
14*4d6fc14bSjoerg #include <cstddef>
15*4d6fc14bSjoerg #include <cstring>
16*4d6fc14bSjoerg #include "atomic_support.h"
17*4d6fc14bSjoerg
18*4d6fc14bSjoerg // MacOS and iOS used to ship with libstdc++, and still support old applications
19*4d6fc14bSjoerg // linking against libstdc++. The libc++ and libstdc++ exceptions are supposed
20*4d6fc14bSjoerg // to be ABI compatible, such that they can be thrown from one library and caught
21*4d6fc14bSjoerg // in the other.
22*4d6fc14bSjoerg //
23*4d6fc14bSjoerg // For that reason, we must look for libstdc++ in the same process and if found,
24*4d6fc14bSjoerg // check the string stored in the exception object to see if it is the GCC empty
25*4d6fc14bSjoerg // string singleton before manipulating the reference count. This is done so that
26*4d6fc14bSjoerg // if an exception is created with a zero-length string in libstdc++, libc++abi
27*4d6fc14bSjoerg // won't try to delete the memory.
28*4d6fc14bSjoerg #if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \
29*4d6fc14bSjoerg defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
30*4d6fc14bSjoerg # define _LIBCPP_CHECK_FOR_GCC_EMPTY_STRING_STORAGE
31*4d6fc14bSjoerg # include <dlfcn.h>
32*4d6fc14bSjoerg # include <mach-o/dyld.h>
33*4d6fc14bSjoerg #endif
34*4d6fc14bSjoerg
35*4d6fc14bSjoerg _LIBCPP_BEGIN_NAMESPACE_STD
36*4d6fc14bSjoerg
37*4d6fc14bSjoerg namespace __refstring_imp { namespace {
38*4d6fc14bSjoerg typedef int count_t;
39*4d6fc14bSjoerg
40*4d6fc14bSjoerg struct _Rep_base {
41*4d6fc14bSjoerg std::size_t len;
42*4d6fc14bSjoerg std::size_t cap;
43*4d6fc14bSjoerg count_t count;
44*4d6fc14bSjoerg };
45*4d6fc14bSjoerg
rep_from_data(const char * data_)46*4d6fc14bSjoerg inline _Rep_base* rep_from_data(const char *data_) noexcept {
47*4d6fc14bSjoerg char *data = const_cast<char *>(data_);
48*4d6fc14bSjoerg return reinterpret_cast<_Rep_base *>(data - sizeof(_Rep_base));
49*4d6fc14bSjoerg }
50*4d6fc14bSjoerg
data_from_rep(_Rep_base * rep)51*4d6fc14bSjoerg inline char * data_from_rep(_Rep_base *rep) noexcept {
52*4d6fc14bSjoerg char *data = reinterpret_cast<char *>(rep);
53*4d6fc14bSjoerg return data + sizeof(*rep);
54*4d6fc14bSjoerg }
55*4d6fc14bSjoerg
56*4d6fc14bSjoerg #if defined(_LIBCPP_CHECK_FOR_GCC_EMPTY_STRING_STORAGE)
57*4d6fc14bSjoerg inline
compute_gcc_empty_string_storage()58*4d6fc14bSjoerg const char* compute_gcc_empty_string_storage() noexcept
59*4d6fc14bSjoerg {
60*4d6fc14bSjoerg void* handle = dlopen("/usr/lib/libstdc++.6.dylib", RTLD_NOLOAD);
61*4d6fc14bSjoerg if (handle == nullptr)
62*4d6fc14bSjoerg return nullptr;
63*4d6fc14bSjoerg void* sym = dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE");
64*4d6fc14bSjoerg if (sym == nullptr)
65*4d6fc14bSjoerg return nullptr;
66*4d6fc14bSjoerg return data_from_rep(reinterpret_cast<_Rep_base *>(sym));
67*4d6fc14bSjoerg }
68*4d6fc14bSjoerg
69*4d6fc14bSjoerg inline
70*4d6fc14bSjoerg const char*
get_gcc_empty_string_storage()71*4d6fc14bSjoerg get_gcc_empty_string_storage() noexcept
72*4d6fc14bSjoerg {
73*4d6fc14bSjoerg static const char* p = compute_gcc_empty_string_storage();
74*4d6fc14bSjoerg return p;
75*4d6fc14bSjoerg }
76*4d6fc14bSjoerg #endif
77*4d6fc14bSjoerg
78*4d6fc14bSjoerg }} // namespace __refstring_imp
79*4d6fc14bSjoerg
80*4d6fc14bSjoerg using namespace __refstring_imp;
81*4d6fc14bSjoerg
82*4d6fc14bSjoerg inline
__libcpp_refstring(const char * msg)83*4d6fc14bSjoerg __libcpp_refstring::__libcpp_refstring(const char* msg) {
84*4d6fc14bSjoerg std::size_t len = strlen(msg);
85*4d6fc14bSjoerg _Rep_base* rep = static_cast<_Rep_base *>(::operator new(sizeof(*rep) + len + 1));
86*4d6fc14bSjoerg rep->len = len;
87*4d6fc14bSjoerg rep->cap = len;
88*4d6fc14bSjoerg rep->count = 0;
89*4d6fc14bSjoerg char *data = data_from_rep(rep);
90*4d6fc14bSjoerg std::memcpy(data, msg, len + 1);
91*4d6fc14bSjoerg __imp_ = data;
92*4d6fc14bSjoerg }
93*4d6fc14bSjoerg
94*4d6fc14bSjoerg inline
__libcpp_refstring(const __libcpp_refstring & s)95*4d6fc14bSjoerg __libcpp_refstring::__libcpp_refstring(const __libcpp_refstring &s) noexcept
96*4d6fc14bSjoerg : __imp_(s.__imp_)
97*4d6fc14bSjoerg {
98*4d6fc14bSjoerg if (__uses_refcount())
99*4d6fc14bSjoerg __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1);
100*4d6fc14bSjoerg }
101*4d6fc14bSjoerg
102*4d6fc14bSjoerg inline
103*4d6fc14bSjoerg __libcpp_refstring& __libcpp_refstring::operator=(__libcpp_refstring const& s) noexcept {
104*4d6fc14bSjoerg bool adjust_old_count = __uses_refcount();
105*4d6fc14bSjoerg struct _Rep_base *old_rep = rep_from_data(__imp_);
106*4d6fc14bSjoerg __imp_ = s.__imp_;
107*4d6fc14bSjoerg if (__uses_refcount())
108*4d6fc14bSjoerg __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1);
109*4d6fc14bSjoerg if (adjust_old_count)
110*4d6fc14bSjoerg {
111*4d6fc14bSjoerg if (__libcpp_atomic_add(&old_rep->count, count_t(-1)) < 0)
112*4d6fc14bSjoerg {
113*4d6fc14bSjoerg ::operator delete(old_rep);
114*4d6fc14bSjoerg }
115*4d6fc14bSjoerg }
116*4d6fc14bSjoerg return *this;
117*4d6fc14bSjoerg }
118*4d6fc14bSjoerg
119*4d6fc14bSjoerg inline
~__libcpp_refstring()120*4d6fc14bSjoerg __libcpp_refstring::~__libcpp_refstring() {
121*4d6fc14bSjoerg if (__uses_refcount()) {
122*4d6fc14bSjoerg _Rep_base* rep = rep_from_data(__imp_);
123*4d6fc14bSjoerg if (__libcpp_atomic_add(&rep->count, count_t(-1)) < 0) {
124*4d6fc14bSjoerg ::operator delete(rep);
125*4d6fc14bSjoerg }
126*4d6fc14bSjoerg }
127*4d6fc14bSjoerg }
128*4d6fc14bSjoerg
129*4d6fc14bSjoerg inline
__uses_refcount()130*4d6fc14bSjoerg bool __libcpp_refstring::__uses_refcount() const {
131*4d6fc14bSjoerg #if defined(_LIBCPP_CHECK_FOR_GCC_EMPTY_STRING_STORAGE)
132*4d6fc14bSjoerg return __imp_ != get_gcc_empty_string_storage();
133*4d6fc14bSjoerg #else
134*4d6fc14bSjoerg return true;
135*4d6fc14bSjoerg #endif
136*4d6fc14bSjoerg }
137*4d6fc14bSjoerg
138*4d6fc14bSjoerg _LIBCPP_END_NAMESPACE_STD
139*4d6fc14bSjoerg
140*4d6fc14bSjoerg #endif //_LIBCPP_REFSTRING_H
141