Skip to content

Commit

Permalink
chore: merge Oleg's mem fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
samcamwilliams committed Feb 12, 2025
1 parent 342aa70 commit a06a3de
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion native/hb_beamr/hb_beamr.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ static void wasm_driver_output(ErlDrvData raw, char *buff, ErlDrvSizeT bufflen)
long size_l = (long)size;
ei_decode_binary(buff, &index, wasm_binary, &size_l);
ei_get_type(buff, &index, &type, &mode_size);
mode = driver_alloc(mode_size);
// the init message size + '\0' character
mode = driver_alloc(mode_size + 1);
ei_decode_atom(buff, &index, mode);
LoadWasmReq* mod_bin = driver_alloc(sizeof(LoadWasmReq));
mod_bin->proc = proc;
Expand Down
9 changes: 7 additions & 2 deletions native/hb_beamr/hb_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,11 @@ void wasm_initialize_runtime(void* raw) {
wasm_module_exports(proc->module, &exports);

// Create Erlang lists for imports
ErlDrvTermData* init_msg = driver_alloc(sizeof(ErlDrvTermData) * (2 + (13 * imports.size) + (11 * exports.size)));
int init_msg_size = sizeof(ErlDrvTermData) * (2 + 3 + 5 + (13 * imports.size) + (11 * exports.size));
ErlDrvTermData* init_msg = driver_alloc(init_msg_size);
int msg_i = 0;

// 2 in the init_msg_size
init_msg[msg_i++] = ERL_DRV_ATOM;
init_msg[msg_i++] = atom_execution_result;

Expand All @@ -179,7 +182,7 @@ void wasm_initialize_runtime(void* raw) {
// TODO: Handle other types of imports?
continue;
}

// 13 items in the each import message
init_msg[msg_i++] = ERL_DRV_ATOM;
init_msg[msg_i++] = driver_mk_atom((char*)wasm_externtype_to_kind_string(type));
init_msg[msg_i++] = ERL_DRV_STRING;
Expand Down Expand Up @@ -255,6 +258,7 @@ void wasm_initialize_runtime(void* raw) {
get_function_sig(type, type_str);
DRV_DEBUG("Export: %s [%s] -> %s", name->data, kind_str, type_str);

// 10 elements for each exported function
init_msg[msg_i++] = ERL_DRV_ATOM;
init_msg[msg_i++] = driver_mk_atom(kind_str);
init_msg[msg_i++] = ERL_DRV_STRING;
Expand All @@ -267,6 +271,7 @@ void wasm_initialize_runtime(void* raw) {
init_msg[msg_i++] = 3;
}

// 5 closing elements
init_msg[msg_i++] = ERL_DRV_NIL;
init_msg[msg_i++] = ERL_DRV_LIST;
init_msg[msg_i++] = (exports.size) + 1;
Expand Down

0 comments on commit a06a3de

Please sign in to comment.