From: Brian Norris <banorris@uci.edu>
Date: Thu, 3 Jan 2013 00:41:35 +0000 (-0800)
Subject: snapshot: change 'struct SnapShotPage' to a simple typedef
X-Git-Tag: oopsla2013~398
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=5f71c5e271a6fd27f2300b87b6bb45e122348320;p=model-checker.git

snapshot: change 'struct SnapShotPage' to a simple typedef

The struct SnapShotPage was being used improperly; it was accessed as
simply an array of data, instead of explicitly accessing its member
variable (which itself was an array). So just make this more
straightforward as a typedef'd array.

This also corrects a pointer cast to use (void *) when appropriate.
---

diff --git a/snapshot.cc b/snapshot.cc
index 5ac9b46..5cb920b 100644
--- a/snapshot.cc
+++ b/snapshot.cc
@@ -66,9 +66,9 @@ static void initSnapShotRecord(unsigned int numbackingpages, unsigned int numsna
 {
 	snapshotrecord = (struct SnapShot *)model_malloc(sizeof(struct SnapShot));
 	snapshotrecord->regionsToSnapShot = (struct MemoryRegion *)model_malloc(sizeof(struct MemoryRegion) * nummemoryregions);
-	snapshotrecord->backingStoreBasePtr = (struct SnapShotPage *)model_malloc(sizeof(struct SnapShotPage) * (numbackingpages + 1));
+	snapshotrecord->backingStoreBasePtr = (void *)model_malloc(sizeof(snapshot_page_t) * (numbackingpages + 1));
 	//Page align the backingstorepages
-	snapshotrecord->backingStore = (struct SnapShotPage *)PageAlignAddressUpward(snapshotrecord->backingStoreBasePtr);
+	snapshotrecord->backingStore = (snapshot_page_t *)PageAlignAddressUpward(snapshotrecord->backingStoreBasePtr);
 	snapshotrecord->backingRecords = (struct BackingPageRecord *)model_malloc(sizeof(struct BackingPageRecord) * numbackingpages);
 	snapshotrecord->snapShots = (struct SnapShotRecord *)model_malloc(sizeof(struct SnapShotRecord) * numsnapshots);
 	snapshotrecord->lastSnapShot = 0;
@@ -100,11 +100,11 @@ static void HandlePF(int sig, siginfo_t *si, void *unused)
 	}
 
 	//copy page
-	memcpy(&(snapshotrecord->backingStore[backingpage]), addr, sizeof(struct SnapShotPage));
+	memcpy(&(snapshotrecord->backingStore[backingpage]), addr, sizeof(snapshot_page_t));
 	//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)) {
+	if (mprotect(addr, sizeof(snapshot_page_t), PROT_READ | PROT_WRITE)) {
 		perror("mprotect");
 		// Handle error by quitting?
 	}
@@ -270,7 +270,7 @@ snapshot_id takeSnapshot()
 {
 #if USE_MPROTECT_SNAPSHOT
 	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) {
+		if (mprotect(snapshotrecord->regionsToSnapShot[region].basePtr, snapshotrecord->regionsToSnapShot[region].sizeInPages * sizeof(snapshot_page_t), PROT_READ) == -1) {
 			perror("mprotect");
 			model_print("Failed to mprotect inside of takeSnapShot\n");
 			exit(EXIT_FAILURE);
@@ -299,7 +299,7 @@ void rollBack(snapshot_id theID)
 #if USE_MPROTECT_SNAPSHOT == 2
 	if (snapshotrecord->lastSnapShot == (theID + 1)) {
 		for (unsigned int page = snapshotrecord->snapShots[theID].firstBackingPage; page < snapshotrecord->lastBackingPage; page++) {
-			memcpy(snapshotrecord->backingRecords[page].basePtrOfPage, &snapshotrecord->backingStore[page], sizeof(struct SnapShotPage));
+			memcpy(snapshotrecord->backingRecords[page].basePtrOfPage, &snapshotrecord->backingStore[page], sizeof(snapshot_page_t));
 		}
 		return;
 	}
@@ -308,7 +308,7 @@ void rollBack(snapshot_id theID)
 #if USE_MPROTECT_SNAPSHOT
 	HashTable< void *, bool, uintptr_t, 4, model_malloc, model_calloc, model_free> 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) {
+		if (mprotect(snapshotrecord->regionsToSnapShot[region].basePtr, snapshotrecord->regionsToSnapShot[region].sizeInPages * sizeof(snapshot_page_t), PROT_READ | PROT_WRITE) == -1) {
 			perror("mprotect");
 			model_print("Failed to mprotect inside of takeSnapShot\n");
 			exit(EXIT_FAILURE);
@@ -317,7 +317,7 @@ void rollBack(snapshot_id theID)
 	for (unsigned int page = snapshotrecord->snapShots[theID].firstBackingPage; page < snapshotrecord->lastBackingPage; page++) {
 		if (!duplicateMap.contains(snapshotrecord->backingRecords[page].basePtrOfPage)) {
 			duplicateMap.put(snapshotrecord->backingRecords[page].basePtrOfPage, true);
-			memcpy(snapshotrecord->backingRecords[page].basePtrOfPage, &snapshotrecord->backingStore[page], sizeof(struct SnapShotPage));
+			memcpy(snapshotrecord->backingRecords[page].basePtrOfPage, &snapshotrecord->backingStore[page], sizeof(snapshot_page_t));
 		}
 	}
 	snapshotrecord->lastSnapShot = theID;
diff --git a/snapshotimp.h b/snapshotimp.h
index fbff35f..3999477 100644
--- a/snapshotimp.h
+++ b/snapshotimp.h
@@ -18,10 +18,8 @@ struct SnapShotRecord {
 	unsigned int firstBackingPage;
 };
 
-//Backing store page struct
-struct SnapShotPage {
-	char data[PAGESIZE];
-};
+/** @brief Backing store page */
+typedef unsigned char snapshot_page_t[PAGESIZE];
 
 //List the base address of the corresponding page in the backing store so we know where to copy it to
 struct BackingPageRecord {
@@ -37,7 +35,7 @@ struct MemoryRegion {
 //Primary struct for snapshotting system....
 struct SnapShot {
 	struct MemoryRegion *regionsToSnapShot; //This pointer references an array of memory regions to snapshot
-	struct SnapShotPage *backingStore; //This pointer references an array of snapshotpage's that form the backing store
+	snapshot_page_t *backingStore; //This pointer references an array of snapshotpage's that form the backing store
 	void *backingStoreBasePtr; //This pointer references an array of snapshotpage's that form the backing store
 	struct BackingPageRecord *backingRecords; //This pointer references an array of backingpagerecord's (same number of elements as backingstore
 	struct SnapShotRecord *snapShots; //This pointer references the snapshot array