blob: a4207d6058ff905931b5e1121a9ecec81ed85b6d [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
Howard Hinnant9844b672013-10-04 22:12:59 +000031#if ! defined(_LIBCPP_MSVC)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032#pragma GCC visibility push(hidden)
Howard Hinnant9844b672013-10-04 22:12:59 +000033#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034
35namespace
36{
37
38class __libcpp_nmstr
39{
40private:
41 const char* str_;
42
43 typedef std::size_t unused_t;
Howard Hinnant4490c4a2012-08-08 16:17:31 +000044 typedef std::ptrdiff_t count_t;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045
46 static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(2*sizeof(unused_t) +
47 sizeof(count_t));
48
Howard Hinnant1e15fd12011-05-26 19:48:01 +000049 count_t& count() const _NOEXCEPT {return (count_t&)(*(str_ - sizeof(count_t)));}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000050public:
51 explicit __libcpp_nmstr(const char* msg);
Howard Hinnante31c4322013-08-22 19:39:03 +000052 __libcpp_nmstr(const __libcpp_nmstr& s) _NOEXCEPT;
53 __libcpp_nmstr& operator=(const __libcpp_nmstr& s) _NOEXCEPT;
54 ~__libcpp_nmstr();
Howard Hinnant1e15fd12011-05-26 19:48:01 +000055 const char* c_str() const _NOEXCEPT {return str_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056};
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;
Howard Hinnant09ca5d42013-06-29 23:53:20 +000066 std::memcpy(const_cast<char*>(c_str()), msg, len + 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000067}
68
69inline
Howard Hinnante31c4322013-08-22 19:39:03 +000070__libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071 : str_(s.str_)
72{
Howard Hinnantadff4892010-05-24 17:49:41 +000073 __sync_add_and_fetch(&count(), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000074}
75
76__libcpp_nmstr&
Howard Hinnante31c4322013-08-22 19:39:03 +000077__libcpp_nmstr::operator=(const __libcpp_nmstr& s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078{
79 const char* p = str_;
80 str_ = s.str_;
Howard Hinnantadff4892010-05-24 17:49:41 +000081 __sync_add_and_fetch(&count(), 1);
Howard Hinnant4490c4a2012-08-08 16:17:31 +000082 if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), count_t(-1)) < 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083 delete [] (p-offset);
84 return *this;
85}
86
87inline
88__libcpp_nmstr::~__libcpp_nmstr()
89{
Howard Hinnant4490c4a2012-08-08 16:17:31 +000090 if (__sync_add_and_fetch(&count(), count_t(-1)) < 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000091 delete [] (str_ - offset);
92}
93
94}
95
Howard Hinnant9844b672013-10-04 22:12:59 +000096#if ! defined(_LIBCPP_MSVC)
Daniel Dunbar04acaca2010-09-04 03:15:51 +000097#pragma GCC visibility pop
Howard Hinnant9844b672013-10-04 22:12:59 +000098#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000099
100namespace std // purposefully not using versioning namespace
101{
102
103logic_error::logic_error(const string& msg)
104{
105 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
106 ::new(&s) __libcpp_nmstr(msg.c_str());
107}
108
109logic_error::logic_error(const char* msg)
110{
111 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
112 ::new(&s) __libcpp_nmstr(msg);
113}
114
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000115logic_error::logic_error(const logic_error& le) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000116{
117 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
118 ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
119}
120
121logic_error&
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000122logic_error::operator=(const logic_error& le) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123{
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
Peter Collingbourned0d308f2013-10-06 22:13:19 +0000130#if !defined(_LIBCPPABI_VERSION) && !defined(LIBSTDCXX)
Howard Hinnante1642e12012-02-17 19:24:42 +0000131
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000132logic_error::~logic_error() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133{
134 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
135 s.~__libcpp_nmstr();
136}
137
138const char*
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000139logic_error::what() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000140{
141 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
142 return s.c_str();
143}
144
Howard Hinnante1642e12012-02-17 19:24:42 +0000145#endif
146
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147runtime_error::runtime_error(const string& msg)
148{
149 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
150 ::new(&s) __libcpp_nmstr(msg.c_str());
151}
152
153runtime_error::runtime_error(const char* msg)
154{
155 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
156 ::new(&s) __libcpp_nmstr(msg);
157}
158
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000159runtime_error::runtime_error(const runtime_error& le) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160{
161 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
162 ::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
163}
164
165runtime_error&
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000166runtime_error::operator=(const runtime_error& le) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167{
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
Peter Collingbourned0d308f2013-10-06 22:13:19 +0000174#if !defined(_LIBCPPABI_VERSION) && !defined(LIBSTDCXX)
Howard Hinnante1642e12012-02-17 19:24:42 +0000175
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000176runtime_error::~runtime_error() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000177{
178 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
179 s.~__libcpp_nmstr();
180}
181
182const char*
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000183runtime_error::what() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184{
185 __libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
186 return s.c_str();
187}
188
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000189domain_error::~domain_error() _NOEXCEPT {}
190invalid_argument::~invalid_argument() _NOEXCEPT {}
191length_error::~length_error() _NOEXCEPT {}
192out_of_range::~out_of_range() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000193
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000194range_error::~range_error() _NOEXCEPT {}
195overflow_error::~overflow_error() _NOEXCEPT {}
196underflow_error::~underflow_error() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197
Howard Hinnante1642e12012-02-17 19:24:42 +0000198#endif
199
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000200} // std