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,7 +11,21 @@
#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)
{ {
@ -39,28 +53,15 @@ static int io_getevents(aio_context_t ctx, long min_nr, long nr,
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)
fd = open(filename, o_flags[mode]|O_CLOEXEC, 0644); return NULL;
if (fd < 0) return NULL;
if (io_setup(128, &ctx) < 0) if (io_setup(128, &ctx) < 0)
{ {
@ -74,6 +75,7 @@ struct nbio_t* nbio_open(const char * filename, unsigned mode)
handle->len = lseek(fd, 0, SEEK_END); handle->len = lseek(fd, 0, SEEK_END);
handle->ptr = malloc(handle->len); handle->ptr = malloc(handle->len);
handle->busy = false; handle->busy = false;
return handle; return handle;
} }
@ -114,10 +116,8 @@ bool nbio_iterate(struct nbio_t* handle)
{ {
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;
} }
@ -125,7 +125,8 @@ 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
* won't work on other nbio implementations */
/* therefore, it's blocked so nobody accidentally relies on it */ /* therefore, it's blocked so nobody accidentally relies on it */
puts("ERROR - attempted file shrink operation, not implemented"); puts("ERROR - attempted file shrink operation, not implemented");
abort(); abort();
@ -133,7 +134,8 @@ void nbio_resize(struct nbio_t* handle, size_t len)
if (ftruncate(handle->fd, len) != 0) if (ftruncate(handle->fd, len) != 0)
{ {
puts("ERROR - couldn't resize file (ftruncate)"); 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 */ abort(); /* this one returns void and I can't find any other way
for it to report failure */
} }
handle->ptr = realloc(handle->ptr, len); handle->ptr = realloc(handle->ptr, len);
handle->len = len; handle->len = len;
@ -141,13 +143,20 @@ void nbio_resize(struct nbio_t* handle, size_t 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)
return;
if (handle->busy) if (handle->busy)
{ {
struct io_event ev; struct io_event ev;
@ -158,6 +167,9 @@ void nbio_cancel(struct nbio_t* handle)
void nbio_free(struct nbio_t* handle) void nbio_free(struct nbio_t* handle)
{ {
if (!handle)
return;
io_destroy(handle->ctx); io_destroy(handle->ctx);
close(handle->fd); close(handle->fd);
free(handle->ptr); free(handle->ptr);