From 87c0882e7f881047a4a6fae0875f481cd490cac4 Mon Sep 17 00:00:00 2001 From: nico Date: Thu, 19 Jul 2007 00:20:41 +0000 Subject: [PATCH] Initial implementation of buffer extending. Debug output reduced. git-svn-id: svn://jdgordon.mine.nu/mob@62 9862a28c-4e93-4879-ac26-10afcf840a8f --- testplugin.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/testplugin.c b/testplugin.c index c954b5e..37b0d58 100644 --- a/testplugin.c +++ b/testplugin.c @@ -119,7 +119,6 @@ static struct memory_handle *add_handle(size_t *data_size) /* make sure buf_widx is 32-bit aligned so that the handle struct is, but before that we check we can actually align. */ if (RINGBUF_ADD_CROSS(buf_widx, 3, buf_ridx) >= 0) { - DEBUGF("no space to even align\n"); return NULL; } buf_widx = (RINGBUF_ADD(buf_widx, 3)) & ~3; @@ -134,7 +133,6 @@ static struct memory_handle *add_handle(size_t *data_size) len -= overlap + 1; } if (len < sizeof(struct memory_handle)) { - DEBUGF("no space to add the handle\n"); return NULL; } @@ -333,7 +331,7 @@ static ssize_t buffer_handle(int handle_id) break; /* rc is the actual amount read */ - int rc = rb->read(h->fd, &buffer[RINGBUF_ADD(h->data, h->widx)], copy_n); + int rc = rb->read(h->fd, &buffer[RINGBUF_ADD(h->data,h->widx)], copy_n); if (rc < 0) { @@ -348,6 +346,24 @@ static ssize_t buffer_handle(int handle_id) h->available += rc; ret += rc; h->filerem -= rc; + + if (h->ridx <= h->widx && h->data_len < h->filerem) { + /* Check whether we can extend the buffer to avoid unnecessary + wrapping. */ + size_t extend_by = 0; + if (h == cur_handle) { + extend_by = RINGBUF_SUB(buf_ridx, buf_widx) - 1; + } else { + extend_by = RINGBUF_SUB((size_t)((void*)h->next-(void*)buffer), + RINGBUF_ADD(h->data, h->data_len)); + } + + h->data_len += extend_by; + if (extend_by > 0) + DEBUGF("extended the handle data buffer by %ld bytes\n", + extend_by); + } + } if (h->filerem == 0) { @@ -378,7 +394,7 @@ static void free_buffer(int handle_id) DEBUGF("old state: data: %ld, data_len: %ld, avail: %ld, ridx: %ld, widx: %ld\n", h->data, h->data_len, h->available, h->ridx, h->widx); - if (h->widx != 0 && h->ridx > h->widx) { + if (h->ridx > h->widx) { /* We don't want to overwrite a useful part of the buffer */ return; } @@ -459,14 +475,14 @@ static size_t used_space(void) */ int bufopen(char *file, size_t offset) { - DEBUGF("bufopen: %s (offset: %ld)\n", file, offset); - int fd = rb->open(file, O_RDONLY); if (fd < 0) return -1; size_t size = rb->filesize(fd) - offset + 1; - DEBUGF("we want to allocate %ld bytes\n", size); + DEBUGF("bufopen: %s (offset: %ld) (%ld bytes needed)... ", + file, offset, size); + struct memory_handle *h = add_handle(&size); if (!h) { @@ -474,7 +490,6 @@ int bufopen(char *file, size_t offset) rb->close(fd); return -1; } - DEBUGF("allocated %ld bytes\n", size); if (offset) rb->lseek(fd, offset, SEEK_SET); rb->strncpy(h->path, file, MAX_PATH); @@ -490,7 +505,7 @@ int bufopen(char *file, size_t offset) buf_widx = RINGBUF_ADD(buf_widx, size); - DEBUGF("added handle : %d\n", h->id); + DEBUGF("allocated %ld bytes. ID: %d\n", size, h->id); return h->id; } -- 2.11.4.GIT