diff runtime/context.c @ 67:d1569087348f

Some small optimizations
author Mike Pavone <pavone@retrodev.com>
date Mon, 07 Jun 2010 01:15:16 -0400
parents d4b44ae2e34a
children a68e6828d896
line wrap: on
line diff
--- a/runtime/context.c	Sun Jun 06 20:29:10 2010 -0400
+++ b/runtime/context.c	Mon Jun 07 01:15:16 2010 -0400
@@ -9,7 +9,7 @@
 	stackchunk * st = malloc(sizeof(stackchunk));
 	st->prev = NULL;
 	st->next = NULL;
-	st->used = 0;
+	st->free_space = st->data;
 	return st;
 }
 
@@ -37,29 +37,22 @@
 {
 	void * ret;
 	stackchunk * current = ct->current_stack;
-	if(size > STACK_CHUNK_SIZE)
-	{
-		fprintf(stderr, "%d is bigger than stack chunk size of %d\n", size, STACK_CHUNK_SIZE);
-		return NULL;
-	}
-	while(current && STACK_CHUNK_SIZE - current->used < size)
+	char * next_free = current->free_space + size;
+	if (next_free <= (current->data + STACK_CHUNK_SIZE))
 	{
-		if(!current->next)
-		{
-			current->next = new_stack();
-			current->next->prev = current;
-		}
-		current = current->next;
+		ret = current->free_space;
+		current->free_space = next_free;
+		return ret;
 	}
-	if(!current)
+	if (!current->next)
 	{
-		fprintf(stderr, "Failed to allocate stack chunk");
-		return NULL;
+		current->next = new_stack();
+		current->next->prev = current;
 	}
+	current = current->next;
 	ct->current_stack = current;
-	ret = current->data + current->used;
-	current->used += size;
-	return ret;
+	current->free_space = current->data + size;
+	return current->data;
 }
 
 calldata * alloc_cdata(context * ct, calldata * lastframe, uint32_t num_params)
@@ -76,17 +69,17 @@
 void free_stack(context * ct, void * data)
 {
 	char * cdata = data;
-	while(cdata < ct->current_stack->data || cdata >= ct->current_stack->data+STACK_CHUNK_SIZE) {
+	while(cdata < ct->current_stack->data || cdata >= ct->current_stack->free_space)
+	{
 		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)
+	ct->current_stack->free_space = data;
+	if(ct->current_stack->free_space == ct->current_stack->data && ct->current_stack->prev)
 		ct->current_stack = ct->current_stack->prev;
 }