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