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 #if ! defined(_LIBCPP_MSVC)
32 #pragma GCC visibility push(hidden)
43 typedef std::size_t unused_t;
44 typedef std::ptrdiff_t count_t;
46 static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(2*sizeof(unused_t) +
49 count_t& count() const _NOEXCEPT {return (count_t&)(*(str_ - sizeof(count_t)));}
51 explicit __libcpp_nmstr(const char* msg);
52 __libcpp_nmstr(const __libcpp_nmstr& s) _NOEXCEPT;
53 __libcpp_nmstr& operator=(const __libcpp_nmstr& s) _NOEXCEPT;
55 const char* c_str() const _NOEXCEPT {return str_;}
58 __libcpp_nmstr::__libcpp_nmstr(const char* msg)
60 std::size_t len = strlen(msg);
61 str_ = new char[len + 1 + offset];
62 unused_t* c = (unused_t*)str_;
66 std::memcpy(const_cast<char*>(c_str()), msg, len + 1);
70 __libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s) _NOEXCEPT
73 __sync_add_and_fetch(&count(), 1);
77 __libcpp_nmstr::operator=(const __libcpp_nmstr& s) _NOEXCEPT
81 __sync_add_and_fetch(&count(), 1);
82 if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), count_t(-1)) < 0)
88 __libcpp_nmstr::~__libcpp_nmstr()
90 if (__sync_add_and_fetch(&count(), count_t(-1)) < 0)
91 delete [] (str_ - offset);
96 #if ! defined(_LIBCPP_MSVC)
97 #pragma GCC visibility pop
100 namespace std // purposefully not using versioning namespace
103 logic_error::logic_error(const string& msg)
105 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
106 ::new(&s) __libcpp_nmstr(msg.c_str());
109 logic_error::logic_error(const char* msg)
111 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
112 ::new(&s) __libcpp_nmstr(msg);
115 logic_error::logic_error(const logic_error& le) _NOEXCEPT
117 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
118 ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
122 logic_error::operator=(const logic_error& le) _NOEXCEPT
124 __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_;
125 const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_;
130 #if !defined(_LIBCPPABI_VERSION) && !defined(LIBSTDCXX)
132 logic_error::~logic_error() _NOEXCEPT
134 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
139 logic_error::what() const _NOEXCEPT
141 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
147 runtime_error::runtime_error(const string& msg)
149 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
150 ::new(&s) __libcpp_nmstr(msg.c_str());
153 runtime_error::runtime_error(const char* msg)
155 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
156 ::new(&s) __libcpp_nmstr(msg);
159 runtime_error::runtime_error(const runtime_error& le) _NOEXCEPT
161 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
162 ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
166 runtime_error::operator=(const runtime_error& le) _NOEXCEPT
168 __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_;
169 const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_;
174 #if !defined(_LIBCPPABI_VERSION) && !defined(LIBSTDCXX)
176 runtime_error::~runtime_error() _NOEXCEPT
178 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
183 runtime_error::what() const _NOEXCEPT
185 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
189 domain_error::~domain_error() _NOEXCEPT {}
190 invalid_argument::~invalid_argument() _NOEXCEPT {}
191 length_error::~length_error() _NOEXCEPT {}
192 out_of_range::~out_of_range() _NOEXCEPT {}
194 range_error::~range_error() _NOEXCEPT {}
195 overflow_error::~overflow_error() _NOEXCEPT {}
196 underflow_error::~underflow_error() _NOEXCEPT {}