blob: 0c4e8323abf3a128c7c98a853aefb51b5122a3d5 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===------------------------ stdexcept.cpp -------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
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 Smithc756f5b2012-04-19 01:36:12 +000018
Howard Hinnant460b4ca2012-09-03 18:13:11 +000019#ifndef __has_include
20#define __has_include(inc) 0
21#endif
22
Marshall Clowdece7fe2013-03-18 17:45:34 +000023#ifdef __APPLE__
Howard Hinnant460b4ca2012-09-03 18:13:11 +000024#include <cxxabi.h>
25#elif defined(LIBCXXRT) || __has_include(<cxxabi.h>)
Howard Hinnante1642e12012-02-17 19:24:42 +000026#include <cxxabi.h>
Richard Smithc756f5b2012-04-19 01:36:12 +000027#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028
29// Note: optimize for size
30
31#pragma GCC visibility push(hidden)
32
33namespace
34{
35
36class __libcpp_nmstr
37{
38private:
39 const char* str_;
40
41 typedef std::size_t unused_t;
Howard Hinnant4490c4a2012-08-08 16:17:31 +000042 typedef std::ptrdiff_t count_t;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000043
44 static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(2*sizeof(unused_t) +
45 sizeof(count_t));
46
Howard Hinnant1e15fd12011-05-26 19:48:01 +000047 count_t& count() const _NOEXCEPT {return (count_t&)(*(str_ - sizeof(count_t)));}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048public:
49 explicit __libcpp_nmstr(const char* msg);
50 __libcpp_nmstr(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW;
51 __libcpp_nmstr& operator=(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW;
52 ~__libcpp_nmstr() _LIBCPP_CANTTHROW;
Howard Hinnant1e15fd12011-05-26 19:48:01 +000053 const char* c_str() const _NOEXCEPT {return str_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000054};
55
56__libcpp_nmstr::__libcpp_nmstr(const char* msg)
57{
58 std::size_t len = strlen(msg);
59 str_ = new char[len + 1 + offset];
60 unused_t* c = (unused_t*)str_;
61 c[0] = c[1] = len;
62 str_ += offset;
63 count() = 0;
64 std::strcpy(const_cast<char*>(c_str()), msg);
65}
66
67inline
68__libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s)
69 : str_(s.str_)
70{
Howard Hinnantadff4892010-05-24 17:49:41 +000071 __sync_add_and_fetch(&count(), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072}
73
74__libcpp_nmstr&
75__libcpp_nmstr::operator=(const __libcpp_nmstr& s)
76{
77 const char* p = str_;
78 str_ = s.str_;
Howard Hinnantadff4892010-05-24 17:49:41 +000079 __sync_add_and_fetch(&count(), 1);
Howard Hinnant4490c4a2012-08-08 16:17:31 +000080 if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), count_t(-1)) < 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000081 delete [] (p-offset);
82 return *this;
83}
84
85inline
86__libcpp_nmstr::~__libcpp_nmstr()
87{
Howard Hinnant4490c4a2012-08-08 16:17:31 +000088 if (__sync_add_and_fetch(&count(), count_t(-1)) < 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000089 delete [] (str_ - offset);
90}
91
92}
93
Daniel Dunbar04acaca2010-09-04 03:15:51 +000094#pragma GCC visibility pop
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095
96namespace std // purposefully not using versioning namespace
97{
98
99logic_error::logic_error(const string& msg)
100{
101 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
102 ::new(&s) __libcpp_nmstr(msg.c_str());
103}
104
105logic_error::logic_error(const char* msg)
106{
107 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
108 ::new(&s) __libcpp_nmstr(msg);
109}
110
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000111logic_error::logic_error(const logic_error& le) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000112{
113 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
114 ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
115}
116
117logic_error&
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000118logic_error::operator=(const logic_error& le) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119{
120 __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_;
121 const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_;
122 s1 = s2;
123 return *this;
124}
125
Howard Hinnante1642e12012-02-17 19:24:42 +0000126#ifndef _LIBCPPABI_VERSION
127
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000128logic_error::~logic_error() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129{
130 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
131 s.~__libcpp_nmstr();
132}
133
134const char*
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000135logic_error::what() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136{
137 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
138 return s.c_str();
139}
140
Howard Hinnante1642e12012-02-17 19:24:42 +0000141#endif
142
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000143runtime_error::runtime_error(const string& msg)
144{
145 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
146 ::new(&s) __libcpp_nmstr(msg.c_str());
147}
148
149runtime_error::runtime_error(const char* msg)
150{
151 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
152 ::new(&s) __libcpp_nmstr(msg);
153}
154
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000155runtime_error::runtime_error(const runtime_error& le) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000156{
157 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
158 ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
159}
160
161runtime_error&
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000162runtime_error::operator=(const runtime_error& le) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163{
164 __libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_;
165 const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_;
166 s1 = s2;
167 return *this;
168}
169
Howard Hinnante1642e12012-02-17 19:24:42 +0000170#ifndef _LIBCPPABI_VERSION
171
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000172runtime_error::~runtime_error() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000173{
174 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
175 s.~__libcpp_nmstr();
176}
177
178const char*
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000179runtime_error::what() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180{
181 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
182 return s.c_str();
183}
184
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000185domain_error::~domain_error() _NOEXCEPT {}
186invalid_argument::~invalid_argument() _NOEXCEPT {}
187length_error::~length_error() _NOEXCEPT {}
188out_of_range::~out_of_range() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000190range_error::~range_error() _NOEXCEPT {}
191overflow_error::~overflow_error() _NOEXCEPT {}
192underflow_error::~underflow_error() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000193
Howard Hinnante1642e12012-02-17 19:24:42 +0000194#endif
195
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000196} // std