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> |
Dan Albert | 273eb32 | 2015-02-05 02:34:59 +0000 | [diff] [blame] | 16 | #ifdef __APPLE__ |
Joerg Sonnenberger | a47c2e7 | 2014-04-30 19:54:11 +0000 | [diff] [blame] | 17 | #include <dlfcn.h> |
| 18 | #include <mach-o/dyld.h> |
| 19 | #endif |
Weiming Zhao | b613db7 | 2017-09-19 23:18:03 +0000 | [diff] [blame] | 20 | #include "atomic_support.h" |
Joerg Sonnenberger | a47c2e7 | 2014-04-30 19:54:11 +0000 | [diff] [blame] | 21 | |
| 22 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 23 | |
Eric Fiselier | 1e65516 | 2016-10-25 19:33:14 +0000 | [diff] [blame] | 24 | namespace __refstring_imp { namespace { |
| 25 | typedef int count_t; |
| 26 | |
| 27 | struct _Rep_base { |
| 28 | std::size_t len; |
| 29 | std::size_t cap; |
| 30 | count_t count; |
| 31 | }; |
| 32 | |
| 33 | inline _Rep_base* rep_from_data(const char *data_) noexcept { |
| 34 | char *data = const_cast<char *>(data_); |
| 35 | return reinterpret_cast<_Rep_base *>(data - sizeof(_Rep_base)); |
| 36 | } |
| 37 | |
| 38 | inline char * data_from_rep(_Rep_base *rep) noexcept { |
| 39 | char *data = reinterpret_cast<char *>(rep); |
| 40 | return data + sizeof(*rep); |
| 41 | } |
| 42 | |
| 43 | #if defined(__APPLE__) |
| 44 | inline |
| 45 | const char* compute_gcc_empty_string_storage() _NOEXCEPT |
Joerg Sonnenberger | a47c2e7 | 2014-04-30 19:54:11 +0000 | [diff] [blame] | 46 | { |
Eric Fiselier | 1e65516 | 2016-10-25 19:33:14 +0000 | [diff] [blame] | 47 | void* handle = dlopen("/usr/lib/libstdc++.6.dylib", RTLD_NOLOAD); |
| 48 | if (handle == nullptr) |
| 49 | return nullptr; |
| 50 | void* sym = dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE"); |
| 51 | if (sym == nullptr) |
| 52 | return nullptr; |
| 53 | return data_from_rep(reinterpret_cast<_Rep_base *>(sym)); |
| 54 | } |
Joerg Sonnenberger | a47c2e7 | 2014-04-30 19:54:11 +0000 | [diff] [blame] | 55 | |
Eric Fiselier | 1e65516 | 2016-10-25 19:33:14 +0000 | [diff] [blame] | 56 | inline |
| 57 | const char* |
| 58 | get_gcc_empty_string_storage() _NOEXCEPT |
| 59 | { |
| 60 | static const char* p = compute_gcc_empty_string_storage(); |
| 61 | return p; |
| 62 | } |
Joerg Sonnenberger | a47c2e7 | 2014-04-30 19:54:11 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Eric Fiselier | 1e65516 | 2016-10-25 19:33:14 +0000 | [diff] [blame] | 65 | }} // namespace __refstring_imp |
Joerg Sonnenberger | a47c2e7 | 2014-04-30 19:54:11 +0000 | [diff] [blame] | 66 | |
Eric Fiselier | 1e65516 | 2016-10-25 19:33:14 +0000 | [diff] [blame] | 67 | using namespace __refstring_imp; |
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 | __libcpp_refstring::__libcpp_refstring(const char* msg) { |
| 71 | std::size_t len = strlen(msg); |
| 72 | _Rep_base* rep = static_cast<_Rep_base *>(::operator new(sizeof(*rep) + len + 1)); |
| 73 | rep->len = len; |
| 74 | rep->cap = len; |
| 75 | rep->count = 0; |
| 76 | char *data = data_from_rep(rep); |
| 77 | std::memcpy(data, msg, len + 1); |
| 78 | __imp_ = data; |
| 79 | } |
| 80 | |
| 81 | inline |
| 82 | __libcpp_refstring::__libcpp_refstring(const __libcpp_refstring &s) _NOEXCEPT |
| 83 | : __imp_(s.__imp_) |
| 84 | { |
| 85 | if (__uses_refcount()) |
Weiming Zhao | b613db7 | 2017-09-19 23:18:03 +0000 | [diff] [blame] | 86 | __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1); |
Eric Fiselier | 1e65516 | 2016-10-25 19:33:14 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | inline |
| 90 | __libcpp_refstring& __libcpp_refstring::operator=(__libcpp_refstring const& s) _NOEXCEPT { |
| 91 | bool adjust_old_count = __uses_refcount(); |
| 92 | struct _Rep_base *old_rep = rep_from_data(__imp_); |
| 93 | __imp_ = s.__imp_; |
| 94 | if (__uses_refcount()) |
Weiming Zhao | b613db7 | 2017-09-19 23:18:03 +0000 | [diff] [blame] | 95 | __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1); |
Eric Fiselier | 1e65516 | 2016-10-25 19:33:14 +0000 | [diff] [blame] | 96 | if (adjust_old_count) |
Joerg Sonnenberger | a47c2e7 | 2014-04-30 19:54:11 +0000 | [diff] [blame] | 97 | { |
Weiming Zhao | b613db7 | 2017-09-19 23:18:03 +0000 | [diff] [blame] | 98 | if (__libcpp_atomic_add(&old_rep->count, count_t(-1)) < 0) |
Joerg Sonnenberger | a47c2e7 | 2014-04-30 19:54:11 +0000 | [diff] [blame] | 99 | { |
Eric Fiselier | 1e65516 | 2016-10-25 19:33:14 +0000 | [diff] [blame] | 100 | ::operator delete(old_rep); |
Joerg Sonnenberger | a47c2e7 | 2014-04-30 19:54:11 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
Eric Fiselier | 1e65516 | 2016-10-25 19:33:14 +0000 | [diff] [blame] | 103 | return *this; |
| 104 | } |
Joerg Sonnenberger | a47c2e7 | 2014-04-30 19:54:11 +0000 | [diff] [blame] | 105 | |
Eric Fiselier | 1e65516 | 2016-10-25 19:33:14 +0000 | [diff] [blame] | 106 | inline |
| 107 | __libcpp_refstring::~__libcpp_refstring() { |
| 108 | if (__uses_refcount()) { |
| 109 | _Rep_base* rep = rep_from_data(__imp_); |
Weiming Zhao | b613db7 | 2017-09-19 23:18:03 +0000 | [diff] [blame] | 110 | if (__libcpp_atomic_add(&rep->count, count_t(-1)) < 0) { |
Eric Fiselier | 1e65516 | 2016-10-25 19:33:14 +0000 | [diff] [blame] | 111 | ::operator delete(rep); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | inline |
| 117 | bool __libcpp_refstring::__uses_refcount() const { |
| 118 | #ifdef __APPLE__ |
| 119 | return __imp_ != get_gcc_empty_string_storage(); |
| 120 | #else |
| 121 | return true; |
| 122 | #endif |
| 123 | } |
Joerg Sonnenberger | a47c2e7 | 2014-04-30 19:54:11 +0000 | [diff] [blame] | 124 | |
| 125 | _LIBCPP_END_NAMESPACE_STD |
| 126 | |
Eric Fiselier | 46123de | 2017-07-12 01:38:35 +0000 | [diff] [blame] | 127 | #endif //_LIBCPP_REFSTRING_H |