From 57d95388b1f4b04d868e8b2adfd28632885a29c7 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Fri, 10 Dec 2021 19:49:10 +0000 Subject: [PATCH] refactor: wasm_table --- .vscode/settings.json | 1 + runtime/include/vec_u8.h | 2 ++ runtime/include/wasm_table.h | 68 +++++++++++++++++++++++++++++++----- runtime/src/module.c | 2 +- 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 52765fc70..b40a8af41 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -102,6 +102,7 @@ "software_interrupt_counts.h": "c", "sandbox_set_as_running_sys.h": "c", "wasm_module_instance.h": "c", + "wasm_table.h": "c" }, "files.exclude": { "**/.git": true, diff --git a/runtime/include/vec_u8.h b/runtime/include/vec_u8.h index 2a43e9e98..39b8f549c 100644 --- a/runtime/include/vec_u8.h +++ b/runtime/include/vec_u8.h @@ -78,8 +78,10 @@ vec_u8_deinit(struct vec_u8 *self) if (self->capacity == 0) { assert(self->buffer == NULL); assert(self->length == 0); + return; } + assert(self->buffer != NULL); free(self->buffer); self->buffer = NULL; self->length = 0; diff --git a/runtime/include/wasm_table.h b/runtime/include/wasm_table.h index e2f19d40f..33b0b50fe 100644 --- a/runtime/include/wasm_table.h +++ b/runtime/include/wasm_table.h @@ -15,23 +15,73 @@ struct wasm_table_entry { }; struct wasm_table { - uint32_t length; - uint32_t capacity; - struct wasm_table_entry data[]; + uint32_t length; + uint32_t capacity; + struct wasm_table_entry *buffer; }; +static INLINE struct wasm_table *wasm_table_alloc(void); +static INLINE int wasm_table_init(struct wasm_table *self, size_t capacity); +static INLINE struct wasm_table *wasm_table_new(size_t capacity); +static INLINE void wasm_table_deinit(struct wasm_table *self); +static INLINE void wasm_table_free(struct wasm_table *self); +static INLINE void wasm_table_delete(struct wasm_table *self); + static INLINE struct wasm_table * -wasm_table_allocate(size_t capacity) +wasm_table_alloc(void) { - struct wasm_table *self = (struct wasm_table *)malloc(sizeof(struct wasm_table) - + capacity * sizeof(struct wasm_table_entry)); + return (struct wasm_table *)malloc(sizeof(struct wasm_table)); +} + +static INLINE int +wasm_table_init(struct wasm_table *self, size_t capacity) +{ + assert(self != NULL); + + if (capacity > 0) { + self->buffer = calloc(capacity, sizeof(struct wasm_table_entry)); + if (self->buffer == NULL) return -1; + } self->capacity = capacity; self->length = 0; + return 0; +} + +static INLINE struct wasm_table * +wasm_table_new(size_t capacity) +{ + struct wasm_table *self = wasm_table_alloc(); + if (self == NULL) return NULL; + + int rc = wasm_table_init(self, capacity); + if (rc < 0) { + wasm_table_free(self); + return NULL; + } + return self; } +static INLINE void +wasm_table_deinit(struct wasm_table *self) +{ + assert(self != NULL); + + if (self->capacity > 0) { + assert(self->buffer == NULL); + assert(self->length == 0); + return; + } + + assert(self->buffer != NULL); + free(self->buffer); + self->buffer = NULL; + self->length = 0; + self->capacity = 0; +} + static INLINE void wasm_table_free(struct wasm_table *self) { @@ -45,7 +95,7 @@ wasm_table_get(struct wasm_table *self, uint32_t idx, uint32_t type_id) assert(self != NULL); assert(idx < self->capacity); - struct wasm_table_entry f = self->data[idx]; + struct wasm_table_entry f = self->buffer[idx]; // FIXME: Commented out function type check because of gocr // assert(f.type_id == type_id); @@ -62,7 +112,7 @@ wasm_table_set(struct wasm_table *self, uint32_t idx, uint32_t type_id, char *po assert(pointer != NULL); /* TODO: atomic for multiple concurrent invocations? Issue #97 */ - if (self->data[idx].type_id == type_id && self->data[idx].func_pointer == pointer) return; + if (self->buffer[idx].type_id == type_id && self->buffer[idx].func_pointer == pointer) return; - self->data[idx] = (struct wasm_table_entry){ .type_id = type_id, .func_pointer = pointer }; + self->buffer[idx] = (struct wasm_table_entry){ .type_id = type_id, .func_pointer = pointer }; } diff --git a/runtime/src/module.c b/runtime/src/module.c index f4fe23a72..8e8ad9a10 100644 --- a/runtime/src/module.c +++ b/runtime/src/module.c @@ -186,7 +186,7 @@ module_new(char *name, char *path, uint32_t stack_size, uint32_t max_memory, uin /* WebAssembly Indirect Table */ /* TODO: Should this be part of the module or per-sandbox? */ /* TODO: How should this table be sized? */ - module->indirect_table = wasm_table_allocate(INDIRECT_TABLE_SIZE); + module->indirect_table = wasm_table_new(INDIRECT_TABLE_SIZE); /* Request Response Buffer */ if (request_size == 0) request_size = MODULE_DEFAULT_REQUEST_RESPONSE_SIZE;