OLD | NEW |
(Empty) | |
| 1 //===------------------------ stdexcept.cpp -------------------------------===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 // Source Licenses. See LICENSE.TXT for details. |
| 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" |
| 18 |
| 19 #ifndef __has_include |
| 20 #define __has_include(inc) 0 |
| 21 #endif |
| 22 |
| 23 #ifdef __APPLE__ |
| 24 #include <cxxabi.h> |
| 25 #elif defined(LIBCXXRT) || __has_include(<cxxabi.h>) |
| 26 #include <cxxabi.h> |
| 27 #endif |
| 28 |
| 29 // Note: optimize for size |
| 30 |
| 31 #if ! defined(_LIBCPP_MSVC) |
| 32 #pragma GCC visibility push(hidden) |
| 33 #endif |
| 34 |
| 35 namespace |
| 36 { |
| 37 |
| 38 class __libcpp_nmstr |
| 39 { |
| 40 private: |
| 41 const char* str_; |
| 42 |
| 43 typedef std::size_t unused_t; |
| 44 typedef std::ptrdiff_t count_t; |
| 45 |
| 46 static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(2*sizeof(un
used_t) + |
| 47 sizeof(co
unt_t)); |
| 48 |
| 49 count_t& count() const _NOEXCEPT {return (count_t&)(*(str_ - sizeof(count_t)
));} |
| 50 public: |
| 51 explicit __libcpp_nmstr(const char* msg); |
| 52 __libcpp_nmstr(const __libcpp_nmstr& s) _NOEXCEPT; |
| 53 __libcpp_nmstr& operator=(const __libcpp_nmstr& s) _NOEXCEPT; |
| 54 ~__libcpp_nmstr(); |
| 55 const char* c_str() const _NOEXCEPT {return str_;} |
| 56 }; |
| 57 |
| 58 __libcpp_nmstr::__libcpp_nmstr(const char* msg) |
| 59 { |
| 60 std::size_t len = strlen(msg); |
| 61 str_ = new char[len + 1 + offset]; |
| 62 unused_t* c = (unused_t*)str_; |
| 63 c[0] = c[1] = len; |
| 64 str_ += offset; |
| 65 count() = 0; |
| 66 std::memcpy(const_cast<char*>(c_str()), msg, len + 1); |
| 67 } |
| 68 |
| 69 inline |
| 70 __libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s) _NOEXCEPT |
| 71 : str_(s.str_) |
| 72 { |
| 73 __sync_add_and_fetch(&count(), 1); |
| 74 } |
| 75 |
| 76 __libcpp_nmstr& |
| 77 __libcpp_nmstr::operator=(const __libcpp_nmstr& s) _NOEXCEPT |
| 78 { |
| 79 const char* p = str_; |
| 80 str_ = s.str_; |
| 81 __sync_add_and_fetch(&count(), 1); |
| 82 if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), count_t(-1)) < 0) |
| 83 delete [] (p-offset); |
| 84 return *this; |
| 85 } |
| 86 |
| 87 inline |
| 88 __libcpp_nmstr::~__libcpp_nmstr() |
| 89 { |
| 90 if (__sync_add_and_fetch(&count(), count_t(-1)) < 0) |
| 91 delete [] (str_ - offset); |
| 92 } |
| 93 |
| 94 } |
| 95 |
| 96 #if ! defined(_LIBCPP_MSVC) |
| 97 #pragma GCC visibility pop |
| 98 #endif |
| 99 |
| 100 namespace std // purposefully not using versioning namespace |
| 101 { |
| 102 |
| 103 logic_error::logic_error(const string& msg) |
| 104 { |
| 105 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 106 ::new(&s) __libcpp_nmstr(msg.c_str()); |
| 107 } |
| 108 |
| 109 logic_error::logic_error(const char* msg) |
| 110 { |
| 111 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 112 ::new(&s) __libcpp_nmstr(msg); |
| 113 } |
| 114 |
| 115 logic_error::logic_error(const logic_error& le) _NOEXCEPT |
| 116 { |
| 117 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 118 ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_); |
| 119 } |
| 120 |
| 121 logic_error& |
| 122 logic_error::operator=(const logic_error& le) _NOEXCEPT |
| 123 { |
| 124 __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_; |
| 125 const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_; |
| 126 s1 = s2; |
| 127 return *this; |
| 128 } |
| 129 |
| 130 #if !defined(_LIBCPPABI_VERSION) && !defined(LIBSTDCXX) |
| 131 |
| 132 logic_error::~logic_error() _NOEXCEPT |
| 133 { |
| 134 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 135 s.~__libcpp_nmstr(); |
| 136 } |
| 137 |
| 138 const char* |
| 139 logic_error::what() const _NOEXCEPT |
| 140 { |
| 141 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 142 return s.c_str(); |
| 143 } |
| 144 |
| 145 #endif |
| 146 |
| 147 runtime_error::runtime_error(const string& msg) |
| 148 { |
| 149 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 150 ::new(&s) __libcpp_nmstr(msg.c_str()); |
| 151 } |
| 152 |
| 153 runtime_error::runtime_error(const char* msg) |
| 154 { |
| 155 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 156 ::new(&s) __libcpp_nmstr(msg); |
| 157 } |
| 158 |
| 159 runtime_error::runtime_error(const runtime_error& le) _NOEXCEPT |
| 160 { |
| 161 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 162 ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_); |
| 163 } |
| 164 |
| 165 runtime_error& |
| 166 runtime_error::operator=(const runtime_error& le) _NOEXCEPT |
| 167 { |
| 168 __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_; |
| 169 const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_; |
| 170 s1 = s2; |
| 171 return *this; |
| 172 } |
| 173 |
| 174 #if !defined(_LIBCPPABI_VERSION) && !defined(LIBSTDCXX) |
| 175 |
| 176 runtime_error::~runtime_error() _NOEXCEPT |
| 177 { |
| 178 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 179 s.~__libcpp_nmstr(); |
| 180 } |
| 181 |
| 182 const char* |
| 183 runtime_error::what() const _NOEXCEPT |
| 184 { |
| 185 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 186 return s.c_str(); |
| 187 } |
| 188 |
| 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 {} |
| 193 |
| 194 range_error::~range_error() _NOEXCEPT {} |
| 195 overflow_error::~overflow_error() _NOEXCEPT {} |
| 196 underflow_error::~underflow_error() _NOEXCEPT {} |
| 197 |
| 198 #endif |
| 199 |
| 200 } // std |
OLD | NEW |