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 | |
| 19 | // Use <cxxabi.h> to determine whether we're linking against libc++abi. |
| 20 | #if __has_include(<cxxabi.h>) |
Howard Hinnant | e1642e1 | 2012-02-17 19:24:42 +0000 | [diff] [blame] | 21 | #include <cxxabi.h> |
Richard Smith | c756f5b | 2012-04-19 01:36:12 +0000 | [diff] [blame] | 22 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | |
| 24 | // Note: optimize for size |
| 25 | |
| 26 | #pragma GCC visibility push(hidden) |
| 27 | |
| 28 | namespace |
| 29 | { |
| 30 | |
| 31 | class __libcpp_nmstr |
| 32 | { |
| 33 | private: |
| 34 | const char* str_; |
| 35 | |
| 36 | typedef std::size_t unused_t; |
| 37 | typedef std::int32_t count_t; |
| 38 | |
| 39 | static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(2*sizeof(unused_t) + |
| 40 | sizeof(count_t)); |
| 41 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 42 | count_t& count() const _NOEXCEPT {return (count_t&)(*(str_ - sizeof(count_t)));} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | public: |
| 44 | explicit __libcpp_nmstr(const char* msg); |
| 45 | __libcpp_nmstr(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW; |
| 46 | __libcpp_nmstr& operator=(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW; |
| 47 | ~__libcpp_nmstr() _LIBCPP_CANTTHROW; |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 48 | const char* c_str() const _NOEXCEPT {return str_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | __libcpp_nmstr::__libcpp_nmstr(const char* msg) |
| 52 | { |
| 53 | std::size_t len = strlen(msg); |
| 54 | str_ = new char[len + 1 + offset]; |
| 55 | unused_t* c = (unused_t*)str_; |
| 56 | c[0] = c[1] = len; |
| 57 | str_ += offset; |
| 58 | count() = 0; |
| 59 | std::strcpy(const_cast<char*>(c_str()), msg); |
| 60 | } |
| 61 | |
| 62 | inline |
| 63 | __libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s) |
| 64 | : str_(s.str_) |
| 65 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 66 | __sync_add_and_fetch(&count(), 1); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | __libcpp_nmstr& |
| 70 | __libcpp_nmstr::operator=(const __libcpp_nmstr& s) |
| 71 | { |
| 72 | const char* p = str_; |
| 73 | str_ = s.str_; |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 74 | __sync_add_and_fetch(&count(), 1); |
| 75 | if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), -1) < 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 76 | delete [] (p-offset); |
| 77 | return *this; |
| 78 | } |
| 79 | |
| 80 | inline |
| 81 | __libcpp_nmstr::~__libcpp_nmstr() |
| 82 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 83 | if (__sync_add_and_fetch(&count(), -1) < 0) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 84 | delete [] (str_ - offset); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
Daniel Dunbar | 04acaca | 2010-09-04 03:15:51 +0000 | [diff] [blame] | 89 | #pragma GCC visibility pop |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 90 | |
| 91 | namespace std // purposefully not using versioning namespace |
| 92 | { |
| 93 | |
| 94 | logic_error::logic_error(const string& msg) |
| 95 | { |
| 96 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 97 | ::new(&s) __libcpp_nmstr(msg.c_str()); |
| 98 | } |
| 99 | |
| 100 | logic_error::logic_error(const char* msg) |
| 101 | { |
| 102 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 103 | ::new(&s) __libcpp_nmstr(msg); |
| 104 | } |
| 105 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 106 | logic_error::logic_error(const logic_error& le) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 107 | { |
| 108 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 109 | ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_); |
| 110 | } |
| 111 | |
| 112 | logic_error& |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 113 | logic_error::operator=(const logic_error& le) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 114 | { |
| 115 | __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_; |
| 116 | const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_; |
| 117 | s1 = s2; |
| 118 | return *this; |
| 119 | } |
| 120 | |
Howard Hinnant | e1642e1 | 2012-02-17 19:24:42 +0000 | [diff] [blame] | 121 | #ifndef _LIBCPPABI_VERSION |
| 122 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 123 | logic_error::~logic_error() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 124 | { |
| 125 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 126 | s.~__libcpp_nmstr(); |
| 127 | } |
| 128 | |
| 129 | const char* |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 130 | logic_error::what() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 131 | { |
| 132 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 133 | return s.c_str(); |
| 134 | } |
| 135 | |
Howard Hinnant | e1642e1 | 2012-02-17 19:24:42 +0000 | [diff] [blame] | 136 | #endif |
| 137 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 138 | runtime_error::runtime_error(const string& msg) |
| 139 | { |
| 140 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 141 | ::new(&s) __libcpp_nmstr(msg.c_str()); |
| 142 | } |
| 143 | |
| 144 | runtime_error::runtime_error(const char* msg) |
| 145 | { |
| 146 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 147 | ::new(&s) __libcpp_nmstr(msg); |
| 148 | } |
| 149 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 150 | runtime_error::runtime_error(const runtime_error& le) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 151 | { |
| 152 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 153 | ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_); |
| 154 | } |
| 155 | |
| 156 | runtime_error& |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 157 | runtime_error::operator=(const runtime_error& le) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 158 | { |
| 159 | __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_; |
| 160 | const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_; |
| 161 | s1 = s2; |
| 162 | return *this; |
| 163 | } |
| 164 | |
Howard Hinnant | e1642e1 | 2012-02-17 19:24:42 +0000 | [diff] [blame] | 165 | #ifndef _LIBCPPABI_VERSION |
| 166 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 167 | runtime_error::~runtime_error() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 168 | { |
| 169 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 170 | s.~__libcpp_nmstr(); |
| 171 | } |
| 172 | |
| 173 | const char* |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 174 | runtime_error::what() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 175 | { |
| 176 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 177 | return s.c_str(); |
| 178 | } |
| 179 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 180 | domain_error::~domain_error() _NOEXCEPT {} |
| 181 | invalid_argument::~invalid_argument() _NOEXCEPT {} |
| 182 | length_error::~length_error() _NOEXCEPT {} |
| 183 | out_of_range::~out_of_range() _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 184 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 185 | range_error::~range_error() _NOEXCEPT {} |
| 186 | overflow_error::~overflow_error() _NOEXCEPT {} |
| 187 | underflow_error::~underflow_error() _NOEXCEPT {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 188 | |
Howard Hinnant | e1642e1 | 2012-02-17 19:24:42 +0000 | [diff] [blame] | 189 | #endif |
| 190 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 191 | } // std |