changeset 68:406b6e8ec54f

Removed a lot of dynamic memory usage I had a lot of usage of dynamic memory that was not actually needed, that is all gone now (making my life much easier)
author Jonathan Pevarnek <pevarnj@gmail.com>
date Fri, 22 Apr 2011 00:19:18 -0400
parents 9816d3510467
children 19000e354e36
files src/fs.c
diffstat 1 files changed, 34 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/src/fs.c	Thu Apr 21 23:58:13 2011 -0400
+++ b/src/fs.c	Fri Apr 22 00:19:18 2011 -0400
@@ -81,32 +81,25 @@
 {
 	int ret = 0;
 	int i;
-	Inode *inPtr = malloc(sizeof(Inode));
-	if(!inPtr) return -1;
-	if(getBlockData(fid, inPtr)) {
+	Inode in;
+	if(getBlockData(fid, &in)) {
 		ret = -1;
 		goto end;
 	}
 
-	u32 size = inPtr->size;
-	for(i = 0; i < inPtr->nblocks; i++) {
-		Inode *filePtr = readFSBlock(inPtr->blocks[i]);
+	u32 size = in.size;
+	for(i = 0; i < in.nblocks; i++) {
+		Inode *filePtr = readFSBlock(in.blocks[i]);
 		if(!filePtr) {
 			ret = -1;
 			goto end;
 		}
 		memcpy(ptr, filePtr, Min_u32(size, FSBLKSIZE));
-		sync(inPtr->blocks[i]); //update the content of the actual file on disk
 		ptr += FSBLKSIZE;
 		size -= FSBLKSIZE;
 	}
-	if(writeFSBlock(fid, inPtr)) {
-		ret = -1;
-		goto end;
-	}
 
 end:
-	free(inPtr);
 	return ret;
 }
 
@@ -114,36 +107,38 @@
 {
 	int ret = 0;
 	unsigned int i;
-	if(setFileSize(fid, size)) return -1; //TODO efficiency (talk to Jeff)
-	Inode *inPtr = malloc(sizeof(Inode));
-	if(!inPtr) return -1;
-	if(getBlockData(fid, inPtr)) {
+	if(setFileSize(fid, size)) { //TODO efficiency (talk to Jeff)
+		ret = -1;
+		goto end; 
+	}
+	Inode in;
+	if(getBlockData(fid, &in)) {
 		ret = -1;
 		goto end;
 	}
 
-	for(i = 0; i < inPtr->nblocks; i++) {
-		Inode *filePtr = readFSBlock(inPtr->blocks[i]);
+	for(i = 0; i < in.nblocks; i++) {
+		Inode *filePtr = readFSBlock(in.blocks[i]);
 		if(!filePtr) {
 			ret = -1;
 			goto end;
 		}
 		memcpy(filePtr, ptr, Min_u32(size, FSBLKSIZE));
+		sync(in.blocks[i]);
 		ptr += FSBLKSIZE;
 		size -= FSBLKSIZE;
 	}
 
-	if(setTod(inPtr)) { //sadly, this needs to be updated...
+	if(setTod(&in)) { //sadly, this needs to be updated...
 		ret = -1;
 		goto end;
 	}
-	if(writeFSBlock(fid, inPtr)) {
+	if(writeFSBlock(fid, &in)) {
 		ret = -1;
 		goto end;
 	}
 
 end:
-	free(inPtr);
 	return ret;
 }
 
@@ -155,46 +150,46 @@
 	int ret = 0;
 	unsigned int i, j;
 	u16 neededBlocks = (size + FSBLKSIZE - 1)/FSBLKSIZE;
-	if(neededBlocks > MAXBLOCKS) goto final;
-	Inode *inPtr = malloc(sizeof(Inode));
-	if(!inPtr) goto final;
-	if(getBlockData(fid, inPtr)) {
+	if(neededBlocks > MAXBLOCKS) {
+		ret = -1;
+		goto end;
+	}
+	Inode in;
+	if(getBlockData(fid, &in)) {
 		ret = -1;
 		goto end;
 	}
 
-	if(inPtr->nblocks >= neededBlocks) { //get rid of some blocks
-		for(i = neededBlocks; i < inPtr->nblocks; i++) { //if it is equal, will skip this loop
-			if(setBlockAt(inPtr->blocks[i], 0)) {
+	if(in.nblocks >= neededBlocks) { //get rid of some blocks
+		for(i = neededBlocks; i < in.nblocks; i++) { //if it is equal, will skip this loop
+			if(setBlockAt(in.blocks[i], 0)) {
 				ret = -1;
 				goto end;
 			}
 		}
 	} else { //there are fewer blocks
-		for(i = inPtr->nblocks; i < neededBlocks; i++) {
-			inPtr->blocks[i] = 0;
-			for(j = inPtr->blocks[i - 1]; inPtr->blocks[i] == 0; j++)
-				if(!isBlockAt(j)) inPtr->blocks[i] = j;
-			if(setBlockAt(inPtr->blocks[i], 1)) {
+		for(i = in.nblocks; i < neededBlocks; i++) {
+			in.blocks[i] = 0;
+			for(j = in.blocks[i - 1]; in.blocks[i] == 0; j++)
+				if(!isBlockAt(j)) in.blocks[i] = j;
+			if(setBlockAt(in.blocks[i], 1)) {
 				ret = -1;
 				goto end;
 			}
 		}
 	}
-	inPtr->nblocks = neededBlocks;
-	inPtr->size = size;
-	if(setTod(inPtr)) {
+	in.nblocks = neededBlocks;
+	in.size = size;
+	if(setTod(&in)) {
 		ret = -1;
 		goto end;
 	}
-	if(writeFSBlock(fid, inPtr)) {
+	if(writeFSBlock(fid, &in)) {
 		ret = -1;
 		goto end;
 	}
 
 end:
-	free(inPtr);
-final:
 	return ret;
 }