Skip to content

Commit

Permalink
add create_satisfying_shared_memories
Browse files Browse the repository at this point in the history
i plan to use this for wasi-threads
  • Loading branch information
yamt committed Dec 24, 2022
1 parent f2394ff commit d0de3ef
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ list(APPEND lib_sources
"lib/waitlist.c")
if(TOYWASM_ENABLE_WASI_THREADS)
list(APPEND lib_sources
"lib/shared_memory.c"
"lib/wasi_threads.c")
endif()
endif()
Expand Down
2 changes: 1 addition & 1 deletion lib/instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ memory_instance_free(struct meminst *mi)
void
memory_instance_destroy(struct meminst *mi)
{
memory_instance_free(mi);
memory_instance_free(mi);
}

/* https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation
Expand Down
3 changes: 3 additions & 0 deletions lib/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ struct meminst;
struct memtype;
int memory_instance_create(struct meminst **mip, const struct memtype *mt);
void memory_instance_destroy(struct meminst *mi);

int create_satisfying_shared_memories(struct module *module,
struct import_object **imop);
80 changes: 80 additions & 0 deletions lib/shared_memory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <assert.h>

#include "instance.h"
#include "type.h"

static void
_dtor(struct import_object *imo)
{
uint32_t i;
for (i = 0; i < imo->nentries; i++) {
struct import_object_entry *e = &imo->entries[i];
struct meminst *mi = e->u.mem;
if (mi != NULL) {
memory_instance_destroy(mi);
}
}
}

int
create_satisfying_shared_memories(struct module *m,
struct import_object **imop)
{
struct import_object *imo = NULL;
uint32_t nsharedimports;
uint32_t i;
struct meminst *mi = NULL;
int ret;

nsharedimports = 0;
for (i = 0; i < m->nimports; i++) {
const struct import *im = &m->imports[i];
if (im->desc.type != IMPORT_MEMORY) {
continue;
}
const struct memtype *mt = &im->desc.u.memtype;
if ((mt->flags & MEMTYPE_FLAG_SHARED) == 0) {
continue;
}
nsharedimports++;
}

ret = import_object_alloc(nsharedimports, &imo);
if (ret != 0) {
goto fail;
}
imo->dtor = _dtor;

uint32_t idx = 0;
for (i = 0; i < m->nimports; i++) {
const struct import *im = &m->imports[i];
if (im->desc.type != IMPORT_MEMORY) {
continue;
}
const struct memtype *mt = &im->desc.u.memtype;
if ((mt->flags & MEMTYPE_FLAG_SHARED) == 0) {
continue;
}
ret = memory_instance_create(&mi, mt);
if (ret != 0) {
goto fail;
}
assert(idx < imo->nentries);
struct import_object_entry *e = &imo->entries[idx++];
e->module_name = &im->module_name;
e->name = &im->name;
e->type = IMPORT_MEMORY;
e->u.mem = mi;
}

*imop = imo;
return 0;
fail:
if (imo != NULL) {
import_object_destroy(imo);
}
if (mi != NULL) {
memory_instance_destroy(mi);
}
return ret;
}

0 comments on commit d0de3ef

Please sign in to comment.