changeset 21:7c2adb65ceac

Started working on dynamic memory I have a slightly adapted version of dynamic memory allocation from the H&R book in the program now, I will soon be adding the free function. I fixed sPrint so it will work with large buffers.
author Jonathan Pevarnek <pevarnj@gmail.com>
date Tue, 15 Mar 2011 22:55:36 -0400
parents def4b04c99f9
children 3fb0ec050ff3
files Makefile include/std.h include/system.h src/init.c src/std.c
diffstat 5 files changed, 87 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Mon Mar 14 15:06:11 2011 -0400
+++ b/Makefile	Tue Mar 15 22:55:36 2011 -0400
@@ -12,7 +12,7 @@
 
 BINS=sarpn
 
-sarpn_OBJS=src/init.o src/io.o src/std.o src/stack.o src/operations.o src/math.o
+sarpn_OBJS=src/init.o arch/io.o src/std.o src/stack.o src/operations.o src/math.o
 
 .PHONY: all build clean tags
 
@@ -27,6 +27,7 @@
 
 tags:
 	cscope -R -b
+	ctags -R
 
 sarpn: $(sarpn_OBJS)
 	$(LD) $(LDFLAGS) -T scripts/linker.script -o $@ $^
--- a/include/std.h	Mon Mar 14 15:06:11 2011 -0400
+++ b/include/std.h	Tue Mar 15 22:55:36 2011 -0400
@@ -1,6 +1,8 @@
 #ifndef __STD_H
 #define __STD_H
 
+typedef unsigned int size_t;
+
 char* ftoa(double x, char *a, unsigned int prec);
 char* itoa(int n, char *a);
 int atoi(char *a);
@@ -15,4 +17,18 @@
 
 char* append(char *dest, char *src);
 
+//MALLOC CODE
+
+union block {
+	struct {
+		union block *next;
+		unsigned int size;
+	} s;
+	double foo;
+};
+typedef union block Block;
+
+void* malloc(size_t size);
+//void free(void *ptr);
+
 #endif
--- a/include/system.h	Mon Mar 14 15:06:11 2011 -0400
+++ b/include/system.h	Tue Mar 15 22:55:36 2011 -0400
@@ -2,6 +2,7 @@
 #define __SYSTEM_H
 
 #define NULL	((void*) 0)
+#define CON_LEN 132
 
 typedef unsigned long long u64;
 typedef signed long long s64;
--- a/src/init.c	Mon Mar 14 15:06:11 2011 -0400
+++ b/src/init.c	Tue Mar 15 22:55:36 2011 -0400
@@ -14,12 +14,37 @@
 		char c[10];
 		itoa(foo, c);
 		sPrint(c);
-		sPrint("\n");
+		sPrint(" ");
+		if(!((i + 1)%20)) sPrint("\n");
 	}
 }
 
 void start(u64 __memsize)
 {
+  int i, n;
+  char *buffer;
+	char input[30];
+
+	while(1) {
+		sPrint("How long do you want the string? ");
+		n = atoi(sGet(input, 30));
+
+		buffer = (char*) malloc(n + 2);
+		if (!buffer) { //allocation failed, too large
+			sPrint("ERROR\n");
+			break;
+		}
+
+		for (i = 0; i < n; i++)
+			buffer[i]='a'; //I need to make rand...
+		buffer[n]='\0';
+
+//		dumpBuffer(buffer, n);
+
+		sPrint(append(itoa((int)buffer, input), "\n"));
+		sPrint(append(buffer, "\n"));
+	}
+/*
 	struct Stack theStack;
 	struct Stack *stack = &theStack;
 	initStack(stack);
@@ -34,7 +59,7 @@
 			push(stack, atof(input));
 		}
 	}
-
+*/
 	for(;;)
 		;
 }
--- a/src/std.c	Mon Mar 14 15:06:11 2011 -0400
+++ b/src/std.c	Tue Mar 15 22:55:36 2011 -0400
@@ -86,7 +86,10 @@
 {
 	char *b = a;
 	while(*b && *++b);
-	putline(a, b - a);
+	do {
+		putline(a, (b - a > CON_LEN)?CON_LEN:(b - a));
+		a += CON_LEN;
+	} while(a < b);
 }
 
 int strcmp(const char *a, const char *b)
@@ -122,3 +125,40 @@
 	while((*dest++ = *src++));
 	return ret;
 }
+
+static Block *allocp = NULL; //the location of the last item allocated by malloc
+
+void* malloc(size_t size)
+{
+	unsigned int nBlocks = 1 + (size + sizeof(Block) - 1)/sizeof(Block);
+	//We are allocating a number of blocks equal to the number needed rounded up
+	//plus one for the actual info block
+	Block *cur, *prev;
+	if(allocp == NULL) {
+		allocp = (void*)0x200000; //this is where memory should start
+		allocp->s.next = allocp;
+		allocp->s.size = (0x1400000/sizeof(Block)); //I am going to limit the heap to 20MB for now
+	}
+	prev = allocp;
+	for(cur = prev->s.next;; ) {
+		if(cur->s.size >= nBlocks) { //this section is pretty much exactly copied from K&R
+			if(cur->s.size == nBlocks) {
+				prev->s.next = cur->s.next;
+			} else {
+				cur->s.size -= nBlocks;
+				cur += cur->s.size;
+				cur->s.size = nBlocks;
+			}
+			allocp = prev;
+			return (void*)(cur + 1);
+		} else if(cur == allocp) { //We went back to the start...
+			return NULL;
+		}
+	}
+}
+
+/*
+void free(void *ptr)
+{
+}
+*/