Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Jun 13, 2024
1 parent 00354a4 commit ac01879
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 111 deletions.
16 changes: 9 additions & 7 deletions lib/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ read_vec_count(const uint8_t **pp, const uint8_t *ep, uint32_t *resultp)
}

int
read_vec_u32(struct mem_context *mctx, const uint8_t **pp, const uint8_t *ep, uint32_t *countp,
uint32_t **resultp)
read_vec_u32(struct mem_context *mctx, const uint8_t **pp, const uint8_t *ep,
uint32_t *countp, uint32_t **resultp)
{
return read_vec(mctx, pp, ep, sizeof(uint32_t),
(read_elem_func_t)read_leb_u32, NULL, countp,
Expand All @@ -89,8 +89,8 @@ read_vec_u32(struct mem_context *mctx, const uint8_t **pp, const uint8_t *ep, ui
* - the ctx pointer
*/
int
_read_vec_with_ctx_impl(struct mem_context *mctx, const uint8_t **pp, const uint8_t *ep,
size_t elem_size,
_read_vec_with_ctx_impl(struct mem_context *mctx, const uint8_t **pp,
const uint8_t *ep, size_t elem_size,
int (*read_elem)(const uint8_t **pp, const uint8_t *ep,
uint32_t idx, void *elem, void *),
void (*clear_elem)(void *elem), void *ctx,
Expand Down Expand Up @@ -154,13 +154,15 @@ read_elem_wrapper(const uint8_t **pp, const uint8_t *ep, uint32_t idx,
}

int
read_vec(struct mem_context *mctx, const uint8_t **pp, const uint8_t *ep, size_t elem_size,
read_vec(struct mem_context *mctx, const uint8_t **pp, const uint8_t *ep,
size_t elem_size,
int (*read_elem)(const uint8_t **pp, const uint8_t *ep, void *elem),
void (*clear_elem)(void *elem), uint32_t *countp, void **resultp)
{
struct read_vec_ctx ctx = {
.read_elem = read_elem,
};
return _read_vec_with_ctx_impl(mctx, pp, ep, elem_size, read_elem_wrapper,
clear_elem, &ctx, countp, resultp);
return _read_vec_with_ctx_impl(mctx, pp, ep, elem_size,
read_elem_wrapper, clear_elem, &ctx,
countp, resultp);
}
31 changes: 16 additions & 15 deletions lib/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ int read_u32(const uint8_t **pp, const uint8_t *ep, uint32_t *resultp);
int read_u64(const uint8_t **pp, const uint8_t *ep, uint64_t *resultp);

int read_vec_count(const uint8_t **pp, const uint8_t *ep, uint32_t *resultp);
int read_vec_u32(struct mem_context *mctx, const uint8_t **pp, const uint8_t *ep, uint32_t *countp,
uint32_t **resultp);
int read_vec_u32(struct mem_context *mctx, const uint8_t **pp,
const uint8_t *ep, uint32_t *countp, uint32_t **resultp);

typedef int (*read_elem_func_t)(const uint8_t **pp, const uint8_t *ep,
void *elem);
typedef int (*read_elem_with_ctx_func_t)(const uint8_t **pp, const uint8_t *ep,
uint32_t idx, void *elem, void *ctx);
typedef void (*clear_elem_func_t)(void *elem);

int read_vec(struct mem_context *mctx, const uint8_t **pp, const uint8_t *ep, size_t elem_size,
read_elem_func_t read_elem, clear_elem_func_t clear_elem,
uint32_t *countp, void **resultp);
int read_vec(struct mem_context *mctx, const uint8_t **pp, const uint8_t *ep,
size_t elem_size, read_elem_func_t read_elem,
clear_elem_func_t clear_elem, uint32_t *countp, void **resultp);

int _read_vec_with_ctx_impl(struct mem_context *mctx, const uint8_t **pp, const uint8_t *ep,
size_t elem_size,
int _read_vec_with_ctx_impl(struct mem_context *mctx, const uint8_t **pp,
const uint8_t *ep, size_t elem_size,
read_elem_with_ctx_func_t read_elem,
clear_elem_func_t clear_elem, void *ctx,
uint32_t *countp, void **resultp);
Expand All @@ -44,24 +44,25 @@ int _read_vec_with_ctx_impl(struct mem_context *mctx, const uint8_t **pp, const

#if defined(toywasm_typeof)
/* a version with non-standard checks */
#define read_vec_with_ctx(mctx, pp, ep, elem_size, read_elem, clear_elem, ctx, \
countp, resultp) \
#define read_vec_with_ctx(mctx, pp, ep, elem_size, read_elem, clear_elem, \
ctx, countp, resultp) \
({ \
int (*_r)(const uint8_t **, const uint8_t *, uint32_t, \
toywasm_typeof(*resultp), void *) = read_elem; \
void (*_c)(toywasm_typeof(*resultp)) = clear_elem; \
assert(sizeof(**resultp) == elem_size); \
_read_vec_with_ctx_impl(mctx, pp, ep, elem_size, \
_read_vec_with_ctx_impl(mctx, pp, ep, elem_size, \
(read_elem_with_ctx_func_t)_r, \
(clear_elem_func_t)_c, ctx, countp, \
(void **)resultp); \
})
#else
#define read_vec_with_ctx(mctx, pp, ep, elem_size, read_elem, clear_elem, ctx, \
countp, resultp) \
_read_vec_with_ctx_impl(mctx, \
pp, ep, elem_size, (read_elem_with_ctx_func_t)read_elem, \
(clear_elem_func_t)clear_elem, ctx, countp, (void **)resultp)
#define read_vec_with_ctx(mctx, pp, ep, elem_size, read_elem, clear_elem, \
ctx, countp, resultp) \
_read_vec_with_ctx_impl(mctx, pp, ep, elem_size, \
(read_elem_with_ctx_func_t)read_elem, \
(clear_elem_func_t)clear_elem, ctx, countp, \
(void **)resultp)
#endif

struct name;
Expand Down
3 changes: 2 additions & 1 deletion lib/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ frame_enter(struct exec_context *ctx, struct instance *inst, uint32_t funcidx,
#endif
if (ei->maxlabels > 1) {
frame->labelidx = ctx->labels.lsize;
ret = VEC_PREALLOC(exec_mctx(ctx), ctx->labels, ei->maxlabels - 1);
ret = VEC_PREALLOC(exec_mctx(ctx), ctx->labels,
ei->maxlabels - 1);
if (ret != 0) {
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/exec_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ struct exec_context {
void *exec_done_arg;
#endif

struct mem_context *mctx;
struct mem_context *mctx;

/* Options */
struct exec_options options;
Expand Down
21 changes: 12 additions & 9 deletions lib/exec_insn_subr.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ memory_getptr2(struct exec_context *ctx, uint32_t memidx, uint32_t ptr,
#endif
size_t need = (size_t)last_byte + 1;
assert(need > meminst->allocated);
void *np = mem_resize(meminst->mctx, meminst->data, meminst->allocated, need);
void *np = mem_resize(meminst->mctx, meminst->data,
meminst->allocated, need);
if (np == NULL) {
return ENOMEM;
}
Expand Down Expand Up @@ -477,16 +478,18 @@ memory_grow_impl(struct exec_context *ctx, struct meminst *mi, uint32_t sz)
if (new_size_in_bytes >> page_shift != new_size) {
ret = EOVERFLOW;
} else {
void *np = mem_resize(mi->mctx, mi->data, mi->allocated, new_size_in_bytes);
void *np =
mem_resize(mi->mctx, mi->data, mi->allocated,
new_size_in_bytes);
if (np == NULL) {
ret = ENOMEM;
ret = ENOMEM;
} else {
ret = 0;
mi->data = np;
assert(new_size_in_bytes > mi->allocated);
memset(mi->data + mi->allocated, 0,
new_size_in_bytes - mi->allocated);
mi->allocated = new_size_in_bytes;
ret = 0;
mi->data = np;
assert(new_size_in_bytes > mi->allocated);
memset(mi->data + mi->allocated, 0,
new_size_in_bytes - mi->allocated);
mi->allocated = new_size_in_bytes;
}
}
#if defined(TOYWASM_ENABLE_WASM_THREADS)
Expand Down
12 changes: 6 additions & 6 deletions lib/insn_impl_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ INSN_IMPL(br_table)
struct mem_context *mctx;
struct mem_context mctx0;
if (VALIDATING) {
mctx = VCTX->mctx;
} else {
mctx = &mctx0;
mem_context_init(mctx);
mctx = VCTX->mctx;
} else {
mctx = &mctx0;
mem_context_init(mctx);
}
ret = read_vec_u32(mctx, &p, ep, &vec_count, &table);
CHECK_RET(ret);
Expand Down Expand Up @@ -404,8 +404,8 @@ INSN_IMPL(br_table)
}
mem_free(mctx, table, vec_count * sizeof(uint32_t));
if (!VALIDATING) {
mem_context_clear(mctx);
}
mem_context_clear(mctx);
}
SAVE_PC;
INSN_SUCCESS;
fail:
Expand Down
26 changes: 16 additions & 10 deletions lib/instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include "bitmap.h"
#include "exec.h"
#include "instance.h"
#include "module.h"
#include "mem.h"
#include "module.h"
#include "nbio.h"
#include "shared_memory_impl.h"
#include "suspend.h"
Expand Down Expand Up @@ -185,7 +185,8 @@ memory_instance_create(struct mem_context *mctx, struct meminst **mip,
if (need_in_bytes > 0) {
mp->data = mem_zalloc(mctx->need_in_bytes);
if (mp->data == NULL) {
mem_free(mctx, mp->shared, sizeof(*mp->shared));
mem_free(mctx, mp->shared,
sizeof(*mp->shared));
mem_free(mctx, mp, sizeof(*mp));
ret = ENOMEM;
goto fail;
Expand Down Expand Up @@ -224,7 +225,8 @@ memory_instance_destroy(struct mem_context *mctx, struct meminst *mi)
}

int
global_instance_create(struct mem_context *mctx, struct globalinst **gip, const struct globaltype *gt)
global_instance_create(struct mem_context *mctx, struct globalinst **gip,
const struct globaltype *gt)
{
struct globalinst *ginst;
int ret;
Expand All @@ -249,7 +251,8 @@ global_instance_destroy(struct mem_context *mctx, struct globalinst *gi)

#if defined(TOYWASM_ENABLE_WASM_EXCEPTION_HANDLING)
int
tag_instance_create(struct mem_context *mctx, struct taginst **tip, const struct functype *ft)
tag_instance_create(struct mem_context *mctx, struct taginst **tip,
const struct functype *ft)
{
struct taginst *ti;
int ret;
Expand All @@ -273,7 +276,8 @@ tag_instance_destroy(struct mem_context *mctx, struct taginst *ti)
#endif

int
table_instance_create(struct mem_context *mctx, struct tableinst **tip, const struct tabletype *tt)
table_instance_create(struct mem_context *mctx, struct tableinst **tip,
const struct tabletype *tt)
{
struct tableinst *tinst;
int ret;
Expand Down Expand Up @@ -309,9 +313,9 @@ table_instance_create(struct mem_context *mctx, struct tableinst **tip, const st
}

void
table_instance_destroy(struct mem_context *mctx ,struct tableinst *ti)
table_instance_destroy(struct mem_context *mctx, struct tableinst *ti)
{
assert(mctx == ti->mctx);
assert(mctx == ti->mctx);
if (ti != NULL) {
uint32_t csz = valtype_cellsize(ti->type->et);
size_t ncells = (size_t)ti->size * csz;
Expand All @@ -324,8 +328,9 @@ table_instance_destroy(struct mem_context *mctx ,struct tableinst *ti)
*/

int
instance_create(struct mem_context *mctx, const struct module *m, struct instance **instp,
const struct import_object *imports, struct report *report)
instance_create(struct mem_context *mctx, const struct module *m,
struct instance **instp, const struct import_object *imports,
struct report *report)
{
struct instance *inst;
int ret;
Expand Down Expand Up @@ -448,7 +453,8 @@ resolve_imports(struct instance *inst, const struct import_object *imports,
}

int
instance_create_no_init(struct mem_context *mctx, const struct module *m, struct instance **instp,
instance_create_no_init(struct mem_context *mctx, const struct module *m,
struct instance **instp,
const struct import_object *imports,
struct report *report)
{
Expand Down
15 changes: 10 additions & 5 deletions lib/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ __BEGIN_EXTERN_C
* This API is inspired from js-api.
* https://webassembly.github.io/spec/js-api/index.html#instances
*/
int instance_create(struct mem_context *mctx, const struct module *m, struct instance **instp,
int instance_create(struct mem_context *mctx, const struct module *m,
struct instance **instp,
const struct import_object *imports,
struct report *report);

Expand All @@ -33,7 +34,8 @@ int instance_create(struct mem_context *mctx, const struct module *m, struct ins
* note than instance_execute_init can return a restartable error.
* see also: instance_execute_continue.
*/
int instance_create_no_init(struct mem_context *mctx, const struct module *m, struct instance **instp,
int instance_create_no_init(struct mem_context *mctx, const struct module *m,
struct instance **instp,
const struct import_object *imports,
struct report *report);
int instance_execute_init(struct exec_context *ctx);
Expand Down Expand Up @@ -123,7 +125,8 @@ int import_object_find_entry(

struct meminst;
struct memtype;
int memory_instance_create(struct mem_context *mctx, struct meminst **mip, const struct memtype *mt);
int memory_instance_create(struct mem_context *mctx, struct meminst **mip,
const struct memtype *mt);
void memory_instance_destroy(struct mem_context *mctx, struct meminst *mi);

struct globalinst;
Expand All @@ -134,7 +137,8 @@ void global_instance_destroy(struct mem_context *mctx, struct globalinst *gi);

struct tableinst;
struct tabletype;
int table_instance_create(struct mem_context *mctx, struct tableinst **tip, const struct tabletype *tt);
int table_instance_create(struct mem_context *mctx, struct tableinst **tip,
const struct tabletype *tt);
void table_instance_destroy(struct mem_context *mctx, struct tableinst *ti);

/*
Expand All @@ -146,7 +150,8 @@ void table_instance_destroy(struct mem_context *mctx, struct tableinst *ti);
* mainly for wasi-threads, where it's host's responsibility to
* provide the linear memory.
*/
int create_satisfying_shared_memories(struct mem_context *mctx, const struct module *module,
int create_satisfying_shared_memories(struct mem_context *mctx,
const struct module *module,
struct import_object **imop);

void instance_print_stats(const struct instance *inst);
Expand Down
21 changes: 10 additions & 11 deletions lib/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,23 @@ mem_alloc(struct mem_context *ctx, size_t sz)
void *
mem_zalloc(struct mem_context *ctx, size_t sz)
{
void *p = mem_alloc(ctx, sz);
if (p != NULL) {
memset(p, 0, sz);
}
return p;
void *p = mem_alloc(ctx, sz);
if (p != NULL) {
memset(p, 0, sz);
}
return p;
}

void *
mem_calloc(struct mem_context *ctx, size_t a, size_t b)
{
size_t sz = a * b;
if (sz / a != b) {
return NULL;
}
return mem_zalloc(ctx, sz);
size_t sz = a * b;
if (sz / a != b) {
return NULL;
}
return mem_zalloc(ctx, sz);
}


static int
mem_reserve(struct mem_context *ctx, size_t diff)
{
Expand Down
9 changes: 6 additions & 3 deletions lib/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ struct mem_context {

void mem_context_init(struct mem_context *ctx);
void mem_context_clear(struct mem_context *ctx);
void *__must_check mem_alloc(struct mem_context *ctx, size_t sz) __malloc_like __alloc_size(2);
void *__must_check mem_zalloc(struct mem_context *ctx, size_t sz) __malloc_like __alloc_size(2);
void *__must_check mem_alloc(struct mem_context *ctx, size_t sz) __malloc_like
__alloc_size(2);
void *__must_check mem_zalloc(struct mem_context *ctx, size_t sz) __malloc_like
__alloc_size(2);
void *__must_check mem_calloc(struct mem_context *ctx, size_t a, size_t b);
void mem_free(struct mem_context *ctx, void *p, size_t sz);
void *__must_check mem_resize(struct mem_context *ctx, void *p, size_t oldsz, size_t newsz) __malloc_like __alloc_size(4);
void *__must_check mem_resize(struct mem_context *ctx, void *p, size_t oldsz,
size_t newsz) __malloc_like __alloc_size(4);
Loading

0 comments on commit ac01879

Please sign in to comment.