ikudrin updated the summary for this revision. ikudrin updated this revision to Diff 51782. ikudrin added a comment.
Make cxa_exception.cpp and cxa_exception_storage.cpp to use the same emergency memory pool. http://reviews.llvm.org/D17815 Files: libcxxabi/trunk/src/CMakeLists.txt libcxxabi/trunk/src/cxa_exception.cpp libcxxabi/trunk/src/cxa_exception_storage.cpp libcxxabi/trunk/src/fallback_malloc.cpp libcxxabi/trunk/src/fallback_malloc.h libcxxabi/trunk/test/test_exception_storage_nodynmem.pass.cpp
Index: libcxxabi/trunk/test/test_exception_storage_nodynmem.pass.cpp =================================================================== --- /dev/null +++ libcxxabi/trunk/test/test_exception_storage_nodynmem.pass.cpp @@ -0,0 +1,21 @@ +//===--------------- test_exception_storage_nodynmem.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 <cstdlib> + +// Override calloc to simulate exhaustion of dynamic memory +void *calloc(size_t, size_t) { return 0; } + +int main(int argc, char *argv[]) { + try { + throw 42; + } catch (...) { + } + return 0; +} Index: libcxxabi/trunk/src/fallback_malloc.h =================================================================== --- /dev/null +++ libcxxabi/trunk/src/fallback_malloc.h @@ -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 + +namespace __cxxabiv1 { + +#pragma GCC visibility push(hidden) + +// Allocate some memory from _somewhere_ +void * __cxa_malloc_with_fallback(size_t size); + +// Allocate and zero-initialize memory from _somewhere_ +void * __cxa_calloc_with_fallback(size_t count, size_t size); + +void __cxa_free_with_fallback(void *ptr); + +#pragma GCC visibility pop + +} // namespace __cxxabiv1 + +#endif Index: libcxxabi/trunk/src/fallback_malloc.cpp =================================================================== --- /dev/null +++ libcxxabi/trunk/src/fallback_malloc.cpp @@ -0,0 +1,49 @@ +//===------------------------ fallback_malloc.ipp -------------------------===// +// +// 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 "config.h" + +#include <cstdlib> // for malloc, calloc, free +#include <cstring> // for memset + +#if !LIBCXXABI_HAS_NO_THREADS +#include <pthread.h> // for fallback_malloc.ipp's mutexes +#endif + +#include "fallback_malloc.h" +#include "fallback_malloc.ipp" + +namespace __cxxabiv1 { + +void * __cxa_malloc_with_fallback(size_t size) { + void *ptr = std::malloc(size); + if (NULL == ptr) // if malloc fails, fall back to emergency stash + ptr = fallback_malloc(size); + return ptr; +} + +void * __cxa_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 __cxa_free_with_fallback(void *ptr) { + if (is_fallback_ptr(ptr)) + fallback_free(ptr); + else + std::free(ptr); +} + +} // namespace __cxxabiv1 Index: libcxxabi/trunk/src/cxa_exception_storage.cpp =================================================================== --- libcxxabi/trunk/src/cxa_exception_storage.cpp +++ libcxxabi/trunk/src/cxa_exception_storage.cpp @@ -45,8 +45,8 @@ #else #include <pthread.h> -#include <cstdlib> // for calloc, free #include "abort_message.h" +#include "fallback_malloc.h" // In general, we treat all pthread errors as fatal. // We cannot call std::terminate() because that will in turn @@ -58,7 +58,7 @@ pthread_once_t flag_ = PTHREAD_ONCE_INIT; void destruct_ (void *p) { - std::free ( p ); + __cxa_free_with_fallback ( p ); if ( 0 != ::pthread_setspecific ( key_, NULL ) ) abort_message("cannot zero out thread value for __cxa_get_globals()"); } @@ -77,7 +77,7 @@ // If this is the first time we've been asked for these globals, create them if ( NULL == retVal ) { retVal = static_cast<__cxa_eh_globals*> - (std::calloc (1, sizeof (__cxa_eh_globals))); + (__cxa_calloc_with_fallback (1, sizeof (__cxa_eh_globals))); if ( NULL == retVal ) abort_message("cannot allocate __cxa_eh_globals"); if ( 0 != pthread_setspecific ( key_, retVal ) ) Index: libcxxabi/trunk/src/cxa_exception.cpp =================================================================== --- libcxxabi/trunk/src/cxa_exception.cpp +++ libcxxabi/trunk/src/cxa_exception.cpp @@ -15,13 +15,10 @@ #include "cxxabi.h" #include <exception> // for std::terminate -#include <cstdlib> // for malloc, free #include <cstring> // for memset -#if !LIBCXXABI_HAS_NO_THREADS -# include <pthread.h> // for fallback_malloc.ipp's mutexes -#endif #include "cxa_exception.hpp" #include "cxa_handlers.hpp" +#include "fallback_malloc.h" // +---------------------------+-----------------------------+---------------+ // | __cxa_exception | _Unwind_Exception CLNGC++\0 | thrown object | @@ -104,20 +101,6 @@ return --exception->handlerCount; } -#include "fallback_malloc.ipp" - -// Allocate some memory from _somewhere_ -static void *do_malloc(size_t size) { - void *ptr = std::malloc(size); - if (NULL == ptr) // if malloc fails, fall back to emergency stash - ptr = fallback_malloc(size); - return ptr; -} - -static void do_free(void *ptr) { - is_fallback_ptr(ptr) ? fallback_free(ptr) : std::free(ptr); -} - /* If reason isn't _URC_FOREIGN_EXCEPTION_CAUGHT, then the terminateHandler stored in exc is called. Otherwise the exceptionDestructor stored in @@ -158,7 +141,8 @@ // user's exception object. _LIBCXXABI_FUNC_VIS void *__cxa_allocate_exception(size_t thrown_size) throw() { size_t actual_size = cxa_exception_size_from_exception_thrown_size(thrown_size); - __cxa_exception* exception_header = static_cast<__cxa_exception*>(do_malloc(actual_size)); + __cxa_exception *exception_header = + static_cast<__cxa_exception *>(__cxa_malloc_with_fallback(actual_size)); if (NULL == exception_header) std::terminate(); std::memset(exception_header, 0, actual_size); @@ -168,16 +152,16 @@ // Free a __cxa_exception object allocated with __cxa_allocate_exception. _LIBCXXABI_FUNC_VIS void __cxa_free_exception(void *thrown_object) throw() { - do_free(cxa_exception_from_thrown_object(thrown_object)); + __cxa_free_with_fallback(cxa_exception_from_thrown_object(thrown_object)); } // This function shall allocate a __cxa_dependent_exception and // return a pointer to it. (Really to the object, not past its' end). // Otherwise, it will work like __cxa_allocate_exception. void * __cxa_allocate_dependent_exception () { size_t actual_size = sizeof(__cxa_dependent_exception); - void *ptr = do_malloc(actual_size); + void *ptr = __cxa_malloc_with_fallback(actual_size); if (NULL == ptr) std::terminate(); std::memset(ptr, 0, actual_size); @@ -188,7 +172,7 @@ // This function shall free a dependent_exception. // It does not affect the reference count of the primary exception. void __cxa_free_dependent_exception (void * dependent_exception) { - do_free(dependent_exception); + __cxa_free_with_fallback(dependent_exception); } Index: libcxxabi/trunk/src/CMakeLists.txt =================================================================== --- libcxxabi/trunk/src/CMakeLists.txt +++ libcxxabi/trunk/src/CMakeLists.txt @@ -14,6 +14,7 @@ cxa_vector.cpp cxa_virtual.cpp exception.cpp + fallback_malloc.cpp private_typeinfo.cpp stdexcept.cpp typeinfo.cpp
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits