changeset 26:c1ad124f2aaf

Re-did errors for pop, added some nice testing for dynamic memory I now have a decent test program for dynamic memory implemented as a separate program (dynamic.c) The stack now stores its current size, this is used in operations.c to check the size Pop should now also be called with a pointer to an eltType. Description of behavior is in stack.h
author Jonathan Pevarnek <pevarnj@gmail.com>
date Sun, 20 Mar 2011 14:17:24 -0400
parents 2b19746a4e97
children 1a070e843bf6
files Makefile include/stack.h include/std.h src/dynamic.c src/operations.c src/stack.c src/std.c
diffstat 7 files changed, 123 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Wed Mar 16 17:29:25 2011 -0400
+++ b/Makefile	Sun Mar 20 14:17:24 2011 -0400
@@ -13,7 +13,7 @@
 BINS=sarpn dynamic
 
 sarpn_OBJS=src/sarpn.o arch/io.o src/std.o src/stack.o src/operations.o src/math.o
-dynamic_OBJS=src/dynamic.o arch/io.o src/std.o
+dynamic_OBJS=src/dynamic.o arch/io.o src/std.o src/stack.o
 
 .PHONY: all build clean tags
 
--- a/include/stack.h	Wed Mar 16 17:29:25 2011 -0400
+++ b/include/stack.h	Sun Mar 20 14:17:24 2011 -0400
@@ -11,16 +11,13 @@
 
 struct STACK {
 	StackElt* top;
+	unsigned int size;
 };
 typedef struct STACK Stack;
 
-struct ELTCON { //contains an eltType and an error value
-	eltType val;
-	short error;
-};
-typedef struct ELTCON eltCon;
-
-eltCon pop(Stack *stack);
+short pop(Stack *stack, eltType *value);
+//pop returns 0 on success and -1 on failure.  If the function call is
+//successful, *value will be the value removed from the stack
 void push(Stack *stack, eltType val);
 Stack* stack_init();
 void stack_destroy(Stack *stack);
--- a/include/std.h	Wed Mar 16 17:29:25 2011 -0400
+++ b/include/std.h	Sun Mar 20 14:17:24 2011 -0400
@@ -4,7 +4,7 @@
 typedef unsigned int size_t;
 
 char* ftoa(double x, char *a, unsigned int prec);
-char* itoa(int n, char *a);
+char* itoa(long long n, char *a, unsigned short base);
 int atoi(char *a);
 double atof(char *a);
 
@@ -30,5 +30,7 @@
 void malloc_init(size_t memSize);
 void* malloc(size_t size);
 void free(void *ptr);
+void printHeader(Header* head);
+void printMem();
 
 #endif
--- a/src/dynamic.c	Wed Mar 16 17:29:25 2011 -0400
+++ b/src/dynamic.c	Sun Mar 20 14:17:24 2011 -0400
@@ -12,7 +12,7 @@
 	for(i = 0; i < b; i++) {
 		int foo = (int) a[i];
 		char c[10];
-		itoa(foo, c);
+		itoa(foo, c, 16);
 		sPrint(c);
 		sPrint(" ");
 		if(!((i + 1)%20)) sPrint("\n");
@@ -26,28 +26,46 @@
 	char *buffer;
 	char input[30];
 
-	while(1) {
+	while(0) {
 		sPrint("How long do you want the string? ");
 		n = atoi(sGet(input, 30));
-
 		buffer = (char*) malloc(n);
 		if (!buffer) { //allocation failed, too large
 			sPrint("ERROR\n");
 			break;
 		}
-
 //		size_t i;
 //		for (i = 0; i < n - 1; i++)
 //			buffer[i]='a'; //I need to make rand...
 //		buffer[n - 1]='\0';
-
 //		sPrint(buffer), sPrint("\n");
 //		dumpBuffer(buffer, n);
-
-		sPrint(append(itoa((unsigned long)buffer, input), "\n"));
+		sPrint(append(itoa((unsigned long)buffer, input, 16), "\n"));
 		free(buffer);
 	}
 
+	printMem();
+
+	Stack *stack = stack_init();
+
+	for(n = 0; n < 1000; n++) {
+		push(stack, n);
+	}
+	stack_destroy(stack);
+
+
+	printMem();
+	void *a = malloc(1223);
+	void *b = malloc(352);
+	void *c = malloc(3539);
+	void *d = malloc(325);
+	printMem();
+	free(b);
+	printMem();
+	b = malloc(400);
+	printMem();
+	sPrint("DONE\n");
+
 	for(;;)
-		;
+		sGet(input, 0);
 }
--- a/src/operations.c	Wed Mar 16 17:29:25 2011 -0400
+++ b/src/operations.c	Sun Mar 20 14:17:24 2011 -0400
@@ -7,30 +7,42 @@
 void (*operation[MAXOP])(Stack*) = {op_print, op_add, op_sub, op_mult,
 	op_div, op_pow, op_dup, op_drop, op_log, op_exp, op_swap};
 
+static void error_unk()
+{
+	sPrint("ERROR: CAUSE UNKNOWN\n");
+}
+
+static void error_small()
+{
+	sPrint("ERROR: STACK TOO SMALL\n");
+}
+
 void op_math1(Stack *stack, eltType (*mathop)(eltType))
 {
-	eltCon con = pop(stack);
-	if(con.error) sPrint("ERROR, no values on stack\n");
-	else push(stack, mathop(con.val));
+	if(stack->size >= 1) {
+		eltType val;
+		if(pop(stack, &val)) error_unk();
+		else push(stack, mathop(val));
+	} else error_small();
 }
 
 void op_math2(Stack *stack, eltType (*mathop)(eltType, eltType))
 {
-	eltCon con2 = pop(stack);
-	eltCon con1 = pop(stack);
-	if(con1.error || con2.error) sPrint("ERROR, stack not large enough\n");
-	else push(stack, mathop(con1.val, con2.val));
+	if(stack->size >= 2) {
+		eltType val1, val2;
+		if(pop(stack, &val2) || pop(stack, &val1)) error_unk();
+		else push(stack, mathop(val1, val2));
+	} else error_small();
 }
 
 void op_print(Stack *stack)
 {
-	eltCon con = pop(stack);
-	if(con.error) sPrint("ERROR, no values on stack\n");
-	else {
-		eltType val = con.val;
+	if(stack->size >= 1) {
 		char output[30];
-		sPrint(append(ftoa(val, output, 10), "\n"));
-	}
+		eltType val;
+		if(pop(stack, &val)) error_unk();
+		else sPrint(append(ftoa(val, output, 10), "\n"));
+	} else error_small();
 }
 
 void op_add(Stack *stack)
@@ -60,17 +72,22 @@
 
 void op_dup(Stack *stack)
 {
-	eltCon con = pop(stack);
-	if(con.error) sPrint("ERROR, no values on stack\n");
-	else {
-		push(stack, con.val);
-		push(stack, con.val);
-	}
+	if(stack->size >= 1) {
+		eltType val;
+		if(pop(stack, &val)) error_unk();
+		else {
+			push(stack, val);
+			push(stack, val);
+		}
+	} else error_small();
 }
 
 void op_drop(Stack *stack)
 {
-	pop(stack);
+	if(stack->size >= 1) {
+		eltType val;
+		pop(stack, &val);
+	} else error_small();
 }
 
 void op_log(Stack *stack)
@@ -85,9 +102,12 @@
 
 void op_swap(Stack *stack)
 {
-	eltCon con2 = pop(stack);
-	eltCon con1 = pop(stack);
-	if(con1.error || con2.error) sPrint("ERROR, stack not large enough\n");
-	push(stack, con2.val);
-	push(stack, con1.val);
+	if(stack->size >= 2) {
+		eltType val1, val2;
+		if(pop(stack, &val2) || pop(stack, &val1)) error_unk();
+		else {
+			push(stack, val2);
+			push(stack, val1);
+		}
+	} else error_small();
 }
--- a/src/stack.c	Wed Mar 16 17:29:25 2011 -0400
+++ b/src/stack.c	Sun Mar 20 14:17:24 2011 -0400
@@ -1,19 +1,18 @@
 #include <stack.h>
 #include <std.h>
 
-eltCon pop(Stack *stack)
+short pop(Stack *stack, eltType *value)
 {
-	eltCon ret;
 	if(stack->top == NULL) {
-		ret.error = 1;
+		return -1; //failure
 	} else {
-		ret.error = 0;
-		StackElt *top = stack->top;
-		ret.val = top->elt;
-		stack->top = top->next;
-		free(top);
+		StackElt *tip = stack->top;
+		*value = tip->elt;
+		stack->top = tip->next;
+		free(tip);
+		stack->size--;
+		return 0; //success
 	}
-	return ret;
 }
 
 void push(Stack *stack, eltType val)
@@ -22,6 +21,7 @@
 	new->elt = val;
 	new->next = stack->top;
 	stack->top = new;
+	stack->size++;
 }
 
 Stack* stack_init()
