Style nits

This commit is contained in:
twinaphex 2017-11-25 02:18:24 +01:00
parent ac651b7581
commit 395e84ede2
1 changed files with 108 additions and 96 deletions

View File

@ -11,155 +11,167 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include <linux/aio_abi.h> #include <linux/aio_abi.h>
/* there's also a Unix AIO thingy, but it's not in glibc and we don't want more dependencies */ struct nbio_t
{
int fd;
bool busy;
aio_context_t ctx;
struct iocb cb;
void* ptr;
size_t len;
};
/* there's also a Unix AIO thingy, but it's not in glibc
* and we don't want more dependencies */
static int io_setup(unsigned nr, aio_context_t * ctxp) static int io_setup(unsigned nr, aio_context_t * ctxp)
{ {
return syscall(__NR_io_setup, nr, ctxp); return syscall(__NR_io_setup, nr, ctxp);
} }
static int io_destroy(aio_context_t ctx) static int io_destroy(aio_context_t ctx)
{ {
return syscall(__NR_io_destroy, ctx); return syscall(__NR_io_destroy, ctx);
} }
static int io_submit(aio_context_t ctx, long nr, struct iocb ** cbp) static int io_submit(aio_context_t ctx, long nr, struct iocb ** cbp)
{ {
return syscall(__NR_io_submit, ctx, nr, cbp); return syscall(__NR_io_submit, ctx, nr, cbp);
} }
static int io_cancel(aio_context_t ctx, struct iocb * iocb, struct io_event * result) static int io_cancel(aio_context_t ctx, struct iocb * iocb, struct io_event * result)
{ {
return syscall(__NR_io_cancel, ctx, iocb, result); return syscall(__NR_io_cancel, ctx, iocb, result);
} }
static int io_getevents(aio_context_t ctx, long min_nr, long nr, static int io_getevents(aio_context_t ctx, long min_nr, long nr,
struct io_event * events, struct timespec * timeout) struct io_event * events, struct timespec * timeout)
{ {
return syscall(__NR_io_getevents, ctx, min_nr, nr, events, timeout); return syscall(__NR_io_getevents, ctx, min_nr, nr, events, timeout);
} }
struct nbio_t
{
int fd;
bool busy;
aio_context_t ctx;
struct iocb cb;
void* ptr;
size_t len;
};
struct nbio_t* nbio_open(const char * filename, unsigned mode) struct nbio_t* nbio_open(const char * filename, unsigned mode)
{ {
static const int o_flags[] = { O_RDONLY, O_RDWR|O_CREAT|O_TRUNC, O_RDWR, O_RDONLY, O_RDWR|O_CREAT|O_TRUNC }; static const int o_flags[] = { O_RDONLY, O_RDWR|O_CREAT|O_TRUNC, O_RDWR, O_RDONLY, O_RDWR|O_CREAT|O_TRUNC };
aio_context_t ctx = 0; aio_context_t ctx = 0;
struct nbio_t* handle; struct nbio_t* handle = NULL;
int fd; int fd = open(filename, o_flags[mode]|O_CLOEXEC, 0644);
if (fd < 0)
return NULL;
fd = open(filename, o_flags[mode]|O_CLOEXEC, 0644); if (io_setup(128, &ctx) < 0)
if (fd < 0) return NULL; {
close(fd);
return NULL;
}
if (io_setup(128, &ctx) < 0) handle = malloc(sizeof(struct nbio_t));
{ handle->fd = fd;
close(fd); handle->ctx = ctx;
return NULL; handle->len = lseek(fd, 0, SEEK_END);
} handle->ptr = malloc(handle->len);
handle->busy = false;
handle = malloc(sizeof(struct nbio_t)); return handle;
handle->fd = fd;
handle->ctx = ctx;
handle->len = lseek(fd, 0, SEEK_END);
handle->ptr = malloc(handle->len);
handle->busy = false;
return handle;
} }
static void nbio_begin_op(struct nbio_t* handle, uint16_t op) static void nbio_begin_op(struct nbio_t* handle, uint16_t op)
{ {
struct iocb * cbp = &handle->cb; struct iocb * cbp = &handle->cb;
memset(&handle->cb, 0, sizeof(handle->cb)); memset(&handle->cb, 0, sizeof(handle->cb));
handle->cb.aio_fildes = handle->fd; handle->cb.aio_fildes = handle->fd;
handle->cb.aio_lio_opcode = op; handle->cb.aio_lio_opcode = op;
handle->cb.aio_buf = (uint64_t)(uintptr_t)handle->ptr; handle->cb.aio_buf = (uint64_t)(uintptr_t)handle->ptr;
handle->cb.aio_offset = 0; handle->cb.aio_offset = 0;
handle->cb.aio_nbytes = handle->len; handle->cb.aio_nbytes = handle->len;
if (io_submit(handle->ctx, 1, &cbp) != 1) if (io_submit(handle->ctx, 1, &cbp) != 1)
{ {
puts("ERROR - io_submit() failed"); puts("ERROR - io_submit() failed");
abort(); abort();
} }
handle->busy = true; handle->busy = true;
} }
void nbio_begin_read(struct nbio_t* handle) void nbio_begin_read(struct nbio_t* handle)
{ {
nbio_begin_op(handle, IOCB_CMD_PREAD); nbio_begin_op(handle, IOCB_CMD_PREAD);
} }
void nbio_begin_write(struct nbio_t* handle) void nbio_begin_write(struct nbio_t* handle)
{ {
nbio_begin_op(handle, IOCB_CMD_PWRITE); nbio_begin_op(handle, IOCB_CMD_PWRITE);
} }
bool nbio_iterate(struct nbio_t* handle) bool nbio_iterate(struct nbio_t* handle)
{ {
if (handle->busy) if (handle->busy)
{ {
struct io_event ev; struct io_event ev;
if (io_getevents(handle->ctx, 0, 1, &ev, NULL) == 1) if (io_getevents(handle->ctx, 0, 1, &ev, NULL) == 1)
{ handle->busy = false;
handle->busy = false; }
} return !handle->busy;
}
return !handle->busy;
} }
void nbio_resize(struct nbio_t* handle, size_t len) void nbio_resize(struct nbio_t* handle, size_t len)
{ {
if (len < handle->len) if (len < handle->len)
{ {
/* this works perfectly fine if this check is removed, but it won't work on other nbio implementations */ /* this works perfectly fine if this check is removed, but it
/* therefore, it's blocked so nobody accidentally relies on it */ * won't work on other nbio implementations */
puts("ERROR - attempted file shrink operation, not implemented"); /* therefore, it's blocked so nobody accidentally relies on it */
abort(); puts("ERROR - attempted file shrink operation, not implemented");
} abort();
if (ftruncate(handle->fd, len) != 0) }
{ if (ftruncate(handle->fd, len) != 0)
puts("ERROR - couldn't resize file (ftruncate)"); {
abort(); /* this one returns void and I can't find any other way for it to report failure */ puts("ERROR - couldn't resize file (ftruncate)");
} abort(); /* this one returns void and I can't find any other way
handle->ptr = realloc(handle->ptr, len); for it to report failure */
handle->len = len; }
handle->ptr = realloc(handle->ptr, len);
handle->len = len;
} }
void* nbio_get_ptr(struct nbio_t* handle, size_t* len) void* nbio_get_ptr(struct nbio_t* handle, size_t* len)
{ {
if (len) *len = handle->len; if (!handle)
if (!handle->busy) return handle->ptr; return NULL;
else return NULL; if (len)
*len = handle->len;
if (!handle->busy)
return handle->ptr;
return NULL;
} }
void nbio_cancel(struct nbio_t* handle) void nbio_cancel(struct nbio_t* handle)
{ {
if (handle->busy) if (!handle)
{ return;
struct io_event ev;
io_cancel(handle->ctx, &handle->cb, &ev); if (handle->busy)
handle->busy = false; {
} struct io_event ev;
io_cancel(handle->ctx, &handle->cb, &ev);
handle->busy = false;
}
} }
void nbio_free(struct nbio_t* handle) void nbio_free(struct nbio_t* handle)
{ {
io_destroy(handle->ctx); if (!handle)
close(handle->fd); return;
free(handle->ptr);
free(handle); io_destroy(handle->ctx);
close(handle->fd);
free(handle->ptr);
free(handle);
} }