Skip to content

Change mem:* API to make memory offset calculation easier #1

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

Open
wants to merge 1 commit into
base: master
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin/
4 changes: 4 additions & 0 deletions tests/malloc.retro
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ n:random
random-value n:print
'Storing_value_to_`region1`... print
region1 random-value mem:set
region1 #8 + random-value mem:set
'===OK print nl nl
```

Expand All @@ -45,6 +46,9 @@ region1 random-value mem:set
'===TEST3:_mem:get print
'Expect_ s:put 'to_contain_ s:put random-value n:put nl
'Actual_value:_____ s:put region1 mem:get n:put nl
'Calculating_memory_offset_should_not_segfault...
region1 #8 + mem:get
'Contents_of_(region1_+_1)_=_ s:put n:put nl
'===OK print nl nl
```

Expand Down
12 changes: 6 additions & 6 deletions vm/nga-c/retro.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,43 +241,43 @@ typedef union {
void malloc_allocate(NgaState *vm) {
// TODO: Conditionally compile based on host word size?
double_cell addr = { .val = malloc(stack_pop(vm)) };
stack_push(vm, addr.msw);
stack_push(vm, addr.lsw);
stack_push(vm, addr.msw);
}

void malloc_free(NgaState *vm) {
double_cell addr;
addr.lsw = stack_pop(vm);
addr.msw = stack_pop(vm);
addr.lsw = stack_pop(vm);
free(addr.val);
}

void malloc_store(NgaState *vm) {
CELL value = stack_pop(vm);
double_cell addr;
addr.lsw = stack_pop(vm);
addr.msw = stack_pop(vm);
addr.lsw = stack_pop(vm);
*(CELL *) addr.val = value;
}

void malloc_fetch(NgaState *vm) {
double_cell addr;
addr.lsw = stack_pop(vm);
addr.msw = stack_pop(vm);
addr.lsw = stack_pop(vm);
CELL value = *(CELL *)addr.val;
stack_push(vm, value);
}

void malloc_realloc(NgaState *vm) {
CELL bytes = stack_pop(vm);
double_cell addr1;
addr1.lsw = stack_pop(vm);
addr1.msw = stack_pop(vm);
addr1.lsw = stack_pop(vm);

double_cell addr2;
addr2.val = realloc(addr1.val, bytes);
stack_push(vm, addr2.msw);
stack_push(vm, addr2.lsw);
stack_push(vm, addr2.msw);
}

void query_malloc(NgaState *vm) {
Expand Down