Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added memory leak error checks at the end of execution #57

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/leak.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[memory(128)]


fn main() {
let x = alloc(8) as &char;
}
45 changes: 23 additions & 22 deletions src/target/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,14 @@ typedef struct machine {
const int STACK_HEAP_COLLISION = 1;
const int NO_FREE_MEMORY = 2;
const int STACK_UNDERFLOW = 3;
const int MEMORY_LEAK = 4;

// Fatal error handler. Always exits program.
void panic(int code) {
printf("panic: ");
switch (code) {
case 1: printf("stack and heap collision during push"); break;
case 2: printf("no free memory left"); break;
case 3: printf("stack underflow"); break;
default: printf("unknown error code");
}
printf("\n");
exit(code);
}

///////////////////////////////////////////////////////////////////////
///////////////////////////// Debug Info //////////////////////////////
///////////////////////////////////////////////////////////////////////
// Print out the state of the virtual machine's stack and heap
void machine_dump(machine *vm) {
int i;
printf("stack: [ ");
for (i=0; i<vm->stack_ptr; i++)
printf("%g ", vm->memory[i]);
for (i=vm->stack_ptr; i<vm->capacity; i++)
printf(" ");
printf("]\nheap: [ ");
for (i=0; i<vm->stack_ptr; i++)
printf(" ");
for (i=vm->stack_ptr; i<vm->capacity; i++)
printf("%g ", vm->memory[i]);
printf("]\nalloc: [ ");
Expand All @@ -58,6 +38,20 @@ void machine_dump(machine *vm) {
printf("TOTAL ALLOC'D %d\n", total);
}

// Fatal error handler. Always exits program.
void panic(int code) {
printf("panic: ");
switch (code) {
case 1: printf("stack and heap collision during push"); break;
case 2: printf("no free memory left"); break;
case 3: printf("stack underflow"); break;
case 4: printf("leaked heap memory"); break;
default: printf("unknown error code");
}
printf("\n");
exit(code);
}


/////////////////////////////////////////////////////////////////////////
///////////////////// Stack manipulation operations /////////////////////
Expand Down Expand Up @@ -120,6 +114,12 @@ machine *machine_new(int global_scope_size, int capacity) {

// Free the virtual machine's memory. This is called at the end of the program.
void machine_drop(machine *vm) {
for (int i=vm->capacity-1; i>=vm->stack_ptr; i--) {
if (vm->allocated[i]) {
machine_dump(vm);
panic(MEMORY_LEAK);
}
}
// machine_dump(vm);
free(vm->memory);
free(vm->allocated);
Expand All @@ -142,8 +142,9 @@ void machine_establish_stack_frame(machine *vm, int arg_size, int local_scope_si
double *args = malloc(arg_size * sizeof(double));
int i;
// Pop the arguments' values off of the stack
for (i=arg_size-1; i>=0; i--)
for (i=arg_size-1; i>=0; i--) {
args[i] = machine_pop(vm);
}

// Push the current base pointer onto the stack so that
// when this function returns, it will be able to resume
Expand Down
73 changes: 40 additions & 33 deletions src/target/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var READER = bufio.NewReader(os.Stdin)
const STACK_HEAP_COLLISION = 1
const NO_FREE_MEMORY = 2
const STACK_UNDERFLOW = 3
const MEMORY_LEAK = 4

func panic(code int) {
fmt.Print("panic: ")
Expand All @@ -24,6 +25,9 @@ func panic(code int) {
case 3:
fmt.Println("stack underflow")
break
case 4:
fmt.Println("leaked heap memory")
break
default:
fmt.Println("unknown error code")
}
Expand Down Expand Up @@ -52,40 +56,43 @@ func machine_new(global_scope_size, capacity int) *machine {
return result
}

func (vm *machine) dump() {
fmt.Print("stack: [ ")
for i := 0; i < vm.stack_ptr; i += 1 {
fmt.Printf("%g ", vm.memory[i])
}
fmt.Println("]")
fmt.Print("heap: [ ")
for i := vm.stack_ptr; i < vm.capacity; i += 1 {
fmt.Printf("%g ", vm.memory[i])
}
fmt.Println("]")
fmt.Print("alloc: [ ")
for i := 0; i < vm.capacity; i += 1 {
if vm.allocated[i] {
fmt.Printf("1 ")
} else {
fmt.Printf("0 ")
}
}
fmt.Println("]")
total := 0
for i := 0; i < vm.capacity; i += 1 {
if vm.allocated[i] {
total += 1
}
}
fmt.Printf("STACK SIZE %d\n", vm.stack_ptr)
fmt.Printf("TOTAL ALLOC'D %d\n", total)
}

func (vm *machine) drop() {
// fmt.Print("stack: [ ")
// for i:=0; i<vm.stack_ptr; i+=1 {
// fmt.Printf("%g ", vm.memory[i])
// }
// for i:=vm.stack_ptr; i<vm.capacity; i+=1 {
// fmt.Print(" ")
// }
// fmt.Println("]")
// fmt.Print("heap: [ ")
// for i:=0; i<vm.stack_ptr; i+=1 {
// fmt.Print(" ")
// }
// for i:=vm.stack_ptr; i<vm.capacity; i+=1 {
// fmt.Printf("%g ", vm.memory[i])
// }
// fmt.Println("]")
// fmt.Print("alloc: [ ")
// for i:=0; i<vm.capacity; i+=1 {
// if vm.allocated[i] {
// fmt.Printf("1 ")
// } else {
// fmt.Printf("0 ")
// }
// }
// fmt.Println("]")
// total := 0;
// for i:=0; i<vm.capacity; i+=1 {
// if vm.allocated[i] {
// total += 1
// }
// }
// fmt.Println("STACK SIZE %d\n", vm.stack_ptr);
// fmt.Println("TOTAL ALLOC'D %d\n", total);
for i := vm.capacity - 1; i >= vm.stack_ptr; i -= 1 {
if vm.allocated[i] {
vm.dump()
panic(MEMORY_LEAK)
}
}
}

func (vm *machine) load_base_ptr() {
Expand Down