1
//===------------------------ stdexcept.cpp -------------------------------===//
3
// The LLVM Compiler Infrastructure
5
// This file is dual licensed under the MIT and the University of Illinois Open
6
// Source Licenses. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
19
#include <mach-o/dyld.h>
22
// Note: optimize for size
24
#pragma GCC visibility push(hidden)
43
static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(sizeof(_Rep_base));
45
count_t& count() const _NOEXCEPT {return ((_Rep_base*)(str_ - offset))->count;}
50
compute_gcc_empty_string_storage() _LIBCPP_CANTTHROW
52
void* handle = dlopen("/usr/lib/libstdc++.6.dylib", RTLD_NOLOAD);
55
return (const char*)dlsym(handle, "_ZNSs4_Rep20_S_empty_rep_storageE") + offset;
60
get_gcc_empty_string_storage() _LIBCPP_CANTTHROW
62
static const void* p = compute_gcc_empty_string_storage();
68
explicit __libcpp_nmstr(const char* msg);
69
__libcpp_nmstr(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW;
70
__libcpp_nmstr& operator=(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW;
71
~__libcpp_nmstr() _LIBCPP_CANTTHROW;
72
const char* c_str() const _NOEXCEPT {return str_;}
75
__libcpp_nmstr::__libcpp_nmstr(const char* msg)
77
std::size_t len = strlen(msg);
78
str_ = static_cast<const char*>(::operator new(len + 1 + offset));
79
_Rep_base* c = (_Rep_base*)str_;
80
c->len = c->cap = len;
83
std::strcpy(const_cast<char*>(c_str()), msg);
87
__libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s)
91
if (str_ != get_gcc_empty_string_storage())
93
__sync_add_and_fetch(&count(), 1);
97
__libcpp_nmstr::operator=(const __libcpp_nmstr& s)
102
if (str_ != get_gcc_empty_string_storage())
104
__sync_add_and_fetch(&count(), 1);
106
if (p != get_gcc_empty_string_storage())
108
if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), count_t(-1)) < 0)
110
::operator delete(const_cast<char*>(p-offset));
116
__libcpp_nmstr::~__libcpp_nmstr()
119
if (str_ != get_gcc_empty_string_storage())
121
if (__sync_add_and_fetch(&count(), count_t(-1)) < 0)
123
::operator delete(const_cast<char*>(str_ - offset));
129
#pragma GCC visibility pop
131
namespace std // purposefully not using versioning namespace
134
logic_error::~logic_error() _NOEXCEPT
136
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
141
logic_error::what() const _NOEXCEPT
143
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
147
runtime_error::~runtime_error() _NOEXCEPT
149
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
154
runtime_error::what() const _NOEXCEPT
156
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
160
domain_error::~domain_error() _NOEXCEPT {}
161
invalid_argument::~invalid_argument() _NOEXCEPT {}
162
length_error::~length_error() _NOEXCEPT {}
163
out_of_range::~out_of_range() _NOEXCEPT {}
165
range_error::~range_error() _NOEXCEPT {}
166
overflow_error::~overflow_error() _NOEXCEPT {}
167
underflow_error::~underflow_error() _NOEXCEPT {}