Mercurial > repos > rhope
diff runtime/context.c @ 8:8d74ef7fa357
Improved helper macros and added separate Rhope stack in runtime
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 13 May 2009 23:37:19 -0400 |
parents | |
children | 31f8182f3433 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/runtime/context.c Wed May 13 23:37:19 2009 -0400 @@ -0,0 +1,64 @@ +#include "context.h" +#include "object.h" +#include <stdlib.h> +#include <stddef.h> +#include <stdio.h> + +stackchunk * new_stack() +{ + stackchunk * st = malloc(sizeof(stackchunk)); + st->prev = NULL; + st->next = NULL; + st->used = 0; + return st; +} + +context * new_context() +{ + context * c = malloc(sizeof(context)); + c->stack_begin = new_stack(); + c->current_stack = c->stack_begin; + c->unwind = NULL; + return c; +} + +void * alloc_stack(context * ct, uint32_t size) +{ + void * ret; + stackchunk * current = ct->current_stack; + if(size > STACK_CHUNK_SIZE) + return NULL; + while(current && STACK_CHUNK_SIZE - current->used < size) + { + if(!current->next) + current->next = new_stack(); + current = current->next; + } + if(!current) + return NULL; + ct->current_stack = current; + ret = current->data + current->used; + current->used += size; + return ret; +} + +calldata * alloc_cdata(context * ct, uint32_t num_params) +{ + calldata * out = alloc_stack(ct, sizeof(calldata)+(num_params-1)*sizeof(object *)); + if(out) + out->ct = ct; + return out; +} + +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); + } + ct->current_stack->used = cdata-ct->current_stack->data; + if(!ct->current_stack->used && ct->current_stack->prev) + ct->current_stack = ct->current_stack->prev; +} +