From 73ab1864efe76d0872872e3c40cd123314f1a3e9 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Tue, 24 Jan 2012 21:41:27 +0000 Subject: [PATCH] Marshall Clow reminded me that fallback_malloc.cpp was broken out for testing purposes. We've agreed to simply rename fallback_malloc.cpp to fallback_malloc.ipp. Thanks Marshall. llvm-svn: 148863 --- libcxxabi/src/cxa_exception.cpp | 162 +--------------------- libcxxabi/src/fallback_malloc.ipp | 174 ++++++++++++++++++++++++ libcxxabi/test/test_fallback_malloc.cpp | 2 +- 3 files changed, 176 insertions(+), 162 deletions(-) create mode 100644 libcxxabi/src/fallback_malloc.ipp diff --git a/libcxxabi/src/cxa_exception.cpp b/libcxxabi/src/cxa_exception.cpp index df10b28a468d..0d86a5b241fb 100644 --- a/libcxxabi/src/cxa_exception.cpp +++ b/libcxxabi/src/cxa_exception.cpp @@ -101,167 +101,7 @@ static inline int decrementHandlerCount(__cxa_exception *exception) throw() { return --exception->handlerCount; } -// 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 { - -static pthread_mutex_t heap_mutex = PTHREAD_MUTEX_INITIALIZER; - -class mutexor { -public: - mutexor ( pthread_mutex_t *m ) : mtx_(m) { pthread_mutex_lock ( mtx_ ); } - ~mutexor () { pthread_mutex_unlock ( mtx_ ); } -private: - mutexor ( const mutexor &rhs ); - mutexor & operator = ( const mutexor &rhs ); - pthread_mutex_t *mtx_; - }; - - -#define HEAP_SIZE 512 -char heap [ HEAP_SIZE ]; - -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 ) throw() - { return (heap_node *) ( heap + ( offset * sizeof (heap_node))); } - -heap_offset offset_from_node ( const heap_node *ptr ) throw() - { return (((char *) ptr ) - heap) / sizeof (heap_node); } - -void init_heap () throw() { - 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) throw() - { return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1; } - -bool is_fallback_ptr ( void *ptr ) throw() - { return ptr >= heap && ptr < ( heap + HEAP_SIZE ); } - -void *fallback_malloc(size_t len) throw() { - 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 -= nelems; - q = p + p->len; - q->next_node = 0; - q->len = 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 ) throw() { return p + p->len; } - -void fallback_free (void *ptr) throw() { - 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 += 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 += 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 +#include "fallback_malloc.ipp" // Allocate some memory from _somewhere_ static void *do_malloc(size_t size) throw() { diff --git a/libcxxabi/src/fallback_malloc.ipp b/libcxxabi/src/fallback_malloc.ipp new file mode 100644 index 000000000000..576ee30dfd54 --- /dev/null +++ b/libcxxabi/src/fallback_malloc.ipp @@ -0,0 +1,174 @@ +//===------------------------ 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. +// +// +// This file implements the "Exception Handling APIs" +// http://www.codesourcery.com/public/cxx-abi/abi-eh.html +// +//===----------------------------------------------------------------------===// + +// 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 { + +static pthread_mutex_t heap_mutex = PTHREAD_MUTEX_INITIALIZER; + +class mutexor { +public: + mutexor ( pthread_mutex_t *m ) : mtx_(m) { pthread_mutex_lock ( mtx_ ); } + ~mutexor () { pthread_mutex_unlock ( mtx_ ); } +private: + mutexor ( const mutexor &rhs ); + mutexor & operator = ( const mutexor &rhs ); + pthread_mutex_t *mtx_; + }; + + +#define HEAP_SIZE 512 +char heap [ HEAP_SIZE ]; + +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 ) throw() + { return (heap_node *) ( heap + ( offset * sizeof (heap_node))); } + +heap_offset offset_from_node ( const heap_node *ptr ) throw() + { return (((char *) ptr ) - heap) / sizeof (heap_node); } + +void init_heap () throw() { + 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) throw() + { return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1; } + +bool is_fallback_ptr ( void *ptr ) throw() + { return ptr >= heap && ptr < ( heap + HEAP_SIZE ); } + +void *fallback_malloc(size_t len) throw() { + 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 -= nelems; + q = p + p->len; + q->next_node = 0; + q->len = 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 ) throw() { return p + p->len; } + +void fallback_free (void *ptr) throw() { + 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 += 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 += 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 diff --git a/libcxxabi/test/test_fallback_malloc.cpp b/libcxxabi/test/test_fallback_malloc.cpp index 474e0b3b3365..3eed3f4d79b6 100644 --- a/libcxxabi/test/test_fallback_malloc.cpp +++ b/libcxxabi/test/test_fallback_malloc.cpp @@ -16,7 +16,7 @@ typedef std::deque container; // #define DEBUG_FALLBACK_MALLOC #define INSTRUMENT_FALLBACK_MALLOC -#include "../src/fallback_malloc.cpp" +#include "../src/fallback_malloc.ipp" container alloc_series ( size_t sz ) { container ptrs;