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 | |
Howard Hinnant | e115af2 | 2012-09-18 21:34:12 +0000 | [diff] [blame^] | 17 | #if __APPLE__ |
| 18 | #include <dlfcn.h> |
| 19 | #include <mach-o/dyld.h> |
| 20 | #endif |
| 21 | |
Howard Hinnant | 3e7d155 | 2012-02-17 19:23:47 +0000 | [diff] [blame] | 22 | // Note: optimize for size |
| 23 | |
| 24 | #pragma GCC visibility push(hidden) |
| 25 | |
| 26 | namespace |
| 27 | { |
| 28 | |
| 29 | class __libcpp_nmstr |
| 30 | { |
| 31 | private: |
| 32 | const char* str_; |
| 33 | |
| 34 | typedef std::size_t unused_t; |
Howard Hinnant | 2c2b55f | 2012-08-08 16:15:16 +0000 | [diff] [blame] | 35 | typedef std::ptrdiff_t count_t; |
Howard Hinnant | 3e7d155 | 2012-02-17 19:23:47 +0000 | [diff] [blame] | 36 | |
| 37 | static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(2*sizeof(unused_t) + |
| 38 | sizeof(count_t)); |
| 39 | |
| 40 | count_t& count() const _NOEXCEPT {return (count_t&)(*(str_ - sizeof(count_t)));} |
Howard Hinnant | e115af2 | 2012-09-18 21:34:12 +0000 | [diff] [blame^] | 41 | |
| 42 | #if __APPLE__ |
| 43 | static |
| 44 | const void* |
| 45 | compute_gcc_empty_string_storage() _LIBCPP_CANTTHROW |
| 46 | { |
| 47 | void* handle = dlopen("libstdc++.dylib", RTLD_LAZY); |
| 48 | if (handle == 0) |
| 49 | return 0; |
| 50 | return (const char*)dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE") + offset; |
| 51 | } |
| 52 | |
| 53 | static |
| 54 | const void* |
| 55 | get_gcc_empty_string_storage() _LIBCPP_CANTTHROW |
| 56 | { |
| 57 | static const void* p = compute_gcc_empty_string_storage(); |
| 58 | return p; |
| 59 | } |
| 60 | #endif |
| 61 | |
Howard Hinnant | 3e7d155 | 2012-02-17 19:23:47 +0000 | [diff] [blame] | 62 | public: |
| 63 | explicit __libcpp_nmstr(const char* msg); |
| 64 | __libcpp_nmstr(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW; |
| 65 | __libcpp_nmstr& operator=(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW; |
| 66 | ~__libcpp_nmstr() _LIBCPP_CANTTHROW; |
| 67 | const char* c_str() const _NOEXCEPT {return str_;} |
| 68 | }; |
| 69 | |
| 70 | __libcpp_nmstr::__libcpp_nmstr(const char* msg) |
| 71 | { |
| 72 | std::size_t len = strlen(msg); |
| 73 | str_ = new char[len + 1 + offset]; |
| 74 | unused_t* c = (unused_t*)str_; |
| 75 | c[0] = c[1] = len; |
| 76 | str_ += offset; |
| 77 | count() = 0; |
| 78 | std::strcpy(const_cast<char*>(c_str()), msg); |
| 79 | } |
| 80 | |
| 81 | inline |
| 82 | __libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s) |
| 83 | : str_(s.str_) |
| 84 | { |
Howard Hinnant | e115af2 | 2012-09-18 21:34:12 +0000 | [diff] [blame^] | 85 | #if __APPLE__ |
| 86 | if (str_ != get_gcc_empty_string_storage()) |
| 87 | #endif |
| 88 | __sync_add_and_fetch(&count(), 1); |
Howard Hinnant | 3e7d155 | 2012-02-17 19:23:47 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | __libcpp_nmstr& |
| 92 | __libcpp_nmstr::operator=(const __libcpp_nmstr& s) |
| 93 | { |
| 94 | const char* p = str_; |
| 95 | str_ = s.str_; |
Howard Hinnant | e115af2 | 2012-09-18 21:34:12 +0000 | [diff] [blame^] | 96 | #if __APPLE__ |
| 97 | if (str_ != get_gcc_empty_string_storage()) |
| 98 | #endif |
| 99 | __sync_add_and_fetch(&count(), 1); |
| 100 | #if __APPLE__ |
| 101 | if (p != get_gcc_empty_string_storage()) |
| 102 | #endif |
| 103 | if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), count_t(-1)) < 0) |
| 104 | delete [] (p-offset); |
Howard Hinnant | 3e7d155 | 2012-02-17 19:23:47 +0000 | [diff] [blame] | 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | inline |
| 109 | __libcpp_nmstr::~__libcpp_nmstr() |
| 110 | { |
Howard Hinnant | e115af2 | 2012-09-18 21:34:12 +0000 | [diff] [blame^] | 111 | #if __APPLE__ |
| 112 | if (str_ != get_gcc_empty_string_storage()) |
| 113 | #endif |
| 114 | if (__sync_add_and_fetch(&count(), count_t(-1)) < 0) |
| 115 | delete [] (str_ - offset); |
Howard Hinnant | 3e7d155 | 2012-02-17 19:23:47 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | } |
| 119 | |
| 120 | #pragma GCC visibility pop |
| 121 | |
| 122 | namespace std // purposefully not using versioning namespace |
| 123 | { |
| 124 | |
| 125 | logic_error::~logic_error() _NOEXCEPT |
| 126 | { |
| 127 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 128 | s.~__libcpp_nmstr(); |
| 129 | } |
| 130 | |
| 131 | const char* |
| 132 | logic_error::what() const _NOEXCEPT |
| 133 | { |
| 134 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 135 | return s.c_str(); |
| 136 | } |
| 137 | |
| 138 | runtime_error::~runtime_error() _NOEXCEPT |
| 139 | { |
| 140 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 141 | s.~__libcpp_nmstr(); |
| 142 | } |
| 143 | |
| 144 | const char* |
| 145 | runtime_error::what() const _NOEXCEPT |
| 146 | { |
| 147 | __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_; |
| 148 | return s.c_str(); |
| 149 | } |
| 150 | |
| 151 | domain_error::~domain_error() _NOEXCEPT {} |
| 152 | invalid_argument::~invalid_argument() _NOEXCEPT {} |
| 153 | length_error::~length_error() _NOEXCEPT {} |
| 154 | out_of_range::~out_of_range() _NOEXCEPT {} |
| 155 | |
| 156 | range_error::~range_error() _NOEXCEPT {} |
| 157 | overflow_error::~overflow_error() _NOEXCEPT {} |
| 158 | underflow_error::~underflow_error() _NOEXCEPT {} |
| 159 | |
| 160 | } // std |