From e2d0d562997a051ee423d19155a302c0665b4b60 Mon Sep 17 00:00:00 2001 From: AlexVanin Date: Wed, 29 Jan 2014 21:37:48 +0400 Subject: [PATCH] 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 --- VaninVM/Context.c | 5 ++++- VaninVM/IOcode.c | 2 +- VaninVM/Main.c | 13 +++++++------ VaninVM/TOS.c | 4 ++-- VaninVM/TOS.h | 2 +- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/VaninVM/Context.c b/VaninVM/Context.c index e1cc2e5..64e5644 100644 --- a/VaninVM/Context.c +++ b/VaninVM/Context.c @@ -27,9 +27,12 @@ void push_context(context* cont) } context* pop_context() { + context* poped = node_last->obj; c_node* last = node_last; node_last = node_last->prev; - return last->obj; + + free(last); + return poped; } //Find Context in Context Stack. diff --git a/VaninVM/IOcode.c b/VaninVM/IOcode.c index ad5ebb5..f1f00c4 100644 --- a/VaninVM/IOcode.c +++ b/VaninVM/IOcode.c @@ -58,7 +58,7 @@ int read_constant(FILE* stream, int* count, char*** index) buffer = (char*)malloc(Const_Header.size_const+1); 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 diff --git a/VaninVM/Main.c b/VaninVM/Main.c index b60273c..e7f7b27 100644 --- a/VaninVM/Main.c +++ b/VaninVM/Main.c @@ -11,7 +11,7 @@ long long cmp_int(long long* a, long long* b); double cmp_double(double* a, double* b); -void run_intepreter(char* filename); +int run_interpreter(char* filename); int main(int argc, char** argv) { @@ -21,8 +21,9 @@ int main(int argc, char** argv) printf("%s", "File is not specified"); return 1; } - return_code = run_interpreter(argv[1]); + return_code = run_interpreter(argv[1]); + return return_code; } int run_interpreter(char* filename) @@ -56,12 +57,13 @@ int run_interpreter(char* filename) startfunc = read_bytecode(input, &phash_table); fclose(input); + initTOS(3000); - initTOS(); initRStack(1000); ip = 0; current_context = create_context(startfunc, phash_table, &code); + node_last = NULL; while (exec_status) @@ -70,7 +72,7 @@ int run_interpreter(char* filename) { case INVALID: //DO(INVALID, "Invalid instruction.", 1) - break; + break; case DLOAD: //DO(DLOAD, "Load double on TOS, inlined into insn stream.", 9) d1 = *((double*)(code+ip)); @@ -602,8 +604,7 @@ int run_interpreter(char* filename) break; } } - remove_context(current_context); - getchar(); + remove_context(current_context); return 0; } diff --git a/VaninVM/TOS.c b/VaninVM/TOS.c index 973fc12..af5da47 100644 --- a/VaninVM/TOS.c +++ b/VaninVM/TOS.c @@ -33,9 +33,9 @@ double pop_double() return num; } -int initTOS() +int initTOS(int count) { - TOS = (double*)calloc(1, 12000); + TOS = (double*)calloc(count, sizeof(double*)); tp = 0; return 0; } \ No newline at end of file diff --git a/VaninVM/TOS.h b/VaninVM/TOS.h index 93572dc..072a0ba 100644 --- a/VaninVM/TOS.h +++ b/VaninVM/TOS.h @@ -9,7 +9,7 @@ typedef union double* TOS; int tp; -int initTOS(); +int initTOS(int); void push_int(long long); void push_double(double); long long pop_int();