Skip to content
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
9 changes: 5 additions & 4 deletions include/uvwasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
extern "C" {
#endif

#include "uv.h"
#include "wasi_types.h"
#include "fd_table.h"

#define UVWASI_VERSION_MAJOR 0
#define UVWASI_VERSION_MINOR 0
Expand Down Expand Up @@ -35,8 +33,10 @@ typedef struct uvwasi_mem_s {
uvwasi_realloc realloc;
} uvwasi_mem_t;

struct uvwasi_fd_table_t;

typedef struct uvwasi_s {
struct uvwasi_fd_table_t fds;
struct uvwasi_fd_table_t* fds;
uvwasi_size_t argc;
char** argv;
char* argv_buf;
Expand Down Expand Up @@ -69,9 +69,10 @@ typedef struct uvwasi_options_s {
/* Embedder API. */
uvwasi_errno_t uvwasi_init(uvwasi_t* uvwasi, uvwasi_options_t* options);
void uvwasi_destroy(uvwasi_t* uvwasi);
/* Use int instead of uv_file to avoid needing uv.h */
uvwasi_errno_t uvwasi_embedder_remap_fd(uvwasi_t* uvwasi,
const uvwasi_fd_t fd,
uv_file new_host_fd);
int new_host_fd);
const char* uvwasi_embedder_err_code_to_string(uvwasi_errno_t code);


Expand Down
26 changes: 15 additions & 11 deletions src/fd_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,25 @@ uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi,
if (uvwasi == NULL || options == NULL || options->fd_table_size < 3)
return UVWASI_EINVAL;

table = &uvwasi->fds;
table->fds = NULL;
table = uvwasi__malloc(uvwasi, sizeof(*table));
if (table == NULL)
return UVWASI_ENOMEM;

table->used = 0;
table->size = options->fd_table_size;
table->fds = uvwasi__calloc(uvwasi,
options->fd_table_size,
sizeof(struct uvwasi_fd_wrap_t*));

if (table->fds == NULL)
if (table->fds == NULL) {
uvwasi__free(uvwasi, table);
return UVWASI_ENOMEM;
}

r = uv_rwlock_init(&table->rwlock);
if (r != 0) {
err = uvwasi__translate_uv_error(r);
/* Free table->fds and set it to NULL here. This is done explicitly instead
of jumping to error_exit because uvwasi_fd_table_free() relies on fds
being NULL to know whether or not to destroy the rwlock.
*/
uvwasi__free(uvwasi, table->fds);
table->fds = NULL;
uvwasi__free(uvwasi, table);
return err;
}

Expand All @@ -217,6 +216,7 @@ uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi,
if (err != UVWASI_ESUCCESS)
goto error_exit;

uvwasi->fds = table;
return UVWASI_ESUCCESS;
error_exit:
uvwasi_fd_table_free(uvwasi, table);
Expand All @@ -228,12 +228,14 @@ void uvwasi_fd_table_free(uvwasi_t* uvwasi, struct uvwasi_fd_table_t* table) {
struct uvwasi_fd_wrap_t* entry;
uint32_t i;

if (table == NULL)
if (uvwasi == NULL || table == NULL)
return;

for (i = 0; i < table->size; i++) {
entry = table->fds[i];
if (entry == NULL) continue;

if (entry == NULL)
continue;

uv_mutex_destroy(&entry->mutex);
uvwasi__free(uvwasi, entry);
Expand All @@ -246,6 +248,8 @@ void uvwasi_fd_table_free(uvwasi_t* uvwasi, struct uvwasi_fd_table_t* table) {
table->used = 0;
uv_rwlock_destroy(&table->rwlock);
}

uvwasi__free(uvwasi, table);
}


Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/path_resolver.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __UVWASI_PATH_RESOLVER_H__
#define __UVWASI_PATH_RESOLVER_H__

#include "fd_table.h"
#include "uvwasi.h"

uvwasi_errno_t uvwasi__normalize_path(const char* path,
Expand Down
2 changes: 1 addition & 1 deletion src/poll_oneoff.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ uvwasi_errno_t uvwasi__poll_oneoff_state_add_fdevent(

/* Get the file descriptor. If UVWASI_EBADF is returned, continue on, but
don't do any polling with the handle. */
err = uvwasi_fd_table_get(&state->uvwasi->fds, fd, &event->wrap, rights, 0);
err = uvwasi_fd_table_get(state->uvwasi->fds, fd, &event->wrap, rights, 0);
if (err == UVWASI_EBADF)
event->wrap = NULL;
else if (err != UVWASI_ESUCCESS)
Expand Down
1 change: 1 addition & 0 deletions src/poll_oneoff.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __UVWASI_POLL_ONEOFF_H__
#define __UVWASI_POLL_ONEOFF_H__

#include "fd_table.h"
#include "wasi_types.h"

struct uvwasi_s;
Expand Down
Loading