Skip to content

Commit

Permalink
refactor: Use INLINE macro throughout compiletime
Browse files Browse the repository at this point in the history
  • Loading branch information
bushidocodes committed Dec 9, 2021
1 parent f51da12 commit 569fdd3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
47 changes: 24 additions & 23 deletions runtime/include/wasm_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ struct wasm_memory {
uint8_t data[];
};

static inline struct wasm_memory *

static INLINE struct wasm_memory *
wasm_memory_allocate(size_t initial, size_t max)
{
assert(initial > 0);
Expand Down Expand Up @@ -54,20 +55,20 @@ wasm_memory_allocate(size_t initial, size_t max)
return self;
}

static inline void
static INLINE void
wasm_memory_free(struct wasm_memory *self)
{
size_t size_to_free = sizeof(struct wasm_memory) + WASM_MEMORY_MAX + /* guard page */ PAGE_SIZE;
munmap(self, size_to_free);
}

static inline void
static INLINE void
wasm_memory_wipe(struct wasm_memory *self)
{
memset(self->data, 0, self->size);
}

static inline int
static INLINE int
wasm_memory_expand(struct wasm_memory *self, size_t size_to_expand)
{
size_t target_size = self->size + size_to_expand;
Expand Down Expand Up @@ -96,7 +97,7 @@ wasm_memory_expand(struct wasm_memory *self, size_t size_to_expand)
* @param bounds_check the size of the thing we are pointing to
* @return void pointer to something in WebAssembly linear memory
*/
static inline void *
static INLINE void *
wasm_memory_get_ptr_void(struct wasm_memory *self, uint32_t offset, uint32_t size)
{
assert(offset + size <= self->size);
Expand All @@ -108,7 +109,7 @@ wasm_memory_get_ptr_void(struct wasm_memory *self, uint32_t offset, uint32_t siz
* @param offset an offset into the WebAssembly linear memory
* @return char at the offset
*/
static inline char
static INLINE char
wasm_memory_get_char(struct wasm_memory *self, uint32_t offset)
{
assert(offset + sizeof(char) <= self->size);
Expand All @@ -120,7 +121,7 @@ wasm_memory_get_char(struct wasm_memory *self, uint32_t offset)
* @param offset an offset into the WebAssembly linear memory
* @return float at the offset
*/
static inline float
static INLINE float
wasm_memory_get_f32(struct wasm_memory *self, uint32_t offset)
{
assert(offset + sizeof(float) <= self->size);
Expand All @@ -132,7 +133,7 @@ wasm_memory_get_f32(struct wasm_memory *self, uint32_t offset)
* @param offset an offset into the WebAssembly linear memory
* @return double at the offset
*/
static inline double
static INLINE double
wasm_memory_get_f64(struct wasm_memory *self, uint32_t offset)
{
assert(offset + sizeof(double) <= self->size);
Expand All @@ -144,7 +145,7 @@ wasm_memory_get_f64(struct wasm_memory *self, uint32_t offset)
* @param offset an offset into the WebAssembly linear memory
* @return int8_t at the offset
*/
static inline int8_t
static INLINE int8_t
wasm_memory_get_i8(struct wasm_memory *self, uint32_t offset)
{
assert(offset + sizeof(int8_t) <= self->size);
Expand All @@ -156,7 +157,7 @@ wasm_memory_get_i8(struct wasm_memory *self, uint32_t offset)
* @param offset an offset into the WebAssembly linear memory
* @return int16_t at the offset
*/
static inline int16_t
static INLINE int16_t
wasm_memory_get_i16(struct wasm_memory *self, uint32_t offset)
{
assert(offset + sizeof(int16_t) <= self->size);
Expand All @@ -168,7 +169,7 @@ wasm_memory_get_i16(struct wasm_memory *self, uint32_t offset)
* @param offset an offset into the WebAssembly linear memory
* @return int32_t at the offset
*/
static inline int32_t
static INLINE int32_t
wasm_memory_get_i32(struct wasm_memory *self, uint32_t offset)
{
assert(offset + sizeof(int32_t) <= self->size);
Expand All @@ -180,14 +181,14 @@ wasm_memory_get_i32(struct wasm_memory *self, uint32_t offset)
* @param offset an offset into the WebAssembly linear memory
* @return int32_t at the offset
*/
static inline int64_t
static INLINE int64_t
wasm_memory_get_i64(struct wasm_memory *self, uint32_t offset)
{
assert(offset + sizeof(int64_t) <= self->size);
return *(int64_t *)&self->data[offset];
}

static inline uint32_t
static INLINE uint32_t
wasm_memory_get_page_count(struct wasm_memory *self)
{
return (uint32_t)(self->size / WASM_PAGE_SIZE);
Expand All @@ -199,7 +200,7 @@ wasm_memory_get_page_count(struct wasm_memory *self)
* @param size the maximum expected length in characters
* @return pointer to the string or NULL if max_length is reached without finding null-terminator
*/
static inline char *
static INLINE char *
wasm_memory_get_string(struct wasm_memory *self, uint32_t offset, uint32_t size)
{
assert(offset + (sizeof(char) * size) <= self->size);
Expand All @@ -215,7 +216,7 @@ wasm_memory_get_string(struct wasm_memory *self, uint32_t offset, uint32_t size)
* @param offset an offset into the WebAssembly linear memory
* @return float at the offset
*/
static inline void
static INLINE void
wasm_memory_set_f32(struct wasm_memory *self, uint32_t offset, float value)
{
assert(offset + sizeof(float) <= self->size);
Expand All @@ -227,7 +228,7 @@ wasm_memory_set_f32(struct wasm_memory *self, uint32_t offset, float value)
* @param offset an offset into the WebAssembly linear memory
* @return double at the offset
*/
static inline void
static INLINE void
wasm_memory_set_f64(struct wasm_memory *self, uint32_t offset, double value)
{
assert(offset + sizeof(double) <= self->size);
Expand All @@ -239,7 +240,7 @@ wasm_memory_set_f64(struct wasm_memory *self, uint32_t offset, double value)
* @param offset an offset into the WebAssembly linear memory
* @return int8_t at the offset
*/
static inline void
static INLINE void
wasm_memory_set_i8(struct wasm_memory *self, uint32_t offset, int8_t value)
{
assert(offset + sizeof(int8_t) <= self->size);
Expand All @@ -251,7 +252,7 @@ wasm_memory_set_i8(struct wasm_memory *self, uint32_t offset, int8_t value)
* @param offset an offset into the WebAssembly linear memory
* @return int16_t at the offset
*/
static inline void
static INLINE void
wasm_memory_set_i16(struct wasm_memory *self, uint32_t offset, int16_t value)
{
assert(offset + sizeof(int16_t) <= self->size);
Expand All @@ -263,7 +264,7 @@ wasm_memory_set_i16(struct wasm_memory *self, uint32_t offset, int16_t value)
* @param offset an offset into the WebAssembly linear memory
* @return int32_t at the offset
*/
static inline void
static INLINE void
wasm_memory_set_i32(struct wasm_memory *self, uint32_t offset, int32_t value)
{
assert(offset + sizeof(int32_t) <= self->size);
Expand All @@ -275,26 +276,26 @@ wasm_memory_set_i32(struct wasm_memory *self, uint32_t offset, int32_t value)
* @param offset an offset into the WebAssembly linear memory
* @return int64_t at the offset
*/
static inline void
static INLINE void
wasm_memory_set_i64(struct wasm_memory *self, uint64_t offset, int64_t value)
{
assert(offset + sizeof(int64_t) <= self->size);
*(int64_t *)&self->data[offset] = value;
}

static inline void
static INLINE void
wasm_memory_set_size(struct wasm_memory *self, size_t size)
{
self->size = size;
}

static inline size_t
static INLINE size_t
wasm_memory_get_size(struct wasm_memory *self)
{
return self->size;
}

static inline void
static INLINE void
wasm_memory_initialize_region(struct wasm_memory *self, uint32_t offset, uint32_t region_size, uint8_t region[])
{
assert((size_t)offset + region_size <= self->size);
Expand Down
5 changes: 3 additions & 2 deletions runtime/include/wasm_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <sys/mman.h>

#include "sandbox_types.h"
#include "types.h"

struct wasm_stack {
size_t capacity; /* Usable capacity. Excludes size of guard page that we need to free */
Expand All @@ -20,7 +21,7 @@ struct wasm_stack {
* @param sandbox sandbox that we want to allocate a stack for
* @returns 0 on success, -1 on error
*/
static inline int
static INLINE int
wasm_stack_allocate(struct wasm_stack *stack, size_t capacity)
{
assert(stack);
Expand Down Expand Up @@ -56,7 +57,7 @@ wasm_stack_allocate(struct wasm_stack *stack, size_t capacity)
goto done;
}

static inline void
static INLINE void
wasm_stack_free(struct wasm_stack *stack)
{
assert(stack != NULL);
Expand Down
10 changes: 6 additions & 4 deletions runtime/include/wasm_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <stdint.h>
#include <stdlib.h>

#include "types.h"

/* memory also provides the table access functions */
#define INDIRECT_TABLE_SIZE (1 << 10)

Expand All @@ -18,7 +20,7 @@ struct wasm_table {
struct wasm_table_entry data[];
};

static inline struct wasm_table *
static INLINE struct wasm_table *
wasm_table_allocate(size_t capacity)
{
struct wasm_table *self = (struct wasm_table *)malloc(sizeof(struct wasm_table)
Expand All @@ -30,14 +32,14 @@ wasm_table_allocate(size_t capacity)
return self;
}

static inline void
static INLINE void
wasm_table_free(struct wasm_table *self)
{
assert(self != NULL);
free(self);
}

static inline void *
static INLINE void *
wasm_table_get(struct wasm_table *self, uint32_t idx, uint32_t type_id)
{
assert(self != NULL);
Expand All @@ -52,7 +54,7 @@ wasm_table_get(struct wasm_table *self, uint32_t idx, uint32_t type_id)
return f.func_pointer;
}

static inline void
static INLINE void
wasm_table_set(struct wasm_table *self, uint32_t idx, uint32_t type_id, char *pointer)
{
assert(self != NULL);
Expand Down

0 comments on commit 569fdd3

Please sign in to comment.