From 3af2b0c0ac1fc2536c8fca7b86d5f7455d82c763 Mon Sep 17 00:00:00 2001 From: Richard Nyberg Date: Sun, 11 Jan 2009 00:34:46 +0100 Subject: Make iobuf more useful for io and use better names in its api. --- misc/iobuf.c | 77 ++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 25 deletions(-) (limited to 'misc/iobuf.c') diff --git a/misc/iobuf.c b/misc/iobuf.c index 45918ab..fa5e33f 100644 --- a/misc/iobuf.c +++ b/misc/iobuf.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,44 +6,66 @@ #include #include "iobuf.h" +#include "subr.h" -#define GROWLEN (1 << 14) - -struct io_buffer -buf_init(size_t size) +struct iobuf +iobuf_init(size_t size) { - struct io_buffer iob; + struct iobuf iob; + iob.size = size; iob.off = 0; - iob.len = size; + iob.skip = 0; iob.error = (iob.buf = malloc(size)) == NULL ? 1 : 0; return iob; } void -buf_free(struct io_buffer *iob) +iobuf_free(struct iobuf *iob) { if (iob->buf != NULL) - free(iob->buf); + free(iob->buf - iob->skip); + iob->buf = NULL; + iob->error = 1; +} + +void +iobuf_consumed(struct iobuf *iob, size_t count) +{ + if (iob->error) + return; + assert(count <= iob->off); + iob->skip += count; + iob->buf += count; + iob->off -= count; } int -buf_grow(struct io_buffer *iob, size_t addlen) +iobuf_accommodate(struct iobuf *iob, size_t count) { if (iob->error) - return iob->error; - char *nbuf = realloc(iob->buf, iob->len + addlen); - if (nbuf == NULL) { - iob->error = 1; return 0; + size_t esize = iob->size - (iob->skip + iob->off); + if (esize >= count) + return 1; + else if (esize + iob->skip >= count) { + bcopy(iob->buf, iob->buf - iob->skip, iob->off); + iob->buf -= iob->skip; + iob->skip = 0; + return 1; } else { - iob->buf = nbuf; - iob->len += addlen; + uint8_t *nbuf = realloc(iob->buf - iob->skip, iob->size + count); + if (nbuf == NULL) { + iob->error = 1; + return 0; + } + iob->buf = nbuf + iob->skip; + iob->size += count; return 1; } } int -buf_print(struct io_buffer *iob, const char *fmt, ...) +iobuf_print(struct iobuf *iob, const char *fmt, ...) { if (iob->error) return 0; @@ -51,9 +74,8 @@ buf_print(struct io_buffer *iob, const char *fmt, ...) va_start(ap, fmt); np = vsnprintf(NULL, 0, fmt, ap); va_end(ap); - if (np + 1 > iob->len - iob->off) - if (!buf_grow(iob, (1 + (np + 1) / GROWLEN) * GROWLEN)) - return 0; + if (!iobuf_accommodate(iob, np + 1)) + return 0; va_start(ap, fmt); vsnprintf(iob->buf + iob->off, np + 1, fmt, ap); va_end(ap); @@ -62,14 +84,19 @@ buf_print(struct io_buffer *iob, const char *fmt, ...) } int -buf_write(struct io_buffer *iob, const void *buf, size_t len) +iobuf_write(struct iobuf *iob, const void *buf, size_t count) { if (iob->error) return 0; - if (len > iob->len - iob->off) - if (!buf_grow(iob, (1 + len / GROWLEN) * GROWLEN)) - return 0; - bcopy(buf, iob->buf + iob->off, len); - iob->off += len; + if (!iobuf_accommodate(iob, count)) + return 0; + bcopy(buf, iob->buf + iob->off, count); + iob->off += count; return 1; } + +void * +iobuf_find(struct iobuf *iob, const void *p, size_t plen) +{ + return iob->error ? NULL : memfind(p, plen, iob->buf, iob->off); +} -- cgit 1.4.1