This commit is contained in:
twinaphex 2017-11-25 05:21:35 +01:00
parent 64a6d6f184
commit 81e543a4c9
2 changed files with 16 additions and 6 deletions

View File

@ -81,15 +81,16 @@ struct nbio_t* nbio_open(const char * filename, unsigned mode)
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));
handle->cb.aio_fildes = handle->fd;
handle->cb.aio_fildes = handle->fd;
handle->cb.aio_lio_opcode = op;
handle->cb.aio_buf = (uint64_t)(uintptr_t)handle->ptr;
handle->cb.aio_offset = 0;
handle->cb.aio_nbytes = handle->len;
handle->cb.aio_buf = (uint64_t)(uintptr_t)handle->ptr;
handle->cb.aio_offset = 0;
handle->cb.aio_nbytes = handle->len;
if (io_submit(handle->ctx, 1, &cbp) != 1)
{
@ -123,6 +124,9 @@ bool nbio_iterate(struct nbio_t* handle)
void nbio_resize(struct nbio_t* handle, size_t len)
{
if (!handle)
return;
if (len < handle->len)
{
/* this works perfectly fine if this check is removed, but it
@ -131,6 +135,7 @@ void nbio_resize(struct nbio_t* handle, size_t len)
puts("ERROR - attempted file shrink operation, not implemented");
abort();
}
if (ftruncate(handle->fd, len) != 0)
{
puts("ERROR - couldn't resize file (ftruncate)");

View File

@ -96,7 +96,10 @@ void nbio_resize(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)
return NULL;
if (len)
*len = handle->len;
return handle->ptr;
}
@ -107,6 +110,8 @@ void nbio_cancel(struct nbio_t* handle)
void nbio_free(struct nbio_t* handle)
{
if (!handle)
return;
close(handle->fd);
munmap(handle->ptr, handle->len);
free(handle);