Change mem:* API to make memory offset calculation easier (from Rick C)

FossilOrigin-Name: 47687a00c3c0e398b7b2baf279ed91c2b236a06b1bf5bbc79fd9bee69d83d456
This commit is contained in:
crc 2022-07-31 00:46:20 +00:00
parent 0317b9074c
commit 1eec7ab446
3 changed files with 11 additions and 6 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
bin/

View file

@ -36,6 +36,7 @@ n:random
random-value n:print random-value n:print
'Storing_value_to_`region1`... print 'Storing_value_to_`region1`... print
region1 random-value mem:set region1 random-value mem:set
region1 #8 + random-value mem:set
'===OK print nl nl '===OK print nl nl
``` ```
@ -45,6 +46,9 @@ region1 random-value mem:set
'===TEST3:_mem:get print '===TEST3:_mem:get print
'Expect_ s:put 'to_contain_ s:put random-value n:put nl 'Expect_ s:put 'to_contain_ s:put random-value n:put nl
'Actual_value:_____ s:put region1 mem:get 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 '===OK print nl nl
``` ```

View file

@ -241,29 +241,29 @@ typedef union {
void malloc_allocate(NgaState *vm) { void malloc_allocate(NgaState *vm) {
// TODO: Conditionally compile based on host word size? // TODO: Conditionally compile based on host word size?
double_cell addr = { .val = malloc(stack_pop(vm)) }; double_cell addr = { .val = malloc(stack_pop(vm)) };
stack_push(vm, addr.msw);
stack_push(vm, addr.lsw); stack_push(vm, addr.lsw);
stack_push(vm, addr.msw);
} }
void malloc_free(NgaState *vm) { void malloc_free(NgaState *vm) {
double_cell addr; double_cell addr;
addr.lsw = stack_pop(vm);
addr.msw = stack_pop(vm); addr.msw = stack_pop(vm);
addr.lsw = stack_pop(vm);
free(addr.val); free(addr.val);
} }
void malloc_store(NgaState *vm) { void malloc_store(NgaState *vm) {
CELL value = stack_pop(vm); CELL value = stack_pop(vm);
double_cell addr; double_cell addr;
addr.lsw = stack_pop(vm);
addr.msw = stack_pop(vm); addr.msw = stack_pop(vm);
addr.lsw = stack_pop(vm);
*(CELL *) addr.val = value; *(CELL *) addr.val = value;
} }
void malloc_fetch(NgaState *vm) { void malloc_fetch(NgaState *vm) {
double_cell addr; double_cell addr;
addr.lsw = stack_pop(vm);
addr.msw = stack_pop(vm); addr.msw = stack_pop(vm);
addr.lsw = stack_pop(vm);
CELL value = *(CELL *)addr.val; CELL value = *(CELL *)addr.val;
stack_push(vm, value); stack_push(vm, value);
} }
@ -271,13 +271,13 @@ void malloc_fetch(NgaState *vm) {
void malloc_realloc(NgaState *vm) { void malloc_realloc(NgaState *vm) {
CELL bytes = stack_pop(vm); CELL bytes = stack_pop(vm);
double_cell addr1; double_cell addr1;
addr1.lsw = stack_pop(vm);
addr1.msw = stack_pop(vm); addr1.msw = stack_pop(vm);
addr1.lsw = stack_pop(vm);
double_cell addr2; double_cell addr2;
addr2.val = realloc(addr1.val, bytes); addr2.val = realloc(addr1.val, bytes);
stack_push(vm, addr2.msw);
stack_push(vm, addr2.lsw); stack_push(vm, addr2.lsw);
stack_push(vm, addr2.msw);
} }
void query_malloc(NgaState *vm) { void query_malloc(NgaState *vm) {