diff --git a/runtime/include/sandbox_types.h b/runtime/include/sandbox_types.h index 13568a6..4e5144b 100644 --- a/runtime/include/sandbox_types.h +++ b/runtime/include/sandbox_types.h @@ -13,7 +13,7 @@ #include "ps_list.h" #include "sandbox_state.h" #include "sandbox_state_history.h" -#include "vec_u8.h" +#include "vec.h" #include "wasm_memory.h" #include "wasm_types.h" #include "wasm_stack.h" @@ -22,6 +22,9 @@ #define SANDBOX_PAGE_ALLOCATION_TIMESTAMP_COUNT 1024 #endif +#define u8 uint8_t +VEC(u8) + /********************* * Structs and Types * ********************/ diff --git a/runtime/include/vec.h b/runtime/include/vec.h new file mode 100644 index 0000000..e441d12 --- /dev/null +++ b/runtime/include/vec.h @@ -0,0 +1,101 @@ +#pragma once + +#include +#include + +#define VEC(TYPE) \ + \ + struct vec_##TYPE { \ + size_t length; \ + size_t capacity; \ + TYPE * buffer; /* Backing heap allocation. Different lifetime because realloc might move this */ \ + }; \ + \ + static inline int vec_##TYPE##_init(struct vec_##TYPE *vec, size_t capacity); \ + static inline struct vec_##TYPE *vec_##TYPE##_alloc(size_t capacity); \ + static inline void vec_##TYPE##_deinit(struct vec_##TYPE *vec); \ + static inline void vec_##TYPE##_free(struct vec_##TYPE *vec); \ + \ + /** \ + * Initializes a vec, allocating a backing buffer for the provided capcity \ + * @param vec pointer to an uninitialized vec \ + * @param capacity \ + * @returns 0 on success, -1 on failure \ + */ \ + static inline int vec_##TYPE##_init(struct vec_##TYPE *vec, size_t capacity) \ + { \ + if (capacity == 0) { \ + vec->buffer = NULL; \ + } else { \ + vec->buffer = calloc(capacity, sizeof(TYPE)); \ + if (vec->buffer == NULL) return -1; \ + } \ + \ + vec->length = 0; \ + vec->capacity = capacity; \ + \ + return 0; \ + } \ + \ + /** \ + * Allocate and initialize a vec with a backing buffer \ + * @param capacity \ + * @returns a pointer to an initialized vec on the heap, ready for use \ + */ \ + static inline struct vec_##TYPE *vec_##TYPE##_alloc(size_t capacity) \ + { \ + struct vec_##TYPE *vec = (struct vec_##TYPE *)malloc(sizeof(struct vec_##TYPE)); \ + if (vec == NULL) return vec; \ + \ + int rc = vec_##TYPE##_init(vec, capacity); \ + if (rc < 0) { \ + vec_##TYPE##_free(vec); \ + return NULL; \ + } \ + \ + return vec; \ + } \ + \ + /** \ + * Deinitialize a vec, clearing out members and releasing the backing buffer \ + * @param vec \ + */ \ + static inline void vec_##TYPE##_deinit(struct vec_##TYPE *vec) \ + { \ + if (vec->capacity == 0) { \ + assert(vec->buffer == NULL); \ + assert(vec->length == 0); \ + return; \ + } \ + \ + assert(vec->buffer != NULL); \ + free(vec->buffer); \ + vec->buffer = NULL; \ + vec->length = 0; \ + vec->capacity = 0; \ + } \ + \ + /** \ + * Deinitializes and frees a vec allocated to the heap \ + * @param vec \ + */ \ + static inline void vec_##TYPE##_free(struct vec_##TYPE *vec) \ + { \ + vec_##TYPE##_deinit(vec); \ + free(vec); \ + } \ + \ + static inline int vec_##TYPE##_insert(struct vec_##TYPE *vec, uint32_t idx, TYPE value) \ + { \ + if (idx >= vec->capacity) return -1; \ + \ + vec->buffer[idx] = value; \ + return 0; \ + } \ + \ + static inline TYPE *vec_##TYPE##_get(struct vec_##TYPE *vec, uint32_t idx) \ + { \ + if (idx >= vec->capacity) return NULL; \ + \ + return &vec->buffer[idx]; \ + } diff --git a/runtime/include/vec_u8.h b/runtime/include/vec_u8.h deleted file mode 100644 index 0ddc46f..0000000 --- a/runtime/include/vec_u8.h +++ /dev/null @@ -1,88 +0,0 @@ -#pragma once - -#include -#include - -struct vec_u8 { - size_t length; - size_t capacity; - uint8_t *buffer; /* Backing heap allocation. Different lifetime because realloc might move this */ -}; - -static inline int vec_u8_init(struct vec_u8 *vec_u8, size_t capacity); -static inline struct vec_u8 *vec_u8_alloc(size_t capacity); -static inline void vec_u8_deinit(struct vec_u8 *vec_u8); -static inline void vec_u8_free(struct vec_u8 *vec_u8); - -/** - * Initializes a vec, allocating a backing buffer for the provided capcity - * @param vec_u8 pointer to an uninitialized vec - * @param capacity - * @returns 0 on success, -1 on failure - */ -static inline int -vec_u8_init(struct vec_u8 *vec_u8, size_t capacity) -{ - if (capacity == 0) { - vec_u8->buffer = NULL; - } else { - vec_u8->buffer = calloc(capacity, sizeof(uint8_t)); - if (vec_u8->buffer == NULL) return -1; - } - - vec_u8->length = 0; - vec_u8->capacity = capacity; - - return 0; -} - -/** - * Allocate and initialize a vec with a backing buffer - * @param capacity - * @returns a pointer to an initialized vec on the heap, ready for use - */ -static inline struct vec_u8 * -vec_u8_alloc(size_t capacity) -{ - struct vec_u8 *vec_u8 = (struct vec_u8 *)malloc(sizeof(struct vec_u8)); - if (vec_u8 == NULL) return vec_u8; - - int rc = vec_u8_init(vec_u8, capacity); - if (rc < 0) { - vec_u8_free(vec_u8); - return NULL; - } - - return vec_u8; -} - -/** - * Deinitialize a vec, clearing out members and releasing the backing buffer - * @param vec_u8 - */ -static inline void -vec_u8_deinit(struct vec_u8 *vec_u8) -{ - if (vec_u8->capacity == 0) { - assert(vec_u8->buffer == NULL); - assert(vec_u8->length == 0); - return; - } - - assert(vec_u8->buffer != NULL); - free(vec_u8->buffer); - vec_u8->buffer = NULL; - vec_u8->length = 0; - vec_u8->capacity = 0; -} - -/** - * Deinitializes and frees a vec allocated to the heap - * @param vec_u8 - */ -static inline void -vec_u8_free(struct vec_u8 *vec_u8) -{ - vec_u8_deinit(vec_u8); - free(vec_u8); -}