2010-05-11 21:42:16 +02:00
|
|
|
//===------------------------ stdexcept.cpp -------------------------------===//
|
|
|
|
//
|
2010-05-11 23:36:01 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
2010-05-11 21:42:16 +02:00
|
|
|
//
|
2010-11-16 23:09:02 +01:00
|
|
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
|
|
|
// Source Licenses. See LICENSE.TXT for details.
|
2010-05-11 21:42:16 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "stdexcept"
|
|
|
|
#include "new"
|
|
|
|
#include "string"
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstddef>
|
|
|
|
#include "system_error"
|
2012-04-19 03:36:12 +02:00
|
|
|
|
2012-09-03 20:13:11 +02:00
|
|
|
#ifndef __has_include
|
|
|
|
#define __has_include(inc) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __APPLE__
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#elif defined(LIBCXXRT) || __has_include(<cxxabi.h>)
|
2012-02-17 20:24:42 +01:00
|
|
|
#include <cxxabi.h>
|
2012-04-19 03:36:12 +02:00
|
|
|
#endif
|
2010-05-11 21:42:16 +02:00
|
|
|
|
|
|
|
// Note: optimize for size
|
|
|
|
|
|
|
|
#pragma GCC visibility push(hidden)
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
class __libcpp_nmstr
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
const char* str_;
|
|
|
|
|
|
|
|
typedef std::size_t unused_t;
|
2012-08-08 18:17:31 +02:00
|
|
|
typedef std::ptrdiff_t count_t;
|
2010-05-11 21:42:16 +02:00
|
|
|
|
|
|
|
static const std::ptrdiff_t offset = static_cast<std::ptrdiff_t>(2*sizeof(unused_t) +
|
|
|
|
sizeof(count_t));
|
|
|
|
|
2011-05-26 21:48:01 +02:00
|
|
|
count_t& count() const _NOEXCEPT {return (count_t&)(*(str_ - sizeof(count_t)));}
|
2010-05-11 21:42:16 +02:00
|
|
|
public:
|
|
|
|
explicit __libcpp_nmstr(const char* msg);
|
|
|
|
__libcpp_nmstr(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW;
|
|
|
|
__libcpp_nmstr& operator=(const __libcpp_nmstr& s) _LIBCPP_CANTTHROW;
|
|
|
|
~__libcpp_nmstr() _LIBCPP_CANTTHROW;
|
2011-05-26 21:48:01 +02:00
|
|
|
const char* c_str() const _NOEXCEPT {return str_;}
|
2010-05-11 21:42:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
__libcpp_nmstr::__libcpp_nmstr(const char* msg)
|
|
|
|
{
|
|
|
|
std::size_t len = strlen(msg);
|
|
|
|
str_ = new char[len + 1 + offset];
|
|
|
|
unused_t* c = (unused_t*)str_;
|
|
|
|
c[0] = c[1] = len;
|
|
|
|
str_ += offset;
|
|
|
|
count() = 0;
|
|
|
|
std::strcpy(const_cast<char*>(c_str()), msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
__libcpp_nmstr::__libcpp_nmstr(const __libcpp_nmstr& s)
|
|
|
|
: str_(s.str_)
|
|
|
|
{
|
2010-05-24 19:49:41 +02:00
|
|
|
__sync_add_and_fetch(&count(), 1);
|
2010-05-11 21:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
__libcpp_nmstr&
|
|
|
|
__libcpp_nmstr::operator=(const __libcpp_nmstr& s)
|
|
|
|
{
|
|
|
|
const char* p = str_;
|
|
|
|
str_ = s.str_;
|
2010-05-24 19:49:41 +02:00
|
|
|
__sync_add_and_fetch(&count(), 1);
|
2012-08-08 18:17:31 +02:00
|
|
|
if (__sync_add_and_fetch((count_t*)(p-sizeof(count_t)), count_t(-1)) < 0)
|
2010-05-11 21:42:16 +02:00
|
|
|
delete [] (p-offset);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
__libcpp_nmstr::~__libcpp_nmstr()
|
|
|
|
{
|
2012-08-08 18:17:31 +02:00
|
|
|
if (__sync_add_and_fetch(&count(), count_t(-1)) < 0)
|
2010-05-11 21:42:16 +02:00
|
|
|
delete [] (str_ - offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-09-04 05:15:51 +02:00
|
|
|
#pragma GCC visibility pop
|
2010-05-11 21:42:16 +02:00
|
|
|
|
|
|
|
namespace std // purposefully not using versioning namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
logic_error::logic_error(const string& msg)
|
|
|
|
{
|
|
|
|
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
|
|
|
|
::new(&s) __libcpp_nmstr(msg.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
logic_error::logic_error(const char* msg)
|
|
|
|
{
|
|
|
|
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
|
|
|
|
::new(&s) __libcpp_nmstr(msg);
|
|
|
|
}
|
|
|
|
|
2011-05-26 21:48:01 +02:00
|
|
|
logic_error::logic_error(const logic_error& le) _NOEXCEPT
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
|
|
|
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
|
|
|
|
::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
|
|
|
|
}
|
|
|
|
|
|
|
|
logic_error&
|
2011-05-26 21:48:01 +02:00
|
|
|
logic_error::operator=(const logic_error& le) _NOEXCEPT
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
|
|
|
__libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_;
|
|
|
|
const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_;
|
|
|
|
s1 = s2;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-02-17 20:24:42 +01:00
|
|
|
#ifndef _LIBCPPABI_VERSION
|
|
|
|
|
2011-05-26 21:48:01 +02:00
|
|
|
logic_error::~logic_error() _NOEXCEPT
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
|
|
|
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
|
|
|
|
s.~__libcpp_nmstr();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
2011-05-26 21:48:01 +02:00
|
|
|
logic_error::what() const _NOEXCEPT
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
|
|
|
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
|
|
|
|
return s.c_str();
|
|
|
|
}
|
|
|
|
|
2012-02-17 20:24:42 +01:00
|
|
|
#endif
|
|
|
|
|
2010-05-11 21:42:16 +02:00
|
|
|
runtime_error::runtime_error(const string& msg)
|
|
|
|
{
|
|
|
|
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
|
|
|
|
::new(&s) __libcpp_nmstr(msg.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime_error::runtime_error(const char* msg)
|
|
|
|
{
|
|
|
|
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
|
|
|
|
::new(&s) __libcpp_nmstr(msg);
|
|
|
|
}
|
|
|
|
|
2011-05-26 21:48:01 +02:00
|
|
|
runtime_error::runtime_error(const runtime_error& le) _NOEXCEPT
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
|
|
|
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
|
|
|
|
::new(&s) __libcpp_nmstr((const __libcpp_nmstr&)le.__imp_);
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime_error&
|
2011-05-26 21:48:01 +02:00
|
|
|
runtime_error::operator=(const runtime_error& le) _NOEXCEPT
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
|
|
|
__libcpp_nmstr& s1 = (__libcpp_nmstr&)__imp_;
|
|
|
|
const __libcpp_nmstr& s2 = (const __libcpp_nmstr&)le.__imp_;
|
|
|
|
s1 = s2;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-02-17 20:24:42 +01:00
|
|
|
#ifndef _LIBCPPABI_VERSION
|
|
|
|
|
2011-05-26 21:48:01 +02:00
|
|
|
runtime_error::~runtime_error() _NOEXCEPT
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
|
|
|
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
|
|
|
|
s.~__libcpp_nmstr();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
2011-05-26 21:48:01 +02:00
|
|
|
runtime_error::what() const _NOEXCEPT
|
2010-05-11 21:42:16 +02:00
|
|
|
{
|
|
|
|
__libcpp_nmstr& s = (__libcpp_nmstr&)__imp_;
|
|
|
|
return s.c_str();
|
|
|
|
}
|
|
|
|
|
2011-05-26 21:48:01 +02:00
|
|
|
domain_error::~domain_error() _NOEXCEPT {}
|
|
|
|
invalid_argument::~invalid_argument() _NOEXCEPT {}
|
|
|
|
length_error::~length_error() _NOEXCEPT {}
|
|
|
|
out_of_range::~out_of_range() _NOEXCEPT {}
|
2010-05-11 21:42:16 +02:00
|
|
|
|
2011-05-26 21:48:01 +02:00
|
|
|
range_error::~range_error() _NOEXCEPT {}
|
|
|
|
overflow_error::~overflow_error() _NOEXCEPT {}
|
|
|
|
underflow_error::~underflow_error() _NOEXCEPT {}
|
2010-05-11 21:42:16 +02:00
|
|
|
|
2012-02-17 20:24:42 +01:00
|
|
|
#endif
|
|
|
|
|
2010-05-11 21:42:16 +02:00
|
|
|
} // std
|