Skip to content

Initial 64bit malloc support when BIT64 is enabled #2

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
12 changes: 6 additions & 6 deletions doc/Glossary-Concise.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1105,25 +1105,25 @@ Compare n1 and n2. Return `TRUE` if n1 is less than n2, or `FALSE` otherwise.
lteq? D: nn-f A: - F: -
Compare n1 and n2. Return `TRUE` if n1 is less than or equal to n2, or `FALSE` otherwise.

mem:alloc D: n-nn A: - F: -
mem:alloc D: n-n A: - F: -
Use malloc to allocate memory. Returns a double cell pointer to this memory.

mem:cell+ D: nnn-n A: - F: -
mem:cell+ D: nn-n A: - F: -
Return address of next cell. Uses a double cell pointer on the stack.

mem:fetch D: nn-n A: - F: -
Fetch value from malloc'd memory region. Address is a double cell value.

mem:fetch-double D: nn-n A: - F: -
mem:fetch-double D: nn-nn A: - F: -
Fetch a double cell value from a malloc memory region.

mem:free D: nn- A: - F: -
mem:free D: n- A: - F: -
Fre a malloc'd region of memory. Pass a double cell pointer to the memory to free.

mem:resize D: mmn- A: - F: -
mem:resize D: mn- A: - F: -
Resize a malloc'd memory area.

mem:store D: xnn- A: - F: -
mem:store D: xn- A: - F: -
Store a value into a malloc'd memory region. Uses a double cell pointer for the address.

mod D: nm-o A: - F: -
Expand Down
16 changes: 8 additions & 8 deletions interface/malloc.retro
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@

---reveal---

:mem:alloc (n--aa) ALLOC mem:invoke ;
:mem:store (an--) STORE mem:invoke ;
:mem:fetch (a--n) FETCH mem:invoke ;
:mem:free (aa--) FREE mem:invoke ;
:mem:resize (aan--) RESIZE mem:invoke ;
:mem:alloc (n--a) ALLOC mem:invoke ;
:mem:store (an--) STORE mem:invoke ;
:mem:fetch (a--n) FETCH mem:invoke ;
:mem:free (a--) FREE mem:invoke ;
:mem:resize (an--) RESIZE mem:invoke ;
}}

:mem:cell+ (nnn-n) #4 * + ;
:mem:cell+ (nn-n) #8 * + ;
:mem:fetch-double (n-nn)
dup-pair #1 mem:cell+ fetch push mem:fetch pop ;
:mem:store-double (aann-nn)
dup #1 mem:cell+ fetch push mem:fetch pop ;
:mem:store-double (ann-nn)
push push dup-pair #1 mem:cell+ pop mem:store pop mem:store ;
~~~
37 changes: 15 additions & 22 deletions vm/nga-c/retro.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ void io_socket(NgaState *); void query_socket(NgaState *);
#endif

#ifdef ENABLE_MALLOC
#ifdef BIT64
void io_malloc(NgaState *); void query_malloc(NgaState *);
#endif
#endif

void io_image(NgaState *); void query_image(NgaState *);

Expand Down Expand Up @@ -231,6 +233,7 @@ void inst_iq(NgaState *); void inst_ii(NgaState *);

/* Dynamic Memory / `malloc` support --------------------------------- */
#ifdef ENABLE_MALLOC
#ifdef BIT64
typedef union {
void* val;
struct {
Expand Down Expand Up @@ -259,45 +262,32 @@ void double_divmod(NgaState *vm) {
}

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.lsw);
stack_push(vm, addr.msw);
stack_push(vm, (CELL)malloc(stack_pop(vm)));
}

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

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

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

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

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

void query_malloc(NgaState *vm) {
Expand All @@ -317,6 +307,7 @@ void io_malloc(NgaState *vm) {
stack_push(vm, -1);
}
#endif
#endif

/* Multi Core Support ------------------------------------------------ */
#ifdef ENABLE_MULTICORE
Expand Down Expand Up @@ -1905,8 +1896,10 @@ int main(int argc, char **argv) {
register_device(vm, io_unix, query_unix);
#endif
#ifdef ENABLE_MALLOC
#ifdef BIT64
register_device(vm, io_malloc, query_malloc);
#endif
#endif
#ifdef ENABLE_CLOCK
register_device(vm, io_clock, query_clock);
#endif
Expand Down