Mercurial > repos > rhope
comparison 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 |
comparison
equal
deleted
inserted
replaced
66:d4b44ae2e34a | 67:d1569087348f |
---|---|
7 stackchunk * new_stack() | 7 stackchunk * new_stack() |
8 { | 8 { |
9 stackchunk * st = malloc(sizeof(stackchunk)); | 9 stackchunk * st = malloc(sizeof(stackchunk)); |
10 st->prev = NULL; | 10 st->prev = NULL; |
11 st->next = NULL; | 11 st->next = NULL; |
12 st->used = 0; | 12 st->free_space = st->data; |
13 return st; | 13 return st; |
14 } | 14 } |
15 | 15 |
16 context * new_context() | 16 context * new_context() |
17 { | 17 { |
35 | 35 |
36 void * alloc_stack(context * ct, uint32_t size) | 36 void * alloc_stack(context * ct, uint32_t size) |
37 { | 37 { |
38 void * ret; | 38 void * ret; |
39 stackchunk * current = ct->current_stack; | 39 stackchunk * current = ct->current_stack; |
40 if(size > STACK_CHUNK_SIZE) | 40 char * next_free = current->free_space + size; |
41 if (next_free <= (current->data + STACK_CHUNK_SIZE)) | |
41 { | 42 { |
42 fprintf(stderr, "%d is bigger than stack chunk size of %d\n", size, STACK_CHUNK_SIZE); | 43 ret = current->free_space; |
43 return NULL; | 44 current->free_space = next_free; |
45 return ret; | |
44 } | 46 } |
45 while(current && STACK_CHUNK_SIZE - current->used < size) | 47 if (!current->next) |
46 { | 48 { |
47 if(!current->next) | 49 current->next = new_stack(); |
48 { | 50 current->next->prev = current; |
49 current->next = new_stack(); | |
50 current->next->prev = current; | |
51 } | |
52 current = current->next; | |
53 } | 51 } |
54 if(!current) | 52 current = current->next; |
55 { | |
56 fprintf(stderr, "Failed to allocate stack chunk"); | |
57 return NULL; | |
58 } | |
59 ct->current_stack = current; | 53 ct->current_stack = current; |
60 ret = current->data + current->used; | 54 current->free_space = current->data + size; |
61 current->used += size; | 55 return current->data; |
62 return ret; | |
63 } | 56 } |
64 | 57 |
65 calldata * alloc_cdata(context * ct, calldata * lastframe, uint32_t num_params) | 58 calldata * alloc_cdata(context * ct, calldata * lastframe, uint32_t num_params) |
66 { | 59 { |
67 //Make sure we have enough space for at least 32 return values | 60 //Make sure we have enough space for at least 32 return values |
74 } | 67 } |
75 | 68 |
76 void free_stack(context * ct, void * data) | 69 void free_stack(context * ct, void * data) |
77 { | 70 { |
78 char * cdata = data; | 71 char * cdata = data; |
79 while(cdata < ct->current_stack->data || cdata >= ct->current_stack->data+STACK_CHUNK_SIZE) { | 72 while(cdata < ct->current_stack->data || cdata >= ct->current_stack->free_space) |
73 { | |
80 if(ct->current_stack == ct->stack_begin) | 74 if(ct->current_stack == ct->stack_begin) |
81 { | 75 { |
82 fprintf(stderr, "Attempt to free memory at %X using free_stack, but %X doesn't appear to be stack allocated\n", data, data); | 76 fprintf(stderr, "Attempt to free memory at %X using free_stack, but %X doesn't appear to be stack allocated\n", data, data); |
83 exit(-1); | 77 exit(-1); |
84 } | 78 } |
85 ct->current_stack->used = 0; | |
86 ct->current_stack = ct->current_stack->prev; | 79 ct->current_stack = ct->current_stack->prev; |
87 } | 80 } |
88 ct->current_stack->used = cdata-ct->current_stack->data; | 81 ct->current_stack->free_space = data; |
89 if(!ct->current_stack->used && ct->current_stack->prev) | 82 if(ct->current_stack->free_space == ct->current_stack->data && ct->current_stack->prev) |
90 ct->current_stack = ct->current_stack->prev; | 83 ct->current_stack = ct->current_stack->prev; |
91 } | 84 } |
92 | 85 |