@@ -33,6 +33,8 @@
 
 void stack_destroy(Stack *stack)
 {
-	while(stack->top) pop(stack);
+	eltType *val = malloc(sizeof(eltType));
+	while(stack->top) pop(stack, val);
 	free(stack);
+	free(val);
 }
--- a/src/std.c	Wed Mar 16 17:29:25 2011 -0400
+++ b/src/std.c	Sun Mar 20 14:17:24 2011 -0400
@@ -1,6 +1,6 @@
 #include <std.h>
 
-char* itoa(int n, char *a)
+char* itoa(long long n, char *a, unsigned short base)
 {
 	char *ret = a;
 	if(n < 0) {
@@ -10,8 +10,10 @@
 	char *b = a;
 	if(!n) *b++ = '0';
 	for(; n; b++) {
-		*b = n%10 + '0';
-		n = n/10;
+		int temp = n%base;
+		if(temp < 10) *b = '0' + temp;
+		else *b = 'a' + temp - 10;
+		n = n/base;
 	}
 	*b-- = '\0';
 	for(; a < b; a++, b--) { //reverse
@@ -27,7 +29,7 @@
 	char *ret = a;
 	int n = (int) x; //integer part
 	double d = x - (double) n; //fractional part;
-	itoa(n, a);
+	itoa(n, a, 10);
 	if(prec) { //only do the decimal part if decimal parts were asked for
 		while(*a && *++a); //get to the null character from itoa
 		int i; //counter variable for the for loop
@@ -139,7 +141,7 @@
 	if((sizeof(blockUnit)%sizeof(u64))) {
 		sPrint("WARNING: MEMORY NOT 8-BYTE ALIGNED\n");
 		char foo[40];
-		sPrint(append("MEMORY BLOCK SIZE IS: ", append(itoa(sizeof(Header), foo), "\n")));
+		sPrint(append("MEMORY BLOCK SIZE IS: ", append(itoa(sizeof(Header), foo, 10), "\n")));
 	}
 }
 
@@ -176,7 +178,7 @@
 		if(scan->next < scan && (toFree > scan || toFree < scan->next)) break;
 	toFree->next = scan->next;
 	scan->next = toFree;
-	if(scan + toFree->size == toFree) {
+	if(scan + scan->size == toFree) {
 		scan->size += toFree->size;
 		scan->next = toFree->next;
 		toFree = scan;
@@ -186,3 +188,27 @@
 		toFree->next = toFree->next->next;
 	}
 }
+
+void printHeader(Header* head)
+{
+	char buffer[20];
+	sPrint("Memory address is: ");
+	sPrint(itoa((size_t)head, buffer, 16));
+	sPrint("\n");
+	sPrint("The size of the block is: ");
+	sPrint(itoa(head->size, buffer, 10));
+	sPrint("\n");
+	sPrint("Next memory address is: ");
+	sPrint(itoa((size_t)head->next, buffer, 16));
+	sPrint("\n\n");
+}
+
+void printMem()
+{
+	Header *cur = allocp;
+	do {
+		printHeader(cur);
+		cur = cur->next;
+	} while(cur != allocp);
+	sPrint("PRINTOUT DONE\n\n");
+}