X-Git-Url: http://plrg.eecs.uci.edu/git/?p=model-checker.git;a=blobdiff_plain;f=mymemory.cc;h=b702d39945fa5cbaa7953b106aad3f7fa748a273;hp=88018e269093c531a67bde16d27de8a95d6d6305;hb=adccaa0225ce474d5d7a8e0833c11d0c0fdf944d;hpb=f7e1577b22ac26c12dc57a43de2761aa9249227c diff --git a/mymemory.cc b/mymemory.cc index 88018e2..b702d39 100644 --- a/mymemory.cc +++ b/mymemory.cc @@ -13,14 +13,40 @@ int howManyFreed = 0; static mspace sStaticSpace = NULL; #endif -/** Non-snapshotting malloc for our use. */ +/** Non-snapshotting calloc for our use. */ +void *MYCALLOC(size_t count, size_t size) { +#if USE_MPROTECT_SNAPSHOT + static void *(*callocp)(size_t count, size_t size)=NULL; + char *error; + void *ptr; + + /* get address of libc malloc */ + if (!callocp) { + callocp = ( void * ( * )( size_t, size_t ) )dlsym(RTLD_NEXT, "calloc"); + if ((error = dlerror()) != NULL) { + fputs(error, stderr); + exit(EXIT_FAILURE); + } + } + ptr = callocp(count, size); + return ptr; +#else + if( !snapshotrecord) { + createSharedMemory(); + } + if( NULL == sStaticSpace ) + sStaticSpace = create_mspace_with_base( ( void * )( snapshotrecord->mSharedMemoryBase ), SHARED_MEMORY_DEFAULT -sizeof( struct SnapShot ), 1 ); + return mspace_calloc( sStaticSpace, count, size ); +#endif +} +/** Non-snapshotting malloc for our use. */ void *MYMALLOC(size_t size) { #if USE_MPROTECT_SNAPSHOT - static void *(*mallocp)(size_t size); + static void *(*mallocp)(size_t size)=NULL; char *error; void *ptr; - + /* get address of libc malloc */ if (!mallocp) { mallocp = ( void * ( * )( size_t ) )dlsym(RTLD_NEXT, "malloc"); @@ -29,14 +55,14 @@ void *MYMALLOC(size_t size) { exit(EXIT_FAILURE); } } - ptr = mallocp(size); + ptr = mallocp(size); return ptr; #else - if( !sTheRecord ){ - createSharedLibrary(); + if( !snapshotrecord) { + createSharedMemory(); } if( NULL == sStaticSpace ) - sStaticSpace = create_mspace_with_base( ( void * )( sTheRecord->mSharedMemoryBase ), SHARED_MEMORY_DEFAULT -sizeof( struct Snapshot ), 1 ); + sStaticSpace = create_mspace_with_base( ( void * )( snapshotrecord->mSharedMemoryBase ), SHARED_MEMORY_DEFAULT -sizeof( struct SnapShot ), 1 ); return mspace_malloc( sStaticSpace, size ); #endif } @@ -100,87 +126,81 @@ mspace mySpace = NULL; /** This global references the unaligned memory address that was malloced for the snapshotting heap */ void * basemySpace = NULL; -/** Adding the fix for not able to allocate through a reimplemented calloc at the beginning before instantiating our allocator -A bit circumspect about adding an sbrk. linux docs say to avoid using it... */ +/** Bootstrap allocation. Problem is that the dynamic linker calls + * require calloc to work and calloc requires the dynamic linker to + * work. */ + +#define BOOTSTRAPBYTES 4096 +char bootstrapmemory[BOOTSTRAPBYTES]; +size_t offset=0; void * HandleEarlyAllocationRequest( size_t sz ){ - if( 0 == mySpace ){ - void * returnAddress = sbrk( sz ); - if( nextRequest >= REQUESTS_BEFORE_ALLOC ){ - exit( EXIT_FAILURE ); - } - allocatedReqs[ nextRequest++ ] = ( size_t )returnAddress; - return returnAddress; + /*Align to 8 byte boundary*/ + sz=(sz+7)&~7; + + if (sz > (BOOTSTRAPBYTES-offset)) { + printf("OUT OF BOOTSTRAP MEMORY\n"); + exit(EXIT_FAILURE); } - return NULL; + + void * pointer= (void *) & bootstrapmemory[offset]; + offset+=sz; + return pointer; } -/** The fact that I am not expecting more than a handful requests is implicit in my not using a binary search here*/ +/** Check whether this is bootstrapped memory that we should not + free. */ bool DontFree( void * ptr ){ - if( howManyFreed == nextRequest ) return false; //a minor optimization to reduce the number of instructions executed on each free call.... - if( NULL == ptr ) return true; - for( int i = nextRequest - 1; i >= 0; --i ){ - if( allocatedReqs[ i ] == ( size_t )ptr ) { - ++howManyFreed; - return true; - } - } - return false; + return (ptr>=(&bootstrapmemory[0])&&ptr<(&bootstrapmemory[BOOTSTRAPBYTES])); } /** Snapshotting malloc implementation for user programs. */ - void *malloc( size_t size ) { - void * earlyReq = HandleEarlyAllocationRequest( size ); - if( earlyReq ) return earlyReq; - return mspace_malloc( mySpace, size ); + if (mySpace) + return mspace_malloc( mySpace, size ); + else + return HandleEarlyAllocationRequest( size ); } /** Snapshotting free implementation for user programs. */ - void free( void * ptr ){ if( DontFree( ptr ) ) return; mspace_free( mySpace, ptr ); } /** Snapshotting realloc implementation for user programs. */ - void *realloc( void *ptr, size_t size ){ return mspace_realloc( mySpace, ptr, size ); } /** Snapshotting calloc implementation for user programs. */ - void * calloc( size_t num, size_t size ){ - void * earlyReq = HandleEarlyAllocationRequest( size * num ); - if( earlyReq ) { - std::memset( earlyReq, 0, size * num ); - return earlyReq; + if (mySpace) + return mspace_calloc( mySpace, num, size ); + else { + void *tmp=HandleEarlyAllocationRequest( size * num ); + std::memset( tmp, 0, size * num ); + return tmp; } - return mspace_calloc( mySpace, num, size ); } /** Snapshotting new operator for user programs. */ - void * operator new(size_t size) throw(std::bad_alloc) { return malloc(size); } /** Snapshotting delete operator for user programs. */ - void operator delete(void *p) throw() { free(p); } /** Snapshotting new[] operator for user programs. */ - void * operator new[](size_t size) throw(std::bad_alloc) { return malloc(size); } /** Snapshotting delete[] operator for user programs. */ - void operator delete[](void *p, size_t size) { free(p); }