diff options
author | David Härdeman <david@hardeman.nu> | 2020-06-10 00:43:05 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2020-06-10 00:43:05 +0200 |
commit | a270b41d55e3ab867e7c9aabf301ce9d9c48929f (patch) | |
tree | c7045efe0d332f47831e2dfe5bea5a0f0de8257c | |
parent | 3ff6ac2e782cdfe1d32240556478a2b5a3b8c057 (diff) |
Make sqe allocation a bit more uniform...and make sure sqes are not leaked
-rw-r--r-- | uring.c | 73 |
1 files changed, 33 insertions, 40 deletions
@@ -16,6 +16,22 @@ struct uring_ev { struct uring_task task; }; +static struct io_uring_sqe * +get_sqe(struct cfg *cfg) +{ + struct io_uring_sqe *sqe; + + sqe = io_uring_get_sqe(&cfg->uev->uring); + if (!sqe) { + io_uring_submit(&cfg->uev->uring); + sqe = io_uring_get_sqe(&cfg->uev->uring); + if (!sqe) + perrordie("Failed to get an sqe!\n"); + } + + return sqe; +} + void uring_task_refdump(struct uring_task *task) { @@ -135,11 +151,9 @@ uring_task_init(struct uring_task *task, const char *name, struct uring_task *pa void uring_close(struct cfg *cfg, struct uring_task *task, int fd, callback_t callback) { - struct io_uring_sqe *sqe = io_uring_get_sqe(&cfg->uev->uring); + struct io_uring_sqe *sqe = get_sqe(cfg); fprintf(stderr, "%s: called with task 0x%p and cb 0x%p\n", __func__, task, callback); - if (!sqe) - perrordie("io_uring_sqe"); if (task) { uring_task_get(cfg, task); @@ -154,16 +168,14 @@ uring_close(struct cfg *cfg, struct uring_task *task, int fd, callback_t callbac void uring_write(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, callback_t callback) { - struct io_uring_sqe *sqe = io_uring_get_sqe(&cfg->uev->uring); - - if (!sqe) - perrordie("io_uring_sqe"); + struct io_uring_sqe *sqe; if (task->fd < 0) { error("uring_write called with no fd set\n"); return; } + sqe = get_sqe(cfg); uring_task_get(cfg, task); task->callback = callback; io_uring_prep_write(sqe, task->fd, buf, len, 0); @@ -173,16 +185,14 @@ uring_write(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, cal void uring_read(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, off_t offset, callback_t callback) { - struct io_uring_sqe *sqe = io_uring_get_sqe(&cfg->uev->uring); - - if (!sqe) - perrordie("io_uring_sqe"); + struct io_uring_sqe *sqe; if (task->fd < 0) { error("uring_read called with no fd set\n"); return; } + sqe = get_sqe(cfg); uring_task_get(cfg, task); task->callback = callback; io_uring_prep_read(sqe, task->fd, buf, len, offset); @@ -192,10 +202,7 @@ uring_read(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, off_ void uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, callback_t callback) { - struct io_uring_sqe *sqe = io_uring_get_sqe(&cfg->uev->uring); - - if (!sqe) - perrordie("io_uring_sqe"); + struct io_uring_sqe *sqe = get_sqe(cfg); uring_task_get(cfg, task); task->callback = callback; @@ -206,10 +213,7 @@ uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, callbac void uring_sendmsg(struct cfg *cfg, struct uring_task *task, struct msghdr *msg, callback_t callback) { - struct io_uring_sqe *sqe = io_uring_get_sqe(&cfg->uev->uring); - - if (!sqe) - perrordie("io_uring_sqe"); + struct io_uring_sqe *sqe = get_sqe(cfg); uring_task_get(cfg, task); task->callback = callback; @@ -220,16 +224,14 @@ uring_sendmsg(struct cfg *cfg, struct uring_task *task, struct msghdr *msg, call void uring_connect(struct cfg *cfg, struct uring_task *task, struct sockaddr_in46 *addr, callback_t callback) { - struct io_uring_sqe *sqe = io_uring_get_sqe(&cfg->uev->uring); - - if (!sqe) - perrordie("io_uring_sqe"); + struct io_uring_sqe *sqe; if (task->fd < 0) { error("uring_connect called with no fd set\n"); return; } + sqe = get_sqe(cfg); uring_task_get(cfg, task); task->callback = callback; io_uring_prep_connect(sqe, task->fd, (struct sockaddr *)&addr->storage, addr->addrlen); @@ -239,17 +241,15 @@ uring_connect(struct cfg *cfg, struct uring_task *task, struct sockaddr_in46 *ad void uring_accept(struct cfg *cfg, struct uring_task *task, struct sockaddr_in46 *addr, callback_t callback) { - struct io_uring_sqe *sqe = io_uring_get_sqe(&cfg->uev->uring); - addr->addrlen = sizeof(addr->storage); - - if (!sqe) - perrordie("io_uring_sqe"); + struct io_uring_sqe *sqe; if (task->fd < 0) { error("uring_accept called with no fd set\n"); return; } + sqe = get_sqe(cfg); + addr->addrlen = sizeof(addr->storage); uring_task_get(cfg, task); task->callback = callback; io_uring_prep_accept(sqe, task->fd, (struct sockaddr *)&addr->storage, &addr->addrlen, 0); @@ -259,16 +259,14 @@ uring_accept(struct cfg *cfg, struct uring_task *task, struct sockaddr_in46 *add void uring_poll(struct cfg *cfg, struct uring_task *task, short poll_mask, callback_t callback) { - struct io_uring_sqe *sqe = io_uring_get_sqe(&cfg->uev->uring); + struct io_uring_sqe *sqe; - if (!sqe) - perrordie("io_uring_sqe"); - if (task->fd < 0) { error("uring_poll called with no fd set\n"); return; } + sqe = get_sqe(cfg); uring_task_get(cfg, task); task->callback = callback; io_uring_prep_poll_add(sqe, task->fd, poll_mask); @@ -278,16 +276,14 @@ uring_poll(struct cfg *cfg, struct uring_task *task, short poll_mask, callback_t void uring_poll_cancel(struct cfg *cfg, struct uring_task *task) { - struct io_uring_sqe *sqe = io_uring_get_sqe(&cfg->uev->uring); + struct io_uring_sqe *sqe; - if (!sqe) - perrordie("io_uring_sqe"); - if (task->fd < 0) { error("uring_poll_cancel called with no fd set\n"); return; } + sqe = get_sqe(cfg); task->dead = true; io_uring_prep_poll_remove(sqe, task); io_uring_sqe_set_data(sqe, NULL); @@ -296,11 +292,8 @@ uring_poll_cancel(struct cfg *cfg, struct uring_task *task) void uring_cancel(struct cfg *cfg, struct uring_task *task) { - struct io_uring_sqe *sqe = io_uring_get_sqe(&cfg->uev->uring); + struct io_uring_sqe *sqe = get_sqe(cfg); - if (!sqe) - perrordie("io_uring_sqe"); - task->dead = true; io_uring_prep_cancel(sqe, task, 0); io_uring_sqe_set_data(sqe, NULL); |