/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/include/refstring.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===----------------------------------------------------------------------===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #ifndef _LIBCPP_REFSTRING_H |
10 | | #define _LIBCPP_REFSTRING_H |
11 | | |
12 | | #include <__config> |
13 | | #include <stdexcept> |
14 | | #include <cstddef> |
15 | | #include <cstring> |
16 | | #include "atomic_support.h" |
17 | | |
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 | | |
35 | | _LIBCPP_BEGIN_NAMESPACE_STD |
36 | | |
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 | 0 | inline _Rep_base* rep_from_data(const char *data_) noexcept { |
47 | 0 | char *data = const_cast<char *>(data_); |
48 | 0 | return reinterpret_cast<_Rep_base *>(data - sizeof(_Rep_base)); |
49 | 0 | } Unexecuted instantiation: stdlib_stdexcept.cpp:std::__1::__refstring_imp::(anonymous namespace)::rep_from_data(char const*) Unexecuted instantiation: stdexcept.cpp:std::__1::__refstring_imp::(anonymous namespace)::rep_from_data(char const*) |
50 | | |
51 | 0 | inline char * data_from_rep(_Rep_base *rep) noexcept { |
52 | 0 | char *data = reinterpret_cast<char *>(rep); |
53 | 0 | return data + sizeof(*rep); |
54 | 0 | } Unexecuted instantiation: stdlib_stdexcept.cpp:std::__1::__refstring_imp::(anonymous namespace)::data_from_rep(std::__1::__refstring_imp::(anonymous namespace)::_Rep_base*) Unexecuted instantiation: stdexcept.cpp:std::__1::__refstring_imp::(anonymous namespace)::data_from_rep(std::__1::__refstring_imp::(anonymous namespace)::_Rep_base*) |
55 | | |
56 | | #if defined(_LIBCPP_CHECK_FOR_GCC_EMPTY_STRING_STORAGE) |
57 | | inline |
58 | | const char* compute_gcc_empty_string_storage() noexcept |
59 | 0 | { |
60 | 0 | void* handle = dlopen("/usr/lib/libstdc++.6.dylib", RTLD_NOLOAD); |
61 | 0 | if (handle == nullptr) |
62 | 0 | return nullptr; |
63 | 0 | void* sym = dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE"); |
64 | 0 | if (sym == nullptr) |
65 | 0 | return nullptr; |
66 | 0 | return data_from_rep(reinterpret_cast<_Rep_base *>(sym)); |
67 | 0 | } Unexecuted instantiation: stdlib_stdexcept.cpp:std::__1::__refstring_imp::(anonymous namespace)::compute_gcc_empty_string_storage() Unexecuted instantiation: stdexcept.cpp:std::__1::__refstring_imp::(anonymous namespace)::compute_gcc_empty_string_storage() |
68 | | |
69 | | inline |
70 | | const char* |
71 | | get_gcc_empty_string_storage() noexcept |
72 | 0 | { |
73 | 0 | static const char* p = compute_gcc_empty_string_storage(); |
74 | 0 | return p; |
75 | 0 | } Unexecuted instantiation: stdlib_stdexcept.cpp:std::__1::__refstring_imp::(anonymous namespace)::get_gcc_empty_string_storage() Unexecuted instantiation: stdexcept.cpp:std::__1::__refstring_imp::(anonymous namespace)::get_gcc_empty_string_storage() |
76 | | #endif |
77 | | |
78 | | }} // namespace __refstring_imp |
79 | | |
80 | | using namespace __refstring_imp; |
81 | | |
82 | | inline |
83 | 0 | __libcpp_refstring::__libcpp_refstring(const char* msg) { |
84 | 0 | std::size_t len = strlen(msg); |
85 | 0 | _Rep_base* rep = static_cast<_Rep_base *>(::operator new(sizeof(*rep) + len + 1)); |
86 | 0 | rep->len = len; |
87 | 0 | rep->cap = len; |
88 | 0 | rep->count = 0; |
89 | 0 | char *data = data_from_rep(rep); |
90 | 0 | std::memcpy(data, msg, len + 1); |
91 | 0 | __imp_ = data; |
92 | 0 | } |
93 | | |
94 | | inline |
95 | | __libcpp_refstring::__libcpp_refstring(const __libcpp_refstring &s) noexcept |
96 | | : __imp_(s.__imp_) |
97 | 0 | { |
98 | 0 | if (__uses_refcount()) |
99 | 0 | __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1); |
100 | 0 | } |
101 | | |
102 | | inline |
103 | 0 | __libcpp_refstring& __libcpp_refstring::operator=(__libcpp_refstring const& s) noexcept { |
104 | 0 | bool adjust_old_count = __uses_refcount(); |
105 | 0 | struct _Rep_base *old_rep = rep_from_data(__imp_); |
106 | 0 | __imp_ = s.__imp_; |
107 | 0 | if (__uses_refcount()) |
108 | 0 | __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1); |
109 | 0 | if (adjust_old_count) |
110 | 0 | { |
111 | 0 | if (__libcpp_atomic_add(&old_rep->count, count_t(-1)) < 0) |
112 | 0 | { |
113 | 0 | ::operator delete(old_rep); |
114 | 0 | } |
115 | 0 | } |
116 | 0 | return *this; |
117 | 0 | } |
118 | | |
119 | | inline |
120 | 0 | __libcpp_refstring::~__libcpp_refstring() { |
121 | 0 | if (__uses_refcount()) { |
122 | 0 | _Rep_base* rep = rep_from_data(__imp_); |
123 | 0 | if (__libcpp_atomic_add(&rep->count, count_t(-1)) < 0) { |
124 | 0 | ::operator delete(rep); |
125 | 0 | } |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | | inline |
130 | 0 | bool __libcpp_refstring::__uses_refcount() const { |
131 | 0 | #if defined(_LIBCPP_CHECK_FOR_GCC_EMPTY_STRING_STORAGE) |
132 | 0 | return __imp_ != get_gcc_empty_string_storage(); |
133 | | #else |
134 | | return true; |
135 | | #endif |
136 | 0 | } |
137 | | |
138 | | _LIBCPP_END_NAMESPACE_STD |
139 | | |
140 | | #endif //_LIBCPP_REFSTRING_H |