From 1982a5aaabc6b8dc74507cfa9893d9aff69a35f3 Mon Sep 17 00:00:00 2001 From: nico Date: Mon, 2 Jul 2007 02:25:07 +0000 Subject: [PATCH] buffer_handle improvement to make it accomodate several calls. I changed the main() to suit my test: I give it 3 files of ~35 MB so the third isn't completely buffered at first and after unbuffering the first two we can finish buffering it and it wraps around the end of the buffer. git-svn-id: svn://jdgordon.mine.nu/mob@6 9862a28c-4e93-4879-ac26-10afcf840a8f --- buffering.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/buffering.c b/buffering.c index 764256e..e4c1103 100644 --- a/buffering.c +++ b/buffering.c @@ -158,6 +158,11 @@ static int buffer_handle(int handle_id) if (!h) return -1; + if (h->filerem == 0) { + /* nothing left to buffer */ + return 0; + } + if (h->fd < 0) /* file closed, reopen */ { if (*h->path) @@ -172,6 +177,7 @@ static int buffer_handle(int handle_id) lseek(h->fd, h->offset, SEEK_SET); } + int ret = 0; while (h->filerem > 0) { /* max amount to copy */ @@ -194,12 +200,18 @@ static int buffer_handle(int handle_id) /* Advance buffer */ buf_widx = RINGBUF_ADD(buf_widx, rc); h->available += rc; + ret += rc; h->filerem -= rc; } - printf("buffered %d of %d bytes (remaining: %d)\n", - h->available, h->filesize, h->filerem); - return h->available; + if (h->filerem == 0) { + /* finished buffering the file */ + close(h->fd); + } + + printf("buffered %d bytes (%d of %d available, remaining: %d)\n", + ret, h->available, h->filesize, h->filerem); + return ret; } /* Request a file be buffered @@ -233,7 +245,7 @@ int bufopen(char *file, size_t offset) h->data = buf_widx; h->available = 0; - printf("added handle : %d\n\n", h->id); + printf("added handle : %d\n", h->id); return h->id; } @@ -300,16 +312,30 @@ int main(int argc, char **argv) buffer_init(); printf("sizeof memory_handle : %d\n", sizeof(struct memory_handle)); + printf("\n"); int i, hdl; for (i = 1; i < argc; i++) { - printf("----\n"); hdl = bufopen(argv[i], 0); buffer_handle(hdl); printf("used: %d, free: %d\n", BUF_USED, BUFFER_SIZE - BUF_USED); - bufclose(hdl); - printf("used: %d, free: %d\n", BUF_USED, BUFFER_SIZE - BUF_USED); + printf("buf_widx: %d\n", buf_widx); + printf("----\n"); } + printf("\n"); + bufclose(0); + printf("used: %d, free: %d\n", BUF_USED, BUFFER_SIZE - BUF_USED); + printf("buf_widx: %d\n", buf_widx); + printf("\n"); + bufclose(1); + printf("used: %d, free: %d\n", BUF_USED, BUFFER_SIZE - BUF_USED); + printf("buf_widx: %d\n", buf_widx); + printf("\n"); + + buffer_handle(2); + printf("used: %d, free: %d\n", BUF_USED, BUFFER_SIZE - BUF_USED); + printf("buf_widx: %d\n", buf_widx); + return 0; } -- 2.11.4.GIT