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"
20 #define __has_include(inc) 0
25 #elif defined(LIBCXXRT) || __has_include(<cxxabi.h>)
29 // Note: optimize for size
31 #pragma GCC visibility push(hidden)
41 typedef std::size_t unused_t;
42 typedef std::ptrdiff_t count_t;
44 static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(2*sizeof(unused_t) +
47 count_t& count() const _NOEXCEPT {return (count_t&)(*(str_ - sizeof(count_t)));}
49 explicit __libcpp_nmstr(const char* msg);
50 __libcpp_nmstr(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW;
51 __libcpp_nmstr& operator=(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW;
52 ~__libcpp_nmstr() _LIBCPP_CANTTHROW;
53 const char* c_str() const _NOEXCEPT {return str_;}
56 __libcpp_nmstr::__libcpp_nmstr(const char* msg)
58 std::size_t len = strlen(msg);
59 str_ = new char[len + 1 + offset];
60 unused_t* c = (unused_t*)str_;
64 std::memcpy(const_cast<char*>(c_str()), msg, len + 1);
68 __libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s)
71 __sync_add_and_fetch(&count(), 1);
75 __libcpp_nmstr::operator=(const __libcpp_nmstr& s)
79 __sync_add_and_fetch(&count(), 1);
80 if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), count_t(-1)) < 0)
86 __libcpp_nmstr::~__libcpp_nmstr()
88 if (__sync_add_and_fetch(&count(), count_t(-1)) < 0)
89 delete [] (str_ - offset);
94 #pragma GCC visibility pop
96 namespace std // purposefully not using versioning namespace
99 logic_error::logic_error(const string& msg)
101 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
102 ::new(&s) __libcpp_nmstr(msg.c_str());
105 logic_error::logic_error(const char* msg)
107 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
108 ::new(&s) __libcpp_nmstr(msg);
111 logic_error::logic_error(const logic_error& le) _NOEXCEPT
113 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
114 ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
118 logic_error::operator=(const logic_error& le) _NOEXCEPT
120 __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_;
121 const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_;
126 #ifndef _LIBCPPABI_VERSION
128 logic_error::~logic_error() _NOEXCEPT
130 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
135 logic_error::what() const _NOEXCEPT
137 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
143 runtime_error::runtime_error(const string& msg)
145 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
146 ::new(&s) __libcpp_nmstr(msg.c_str());
149 runtime_error::runtime_error(const char* msg)
151 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
152 ::new(&s) __libcpp_nmstr(msg);
155 runtime_error::runtime_error(const runtime_error& le) _NOEXCEPT
157 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
158 ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
162 runtime_error::operator=(const runtime_error& le) _NOEXCEPT
164 __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_;
165 const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_;
170 #ifndef _LIBCPPABI_VERSION
172 runtime_error::~runtime_error() _NOEXCEPT
174 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
179 runtime_error::what() const _NOEXCEPT
181 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
185 domain_error::~domain_error() _NOEXCEPT {}
186 invalid_argument::~invalid_argument() _NOEXCEPT {}
187 length_error::~length_error() _NOEXCEPT {}
188 out_of_range::~out_of_range() _NOEXCEPT {}
190 range_error::~range_error() _NOEXCEPT {}
191 overflow_error::~overflow_error() _NOEXCEPT {}
192 underflow_error::~underflow_error() _NOEXCEPT {}