Mercurial > repos > rhope
comparison 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 |
comparison
equal
deleted
inserted
replaced
7:d61550e2c001 | 8:8d74ef7fa357 |
---|---|
1 #include "context.h" | |
2 #include "object.h" | |
3 #include <stdlib.h> | |
4 #include <stddef.h> | |
5 #include <stdio.h> | |
6 | |
7 stackchunk * new_stack() | |
8 { | |
9 stackchunk * st = malloc(sizeof(stackchunk)); | |
10 st->prev = NULL; | |
11 st->next = NULL; | |
12 st->used = 0; | |
13 return st; | |
14 } | |
15 | |
16 context * new_context() | |
17 { | |
18 context * c = malloc(sizeof(context)); | |
19 c->stack_begin = new_stack(); | |
20 c->current_stack = c->stack_begin; | |
21 c->unwind = NULL; | |
22 return c; | |
23 } | |
24 | |
25 void * alloc_stack(context * ct, uint32_t size) | |
26 { | |
27 void * ret; | |
28 stackchunk * current = ct->current_stack; | |
29 if(size > STACK_CHUNK_SIZE) | |
30 return NULL; | |
31 while(current && STACK_CHUNK_SIZE - current->used < size) | |
32 { | |
33 if(!current->next) | |
34 current->next = new_stack(); | |
35 current = current->next; | |
36 } | |
37 if(!current) | |
38 return NULL; | |
39 ct->current_stack = current; | |
40 ret = current->data + current->used; | |
41 current->used += size; | |
42 return ret; | |
43 } | |
44 | |
45 calldata * alloc_cdata(context * ct, uint32_t num_params) | |
46 { | |
47 calldata * out = alloc_stack(ct, sizeof(calldata)+(num_params-1)*sizeof(object *)); | |
48 if(out) | |
49 out->ct = ct; | |
50 return out; | |
51 } | |
52 | |
53 void free_stack(context * ct, void * data) | |
54 { | |
55 char * cdata = data; | |
56 if(cdata < ct->current_stack->data || cdata >= ct->current_stack->data+STACK_CHUNK_SIZE) { | |
57 fputs("Tried to free stack data from outside of current stack chunk!", stderr); | |
58 exit(-1); | |
59 } | |
60 ct->current_stack->used = cdata-ct->current_stack->data; | |
61 if(!ct->current_stack->used && ct->current_stack->prev) | |
62 ct->current_stack = ct->current_stack->prev; | |
63 } | |
64 |