diff runtime/context.c @ 12:31f8182f3433

Finished fib test and did some small work on the c backend
author Mike Pavone <pavone@retrodev.com>
date Mon, 25 May 2009 23:34:36 -0400
parents 8d74ef7fa357
children 640f541e9116
line wrap: on
line diff
--- a/runtime/context.c	Tue May 19 23:29:55 2009 -0400
+++ b/runtime/context.c	Mon May 25 23:34:36 2009 -0400
@@ -31,7 +31,10 @@
 	while(current && STACK_CHUNK_SIZE - current->used < size)
 	{
 		if(!current->next)
+		{
 			current->next = new_stack();
+			current->next->prev = current;
+		}
 		current = current->next;
 	}
 	if(!current)
@@ -53,9 +56,14 @@
 void free_stack(context * ct, void * data)
 {
 	char * cdata = data;
-	if(cdata < ct->current_stack->data || cdata >= ct->current_stack->data+STACK_CHUNK_SIZE) {
-		fputs("Tried to free stack data from outside of current stack chunk!", stderr);
-		exit(-1);
+	while(cdata < ct->current_stack->data || cdata >= ct->current_stack->data+STACK_CHUNK_SIZE) {
+		if(ct->current_stack == ct->stack_begin)
+		{
+			fprintf(stderr, "Attempt to free memory at %X using free_stack, but %X doesn't appear to be stack allocated\n", data, data);
+			exit(-1);
+		}
+		ct->current_stack->used = 0;
+		ct->current_stack = ct->current_stack->prev;
 	}
 	ct->current_stack->used = cdata-ct->current_stack->data;
 	if(!ct->current_stack->used && ct->current_stack->prev)