summary refs log tree commit diff
path: root/misc/iobuf.c
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2009-01-11 00:34:46 +0100
committerRichard Nyberg <rnyberg@murmeldjur.se>2009-01-11 00:34:46 +0100
commit3af2b0c0ac1fc2536c8fca7b86d5f7455d82c763 (patch)
tree3fe666d14011fb536d5737c7eb9b9dddff0feaf8 /misc/iobuf.c
parent186113e4ee392535aa5de02c209fd8f4996545af (diff)
downloadbtpd-3af2b0c0ac1fc2536c8fca7b86d5f7455d82c763.tar.gz
btpd-3af2b0c0ac1fc2536c8fca7b86d5f7455d82c763.zip
Make iobuf more useful for io and use better names in its api.
Diffstat (limited to 'misc/iobuf.c')
-rw-r--r--misc/iobuf.c77
1 files changed, 52 insertions, 25 deletions
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 <assert.h>
 #include <inttypes.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -5,44 +6,66 @@
 #include <string.h>
 
 #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);
+}