From 7440c4a5a7aa0a1eb67c303afbc56c23e103e3e4 Mon Sep 17 00:00:00 2001 From: Alcaro Date: Tue, 17 Feb 2015 00:15:29 +0100 Subject: [PATCH] (nbio) Change this API around a little. --- nbio/nbio.h | 7 ++++++- nbio/nbio_stdio.c | 19 +++++++++++++------ nbio/nbio_test.c | 12 ++---------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/nbio/nbio.h b/nbio/nbio.h index 7e52af24d6..e9dde535a2 100644 --- a/nbio/nbio.h +++ b/nbio/nbio.h @@ -32,7 +32,7 @@ void nbio_begin_write(struct nbio_t* handle); * Performs part of the requested operation, or checks how it's going. * When it returns true, it's done. */ -bool nbio_iterate(struct nbio_t* handle, size_t* progress, size_t* len); +bool nbio_iterate(struct nbio_t* handle); /* * Resizes the file up to the given size; cannot shrink. @@ -46,6 +46,11 @@ void nbio_resize(struct nbio_t* handle, size_t len); */ void* nbio_get_ptr(struct nbio_t* handle, size_t* len); +/* + * Stops any pending operation, allowing the object to be freed. + */ +void nbio_cancel(struct nbio_t* handle); + /* * Deletes the nbio structure and its associated pointer. */ diff --git a/nbio/nbio_stdio.c b/nbio/nbio_stdio.c index 9695fd9c6f..4ab03be0cb 100644 --- a/nbio/nbio_stdio.c +++ b/nbio/nbio_stdio.c @@ -85,7 +85,7 @@ void nbio_begin_write(struct nbio_t* handle) handle->progress = 0; } -bool nbio_iterate(struct nbio_t* handle, size_t* progress, size_t* len) +bool nbio_iterate(struct nbio_t* handle) { size_t amount = 65536; @@ -99,11 +99,6 @@ bool nbio_iterate(struct nbio_t* handle, size_t* progress, size_t* len) handle->progress += amount; - if (progress) - *progress = handle->progress; - if (len) - *len = handle->len; - if (handle->progress == handle->len) handle->op = -1; return (handle->op < 0); @@ -125,6 +120,7 @@ void nbio_resize(struct nbio_t* handle, size_t len) handle->len = len; handle->data = realloc(handle->data, handle->len); handle->op = -1; + handle->progress = handle->len; } void* nbio_get_ptr(struct nbio_t* handle, size_t* len) @@ -136,8 +132,19 @@ void* nbio_get_ptr(struct nbio_t* handle, size_t* len) return NULL; } +void nbio_cancel(struct nbio_t* handle) +{ + handle->op = -1; + handle->progress = handle->len; +} + void nbio_free(struct nbio_t* handle) { + if (handle->op >= 0) + { + puts("ERROR - attempted free() while busy"); + abort(); + } if (!handle) return; fclose(handle->f); diff --git a/nbio/nbio_test.c b/nbio/nbio_test.c index 103d180571..4e2f4ab9c2 100644 --- a/nbio/nbio_test.c +++ b/nbio/nbio_test.c @@ -18,11 +18,7 @@ static void nbio_write_test(void) memset(ptr, 0x42, 1024*1024); nbio_begin_write(write); - while (!nbio_iterate(write, &prog, &size)) - { - printf("%u/%u\n", (unsigned)prog, (unsigned)size); - looped=true; - } + while (!nbio_iterate(write, &prog, &size)) looped=true; if (!looped) puts("Write finished immediately?"); @@ -44,11 +40,7 @@ static void nbio_read_test(void) nbio_begin_read(read); - while (!nbio_iterate(read, &prog, &size)) - { - printf("%u/%u\n", (unsigned)prog, (unsigned)size); - looped=true; - } + while (!nbio_iterate(read)) looped=true; if (!looped) puts("Read finished immediately?");