Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom allocator option #60

Merged
merged 1 commit into from
Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ int main(void) {
init_options.preopens = calloc(1, sizeof(uvwasi_preopen_t));
init_options.preopens[0].mapped_path = "/var";
init_options.preopens[0].real_path = ".";
init_options.allocator = NULL;

r = uvwasi_init(uvw, &init_options);
assert(r == 0);
Expand Down
13 changes: 9 additions & 4 deletions include/fd_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# define PATH_MAX_BYTES (PATH_MAX)
#endif

struct uvwasi_s;

struct uvwasi_fd_wrap_t {
uvwasi_fd_t id;
Expand All @@ -37,14 +38,18 @@ struct uvwasi_fd_table_t {
uv_rwlock_t rwlock;
};

uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_fd_table_t* table,
uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table,
uint32_t init_size);
void uvwasi_fd_table_free(struct uvwasi_fd_table_t* table);
uvwasi_errno_t uvwasi_fd_table_insert_preopen(struct uvwasi_fd_table_t* table,
void uvwasi_fd_table_free(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table);
uvwasi_errno_t uvwasi_fd_table_insert_preopen(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table,
const uv_file fd,
const char* path,
const char* real_path);
uvwasi_errno_t uvwasi_fd_table_insert_fd(struct uvwasi_fd_table_t* table,
uvwasi_errno_t uvwasi_fd_table_insert_fd(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table,
const uv_file fd,
const int flags,
const char* path,
Expand Down
15 changes: 14 additions & 1 deletion include/uvwasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ extern "C" {
UVWASI_STRINGIFY(UVWASI_VERSION_PATCH)
#define UVWASI_VERSION_WASI "snapshot_0"

typedef void* (*uvwasi_malloc)(size_t size, void* mem_user_data);
typedef void (*uvwasi_free)(void* ptr, void* mem_user_data);
typedef void* (*uvwasi_calloc)(size_t nmemb, size_t size, void* mem_user_data);
typedef void* (*uvwasi_realloc)(void* ptr, size_t size, void* mem_user_data);

typedef struct uvwasi_mem_s {
void* mem_user_data;
uvwasi_malloc malloc;
uvwasi_free free;
uvwasi_calloc calloc;
uvwasi_realloc realloc;
} uvwasi_mem_t;

typedef struct uvwasi_s {
struct uvwasi_fd_table_t fds;
Expand All @@ -33,6 +45,7 @@ typedef struct uvwasi_s {
char** env;
char* env_buf;
size_t env_buf_size;
const uvwasi_mem_t* allocator;
} uvwasi_t;

typedef struct uvwasi_preopen_s {
Expand All @@ -47,9 +60,9 @@ typedef struct uvwasi_options_s {
size_t argc;
char** argv;
char** envp;
const uvwasi_mem_t* allocator;
} uvwasi_options_t;


// Embedder API.
uvwasi_errno_t uvwasi_init(uvwasi_t* uvwasi, uvwasi_options_t* options);
void uvwasi_destroy(uvwasi_t* uvwasi);
Expand Down
34 changes: 22 additions & 12 deletions src/fd_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "fd_table.h"
#include "wasi_types.h"
#include "uv_mapping.h"
#include "uvwasi_alloc.h"


#define UVWASI__RIGHTS_ALL (UVWASI_RIGHT_FD_DATASYNC | \
Expand Down Expand Up @@ -175,7 +176,8 @@ static uvwasi_errno_t uvwasi__get_type_and_rights(uv_file fd,
}


static uvwasi_errno_t uvwasi__fd_table_insert(struct uvwasi_fd_table_t* table,
static uvwasi_errno_t uvwasi__fd_table_insert(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
uv_file fd,
const char* mapped_path,
const char* real_path,
Expand All @@ -197,7 +199,7 @@ static uvwasi_errno_t uvwasi__fd_table_insert(struct uvwasi_fd_table_t* table,
/* Check that there is room for a new item. If there isn't, grow the table. */
if (table->used >= table->size) {
new_size = table->size * 2;
new_fds = realloc(table->fds, new_size * sizeof(*new_fds));
new_fds = uvwasi__realloc(uvwasi, table->fds, new_size * sizeof(*new_fds));
if (new_fds == NULL) {
err = UVWASI_ENOMEM;
goto exit;
Expand Down Expand Up @@ -255,7 +257,8 @@ static uvwasi_errno_t uvwasi__fd_table_insert(struct uvwasi_fd_table_t* table,
}


uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_fd_table_t* table,
uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
uint32_t init_size) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_filetype_t type;
Expand All @@ -276,7 +279,9 @@ uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_fd_table_t* table,

table->used = 0;
table->size = init_size;
table->fds = calloc(init_size, sizeof(struct uvwasi_fd_wrap_t));
table->fds = uvwasi__calloc(uvwasi,
init_size,
sizeof(struct uvwasi_fd_wrap_t));

if (table->fds == NULL) {
err = UVWASI_ENOMEM;
Expand All @@ -293,7 +298,8 @@ uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_fd_table_t* table,
if (err != UVWASI_ESUCCESS)
goto error_exit;

err = uvwasi__fd_table_insert(table,
err = uvwasi__fd_table_insert(uvwasi,
table,
i,
"",
"",
Expand All @@ -313,24 +319,25 @@ uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_fd_table_t* table,

return UVWASI_ESUCCESS;
error_exit:
uvwasi_fd_table_free(table);
uvwasi_fd_table_free(uvwasi, table);
return err;
}


void uvwasi_fd_table_free(struct uvwasi_fd_table_t* table) {
void uvwasi_fd_table_free(uvwasi_t* uvwasi, struct uvwasi_fd_table_t* table) {
if (table == NULL)
return;

free(table->fds);
uvwasi__free(uvwasi, table->fds);
table->fds = NULL;
table->size = 0;
table->used = 0;
uv_rwlock_destroy(&table->rwlock);
}


uvwasi_errno_t uvwasi_fd_table_insert_preopen(struct uvwasi_fd_table_t* table,
uvwasi_errno_t uvwasi_fd_table_insert_preopen(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
const uv_file fd,
const char* path,
const char* real_path) {
Expand All @@ -349,7 +356,8 @@ uvwasi_errno_t uvwasi_fd_table_insert_preopen(struct uvwasi_fd_table_t* table,
if (type != UVWASI_FILETYPE_DIRECTORY)
return UVWASI_ENOTDIR;

err = uvwasi__fd_table_insert(table,
err = uvwasi__fd_table_insert(uvwasi,
table,
fd,
path,
real_path,
Expand All @@ -365,7 +373,8 @@ uvwasi_errno_t uvwasi_fd_table_insert_preopen(struct uvwasi_fd_table_t* table,
}


uvwasi_errno_t uvwasi_fd_table_insert_fd(struct uvwasi_fd_table_t* table,
uvwasi_errno_t uvwasi_fd_table_insert_fd(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
const uv_file fd,
const int flags,
const char* path,
Expand All @@ -385,7 +394,8 @@ uvwasi_errno_t uvwasi_fd_table_insert_fd(struct uvwasi_fd_table_t* table,
if (r != UVWASI_ESUCCESS)
return r;

r = uvwasi__fd_table_insert(table,
r = uvwasi__fd_table_insert(uvwasi,
table,
fd,
path,
path,
Expand Down
Loading