1 //===------------------------ stdexcept.cpp -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
17 #include "system_error"
19 // Note: optimize for size
21 #pragma GCC visibility push(hidden)
31 typedef std::size_t unused_t;
32 typedef std::int32_t count_t;
34 static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(2*sizeof(unused_t) +
37 count_t& count() const _NOEXCEPT {return (count_t&)(*(str_ - sizeof(count_t)));}
39 explicit __libcpp_nmstr(const char* msg);
40 __libcpp_nmstr(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW;
41 __libcpp_nmstr& operator=(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW;
42 ~__libcpp_nmstr() _LIBCPP_CANTTHROW;
43 const char* c_str() const _NOEXCEPT {return str_;}
46 __libcpp_nmstr::__libcpp_nmstr(const char* msg)
48 std::size_t len = strlen(msg);
49 str_ = new char[len + 1 + offset];
50 unused_t* c = (unused_t*)str_;
54 std::strcpy(const_cast<char*>(c_str()), msg);
58 __libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s)
61 __sync_add_and_fetch(&count(), 1);
65 __libcpp_nmstr::operator=(const __libcpp_nmstr& s)
69 __sync_add_and_fetch(&count(), 1);
70 if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), -1) < 0)
76 __libcpp_nmstr::~__libcpp_nmstr()
78 if (__sync_add_and_fetch(&count(), -1) < 0)
79 delete [] (str_ - offset);
84 #pragma GCC visibility pop
86 namespace std // purposefully not using versioning namespace
89 logic_error::logic_error(const string& msg)
91 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
92 ::new(&s) __libcpp_nmstr(msg.c_str());
95 logic_error::logic_error(const char* msg)
97 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
98 ::new(&s) __libcpp_nmstr(msg);
101 logic_error::logic_error(const logic_error& le) _NOEXCEPT
103 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
104 ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
108 logic_error::operator=(const logic_error& le) _NOEXCEPT
110 __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_;
111 const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_;
116 logic_error::~logic_error() _NOEXCEPT
118 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
123 logic_error::what() const _NOEXCEPT
125 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
129 runtime_error::runtime_error(const string& msg)
131 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
132 ::new(&s) __libcpp_nmstr(msg.c_str());
135 runtime_error::runtime_error(const char* msg)
137 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
138 ::new(&s) __libcpp_nmstr(msg);
141 runtime_error::runtime_error(const runtime_error& le) _NOEXCEPT
143 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
144 ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
148 runtime_error::operator=(const runtime_error& le) _NOEXCEPT
150 __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_;
151 const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_;
156 runtime_error::~runtime_error() _NOEXCEPT
158 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
163 runtime_error::what() const _NOEXCEPT
165 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
169 domain_error::~domain_error() _NOEXCEPT {}
170 invalid_argument::~invalid_argument() _NOEXCEPT {}
171 length_error::~length_error() _NOEXCEPT {}
172 out_of_range::~out_of_range() _NOEXCEPT {}
174 range_error::~range_error() _NOEXCEPT {}
175 overflow_error::~overflow_error() _NOEXCEPT {}
176 underflow_error::~underflow_error() _NOEXCEPT {}