(nbio) Change this API around a little.

This commit is contained in:
Alcaro 2015-02-17 00:15:29 +01:00
parent 16b2a5877a
commit 7440c4a5a7
3 changed files with 21 additions and 17 deletions

View File

@ -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.
*/

View File

@ -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);

View File

@ -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?");