factor page alignment into function call...place near existing call
[cdsspec-compiler.git] / snapshot.cc
index 9a92aac259d644a62c3fef5b672b621d55d4d888..20980015fe50d9678e67a606f37e2a3b934aa3e6 100644 (file)
@@ -61,7 +61,7 @@ void initSnapShotRecord(unsigned int numbackingpages, unsigned int numsnapshots,
        snapshotrecord->regionsToSnapShot=( struct MemoryRegion * )MYMALLOC(sizeof(struct MemoryRegion)*nummemoryregions);
        snapshotrecord->backingStoreBasePtr= ( struct SnapShotPage * )MYMALLOC( sizeof( struct SnapShotPage ) * (numbackingpages + 1) );
        //Page align the backingstorepages
-       snapshotrecord->backingStore=( struct SnapShotPage * )ReturnPageAlignedAddress((void*) ((uintptr_t)(snapshotrecord->backingStoreBasePtr)+sizeof(struct SnapShotPage)-1));
+       snapshotrecord->backingStore=( struct SnapShotPage * )PageAlignAddressUpward(snapshotrecord->backingStoreBasePtr);
        snapshotrecord->backingRecords=( struct BackingPageRecord * )MYMALLOC(sizeof(struct BackingPageRecord)*numbackingpages);
        snapshotrecord->snapShots= ( struct SnapShotRecord * )MYMALLOC(sizeof(struct SnapShotRecord)*numsnapshots);
        snapshotrecord->lastSnapShot=0;
@@ -91,7 +91,10 @@ void HandlePF( int sig, siginfo_t *si, void * unused){
        //remember where to copy page back to
        snapshotrecord->backingRecords[backingpage].basePtrOfPage=addr;
        //set protection to read/write
-       mprotect( addr, sizeof(struct SnapShotPage), PROT_READ | PROT_WRITE );  
+       if (mprotect( addr, sizeof(struct SnapShotPage), PROT_READ | PROT_WRITE )) {
+               perror("mprotect");
+               // Handle error by quitting?
+       }
 #endif //nothing to handle for non snapshotting case.
 }
 
@@ -100,6 +103,12 @@ void HandlePF( int sig, siginfo_t *si, void * unused){
 void * ReturnPageAlignedAddress(void * addr) {
        return (void *)(((uintptr_t)addr)&~(PAGESIZE-1));
 }
+
+//Return a page aligned address for the address being added
+//as a side effect the numBytes are also changed.
+void * PageAlignAddressUpward(void * addr) {
+       return (void *)((((uintptr_t)addr)+PAGESIZE-1)&~(PAGESIZE-1));
+}
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -134,8 +143,11 @@ void initSnapShotLibrary(unsigned int numbackingpages, unsigned int numsnapshots
                exit(-1);
        }
        initSnapShotRecord(numbackingpages, numsnapshots, nummemoryregions);
-       mySpace = create_mspace( numheappages*PAGESIZE, 1 );
-       addMemoryRegionToSnapShot(mySpace, numheappages);
+       
+       basemySpace=MYMALLOC((numheappages+1)*PAGESIZE);
+       void * pagealignedbase=PageAlignAddressUpward(basemySpace);
+       mySpace = create_mspace_with_base(pagealignedbase,  numheappages*PAGESIZE, 1 );
+       addMemoryRegionToSnapShot(pagealignedbase, numheappages);
        entryPoint();
 #else
        //add a signal to indicate that the process is going to terminate.
@@ -226,6 +238,7 @@ snapshot_id takeSnapshot( ){
 #if USE_CHECKPOINTING
        for(unsigned int region=0; region<snapshotrecord->lastRegion;region++) {
                if( mprotect(snapshotrecord->regionsToSnapShot[region].basePtr, snapshotrecord->regionsToSnapShot[region].sizeInPages*sizeof(struct SnapShotPage), PROT_READ ) == -1 ){
+                       perror("mprotect");
                        printf("Failed to mprotect inside of takeSnapShot\n");
                        exit(-1);
                }               
@@ -248,6 +261,7 @@ void rollBack( snapshot_id theID ){
        std::map< void *, bool, std::less< void * >, MyAlloc< std::pair< const void *, bool > > > duplicateMap;
        for(unsigned int region=0; region<snapshotrecord->lastRegion;region++) {
                if( mprotect(snapshotrecord->regionsToSnapShot[region].basePtr, snapshotrecord->regionsToSnapShot[region].sizeInPages*sizeof(struct SnapShotPage), PROT_READ | PROT_WRITE ) == -1 ){
+                       perror("mprotect");
                        printf("Failed to mprotect inside of takeSnapShot\n");
                        exit(-1);
                }