+/** HandlePF is the page fault handler for mprotect based snapshotting
+ * algorithm.
+ */
+static void HandlePF( int sig, siginfo_t *si, void * unused){
+ if( si->si_code == SEGV_MAPERR ){
+ printf("Real Fault at %p\n", si->si_addr);
+ print_trace();
+ exit( EXIT_FAILURE );
+ }
+ void* addr = ReturnPageAlignedAddress(si->si_addr);
+
+ unsigned int backingpage=snapshotrecord->lastBackingPage++; //Could run out of pages...
+ if (backingpage==snapshotrecord->maxBackingPages) {
+ printf("Out of backing pages at %p\n", si->si_addr);
+ exit( EXIT_FAILURE );
+ }
+
+ //copy page
+ memcpy(&(snapshotrecord->backingStore[backingpage]), addr, sizeof(struct SnapShotPage));
+ //remember where to copy page back to
+ snapshotrecord->backingRecords[backingpage].basePtrOfPage=addr;
+ //set protection to read/write
+ if (mprotect( addr, sizeof(struct SnapShotPage), PROT_READ | PROT_WRITE )) {
+ perror("mprotect");
+ // Handle error by quitting?
+ }
+}
+#endif /* USE_MPROTECT_SNAPSHOT */
+
+#if !USE_MPROTECT_SNAPSHOT
+void createSharedMemory(){
+ //step 1. create shared memory.
+ void * memMapBase = mmap( 0, SHARED_MEMORY_DEFAULT + STACK_SIZE_DEFAULT, PROT_READ | PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0 );
+ if( MAP_FAILED == memMapBase )
+ FAILURE("mmap");
+
+ //Setup snapshot record at top of free region
+ snapshotrecord = ( struct SnapShot * )memMapBase;
+ snapshotrecord->mSharedMemoryBase = (void *)((uintptr_t)memMapBase + sizeof(struct SnapShot));
+ snapshotrecord->mStackBase = (void *)((uintptr_t)memMapBase + SHARED_MEMORY_DEFAULT);
+ snapshotrecord->mStackSize = STACK_SIZE_DEFAULT;
+ snapshotrecord->mIDToRollback = -1;
+ snapshotrecord->currSnapShotID = 0;