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