changeset 60:21089dc88985

Removed some redundancy in my code. A new function cacheAdd will specify that some data is in the cache but will not actually move the data there.
author Jonathan Pevarnek <pevarnj@gmail.com>
date Thu, 14 Apr 2011 00:23:28 -0400
parents f9aba6b4a9cf
children 5e1d4b26c2ef
files src/fs.c
diffstat 1 files changed, 21 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/fs.c	Wed Apr 13 23:55:59 2011 -0400
+++ b/src/fs.c	Thu Apr 14 00:23:28 2011 -0400
@@ -2,9 +2,10 @@
 #include <std.h>
 
 static u32 getCacheLoc(u32 blkno);
+static u32 cacheAdd(u32 blkno);
+static int setBlockAt(u32 block, short isBlock);
 static void* readFSBlock(u32 blkno);
 static int writeFSBlock(u32 blkno, void *ptr);
-static int setBlockAt(u32 block, short isBlock);
 static int sync(u32 blkno);
 
 static u32 DevID;
@@ -33,6 +34,7 @@
 	Superblock *sb;
 	sb = readFSBlock(1);
 	if(!sb) return -1;
+	if(sb->magic != 0x42420374) return -1;
 	Inode *rootINode;
 	u32 rootLoc = sb->root_inode;
 	rootINode = readFSBlock(rootLoc);
@@ -176,22 +178,30 @@
 	return CacheCap;
 }
 
+//PRECON:   block blkno is not already in the cache
+//sets aside space in the cache for some item.  NOTE: this does not read the
+//current contents of the disk to the cache
+static u32 cacheAdd(u32 blkno)
+{
+	u32 ret = CacheNext;
+	CacheLocs[CacheNext] = blkno;
+	if(CacheSize != CacheCap) CacheSize += 1;
+	if(CacheNext != CacheCap - 1) CacheNext += 1;
+	else CacheNext = 0;
+	return ret;
+}
+
 //returns a pointer pointing to a location where the data from blkno will be
 //stored
 static void* readFSBlock(u32 blkno)
 {
 	u32 cacheLoc = getCacheLoc(blkno); //Do not reload if in cache
 	if(cacheLoc == CacheCap) { //It was not found in the cache
-		cacheLoc = CacheNext; //CacheNext is the next place a value should be placed
-		FSBlk *nextFSB = FS_Cache + CacheNext;
+		cacheLoc = cacheAdd(blkno);
+		FSBlk *nextFSB = FS_Cache + cacheLoc;
 		DskBlk *nextDSKB = (DskBlk *) nextFSB; //block to put data in
 		if(fba_read_blk(DevID, blkno*2, nextDSKB)) return NULL;
 		if(fba_read_blk(DevID, blkno*2 + 1, nextDSKB + 1)) return NULL;
-
-		CacheLocs[cacheLoc] = blkno;
-		if(CacheSize != CacheCap) CacheSize += 1;
-		if(CacheNext != CacheCap - 1) CacheNext += 1;
-		else CacheNext = 0;
 	}
 	return (void *)(FS_Cache + cacheLoc);
 }
@@ -200,17 +210,13 @@
 static int writeFSBlock(u32 blkno, void *ptr)
 {
 	u32 cacheLoc = getCacheLoc(blkno);
-	if(cacheLoc == CacheCap) { //if the block is not in the cache, set aside room
-		cacheLoc = CacheNext;
-		CacheLocs[cacheLoc] = blkno;
-		if(CacheSize != CacheCap) CacheSize += 1;
-		if(CacheNext != CacheCap - 1) CacheNext += 1;
-		else CacheNext = 0;
-	}
+	if(cacheLoc == CacheCap)
+		cacheLoc = cacheAdd(blkno); //need room in cache for data
 	FSBlk *nextFSB = FS_Cache + cacheLoc;
 	memcpy(nextFSB, ptr, FSBLKSIZE); //copy the data to the cache
 	DskBlk *nextDSKB = (DskBlk *) nextFSB; //where to get the data from
 	if(fba_write_blk(DevID, blkno*2, nextDSKB)) return -1; //load from the cache to the disk
+	
 	if(fba_write_blk(DevID, blkno*2 + 1, nextDSKB + 1)) return -1;
 	if(!isBlockAt(blkno))
 		if(setBlockAt(blkno, 1)) return -1;