Fixed major bug with compiling in release-mode: wrong allocated memory. initTOS now takes size of TOS as parameter. Improved work with contexts: now memory releases after popping context from stack

This commit is contained in:
Alex Vanin 2014-01-29 21:37:48 +04:00
parent ff0ef7f0bb
commit e2d0d56299
5 changed files with 15 additions and 11 deletions

View file

@ -27,9 +27,12 @@ void push_context(context* cont)
} }
context* pop_context() context* pop_context()
{ {
context* poped = node_last->obj;
c_node* last = node_last; c_node* last = node_last;
node_last = node_last->prev; node_last = node_last->prev;
return last->obj;
free(last);
return poped;
} }
//Find Context in Context Stack. //Find Context in Context Stack.

View file

@ -58,7 +58,7 @@ int read_constant(FILE* stream, int* count, char*** index)
buffer = (char*)malloc(Const_Header.size_const+1); buffer = (char*)malloc(Const_Header.size_const+1);
buffer[0]=0; buffer[0]=0;
*(index) = (char**)malloc(sizeof(char**)*Const_Header.count_const); *(index) = (char**)malloc(sizeof(char**)*Const_Header.count_const+1);
fread_s(buffer+1, Const_Header.size_const, sizeof(char), Const_Header.size_const, stream); //Reading constant values fread_s(buffer+1, Const_Header.size_const, sizeof(char), Const_Header.size_const, stream); //Reading constant values

View file

@ -11,7 +11,7 @@
long long cmp_int(long long* a, long long* b); long long cmp_int(long long* a, long long* b);
double cmp_double(double* a, double* b); double cmp_double(double* a, double* b);
void run_intepreter(char* filename); int run_interpreter(char* filename);
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
@ -21,8 +21,9 @@ int main(int argc, char** argv)
printf("%s", "File is not specified"); printf("%s", "File is not specified");
return 1; return 1;
} }
return_code = run_interpreter(argv[1]);
return_code = run_interpreter(argv[1]);
return return_code;
} }
int run_interpreter(char* filename) int run_interpreter(char* filename)
@ -56,12 +57,13 @@ int run_interpreter(char* filename)
startfunc = read_bytecode(input, &phash_table); startfunc = read_bytecode(input, &phash_table);
fclose(input); fclose(input);
initTOS(3000);
initTOS();
initRStack(1000); initRStack(1000);
ip = 0; ip = 0;
current_context = create_context(startfunc, phash_table, &code); current_context = create_context(startfunc, phash_table, &code);
node_last = NULL; node_last = NULL;
while (exec_status) while (exec_status)
@ -70,7 +72,7 @@ int run_interpreter(char* filename)
{ {
case INVALID: case INVALID:
//DO(INVALID, "Invalid instruction.", 1) //DO(INVALID, "Invalid instruction.", 1)
break; break;
case DLOAD: case DLOAD:
//DO(DLOAD, "Load double on TOS, inlined into insn stream.", 9) //DO(DLOAD, "Load double on TOS, inlined into insn stream.", 9)
d1 = *((double*)(code+ip)); d1 = *((double*)(code+ip));
@ -602,8 +604,7 @@ int run_interpreter(char* filename)
break; break;
} }
} }
remove_context(current_context); remove_context(current_context);
getchar();
return 0; return 0;
} }

View file

@ -33,9 +33,9 @@ double pop_double()
return num; return num;
} }
int initTOS() int initTOS(int count)
{ {
TOS = (double*)calloc(1, 12000); TOS = (double*)calloc(count, sizeof(double*));
tp = 0; tp = 0;
return 0; return 0;
} }

View file

@ -9,7 +9,7 @@ typedef union
double* TOS; double* TOS;
int tp; int tp;
int initTOS(); int initTOS(int);
void push_int(long long); void push_int(long long);
void push_double(double); void push_double(double);
long long pop_int(); long long pop_int();