-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
0002-Revert-Make-libc-abi-use-the-implementation-of-__num.patch
179 lines (168 loc) · 4.33 KB
/
0002-Revert-Make-libc-abi-use-the-implementation-of-__num.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
From c3520f9c833c6ae5d00530a54028ff4a5befc463 Mon Sep 17 00:00:00 2001
From: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
Date: Mon, 29 Dec 2014 22:05:22 -0800
Subject: [PATCH 2/2] Revert "Make libc++abi use the implementation of __numstr
from libc++. No functionality change, just removal of duplicated code."
This reverts commit f7a83f7f3b5788e3ead1c7f2063994699df55085.
---
src/stdexcept.cpp | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 127 insertions(+), 6 deletions(-)
diff --git a/src/stdexcept.cpp b/src/stdexcept.cpp
index bd6789e..246eea4 100644
--- a/src/stdexcept.cpp
+++ b/src/stdexcept.cpp
@@ -7,7 +7,6 @@
//
//===----------------------------------------------------------------------===//
-#include "__refstring"
#include "stdexcept"
#include "new"
#include <cstdlib>
@@ -15,25 +14,147 @@
#include <cstdint>
#include <cstddef>
-static_assert(sizeof(std::__libcpp_refstring) == sizeof(const char *), "");
+#if __APPLE__
+#include <dlfcn.h>
+#include <mach-o/dyld.h>
+#endif
+
+// Note: optimize for size
+
+#pragma GCC visibility push(hidden)
+
+namespace
+{
+
+class __libcpp_nmstr
+{
+private:
+ const char* str_;
+
+ typedef int count_t;
+
+ struct _Rep_base
+ {
+ std::size_t len;
+ std::size_t cap;
+ count_t count;
+ };
+
+ static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(sizeof(_Rep_base));
+
+ count_t& count() const _NOEXCEPT {return ((_Rep_base*)(str_ - offset))->count;}
+
+#if __APPLE__
+ static
+ const void*
+ compute_gcc_empty_string_storage() _NOEXCEPT
+ {
+ void* handle = dlopen("/usr/lib/libstdc++.6.dylib", RTLD_NOLOAD);
+ if (handle == 0)
+ return 0;
+ return (const char*)dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE") + offset;
+ }
+
+ static
+ const void*
+ get_gcc_empty_string_storage() _NOEXCEPT
+ {
+ static const void* p = compute_gcc_empty_string_storage();
+ return p;
+ }
+#endif
+
+public:
+ explicit __libcpp_nmstr(const char* msg);
+ __libcpp_nmstr(const __libcpp_nmstr& s) _NOEXCEPT;
+ __libcpp_nmstr& operator=(const __libcpp_nmstr& s) _NOEXCEPT;
+ ~__libcpp_nmstr();
+ const char* c_str() const _NOEXCEPT {return str_;}
+};
+
+__libcpp_nmstr::__libcpp_nmstr(const char* msg)
+{
+ std::size_t len = strlen(msg);
+ str_ = static_cast<const char*>(::operator new(len + 1 + offset));
+ _Rep_base* c = (_Rep_base*)str_;
+ c->len = c->cap = len;
+ str_ += offset;
+ count() = 0;
+ std::memcpy(const_cast<char*>(c_str()), msg, len + 1);
+}
+
+inline
+__libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s) _NOEXCEPT
+ : str_(s.str_)
+{
+#if __APPLE__
+ if (str_ != get_gcc_empty_string_storage())
+#endif
+ __sync_add_and_fetch(&count(), 1);
+}
+
+__libcpp_nmstr&
+__libcpp_nmstr::operator=(const __libcpp_nmstr& s) _NOEXCEPT
+{
+ const char* p = str_;
+ str_ = s.str_;
+#if __APPLE__
+ if (str_ != get_gcc_empty_string_storage())
+#endif
+ __sync_add_and_fetch(&count(), 1);
+#if __APPLE__
+ if (p != get_gcc_empty_string_storage())
+#endif
+ if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), count_t(-1)) < 0)
+ {
+ ::operator delete(const_cast<char*>(p-offset));
+ }
+ return *this;
+}
+
+inline
+__libcpp_nmstr::~__libcpp_nmstr()
+{
+#if __APPLE__
+ if (str_ != get_gcc_empty_string_storage())
+#endif
+ if (__sync_add_and_fetch(&count(), count_t(-1)) < 0)
+ {
+ ::operator delete(const_cast<char*>(str_ - offset));
+ }
+}
+
+}
+
+#pragma GCC visibility pop
namespace std // purposefully not using versioning namespace
{
-logic_error::~logic_error() _NOEXCEPT {}
+logic_error::~logic_error() _NOEXCEPT
+{
+ __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
+ s.~__libcpp_nmstr();
+}
const char*
logic_error::what() const _NOEXCEPT
{
- return __imp_.c_str();
+ __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
+ return s.c_str();
}
-runtime_error::~runtime_error() _NOEXCEPT {}
+runtime_error::~runtime_error() _NOEXCEPT
+{
+ __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
+ s.~__libcpp_nmstr();
+}
const char*
runtime_error::what() const _NOEXCEPT
{
- return __imp_.c_str();
+ __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
+ return s.c_str();
}
domain_error::~domain_error() _NOEXCEPT {}
--
2.6.2