Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@ -0,0 +1,204 @@
# Get sources
set(LIBCXXABI_SOURCES
# C++ABI files
cxa_aux_runtime.cpp
cxa_default_handlers.cpp
cxa_demangle.cpp
cxa_exception_storage.cpp
cxa_guard.cpp
cxa_handlers.cpp
cxa_unexpected.cpp
cxa_vector.cpp
cxa_virtual.cpp
# C++ STL files
stdlib_exception.cpp
stdlib_stdexcept.cpp
stdlib_typeinfo.cpp
# Internal files
abort_message.cpp
fallback_malloc.cpp
private_typeinfo.cpp
)
if (LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS)
list(APPEND LIBCXXABI_SOURCES stdlib_new_delete.cpp)
endif()
if (LIBCXXABI_ENABLE_EXCEPTIONS)
list(APPEND LIBCXXABI_SOURCES cxa_exception.cpp)
list(APPEND LIBCXXABI_SOURCES cxa_personality.cpp)
else()
list(APPEND LIBCXXABI_SOURCES cxa_noexception.cpp)
endif()
if (LIBCXXABI_ENABLE_THREADS AND UNIX AND NOT (APPLE OR CYGWIN))
list(APPEND LIBCXXABI_SOURCES cxa_thread_atexit.cpp)
endif()
set(LIBCXXABI_HEADERS ../include/cxxabi.h)
# Add all the headers to the project for IDEs.
if (MSVC_IDE OR XCODE)
# Force them all into the headers dir on MSVC, otherwise they end up at
# project scope because they don't have extensions.
if (MSVC_IDE)
source_group("Header Files" FILES ${LIBCXXABI_HEADERS})
endif()
endif()
include_directories("${LIBCXXABI_LIBCXX_INCLUDES}")
if (LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL)
add_definitions(-DHAVE___CXA_THREAD_ATEXIT_IMPL)
endif()
if (LIBCXXABI_ENABLE_THREADS)
add_library_flags_if(LIBCXXABI_HAS_PTHREAD_LIB pthread)
endif()
add_library_flags_if(LIBCXXABI_HAS_C_LIB c)
if (LIBCXXABI_USE_LLVM_UNWINDER)
# Prefer using the in-tree version of libunwind, either shared or static. If
# none are found fall back to using -lunwind.
# FIXME: Is it correct to prefer the static version of libunwind?
if (NOT LIBCXXABI_ENABLE_STATIC_UNWINDER AND (TARGET unwind_shared OR HAVE_LIBUNWIND))
list(APPEND LIBCXXABI_LIBRARIES unwind_shared)
elseif (LIBCXXABI_ENABLE_STATIC_UNWINDER AND (TARGET unwind_static OR HAVE_LIBUNWIND))
list(APPEND LIBCXXABI_LIBRARIES unwind_static)
else()
list(APPEND LIBCXXABI_LIBRARIES unwind)
endif()
else()
add_library_flags_if(LIBCXXABI_HAS_GCC_S_LIB gcc_s)
endif()
if (MINGW)
# MINGW_LIBRARIES is defined in config-ix.cmake
list(APPEND LIBCXXABI_LIBRARIES ${MINGW_LIBRARIES})
endif()
# Setup flags.
add_link_flags_if_supported(-nodefaultlibs)
set(LIBCXXABI_SHARED_LINK_FLAGS)
if ( APPLE )
if ( CMAKE_OSX_DEPLOYMENT_TARGET STREQUAL "10.6" )
list(APPEND LIBCXXABI_COMPILE_FLAGS "-U__STRICT_ANSI__")
list(APPEND LIBCXXABI_SHARED_LINK_FLAGS
"-compatibility_version 1"
"-current_version 1"
"-install_name /usr/lib/libc++abi.1.dylib")
list(APPEND LIBCXXABI_LINK_FLAGS
"/usr/lib/libSystem.B.dylib")
else()
list(APPEND LIBCXXABI_SHARED_LINK_FLAGS
"-compatibility_version 1"
"-install_name /usr/lib/libc++abi.1.dylib")
endif()
endif()
split_list(LIBCXXABI_COMPILE_FLAGS)
split_list(LIBCXXABI_LINK_FLAGS)
split_list(LIBCXXABI_SHARED_LINK_FLAGS)
# FIXME: libc++abi.so will not link when modules are enabled because it depends
# on symbols defined in libc++.so which has not yet been built.
if (LLVM_ENABLE_MODULES)
string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
endif()
# Add a object library that contains the compiled source files.
add_library(cxxabi_objects OBJECT ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
set_target_properties(cxxabi_objects
PROPERTIES
CXX_EXTENSIONS
OFF
CXX_STANDARD
11
CXX_STANDARD_REQUIRED
ON
COMPILE_FLAGS
"${LIBCXXABI_COMPILE_FLAGS}"
POSITION_INDEPENDENT_CODE
ON)
set(LIBCXXABI_TARGETS)
# Build the shared library.
if (LIBCXXABI_ENABLE_SHARED)
add_library(cxxabi_shared SHARED $<TARGET_OBJECTS:cxxabi_objects>)
target_link_libraries(cxxabi_shared ${LIBCXXABI_LIBRARIES})
set_target_properties(cxxabi_shared
PROPERTIES
CXX_EXTENSIONS
OFF
CXX_STANDARD
11
CXX_STANDARD_REQUIRED
ON
LINK_FLAGS
"${LIBCXXABI_LINK_FLAGS} ${LIBCXXABI_SHARED_LINK_FLAGS}"
OUTPUT_NAME
"c++abi"
POSITION_INDEPENDENT_CODE
ON
SOVERSION
"1"
VERSION
"1.0")
list(APPEND LIBCXXABI_TARGETS "cxxabi_shared")
endif()
# Build the static library.
if (LIBCXXABI_ENABLE_STATIC)
set(cxxabi_static_sources $<TARGET_OBJECTS:cxxabi_objects>)
if (LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_ENABLE_STATIC_UNWINDER)
if (TARGET unwind_static OR HAVE_LIBUNWIND)
list(APPEND cxxabi_static_sources $<TARGET_OBJECTS:unwind_objects>)
endif()
endif()
add_library(cxxabi_static STATIC ${cxxabi_static_sources})
target_link_libraries(cxxabi_static ${LIBCXXABI_LIBRARIES})
set_target_properties(cxxabi_static
PROPERTIES
CXX_EXTENSIONS
OFF
CXX_STANDARD
11
CXX_STANDARD_REQUIRED
ON
LINK_FLAGS
"${LIBCXXABI_LINK_FLAGS}"
OUTPUT_NAME
"c++abi"
POSITION_INDEPENDENT_CODE
ON)
list(APPEND LIBCXXABI_TARGETS "cxxabi_static")
endif()
# Add a meta-target for both libraries.
add_custom_target(cxxabi DEPENDS ${LIBCXXABI_TARGETS})
if (LIBCXXABI_INSTALL_LIBRARY)
install(TARGETS ${LIBCXXABI_TARGETS}
LIBRARY DESTINATION ${LIBCXXABI_INSTALL_PREFIX}lib${LIBCXXABI_LIBDIR_SUFFIX} COMPONENT cxxabi
ARCHIVE DESTINATION ${LIBCXXABI_INSTALL_PREFIX}lib${LIBCXXABI_LIBDIR_SUFFIX} COMPONENT cxxabi
)
endif()
if (NOT CMAKE_CONFIGURATION_TYPES AND LIBCXXABI_INSTALL_LIBRARY)
add_custom_target(install-cxxabi
DEPENDS cxxabi
COMMAND "${CMAKE_COMMAND}"
-DCMAKE_INSTALL_COMPONENT=cxxabi
-P "${LIBCXXABI_BINARY_DIR}/cmake_install.cmake")
add_custom_target(install-cxxabi-stripped
DEPENDS cxxabi
COMMAND "${CMAKE_COMMAND}"
-DCMAKE_INSTALL_COMPONENT=cxxabi
-DCMAKE_INSTALL_DO_STRIP=1
-P "${LIBCXXABI_BINARY_DIR}/cmake_install.cmake")
# TODO: This is a legacy target name and should be removed at some point.
add_custom_target(install-libcxxabi DEPENDS install-cxxabi)
endif()

View File

@ -0,0 +1,78 @@
//===------------------------- abort_message.cpp --------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "abort_message.h"
#ifdef __BIONIC__
#include <android/api-level.h>
#if __ANDROID_API__ >= 21
#include <syslog.h>
extern "C" void android_set_abort_message(const char* msg);
#else
#include <assert.h>
#endif // __ANDROID_API__ >= 21
#endif // __BIONIC__
#ifdef __APPLE__
# if defined(__has_include) && __has_include(<CrashReporterClient.h>)
# define HAVE_CRASHREPORTERCLIENT_H
# include <CrashReporterClient.h>
# endif
#endif
void abort_message(const char* format, ...)
{
// write message to stderr
#if !defined(NDEBUG) || !defined(LIBCXXABI_BAREMETAL)
#ifdef __APPLE__
fprintf(stderr, "libc++abi.dylib: ");
#endif
va_list list;
va_start(list, format);
vfprintf(stderr, format, list);
va_end(list);
fprintf(stderr, "\n");
#endif
#if defined(__APPLE__) && defined(HAVE_CRASHREPORTERCLIENT_H)
// record message in crash report
char* buffer;
va_list list2;
va_start(list2, format);
vasprintf(&buffer, format, list2);
va_end(list2);
CRSetCrashLogMessage(buffer);
#elif defined(__BIONIC__)
char* buffer;
va_list list2;
va_start(list2, format);
vasprintf(&buffer, format, list2);
va_end(list2);
#if __ANDROID_API__ >= 21
// Show error in tombstone.
android_set_abort_message(buffer);
// Show error in logcat.
openlog("libc++abi", 0, 0);
syslog(LOG_CRIT, "%s", buffer);
closelog();
#else
// The good error reporting wasn't available in Android until L. Since we're
// about to abort anyway, just call __assert2, which will log _somewhere_
// (tombstone and/or logcat) in older releases.
__assert2(__FILE__, __LINE__, __func__, buffer);
#endif // __ANDROID_API__ >= 21
#endif // __BIONIC__
abort();
}

View File

@ -0,0 +1,27 @@
//===-------------------------- abort_message.h-----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef __ABORT_MESSAGE_H_
#define __ABORT_MESSAGE_H_
#include "cxxabi.h"
#ifdef __cplusplus
extern "C" {
#endif
_LIBCXXABI_HIDDEN _LIBCXXABI_NORETURN void
abort_message(const char *format, ...) __attribute__((format(printf, 1, 2)));
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,44 @@
//===------------------------ cxa_aux_runtime.cpp -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//
// This file implements the "Auxiliary Runtime APIs"
// http://mentorembedded.github.io/cxx-abi/abi-eh.html#cxx-aux
//===----------------------------------------------------------------------===//
#include "cxxabi.h"
#include <new>
#include <typeinfo>
namespace __cxxabiv1 {
extern "C" {
_LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void __cxa_bad_cast(void) {
#ifndef _LIBCXXABI_NO_EXCEPTIONS
throw std::bad_cast();
#else
std::terminate();
#endif
}
_LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void __cxa_bad_typeid(void) {
#ifndef _LIBCXXABI_NO_EXCEPTIONS
throw std::bad_typeid();
#else
std::terminate();
#endif
}
_LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN void
__cxa_throw_bad_array_new_length(void) {
#ifndef _LIBCXXABI_NO_EXCEPTIONS
throw std::bad_array_new_length();
#else
std::terminate();
#endif
}
} // extern "C"
} // abi

View File

@ -0,0 +1,133 @@
//===------------------------- cxa_default_handlers.cpp -------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//
// This file implements the default terminate_handler and unexpected_handler.
//===----------------------------------------------------------------------===//
#include <stdexcept>
#include <new>
#include <exception>
#include <cstdlib>
#include "abort_message.h"
#include "cxxabi.h"
#include "cxa_handlers.hpp"
#include "cxa_exception.hpp"
#include "private_typeinfo.h"
static const char* cause = "uncaught";
__attribute__((noreturn))
static void demangling_terminate_handler()
{
// If there might be an uncaught exception
using namespace __cxxabiv1;
__cxa_eh_globals* globals = __cxa_get_globals_fast();
if (globals)
{
__cxa_exception* exception_header = globals->caughtExceptions;
// If there is an uncaught exception
if (exception_header)
{
_Unwind_Exception* unwind_exception =
reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1;
bool native_exception =
(unwind_exception->exception_class & get_vendor_and_language) ==
(kOurExceptionClass & get_vendor_and_language);
if (native_exception)
{
void* thrown_object =
unwind_exception->exception_class == kOurDependentExceptionClass ?
((__cxa_dependent_exception*)exception_header)->primaryException :
exception_header + 1;
const __shim_type_info* thrown_type =
static_cast<const __shim_type_info*>(exception_header->exceptionType);
// Try to get demangled name of thrown_type
int status;
char buf[1024];
size_t len = sizeof(buf);
const char* name = __cxa_demangle(thrown_type->name(), buf, &len, &status);
if (status != 0)
name = thrown_type->name();
// If the uncaught exception can be caught with std::exception&
const __shim_type_info* catch_type =
static_cast<const __shim_type_info*>(&typeid(std::exception));
if (catch_type->can_catch(thrown_type, thrown_object))
{
// Include the what() message from the exception
const std::exception* e = static_cast<const std::exception*>(thrown_object);
abort_message("terminating with %s exception of type %s: %s",
cause, name, e->what());
}
else
// Else just note that we're terminating with an exception
abort_message("terminating with %s exception of type %s",
cause, name);
}
else
// Else we're terminating with a foreign exception
abort_message("terminating with %s foreign exception", cause);
}
}
// Else just note that we're terminating
abort_message("terminating");
}
__attribute__((noreturn))
static void demangling_unexpected_handler()
{
cause = "unexpected";
std::terminate();
}
#if !defined(LIBCXXABI_SILENT_TERMINATE)
static std::terminate_handler default_terminate_handler = demangling_terminate_handler;
static std::terminate_handler default_unexpected_handler = demangling_unexpected_handler;
#else
static std::terminate_handler default_terminate_handler = std::abort;
static std::terminate_handler default_unexpected_handler = std::terminate;
#endif
//
// Global variables that hold the pointers to the current handler
//
_LIBCXXABI_DATA_VIS
std::terminate_handler __cxa_terminate_handler = default_terminate_handler;
_LIBCXXABI_DATA_VIS
std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
// In the future these will become:
// std::atomic<std::terminate_handler> __cxa_terminate_handler(default_terminate_handler);
// std::atomic<std::unexpected_handler> __cxa_unexpected_handler(default_unexpected_handler);
namespace std
{
unexpected_handler
set_unexpected(unexpected_handler func) _NOEXCEPT
{
if (func == 0)
func = default_unexpected_handler;
return __atomic_exchange_n(&__cxa_unexpected_handler, func,
__ATOMIC_ACQ_REL);
// Using of C++11 atomics this should be rewritten
// return __cxa_unexpected_handler.exchange(func, memory_order_acq_rel);
}
terminate_handler
set_terminate(terminate_handler func) _NOEXCEPT
{
if (func == 0)
func = default_terminate_handler;
return __atomic_exchange_n(&__cxa_terminate_handler, func,
__ATOMIC_ACQ_REL);
// Using of C++11 atomics this should be rewritten
// return __cxa_terminate_handler.exchange(func, memory_order_acq_rel);
}
}

View File

@ -0,0 +1 @@
9cfd99607bffedcff4d9eaec0f370097d0713512

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,116 @@
//===------------------------- cxa_exception.hpp --------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//
// This file implements the "Exception Handling APIs"
// http://mentorembedded.github.io/cxx-abi/abi-eh.html
//
//===----------------------------------------------------------------------===//
#ifndef _CXA_EXCEPTION_H
#define _CXA_EXCEPTION_H
#include <exception> // for std::unexpected_handler and std::terminate_handler
#include "cxxabi.h"
#include "unwind.h"
namespace __cxxabiv1 {
static const uint64_t kOurExceptionClass = 0x434C4E47432B2B00; // CLNGC++\0
static const uint64_t kOurDependentExceptionClass = 0x434C4E47432B2B01; // CLNGC++\1
static const uint64_t get_vendor_and_language = 0xFFFFFFFFFFFFFF00; // mask for CLNGC++
struct _LIBCXXABI_HIDDEN __cxa_exception {
#if defined(__LP64__) || defined(_LIBCXXABI_ARM_EHABI)
// This is a new field to support C++ 0x exception_ptr.
// For binary compatibility it is at the start of this
// struct which is prepended to the object thrown in
// __cxa_allocate_exception.
size_t referenceCount;
#endif
// Manage the exception object itself.
std::type_info *exceptionType;
void (*exceptionDestructor)(void *);
std::unexpected_handler unexpectedHandler;
std::terminate_handler terminateHandler;
__cxa_exception *nextException;
int handlerCount;
#if defined(_LIBCXXABI_ARM_EHABI)
__cxa_exception* nextPropagatingException;
int propagationCount;
#else
int handlerSwitchValue;
const unsigned char *actionRecord;
const unsigned char *languageSpecificData;
void *catchTemp;
void *adjustedPtr;
#endif
#if !defined(__LP64__) && !defined(_LIBCXXABI_ARM_EHABI)
// This is a new field to support C++ 0x exception_ptr.
// For binary compatibility it is placed where the compiler
// previously adding padded to 64-bit align unwindHeader.
size_t referenceCount;
#endif
_Unwind_Exception unwindHeader;
};
// http://sourcery.mentor.com/archives/cxx-abi-dev/msg01924.html
// The layout of this structure MUST match the layout of __cxa_exception, with
// primaryException instead of referenceCount.
struct _LIBCXXABI_HIDDEN __cxa_dependent_exception {
#if defined(__LP64__) || defined(_LIBCXXABI_ARM_EHABI)
void* primaryException;
#endif
std::type_info *exceptionType;
void (*exceptionDestructor)(void *);
std::unexpected_handler unexpectedHandler;
std::terminate_handler terminateHandler;
__cxa_exception *nextException;
int handlerCount;
#if defined(_LIBCXXABI_ARM_EHABI)
__cxa_exception* nextPropagatingException;
int propagationCount;
#else
int handlerSwitchValue;
const unsigned char *actionRecord;
const unsigned char *languageSpecificData;
void * catchTemp;
void *adjustedPtr;
#endif
#if !defined(__LP64__) && !defined(_LIBCXXABI_ARM_EHABI)
void* primaryException;
#endif
_Unwind_Exception unwindHeader;
};
struct _LIBCXXABI_HIDDEN __cxa_eh_globals {
__cxa_exception * caughtExceptions;
unsigned int uncaughtExceptions;
#if defined(_LIBCXXABI_ARM_EHABI)
__cxa_exception* propagatingExceptions;
#endif
};
extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals ();
extern "C" _LIBCXXABI_FUNC_VIS __cxa_eh_globals * __cxa_get_globals_fast ();
extern "C" _LIBCXXABI_FUNC_VIS void * __cxa_allocate_dependent_exception ();
extern "C" _LIBCXXABI_FUNC_VIS void __cxa_free_dependent_exception (void * dependent_exception);
} // namespace __cxxabiv1
#endif // _CXA_EXCEPTION_H

View File

@ -0,0 +1,102 @@
//===--------------------- cxa_exception_storage.cpp ----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//
// This file implements the storage for the "Caught Exception Stack"
// http://mentorembedded.github.io/cxx-abi/abi-eh.html (section 2.2.2)
//
//===----------------------------------------------------------------------===//
#include "cxa_exception.hpp"
#include <__threading_support>
#if defined(_LIBCXXABI_HAS_NO_THREADS)
namespace __cxxabiv1 {
extern "C" {
static __cxa_eh_globals eh_globals;
__cxa_eh_globals *__cxa_get_globals() { return &eh_globals; }
__cxa_eh_globals *__cxa_get_globals_fast() { return &eh_globals; }
}
}
#elif defined(HAS_THREAD_LOCAL)
namespace __cxxabiv1 {
namespace {
__cxa_eh_globals * __globals () {
static thread_local __cxa_eh_globals eh_globals;
return &eh_globals;
}
}
extern "C" {
__cxa_eh_globals * __cxa_get_globals () { return __globals (); }
__cxa_eh_globals * __cxa_get_globals_fast () { return __globals (); }
}
}
#else
#include "abort_message.h"
#include "fallback_malloc.h"
// In general, we treat all threading errors as fatal.
// We cannot call std::terminate() because that will in turn
// call __cxa_get_globals() and cause infinite recursion.
namespace __cxxabiv1 {
namespace {
std::__libcpp_tls_key key_;
std::__libcpp_exec_once_flag flag_ = _LIBCPP_EXEC_ONCE_INITIALIZER;
void _LIBCPP_TLS_DESTRUCTOR_CC destruct_ (void *p) {
__free_with_fallback ( p );
if ( 0 != std::__libcpp_tls_set ( key_, NULL ) )
abort_message("cannot zero out thread value for __cxa_get_globals()");
}
void construct_ () {
if ( 0 != std::__libcpp_tls_create ( &key_, destruct_ ) )
abort_message("cannot create thread specific key for __cxa_get_globals()");
}
}
extern "C" {
__cxa_eh_globals * __cxa_get_globals () {
// Try to get the globals for this thread
__cxa_eh_globals* retVal = __cxa_get_globals_fast ();
// If this is the first time we've been asked for these globals, create them
if ( NULL == retVal ) {
retVal = static_cast<__cxa_eh_globals*>
(__calloc_with_fallback (1, sizeof (__cxa_eh_globals)));
if ( NULL == retVal )
abort_message("cannot allocate __cxa_eh_globals");
if ( 0 != std::__libcpp_tls_set ( key_, retVal ) )
abort_message("std::__libcpp_tls_set failure in __cxa_get_globals()");
}
return retVal;
}
// Note that this implementation will reliably return NULL if not
// preceded by a call to __cxa_get_globals(). This is an extension
// to the Itanium ABI and is taken advantage of in several places in
// libc++abi.
__cxa_eh_globals * __cxa_get_globals_fast () {
// First time through, create the key.
if (0 != std::__libcpp_execute_once(&flag_, construct_))
abort_message("execute once failure in __cxa_get_globals_fast()");
// static int init = construct_();
return static_cast<__cxa_eh_globals*>(std::__libcpp_tls_get(key_));
}
}
}
#endif

View File

@ -0,0 +1,265 @@
//===---------------------------- cxa_guard.cpp ---------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "__cxxabi_config.h"
#include "abort_message.h"
#include <__threading_support>
#include <stdint.h>
/*
This implementation must be careful to not call code external to this file
which will turn around and try to call __cxa_guard_acquire reentrantly.
For this reason, the headers of this file are as restricted as possible.
Previous implementations of this code for __APPLE__ have used
std::__libcpp_mutex_lock and the abort_message utility without problem. This
implementation also uses std::__libcpp_condvar_wait which has tested
to not be a problem.
*/
namespace __cxxabiv1
{
namespace
{
#ifdef __arm__
// A 32-bit, 4-byte-aligned static data value. The least significant 2 bits must
// be statically initialized to 0.
typedef uint32_t guard_type;
inline void set_initialized(guard_type* guard_object) {
*guard_object |= 1;
}
#else
typedef uint64_t guard_type;
void set_initialized(guard_type* guard_object) {
char* initialized = (char*)guard_object;
*initialized = 1;
}
#endif
#if defined(_LIBCXXABI_HAS_NO_THREADS) || (defined(__APPLE__) && !defined(__arm__))
#ifdef __arm__
// Test the lowest bit.
inline bool is_initialized(guard_type* guard_object) {
return (*guard_object) & 1;
}
#else
bool is_initialized(guard_type* guard_object) {
char* initialized = (char*)guard_object;
return *initialized;
}
#endif
#endif
#ifndef _LIBCXXABI_HAS_NO_THREADS
std::__libcpp_mutex_t guard_mut = _LIBCPP_MUTEX_INITIALIZER;
std::__libcpp_condvar_t guard_cv = _LIBCPP_CONDVAR_INITIALIZER;
#endif
#if defined(__APPLE__) && !defined(__arm__)
typedef uint32_t lock_type;
#if __LITTLE_ENDIAN__
inline
lock_type
get_lock(uint64_t x)
{
return static_cast<lock_type>(x >> 32);
}
inline
void
set_lock(uint64_t& x, lock_type y)
{
x = static_cast<uint64_t>(y) << 32;
}
#else // __LITTLE_ENDIAN__
inline
lock_type
get_lock(uint64_t x)
{
return static_cast<lock_type>(x);
}
inline
void
set_lock(uint64_t& x, lock_type y)
{
x = y;
}
#endif // __LITTLE_ENDIAN__
#else // !__APPLE__ || __arm__
typedef bool lock_type;
#if !defined(__arm__)
static_assert(std::is_same<guard_type, uint64_t>::value, "");
inline lock_type get_lock(uint64_t x)
{
union
{
uint64_t guard;
uint8_t lock[2];
} f = {x};
return f.lock[1] != 0;
}
inline void set_lock(uint64_t& x, lock_type y)
{
union
{
uint64_t guard;
uint8_t lock[2];
} f = {0};
f.lock[1] = y;
x = f.guard;
}
#else // defined(__arm__)
static_assert(std::is_same<guard_type, uint32_t>::value, "");
inline lock_type get_lock(uint32_t x)
{
union
{
uint32_t guard;
uint8_t lock[2];
} f = {x};
return f.lock[1] != 0;
}
inline void set_lock(uint32_t& x, lock_type y)
{
union
{
uint32_t guard;
uint8_t lock[2];
} f = {0};
f.lock[1] = y;
x = f.guard;
}
#endif // !defined(__arm__)
#endif // __APPLE__ && !__arm__
} // unnamed namespace
extern "C"
{
#ifndef _LIBCXXABI_HAS_NO_THREADS
_LIBCXXABI_FUNC_VIS int __cxa_guard_acquire(guard_type *guard_object) {
char* initialized = (char*)guard_object;
if (std::__libcpp_mutex_lock(&guard_mut))
abort_message("__cxa_guard_acquire failed to acquire mutex");
int result = *initialized == 0;
if (result)
{
#if defined(__APPLE__) && !defined(__arm__)
// This is a special-case pthread dependency for Mac. We can't pull this
// out into libcxx's threading API (__threading_support) because not all
// supported Mac environments provide this function (in pthread.h). To
// make it possible to build/use libcxx in those environments, we have to
// keep this pthread dependency local to libcxxabi. If there is some
// convenient way to detect precisely when pthread_mach_thread_np is
// available in a given Mac environment, it might still be possible to
// bury this dependency in __threading_support.
#ifdef _LIBCPP_HAS_THREAD_API_PTHREAD
const lock_type id = pthread_mach_thread_np(std::__libcpp_thread_get_current_id());
#else
#error "How do I pthread_mach_thread_np()?"
#endif
lock_type lock = get_lock(*guard_object);
if (lock)
{
// if this thread set lock for this same guard_object, abort
if (lock == id)
abort_message("__cxa_guard_acquire detected deadlock");
do
{
if (std::__libcpp_condvar_wait(&guard_cv, &guard_mut))
abort_message("__cxa_guard_acquire condition variable wait failed");
lock = get_lock(*guard_object);
} while (lock);
result = !is_initialized(guard_object);
if (result)
set_lock(*guard_object, id);
}
else
set_lock(*guard_object, id);
#else // !__APPLE__ || __arm__
while (get_lock(*guard_object))
if (std::__libcpp_condvar_wait(&guard_cv, &guard_mut))
abort_message("__cxa_guard_acquire condition variable wait failed");
result = *initialized == 0;
if (result)
set_lock(*guard_object, true);
#endif // !__APPLE__ || __arm__
}
if (std::__libcpp_mutex_unlock(&guard_mut))
abort_message("__cxa_guard_acquire failed to release mutex");
return result;
}
_LIBCXXABI_FUNC_VIS void __cxa_guard_release(guard_type *guard_object) {
if (std::__libcpp_mutex_lock(&guard_mut))
abort_message("__cxa_guard_release failed to acquire mutex");
*guard_object = 0;
set_initialized(guard_object);
if (std::__libcpp_mutex_unlock(&guard_mut))
abort_message("__cxa_guard_release failed to release mutex");
if (std::__libcpp_condvar_broadcast(&guard_cv))
abort_message("__cxa_guard_release failed to broadcast condition variable");
}
_LIBCXXABI_FUNC_VIS void __cxa_guard_abort(guard_type *guard_object) {
if (std::__libcpp_mutex_lock(&guard_mut))
abort_message("__cxa_guard_abort failed to acquire mutex");
*guard_object = 0;
if (std::__libcpp_mutex_unlock(&guard_mut))
abort_message("__cxa_guard_abort failed to release mutex");
if (std::__libcpp_condvar_broadcast(&guard_cv))
abort_message("__cxa_guard_abort failed to broadcast condition variable");
}
#else // _LIBCXXABI_HAS_NO_THREADS
_LIBCXXABI_FUNC_VIS int __cxa_guard_acquire(guard_type *guard_object) {
return !is_initialized(guard_object);
}
_LIBCXXABI_FUNC_VIS void __cxa_guard_release(guard_type *guard_object) {
*guard_object = 0;
set_initialized(guard_object);
}
_LIBCXXABI_FUNC_VIS void __cxa_guard_abort(guard_type *guard_object) {
*guard_object = 0;
}
#endif // !_LIBCXXABI_HAS_NO_THREADS
} // extern "C"
} // __cxxabiv1

View File

@ -0,0 +1,125 @@
//===------------------------- cxa_handlers.cpp ---------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//
// This file implements the functionality associated with the terminate_handler,
// unexpected_handler, and new_handler.
//===----------------------------------------------------------------------===//
#include <stdexcept>
#include <new>
#include <exception>
#include "abort_message.h"
#include "cxxabi.h"
#include "cxa_handlers.hpp"
#include "cxa_exception.hpp"
#include "private_typeinfo.h"
namespace std
{
unexpected_handler
get_unexpected() _NOEXCEPT
{
return __sync_fetch_and_add(&__cxa_unexpected_handler, (unexpected_handler)0);
// The above is safe but overkill on x86
// Using of C++11 atomics this should be rewritten
// return __cxa_unexpected_handler.load(memory_order_acq);
}
void
__unexpected(unexpected_handler func)
{
func();
// unexpected handler should not return
abort_message("unexpected_handler unexpectedly returned");
}
__attribute__((noreturn))
void
unexpected()
{
__unexpected(get_unexpected());
}
terminate_handler
get_terminate() _NOEXCEPT
{
return __sync_fetch_and_add(&__cxa_terminate_handler, (terminate_handler)0);
// The above is safe but overkill on x86
// Using of C++11 atomics this should be rewritten
// return __cxa_terminate_handler.load(memory_order_acq);
}
void
__terminate(terminate_handler func) _NOEXCEPT
{
#ifndef _LIBCXXABI_NO_EXCEPTIONS
try
{
#endif // _LIBCXXABI_NO_EXCEPTIONS
func();
// handler should not return
abort_message("terminate_handler unexpectedly returned");
#ifndef _LIBCXXABI_NO_EXCEPTIONS
}
catch (...)
{
// handler should not throw exception
abort_message("terminate_handler unexpectedly threw an exception");
}
#endif // _LIBCXXABI_NO_EXCEPTIONS
}
__attribute__((noreturn))
void
terminate() _NOEXCEPT
{
// If there might be an uncaught exception
using namespace __cxxabiv1;
__cxa_eh_globals* globals = __cxa_get_globals_fast();
if (globals)
{
__cxa_exception* exception_header = globals->caughtExceptions;
if (exception_header)
{
_Unwind_Exception* unwind_exception =
reinterpret_cast<_Unwind_Exception*>(exception_header + 1) - 1;
bool native_exception =
(unwind_exception->exception_class & get_vendor_and_language) ==
(kOurExceptionClass & get_vendor_and_language);
if (native_exception)
__terminate(exception_header->terminateHandler);
}
}
__terminate(get_terminate());
}
// In the future this will become:
// std::atomic<std::new_handler> __cxa_new_handler(0);
extern "C" {
new_handler __cxa_new_handler = 0;
}
new_handler
set_new_handler(new_handler handler) _NOEXCEPT
{
return __atomic_exchange_n(&__cxa_new_handler, handler, __ATOMIC_ACQ_REL);
// Using of C++11 atomics this should be rewritten
// return __cxa_new_handler.exchange(handler, memory_order_acq_rel);
}
new_handler
get_new_handler() _NOEXCEPT
{
return __sync_fetch_and_add(&__cxa_new_handler, (new_handler)0);
// The above is safe but overkill on x86
// Using of C++11 atomics this should be rewritten
// return __cxa_new_handler.load(memory_order_acq);
}
} // std

View File

@ -0,0 +1,56 @@
//===------------------------- cxa_handlers.cpp ---------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//
// This file implements the functionality associated with the terminate_handler,
// unexpected_handler, and new_handler.
//===----------------------------------------------------------------------===//
#ifndef _CXA_HANDLERS_H
#define _CXA_HANDLERS_H
#include <__cxxabi_config.h>
#include <exception>
namespace std
{
_LIBCXXABI_HIDDEN _LIBCXXABI_NORETURN
void
__unexpected(unexpected_handler func);
_LIBCXXABI_HIDDEN _LIBCXXABI_NORETURN
void
__terminate(terminate_handler func) _NOEXCEPT;
} // std
extern "C"
{
_LIBCXXABI_DATA_VIS extern void (*__cxa_terminate_handler)();
_LIBCXXABI_DATA_VIS extern void (*__cxa_unexpected_handler)();
_LIBCXXABI_DATA_VIS extern void (*__cxa_new_handler)();
/*
At some point in the future these three symbols will become
C++11 atomic variables:
extern std::atomic<std::terminate_handler> __cxa_terminate_handler;
extern std::atomic<std::unexpected_handler> __cxa_unexpected_handler;
extern std::atomic<std::new_handler> __cxa_new_handler;
This change will not impact their ABI. But it will allow for a
portable performance optimization.
*/
} // extern "C"
#endif // _CXA_HANDLERS_H

View File

@ -0,0 +1,55 @@
//===------------------------- cxa_exception.cpp --------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//
// This file implements the "Exception Handling APIs"
// http://mentorembedded.github.io/cxx-abi/abi-eh.html
//
//===----------------------------------------------------------------------===//
// Support functions for the no-exceptions libc++ library
#include "cxxabi.h"
#include <exception> // for std::terminate
#include "cxa_exception.hpp"
#include "cxa_handlers.hpp"
namespace __cxxabiv1 {
extern "C" {
void
__cxa_increment_exception_refcount(void *thrown_object) throw() {
if (thrown_object != nullptr)
std::terminate();
}
void
__cxa_decrement_exception_refcount(void *thrown_object) throw() {
if (thrown_object != nullptr)
std::terminate();
}
void *__cxa_current_primary_exception() throw() { return nullptr; }
void
__cxa_rethrow_primary_exception(void* thrown_object) {
if (thrown_object != nullptr)
std::terminate();
}
bool
__cxa_uncaught_exception() throw() { return false; }
unsigned int
__cxa_uncaught_exceptions() throw() { return 0; }
} // extern "C"
} // abi

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,140 @@
//===----------------------- cxa_thread_atexit.cpp ------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "abort_message.h"
#include "cxxabi.h"
#include <__threading_support>
#include <cstdlib>
namespace __cxxabiv1 {
using Dtor = void(*)(void*);
extern "C"
#ifndef HAVE___CXA_THREAD_ATEXIT_IMPL
// A weak symbol is used to detect this function's presence in the C library
// at runtime, even if libc++ is built against an older libc
_LIBCXXABI_WEAK
#endif
int __cxa_thread_atexit_impl(Dtor, void*, void*);
#ifndef HAVE___CXA_THREAD_ATEXIT_IMPL
namespace {
// This implementation is used if the C library does not provide
// __cxa_thread_atexit_impl() for us. It has a number of limitations that are
// difficult to impossible to address without ..._impl():
//
// - dso_symbol is ignored. This means that a shared library may be unloaded
// (via dlclose()) before its thread_local destructors have run.
//
// - thread_local destructors for the main thread are run by the destructor of
// a static object. This is later than expected; they should run before the
// destructors of any objects with static storage duration.
//
// - thread_local destructors on non-main threads run on the first iteration
// through the __libccpp_tls_key destructors.
// std::notify_all_at_thread_exit() and similar functions must be careful to
// wait until the second iteration to provide their intended ordering
// guarantees.
//
// Another limitation, though one shared with ..._impl(), is that any
// thread_locals that are first initialized after non-thread_local global
// destructors begin to run will not be destroyed. [basic.start.term] states
// that all thread_local destructors are sequenced before the destruction of
// objects with static storage duration, resulting in a contradiction if a
// thread_local is constructed after that point. Thus we consider such
// programs ill-formed, and don't bother to run those destructors. (If the
// program terminates abnormally after such a thread_local is constructed,
// the destructor is not expected to run and thus there is no contradiction.
// So construction still has to work.)
struct DtorList {
Dtor dtor;
void* obj;
DtorList* next;
};
// The linked list of thread-local destructors to run
__thread DtorList* dtors = nullptr;
// True if the destructors are currently scheduled to run on this thread
__thread bool dtors_alive = false;
// Used to trigger destructors on thread exit; value is ignored
std::__libcpp_tls_key dtors_key;
void run_dtors(void*) {
while (auto head = dtors) {
dtors = head->next;
head->dtor(head->obj);
std::free(head);
}
dtors_alive = false;
}
struct DtorsManager {
DtorsManager() {
// There is intentionally no matching std::__libcpp_tls_delete call, as
// __cxa_thread_atexit() may be called arbitrarily late (for example, from
// global destructors or atexit() handlers).
if (std::__libcpp_tls_create(&dtors_key, run_dtors) != 0) {
abort_message("std::__libcpp_tls_create() failed in __cxa_thread_atexit()");
}
}
~DtorsManager() {
// std::__libcpp_tls_key destructors do not run on threads that call exit()
// (including when the main thread returns from main()), so we explicitly
// call the destructor here. This runs at exit time (potentially earlier
// if libc++abi is dlclose()'d). Any thread_locals initialized after this
// point will not be destroyed.
run_dtors(nullptr);
}
};
} // namespace
#endif // HAVE___CXA_THREAD_ATEXIT_IMPL
extern "C" {
_LIBCXXABI_FUNC_VIS int __cxa_thread_atexit(Dtor dtor, void* obj, void* dso_symbol) throw() {
#ifdef HAVE___CXA_THREAD_ATEXIT_IMPL
return __cxa_thread_atexit_impl(dtor, obj, dso_symbol);
#else
if (__cxa_thread_atexit_impl) {
return __cxa_thread_atexit_impl(dtor, obj, dso_symbol);
} else {
// Initialize the dtors std::__libcpp_tls_key (uses __cxa_guard_*() for
// one-time initialization and __cxa_atexit() for destruction)
static DtorsManager manager;
if (!dtors_alive) {
if (std::__libcpp_tls_set(dtors_key, &dtors_key) != 0) {
return -1;
}
dtors_alive = true;
}
auto head = static_cast<DtorList*>(std::malloc(sizeof(DtorList)));
if (!head) {
return -1;
}
head->dtor = dtor;
head->obj = obj;
head->next = dtors;
dtors = head;
return 0;
}
#endif // HAVE___CXA_THREAD_ATEXIT_IMPL
}
} // extern "C"
} // namespace __cxxabiv1

View File

@ -0,0 +1,23 @@
//===------------------------- cxa_unexpected.cpp -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <exception>
#include "cxxabi.h"
#include "cxa_exception.hpp"
namespace __cxxabiv1
{
extern "C"
{
}
} // namespace __cxxabiv1

View File

@ -0,0 +1,367 @@
//===-------------------------- cxa_vector.cpp ---------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//
// This file implements the "Array Construction and Destruction APIs"
// http://mentorembedded.github.io/cxx-abi/abi.html#array-ctor
//
//===----------------------------------------------------------------------===//
#include "cxxabi.h"
#include <exception> // for std::terminate
namespace __cxxabiv1 {
#if 0
#pragma mark --Helper routines and classes --
#endif
namespace {
inline static size_t __get_element_count ( void *p ) {
return static_cast <size_t *> (p)[-1];
}
inline static void __set_element_count ( void *p, size_t element_count ) {
static_cast <size_t *> (p)[-1] = element_count;
}
// A pair of classes to simplify exception handling and control flow.
// They get passed a block of memory in the constructor, and unless the
// 'release' method is called, they deallocate the memory in the destructor.
// Preferred usage is to allocate some memory, attach it to one of these objects,
// and then, when all the operations to set up the memory block have succeeded,
// call 'release'. If any of the setup operations fail, or an exception is
// thrown, then the block is automatically deallocated.
//
// The only difference between these two classes is the signature for the
// deallocation function (to match new2/new3 and delete2/delete3.
class st_heap_block2 {
public:
typedef void (*dealloc_f)(void *);
st_heap_block2 ( dealloc_f dealloc, void *ptr )
: dealloc_ ( dealloc ), ptr_ ( ptr ), enabled_ ( true ) {}
~st_heap_block2 () { if ( enabled_ ) dealloc_ ( ptr_ ) ; }
void release () { enabled_ = false; }
private:
dealloc_f dealloc_;
void *ptr_;
bool enabled_;
};
class st_heap_block3 {
public:
typedef void (*dealloc_f)(void *, size_t);
st_heap_block3 ( dealloc_f dealloc, void *ptr, size_t size )
: dealloc_ ( dealloc ), ptr_ ( ptr ), size_ ( size ), enabled_ ( true ) {}
~st_heap_block3 () { if ( enabled_ ) dealloc_ ( ptr_, size_ ) ; }
void release () { enabled_ = false; }
private:
dealloc_f dealloc_;
void *ptr_;
size_t size_;
bool enabled_;
};
class st_cxa_cleanup {
public:
typedef void (*destruct_f)(void *);
st_cxa_cleanup ( void *ptr, size_t &idx, size_t element_size, destruct_f destructor )
: ptr_ ( ptr ), idx_ ( idx ), element_size_ ( element_size ),
destructor_ ( destructor ), enabled_ ( true ) {}
~st_cxa_cleanup () {
if ( enabled_ )
__cxa_vec_cleanup ( ptr_, idx_, element_size_, destructor_ );
}
void release () { enabled_ = false; }
private:
void *ptr_;
size_t &idx_;
size_t element_size_;
destruct_f destructor_;
bool enabled_;
};
class st_terminate {
public:
st_terminate ( bool enabled = true ) : enabled_ ( enabled ) {}
~st_terminate () { if ( enabled_ ) std::terminate (); }
void release () { enabled_ = false; }
private:
bool enabled_ ;
};
}
#if 0
#pragma mark --Externally visible routines--
#endif
extern "C" {
// Equivalent to
//
// __cxa_vec_new2(element_count, element_size, padding_size, constructor,
// destructor, &::operator new[], &::operator delete[])
_LIBCXXABI_FUNC_VIS void *
__cxa_vec_new(size_t element_count, size_t element_size, size_t padding_size,
void (*constructor)(void *), void (*destructor)(void *)) {
return __cxa_vec_new2 ( element_count, element_size, padding_size,
constructor, destructor, &::operator new [], &::operator delete [] );
}
// Given the number and size of elements for an array and the non-negative
// size of prefix padding for a cookie, allocate space (using alloc) for
// the array preceded by the specified padding, initialize the cookie if
// the padding is non-zero, and call the given constructor on each element.
// Return the address of the array proper, after the padding.
//
// If alloc throws an exception, rethrow the exception. If alloc returns
// NULL, return NULL. If the constructor throws an exception, call
// destructor for any already constructed elements, and rethrow the
// exception. If the destructor throws an exception, call std::terminate.
//
// The constructor may be NULL, in which case it must not be called. If the
// padding_size is zero, the destructor may be NULL; in that case it must
// not be called.
//
// Neither alloc nor dealloc may be NULL.
_LIBCXXABI_FUNC_VIS void *
__cxa_vec_new2(size_t element_count, size_t element_size, size_t padding_size,
void (*constructor)(void *), void (*destructor)(void *),
void *(*alloc)(size_t), void (*dealloc)(void *)) {
const size_t heap_size = element_count * element_size + padding_size;
char * const heap_block = static_cast<char *> ( alloc ( heap_size ));
char *vec_base = heap_block;
if ( NULL != vec_base ) {
st_heap_block2 heap ( dealloc, heap_block );
// put the padding before the array elements
if ( 0 != padding_size ) {
vec_base += padding_size;
__set_element_count ( vec_base, element_count );
}
// Construct the elements
__cxa_vec_ctor ( vec_base, element_count, element_size, constructor, destructor );
heap.release (); // We're good!
}
return vec_base;
}
// Same as __cxa_vec_new2 except that the deallocation function takes both
// the object address and its size.
_LIBCXXABI_FUNC_VIS void *
__cxa_vec_new3(size_t element_count, size_t element_size, size_t padding_size,
void (*constructor)(void *), void (*destructor)(void *),
void *(*alloc)(size_t), void (*dealloc)(void *, size_t)) {
const size_t heap_size = element_count * element_size + padding_size;
char * const heap_block = static_cast<char *> ( alloc ( heap_size ));
char *vec_base = heap_block;
if ( NULL != vec_base ) {
st_heap_block3 heap ( dealloc, heap_block, heap_size );
// put the padding before the array elements
if ( 0 != padding_size ) {
vec_base += padding_size;
__set_element_count ( vec_base, element_count );
}
// Construct the elements
__cxa_vec_ctor ( vec_base, element_count, element_size, constructor, destructor );
heap.release (); // We're good!
}
return vec_base;
}
// Given the (data) addresses of a destination and a source array, an
// element count and an element size, call the given copy constructor to
// copy each element from the source array to the destination array. The
// copy constructor's arguments are the destination address and source
// address, respectively. If an exception occurs, call the given destructor
// (if non-NULL) on each copied element and rethrow. If the destructor
// throws an exception, call terminate(). The constructor and or destructor
// pointers may be NULL. If either is NULL, no action is taken when it
// would have been called.
_LIBCXXABI_FUNC_VIS void __cxa_vec_cctor(void *dest_array, void *src_array,
size_t element_count,
size_t element_size,
void (*constructor)(void *, void *),
void (*destructor)(void *)) {
if ( NULL != constructor ) {
size_t idx = 0;
char *src_ptr = static_cast<char *>(src_array);
char *dest_ptr = static_cast<char *>(dest_array);
st_cxa_cleanup cleanup ( dest_array, idx, element_size, destructor );
for ( idx = 0; idx < element_count;
++idx, src_ptr += element_size, dest_ptr += element_size )
constructor ( dest_ptr, src_ptr );
cleanup.release (); // We're good!
}
}
// Given the (data) address of an array, not including any cookie padding,
// and the number and size of its elements, call the given constructor on
// each element. If the constructor throws an exception, call the given
// destructor for any already-constructed elements, and rethrow the
// exception. If the destructor throws an exception, call terminate(). The
// constructor and/or destructor pointers may be NULL. If either is NULL,
// no action is taken when it would have been called.
_LIBCXXABI_FUNC_VIS void
__cxa_vec_ctor(void *array_address, size_t element_count, size_t element_size,
void (*constructor)(void *), void (*destructor)(void *)) {
if ( NULL != constructor ) {
size_t idx;
char *ptr = static_cast <char *> ( array_address );
st_cxa_cleanup cleanup ( array_address, idx, element_size, destructor );
// Construct the elements
for ( idx = 0; idx < element_count; ++idx, ptr += element_size )
constructor ( ptr );
cleanup.release (); // We're good!
}
}
// Given the (data) address of an array, the number of elements, and the
// size of its elements, call the given destructor on each element. If the
// destructor throws an exception, rethrow after destroying the remaining
// elements if possible. If the destructor throws a second exception, call
// terminate(). The destructor pointer may be NULL, in which case this
// routine does nothing.
_LIBCXXABI_FUNC_VIS void __cxa_vec_dtor(void *array_address,
size_t element_count,
size_t element_size,
void (*destructor)(void *)) {
if ( NULL != destructor ) {
char *ptr = static_cast <char *> (array_address);
size_t idx = element_count;
st_cxa_cleanup cleanup ( array_address, idx, element_size, destructor );
{
st_terminate exception_guard (__cxa_uncaught_exception ());
ptr += element_count * element_size; // one past the last element
while ( idx-- > 0 ) {
ptr -= element_size;
destructor ( ptr );
}
exception_guard.release (); // We're good !
}
cleanup.release (); // We're still good!
}
}
// Given the (data) address of an array, the number of elements, and the
// size of its elements, call the given destructor on each element. If the
// destructor throws an exception, call terminate(). The destructor pointer
// may be NULL, in which case this routine does nothing.
_LIBCXXABI_FUNC_VIS void __cxa_vec_cleanup(void *array_address,
size_t element_count,
size_t element_size,
void (*destructor)(void *)) {
if ( NULL != destructor ) {
char *ptr = static_cast <char *> (array_address);
size_t idx = element_count;
st_terminate exception_guard;
ptr += element_count * element_size; // one past the last element
while ( idx-- > 0 ) {
ptr -= element_size;
destructor ( ptr );
}
exception_guard.release (); // We're done!
}
}
// If the array_address is NULL, return immediately. Otherwise, given the
// (data) address of an array, the non-negative size of prefix padding for
// the cookie, and the size of its elements, call the given destructor on
// each element, using the cookie to determine the number of elements, and
// then delete the space by calling ::operator delete[](void *). If the
// destructor throws an exception, rethrow after (a) destroying the
// remaining elements, and (b) deallocating the storage. If the destructor
// throws a second exception, call terminate(). If padding_size is 0, the
// destructor pointer must be NULL. If the destructor pointer is NULL, no
// destructor call is to be made.
//
// The intent of this function is to permit an implementation to call this
// function when confronted with an expression of the form delete[] p in
// the source code, provided that the default deallocation function can be
// used. Therefore, the semantics of this function are consistent with
// those required by the standard. The requirement that the deallocation
// function be called even if the destructor throws an exception derives
// from the resolution to DR 353 to the C++ standard, which was adopted in
// April, 2003.
_LIBCXXABI_FUNC_VIS void __cxa_vec_delete(void *array_address,
size_t element_size,
size_t padding_size,
void (*destructor)(void *)) {
__cxa_vec_delete2 ( array_address, element_size, padding_size,
destructor, &::operator delete [] );
}
// Same as __cxa_vec_delete, except that the given function is used for
// deallocation instead of the default delete function. If dealloc throws
// an exception, the result is undefined. The dealloc pointer may not be
// NULL.
_LIBCXXABI_FUNC_VIS void
__cxa_vec_delete2(void *array_address, size_t element_size, size_t padding_size,
void (*destructor)(void *), void (*dealloc)(void *)) {
if ( NULL != array_address ) {
char *vec_base = static_cast <char *> (array_address);
char *heap_block = vec_base - padding_size;
st_heap_block2 heap ( dealloc, heap_block );
if ( 0 != padding_size && NULL != destructor ) // call the destructors
__cxa_vec_dtor ( array_address, __get_element_count ( vec_base ),
element_size, destructor );
}
}
// Same as __cxa_vec_delete, except that the given function is used for
// deallocation instead of the default delete function. The deallocation
// function takes both the object address and its size. If dealloc throws
// an exception, the result is undefined. The dealloc pointer may not be
// NULL.
_LIBCXXABI_FUNC_VIS void
__cxa_vec_delete3(void *array_address, size_t element_size, size_t padding_size,
void (*destructor)(void *), void (*dealloc)(void *, size_t)) {
if ( NULL != array_address ) {
char *vec_base = static_cast <char *> (array_address);
char *heap_block = vec_base - padding_size;
const size_t element_count = padding_size ? __get_element_count ( vec_base ) : 0;
const size_t heap_block_size = element_size * element_count + padding_size;
st_heap_block3 heap ( dealloc, heap_block, heap_block_size );
if ( 0 != padding_size && NULL != destructor ) // call the destructors
__cxa_vec_dtor ( array_address, element_count, element_size, destructor );
}
}
} // extern "C"
} // abi

View File

@ -0,0 +1,25 @@
//===-------------------------- cxa_virtual.cpp ---------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "cxxabi.h"
#include "abort_message.h"
namespace __cxxabiv1 {
extern "C" {
_LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN
void __cxa_pure_virtual(void) {
abort_message("Pure virtual function called!");
}
_LIBCXXABI_FUNC_VIS _LIBCXXABI_NORETURN
void __cxa_deleted_virtual(void) {
abort_message("Deleted virtual function called!");
}
} // extern "C"
} // abi

View File

@ -0,0 +1,252 @@
//===------------------------ fallback_malloc.cpp -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "fallback_malloc.h"
#include <__threading_support>
#include <cstdlib> // for malloc, calloc, free
#include <cstring> // for memset
// A small, simple heap manager based (loosely) on
// the startup heap manager from FreeBSD, optimized for space.
//
// Manages a fixed-size memory pool, supports malloc and free only.
// No support for realloc.
//
// Allocates chunks in multiples of four bytes, with a four byte header
// for each chunk. The overhead of each chunk is kept low by keeping pointers
// as two byte offsets within the heap, rather than (4 or 8 byte) pointers.
namespace {
// When POSIX threads are not available, make the mutex operations a nop
#ifndef _LIBCXXABI_HAS_NO_THREADS
_LIBCPP_SAFE_STATIC
static std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER;
#else
static void* heap_mutex = 0;
#endif
class mutexor {
public:
#ifndef _LIBCXXABI_HAS_NO_THREADS
mutexor(std::__libcpp_mutex_t* m) : mtx_(m) {
std::__libcpp_mutex_lock(mtx_);
}
~mutexor() { std::__libcpp_mutex_unlock(mtx_); }
#else
mutexor(void*) {}
~mutexor() {}
#endif
private:
mutexor(const mutexor& rhs);
mutexor& operator=(const mutexor& rhs);
#ifndef _LIBCXXABI_HAS_NO_THREADS
std::__libcpp_mutex_t* mtx_;
#endif
};
static const size_t HEAP_SIZE = 512;
char heap[HEAP_SIZE] __attribute__((aligned));
typedef unsigned short heap_offset;
typedef unsigned short heap_size;
struct heap_node {
heap_offset next_node; // offset into heap
heap_size len; // size in units of "sizeof(heap_node)"
};
static const heap_node* list_end =
(heap_node*)(&heap[HEAP_SIZE]); // one past the end of the heap
static heap_node* freelist = NULL;
heap_node* node_from_offset(const heap_offset offset) {
return (heap_node*)(heap + (offset * sizeof(heap_node)));
}
heap_offset offset_from_node(const heap_node* ptr) {
return static_cast<heap_offset>(
static_cast<size_t>(reinterpret_cast<const char*>(ptr) - heap) /
sizeof(heap_node));
}
void init_heap() {
freelist = (heap_node*)heap;
freelist->next_node = offset_from_node(list_end);
freelist->len = HEAP_SIZE / sizeof(heap_node);
}
// How big a chunk we allocate
size_t alloc_size(size_t len) {
return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1;
}
bool is_fallback_ptr(void* ptr) {
return ptr >= heap && ptr < (heap + HEAP_SIZE);
}
void* fallback_malloc(size_t len) {
heap_node *p, *prev;
const size_t nelems = alloc_size(len);
mutexor mtx(&heap_mutex);
if (NULL == freelist)
init_heap();
// Walk the free list, looking for a "big enough" chunk
for (p = freelist, prev = 0; p && p != list_end;
prev = p, p = node_from_offset(p->next_node)) {
if (p->len > nelems) { // chunk is larger, shorten, and return the tail
heap_node* q;
p->len = static_cast<heap_size>(p->len - nelems);
q = p + p->len;
q->next_node = 0;
q->len = static_cast<heap_size>(nelems);
return (void*)(q + 1);
}
if (p->len == nelems) { // exact size match
if (prev == 0)
freelist = node_from_offset(p->next_node);
else
prev->next_node = p->next_node;
p->next_node = 0;
return (void*)(p + 1);
}
}
return NULL; // couldn't find a spot big enough
}
// Return the start of the next block
heap_node* after(struct heap_node* p) { return p + p->len; }
void fallback_free(void* ptr) {
struct heap_node* cp = ((struct heap_node*)ptr) - 1; // retrieve the chunk
struct heap_node *p, *prev;
mutexor mtx(&heap_mutex);
#ifdef DEBUG_FALLBACK_MALLOC
std::cout << "Freeing item at " << offset_from_node(cp) << " of size "
<< cp->len << std::endl;
#endif
for (p = freelist, prev = 0; p && p != list_end;
prev = p, p = node_from_offset(p->next_node)) {
#ifdef DEBUG_FALLBACK_MALLOC
std::cout << " p, cp, after (p), after(cp) " << offset_from_node(p) << ' '
<< offset_from_node(cp) << ' ' << offset_from_node(after(p))
<< ' ' << offset_from_node(after(cp)) << std::endl;
#endif
if (after(p) == cp) {
#ifdef DEBUG_FALLBACK_MALLOC
std::cout << " Appending onto chunk at " << offset_from_node(p)
<< std::endl;
#endif
p->len = static_cast<heap_size>(
p->len + cp->len); // make the free heap_node larger
return;
} else if (after(cp) == p) { // there's a free heap_node right after
#ifdef DEBUG_FALLBACK_MALLOC
std::cout << " Appending free chunk at " << offset_from_node(p)
<< std::endl;
#endif
cp->len = static_cast<heap_size>(cp->len + p->len);
if (prev == 0) {
freelist = cp;
cp->next_node = p->next_node;
} else
prev->next_node = offset_from_node(cp);
return;
}
}
// Nothing to merge with, add it to the start of the free list
#ifdef DEBUG_FALLBACK_MALLOC
std::cout << " Making new free list entry " << offset_from_node(cp)
<< std::endl;
#endif
cp->next_node = offset_from_node(freelist);
freelist = cp;
}
#ifdef INSTRUMENT_FALLBACK_MALLOC
size_t print_free_list() {
struct heap_node *p, *prev;
heap_size total_free = 0;
if (NULL == freelist)
init_heap();
for (p = freelist, prev = 0; p && p != list_end;
prev = p, p = node_from_offset(p->next_node)) {
std::cout << (prev == 0 ? "" : " ") << "Offset: " << offset_from_node(p)
<< "\tsize: " << p->len << " Next: " << p->next_node << std::endl;
total_free += p->len;
}
std::cout << "Total Free space: " << total_free << std::endl;
return total_free;
}
#endif
} // end unnamed namespace
namespace __cxxabiv1 {
struct __attribute__((aligned)) __aligned_type {};
void* __aligned_malloc_with_fallback(size_t size) {
#if defined(_WIN32)
if (void* dest = _aligned_malloc(size, alignof(__aligned_type)))
return dest;
#elif defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
if (void* dest = std::malloc(size))
return dest;
#else
if (size == 0)
size = 1;
void* dest;
if (::posix_memalign(&dest, alignof(__aligned_type), size) == 0)
return dest;
#endif
return fallback_malloc(size);
}
void* __calloc_with_fallback(size_t count, size_t size) {
void* ptr = std::calloc(count, size);
if (NULL != ptr)
return ptr;
// if calloc fails, fall back to emergency stash
ptr = fallback_malloc(size * count);
if (NULL != ptr)
std::memset(ptr, 0, size * count);
return ptr;
}
void __aligned_free_with_fallback(void* ptr) {
if (is_fallback_ptr(ptr))
fallback_free(ptr);
else {
#if defined(_WIN32)
::_aligned_free(ptr);
#else
std::free(ptr);
#endif
}
}
void __free_with_fallback(void* ptr) {
if (is_fallback_ptr(ptr))
fallback_free(ptr);
else
std::free(ptr);
}
} // namespace __cxxabiv1

View File

@ -0,0 +1,29 @@
//===------------------------- fallback_malloc.h --------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef _FALLBACK_MALLOC_H
#define _FALLBACK_MALLOC_H
#include "__cxxabi_config.h"
#include <cstddef> // for size_t
namespace __cxxabiv1 {
// Allocate some memory from _somewhere_
_LIBCXXABI_HIDDEN void * __aligned_malloc_with_fallback(size_t size);
// Allocate and zero-initialize memory from _somewhere_
_LIBCXXABI_HIDDEN void * __calloc_with_fallback(size_t count, size_t size);
_LIBCXXABI_HIDDEN void __aligned_free_with_fallback(void *ptr);
_LIBCXXABI_HIDDEN void __free_with_fallback(void *ptr);
} // namespace __cxxabiv1
#endif

Some files were not shown because too many files have changed in this diff Show More