Howard Hinnant | 3e7d155 | 2012-02-17 19:23:47 +0000 | [diff] [blame] | 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 <cstdlib> |
| 13 | #include <cstring> |
| 14 | #include <cstdint> |
| 15 | #include <cstddef> |
| 16 | |
| 17 | // Note: optimize for size |
| 18 | |
| 19 | #pragma GCC visibility push(hidden) |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | |
| 24 | class __libcpp_nmstr |
| 25 | { |
| 26 | private: |
| 27 | const char* str_; |
| 28 | |
| 29 | typedef std::size_t unused_t; |
Howard Hinnant | 2c2b55f | 2012-08-08 16:15:16 +0000 | [diff] [blame] | 30 | typedef std::ptrdiff_t count_t; |
Howard Hinnant | 3e7d155 | 2012-02-17 19:23:47 +0000 | [diff] [blame] | 31 | |
| 32 | static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(2*sizeof(unused_t) + |
| 33 | sizeof(count_t)); |
| 34 | |
| 35 | count_t& count() const _NOEXCEPT {return (count_t&)(*(str_ - sizeof(count_t)));} |
| 36 | public: |
| 37 | explicit __libcpp_nmstr(const char* msg); |
| 38 | __libcpp_nmstr(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW; |
| 39 | __libcpp_nmstr& operator=(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW; |
| 40 | ~__libcpp_nmstr() _LIBCPP_CANTTHROW; |
| 41 | const char* c_str() const _NOEXCEPT {return str_;} |
| 42 | }; |
| 43 | |
| 44 | __libcpp_nmstr::__libcpp_nmstr(const char* msg) |
| 45 | { |
| 46 | std::size_t len = strlen(msg); |
| 47 | str_ = new char[len + 1 + offset]; |
| 48 | unused_t* c = (unused_t*)str_; |
| 49 | c[0] = c[1] = len; |
| 50 | str_ += offset; |
| 51 | count() = 0; |
| 52 | std::strcpy(const_cast<char*>(c_str()), msg); |
| 53 | } |
| 54 | |
| 55 | inline |
| 56 | __libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s) |
| 57 | : str_(s.str_) |
| 58 | { |
| 59 | __sync_add_and_fetch(&count(), 1); |
| 60 | } |
| 61 | |
| 62 | __libcpp_nmstr& |
| 63 | __libcpp_nmstr::operator=(const __libcpp_nmstr& s) |
| 64 | { |
| 65 | const char* p = str_; |
| 66 | str_ = s.str_; |
| 67 | __sync_add_and_fetch(&count(), 1); |
Howard Hinnant | 2c2b55f | 2012-08-08 16:15:16 +0000 | [diff] [blame] | 68 | if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), count_t(-1)) < 0) |
Howard Hinnant | 3e7d155 | 2012-02-17 19:23:47 +0000 | [diff] [blame] | 69 | delete [] (p-offset); |
| 70 | return *this; |
| 71 | } |
| 72 | |
| 73 | inline |
| 74 | __libcpp_nmstr::~__libcpp_nmstr() |
| 75 | { |
Howard Hinnant | 2c2b55f | 2012-08-08 16:15:16 +0000 | [diff] [blame] | 76 | if (__sync_add_and_fetch(&count(), count_t(-1)) < 0) |
Howard Hinnant | 3e7d155 | 2012-02-17 19:23:47 +0000 | [diff] [blame] | 77 | delete [] (str_ - offset); |
| 78 | } |
| 79 | |
| 80 | } |
| 81 | |
| 82 | #pragma GCC visibility pop |
| 83 | |
| 84 | namespace std // purposefully not using versioning namespace |
| 85 | { |
| 86 | |
| 87 | logic_error::~logic_error() _NOEXCEPT |
| 88 | { |
| 89 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 90 | s.~__libcpp_nmstr(); |
| 91 | } |
| 92 | |
| 93 | const char* |
| 94 | logic_error::what() const _NOEXCEPT |
| 95 | { |
| 96 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 97 | return s.c_str(); |
| 98 | } |
| 99 | |
| 100 | runtime_error::~runtime_error() _NOEXCEPT |
| 101 | { |
| 102 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 103 | s.~__libcpp_nmstr(); |
| 104 | } |
| 105 | |
| 106 | const char* |
| 107 | runtime_error::what() const _NOEXCEPT |
| 108 | { |
| 109 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 110 | return s.c_str(); |
| 111 | } |
| 112 | |
| 113 | domain_error::~domain_error() _NOEXCEPT {} |
| 114 | invalid_argument::~invalid_argument() _NOEXCEPT {} |
| 115 | length_error::~length_error() _NOEXCEPT {} |
| 116 | out_of_range::~out_of_range() _NOEXCEPT {} |
| 117 | |
| 118 | range_error::~range_error() _NOEXCEPT {} |
| 119 | overflow_error::~overflow_error() _NOEXCEPT {} |
| 120 | underflow_error::~underflow_error() _NOEXCEPT {} |
| 121 | |
| 122 | } // std |