Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===------------------------ stdexcept.cpp -------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "stdexcept" |
| 11 | #include "new" |
| 12 | #include "string" |
| 13 | #include <cstdlib> |
| 14 | #include <cstring> |
| 15 | #include <cstdint> |
| 16 | #include <cstddef> |
| 17 | #include "system_error" |
Richard Smith | c756f5b | 2012-04-19 01:36:12 +0000 | [diff] [blame] | 18 | |
Howard Hinnant | 460b4ca | 2012-09-03 18:13:11 +0000 | [diff] [blame] | 19 | #ifndef __has_include |
| 20 | #define __has_include(inc) 0 |
| 21 | #endif |
| 22 | |
Marshall Clow | dece7fe | 2013-03-18 17:45:34 +0000 | [diff] [blame] | 23 | #ifdef __APPLE__ |
Howard Hinnant | 460b4ca | 2012-09-03 18:13:11 +0000 | [diff] [blame] | 24 | #include <cxxabi.h> |
| 25 | #elif defined(LIBCXXRT) || __has_include(<cxxabi.h>) |
Howard Hinnant | e1642e1 | 2012-02-17 19:24:42 +0000 | [diff] [blame] | 26 | #include <cxxabi.h> |
Richard Smith | c756f5b | 2012-04-19 01:36:12 +0000 | [diff] [blame] | 27 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | |
| 29 | // Note: optimize for size |
| 30 | |
| 31 | #pragma GCC visibility push(hidden) |
| 32 | |
| 33 | namespace |
| 34 | { |
| 35 | |
| 36 | class __libcpp_nmstr |
| 37 | { |
| 38 | private: |
| 39 | const char* str_; |
| 40 | |
| 41 | typedef std::size_t unused_t; |
Howard Hinnant | 4490c4a | 2012-08-08 16:17:31 +0000 | [diff] [blame] | 42 | typedef std::ptrdiff_t count_t; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | |
| 44 | static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(2*sizeof(unused_t) + |
| 45 | sizeof(count_t)); |
| 46 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 47 | count_t& count() const _NOEXCEPT {return (count_t&)(*(str_ - sizeof(count_t)));} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 48 | public: |
| 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; |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 53 | const char* c_str() const _NOEXCEPT {return str_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | __libcpp_nmstr::__libcpp_nmstr(const char* msg) |
| 57 | { |
| 58 | std::size_t len = strlen(msg); |
| 59 | str_ = new char[len + 1 + offset]; |
| 60 | unused_t* c = (unused_t*)str_; |
| 61 | c[0] = c[1] = len; |
| 62 | str_ += offset; |
| 63 | count() = 0; |
| 64 | std::strcpy(const_cast<char*>(c_str()), msg); |
| 65 | } |
| 66 | |
| 67 | inline |
| 68 | __libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s) |
| 69 | : str_(s.str_) |
| 70 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 71 | __sync_add_and_fetch(&count(), 1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | __libcpp_nmstr& |
| 75 | __libcpp_nmstr::operator=(const __libcpp_nmstr& s) |
| 76 | { |
| 77 | const char* p = str_; |
| 78 | str_ = s.str_; |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 79 | __sync_add_and_fetch(&count(), 1); |
Howard Hinnant | 4490c4a | 2012-08-08 16:17:31 +0000 | [diff] [blame] | 80 | if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), count_t(-1)) < 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 81 | delete [] (p-offset); |
| 82 | return *this; |
| 83 | } |
| 84 | |
| 85 | inline |
| 86 | __libcpp_nmstr::~__libcpp_nmstr() |
| 87 | { |
Howard Hinnant | 4490c4a | 2012-08-08 16:17:31 +0000 | [diff] [blame] | 88 | if (__sync_add_and_fetch(&count(), count_t(-1)) < 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 89 | delete [] (str_ - offset); |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | |
Daniel Dunbar | 04acaca | 2010-09-04 03:15:51 +0000 | [diff] [blame] | 94 | #pragma GCC visibility pop |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 95 | |
| 96 | namespace std // purposefully not using versioning namespace |
| 97 | { |
| 98 | |
| 99 | logic_error::logic_error(const string& msg) |
| 100 | { |
| 101 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 102 | ::new(&s) __libcpp_nmstr(msg.c_str()); |
| 103 | } |
| 104 | |
| 105 | logic_error::logic_error(const char* msg) |
| 106 | { |
| 107 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 108 | ::new(&s) __libcpp_nmstr(msg); |
| 109 | } |
| 110 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 111 | logic_error::logic_error(const logic_error& le) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 112 | { |
| 113 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 114 | ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_); |
| 115 | } |
| 116 | |
| 117 | logic_error& |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 118 | logic_error::operator=(const logic_error& le) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 119 | { |
| 120 | __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_; |
| 121 | const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_; |
| 122 | s1 = s2; |
| 123 | return *this; |
| 124 | } |
| 125 | |
Howard Hinnant | e1642e1 | 2012-02-17 19:24:42 +0000 | [diff] [blame] | 126 | #ifndef _LIBCPPABI_VERSION |
| 127 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 128 | logic_error::~logic_error() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 129 | { |
| 130 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 131 | s.~__libcpp_nmstr(); |
| 132 | } |
| 133 | |
| 134 | const char* |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 135 | logic_error::what() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 136 | { |
| 137 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 138 | return s.c_str(); |
| 139 | } |
| 140 | |
Howard Hinnant | e1642e1 | 2012-02-17 19:24:42 +0000 | [diff] [blame] | 141 | #endif |
| 142 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 143 | runtime_error::runtime_error(const string& msg) |
| 144 | { |
| 145 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 146 | ::new(&s) __libcpp_nmstr(msg.c_str()); |
| 147 | } |
| 148 | |
| 149 | runtime_error::runtime_error(const char* msg) |
| 150 | { |
| 151 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 152 | ::new(&s) __libcpp_nmstr(msg); |
| 153 | } |
| 154 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 155 | runtime_error::runtime_error(const runtime_error& le) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 156 | { |
| 157 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 158 | ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_); |
| 159 | } |
| 160 | |
| 161 | runtime_error& |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 162 | runtime_error::operator=(const runtime_error& le) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 163 | { |
| 164 | __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_; |
| 165 | const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_; |
| 166 | s1 = s2; |
| 167 | return *this; |
| 168 | } |
| 169 | |
Howard Hinnant | e1642e1 | 2012-02-17 19:24:42 +0000 | [diff] [blame] | 170 | #ifndef _LIBCPPABI_VERSION |
| 171 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 172 | runtime_error::~runtime_error() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 173 | { |
| 174 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 175 | s.~__libcpp_nmstr(); |
| 176 | } |
| 177 | |
| 178 | const char* |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 179 | runtime_error::what() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 180 | { |
| 181 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 182 | return s.c_str(); |
| 183 | } |
| 184 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 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 {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 189 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 190 | range_error::~range_error() _NOEXCEPT {} |
| 191 | overflow_error::~overflow_error() _NOEXCEPT {} |
| 192 | underflow_error::~underflow_error() _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 193 | |
Howard Hinnant | e1642e1 | 2012-02-17 19:24:42 +0000 | [diff] [blame] | 194 | #endif |
| 195 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 196 | } // std |