X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=mymemory.cc;h=2add16c33868ef00d7cca763e5d87187d95baa8f;hb=0ebd310446ea2a5675ee33eb6f9d75e311af7d36;hp=fcc98db547e67633bb8b0c665327f3402edfdea0;hpb=dd0190cef49ef760f8d86a6e92dd6eeb07197854;p=model-checker.git diff --git a/mymemory.cc b/mymemory.cc index fcc98db..2add16c 100644 --- a/mymemory.cc +++ b/mymemory.cc @@ -13,10 +13,37 @@ int howManyFreed = 0; static mspace sStaticSpace = NULL; #endif +/** Non-snapshotting calloc for our use. */ +void *model_calloc(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) { +void *model_malloc(size_t size) { #if USE_MPROTECT_SNAPSHOT - static void *(*mallocp)(size_t size); + static void *(*mallocp)(size_t size)=NULL; char *error; void *ptr; @@ -31,11 +58,11 @@ void *MYMALLOC(size_t 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 } @@ -73,7 +100,7 @@ void system_free( void * ptr ){ } /** Non-snapshotting free for our use. */ -void MYFREE(void *ptr) { +void model_free(void *ptr) { #if USE_MPROTECT_SNAPSHOT static void (*freep)(void *); char *error; @@ -99,38 +126,41 @@ 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. */ @@ -146,12 +176,13 @@ void *realloc( void *ptr, size_t 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. */