changeset 67:d1569087348f

Some small optimizations
author Mike Pavone <pavone@retrodev.com>
date Mon, 07 Jun 2010 01:15:16 -0400
parents d4b44ae2e34a
children 38d9cd036d49
files runtime/context.c runtime/context.h runtime/fixed_alloc.c runtime/fixed_alloc.h
diffstat 4 files changed, 28 insertions(+), 35 deletions(-) [+]
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;
 }
 
--- a/runtime/context.h	Sun Jun 06 20:29:10 2010 -0400
+++ b/runtime/context.h	Mon Jun 07 01:15:16 2010 -0400
@@ -5,12 +5,12 @@
 #include "plat_types.h"
 #include "func.h"
 
-#define STACK_CHUNK_SIZE 4096-(sizeof(struct stackchunk *)*2+sizeof(uint32_t))
+#define STACK_CHUNK_SIZE 4096-(sizeof(struct stackchunk *)*2+sizeof(char *))
 
 typedef struct stackchunk {
 	struct    stackchunk * next;
 	struct    stackchunk * prev;
-	uint32_t  used;
+	char      *free_space;
 	char      data[STACK_CHUNK_SIZE];
 } stackchunk;
 
--- a/runtime/fixed_alloc.c	Sun Jun 06 20:29:10 2010 -0400
+++ b/runtime/fixed_alloc.c	Mon Jun 07 01:15:16 2010 -0400
@@ -1,5 +1,5 @@
 #include "fixed_alloc.h"
-#include <stdlib.h>
+#include <stdlib.h>
 #include <string.h>
 
 uint16_t max_free[(MAX_SIZE-MIN_SIZE)/STRIDE];
@@ -36,13 +36,13 @@
 		if(block)
 		{
 			--manager->freecount;
-			manager->freelist = block->next;
-			memset(block, 0xCD, BLOCK_SIZE);
+			manager->freelist = block->next;
+			//memset(block, 0xCD, BLOCK_SIZE);
 		}
 		else
 		{
-			block = block_alloc(BLOCK_SIZE);
-			memset(block, 0xAB, BLOCK_SIZE);
+			block = block_alloc(BLOCK_SIZE);
+			//memset(block, 0xAB, BLOCK_SIZE);
 		}
 		manager->inuse[bucket] = block;
 		block->next = NULL;
@@ -94,8 +94,8 @@
 		return;
 	}
 	//puts("ffree");
-	size = ADJUST_SIZE(size);
-	memset(ptr, 0xEF, size);
+	size = ADJUST_SIZE(size);
+	//memset(ptr, 0xEF, size);
 	block = GET_BLOCK(ptr);
 	i = (((((char *)block) + BLOCK_SIZE) - ((char *)ptr))/size)-1;
 	bit = i & 0x7;
@@ -121,9 +121,9 @@
 			manager->inuse[bucket] = block->next;
 		if(block->next)
 			block->next->last = block->last;
-		if(manager->freecount == MAX_FREE)
+		if(manager->freecount == MAX_FREE)
 		{
-			block_free(block, BLOCK_SIZE);
+			block_free(block, BLOCK_SIZE);
 		}
 		else
 		{
--- a/runtime/fixed_alloc.h	Sun Jun 06 20:29:10 2010 -0400
+++ b/runtime/fixed_alloc.h	Mon Jun 07 01:15:16 2010 -0400
@@ -10,7 +10,7 @@
 #define MAX_SIZE   (BLOCK_SIZE/32)
 #define STRIDE     (BLOCK_SIZE/1024)
 #define MIN_SIZE   (BLOCK_SIZE/1024)
-#define MAX_FREE   16
+#define MAX_FREE   64
 
 #define ADJUST_SIZE(requested) (((requested)+(STRIDE-1)) & (~(STRIDE-1)))