]> mj.ucw.cz Git - libucw.git/commitdiff
simple fastbuf on memory pools
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Wed, 6 Jun 2007 07:03:20 +0000 (09:03 +0200)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Wed, 6 Jun 2007 07:03:20 +0000 (09:03 +0200)
lib/Makefile
lib/fastbuf.h
lib/fb-pool.c [new file with mode: 0644]
lib/fb-pool.t [new file with mode: 0644]

index c23ac8c576850cda75d49f51a718735c1ec3d892..80d1c3a2322197d0e512b90e7ce8278aa316fd59 100644 (file)
@@ -17,7 +17,7 @@ LIBUCW_MODS= \
        ipaccess \
        profile \
        fastbuf ff-binary ff-string ff-printf ff-utf8 \
-       fb-file carefulio fb-mem fb-temp fb-mmap fb-limfd fb-buffer fb-grow fb-atomic fb-param \
+       fb-file carefulio fb-mem fb-temp fb-mmap fb-limfd fb-buffer fb-grow fb-pool fb-atomic fb-param \
        str_ctype str_upper str_lower unicode-utf8 stkstring \
        wildmatch wordsplit ctmatch patimatch patmatch regex \
        prime primetable random timer randomkey \
@@ -99,7 +99,7 @@ $(o)/lib/kmp-test: $(o)/lib/kmp-test.o $(LIBUCW) $(LIBCHARSET)
 $(o)/lib/ipaccess-test: $(o)/lib/ipaccess-test.o $(LIBUCW)
 
 TESTS+=$(addprefix $(o)/lib/,regex.test unicode-utf8.test hash-test.test mempool.test stkstring.test \
-    slists.test kmp-test.test bbuf.test getopt.test)
+    slists.test kmp-test.test bbuf.test getopt.test fb-pool.test)
 
 $(o)/lib/regex.test: $(o)/lib/regex-t
 $(o)/lib/unicode-utf8.test: $(o)/lib/unicode-utf8-t
@@ -111,6 +111,7 @@ $(o)/lib/slists.test: $(o)/lib/slists-t
 $(o)/lib/kmp-test.test: $(o)/lib/kmp-test
 $(o)/lib/bbuf.test: $(o)/lib/bbuf-t
 $(o)/lib/getopt.test: $(o)/lib/getopt-t
+$(o)/lib/fb-pool.test: $(o)/lib/fb-pool-t
 
 ifdef CONFIG_UCW_THREADS
 TESTS+=$(addprefix $(o)/lib/,asio.test)
index 8535425335c04b1616fd024bedd34c49ecefd4df..1020427b5d80f301b4044a6dd2d79d55742f3f37 100644 (file)
@@ -144,6 +144,18 @@ struct fastbuf *fbgrow_create(unsigned basic_size);
 void fbgrow_reset(struct fastbuf *b);                  /* Reset stream and prepare for writing */
 void fbgrow_rewind(struct fastbuf *b);                 /* Prepare for reading */
 
+/* FastO on memory pools */
+
+struct mempool;
+struct fbpool {
+  struct fastbuf fb;
+  struct mempool *mp;
+};
+
+void fbpool_init(struct fbpool *fb);
+void fbpool_start(struct fbpool *fb, struct mempool *mp, uns init_size);
+void *fbpool_end(struct fbpool *fb);
+
 /* FastO with atomic writes for multi-threaded programs */
 
 struct fb_atomic {
diff --git a/lib/fb-pool.c b/lib/fb-pool.c
new file mode 100644 (file)
index 0000000..e847b7d
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ *     UCW Library -- Fast Buffered I/O on Memory Pools
+ *
+ *     (c) 2007 Pavel Charvat <pchar@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#include "lib/lib.h"
+#include "lib/mempool.h"
+#include "lib/fastbuf.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define FB_POOL(f) ((struct fbpool *)(f)->is_fastbuf)
+
+static void
+fbpool_spout(struct fastbuf *b)
+{
+  if (b->bptr >= b->bufend)
+    {
+      uns len = b->bufend - b->buffer;
+      b->buffer = mp_expand(FB_POOL(b)->mp);
+      b->bufend = b->buffer + mp_avail(FB_POOL(b)->mp);
+      b->bstop = b->buffer;
+      b->bptr = b->buffer + len;
+    }
+}
+
+void
+fbpool_start(struct fbpool *b, struct mempool *mp, uns init_size)
+{
+  b->mp = mp;
+  b->fb.buffer = b->fb.bstop = b->fb.bptr = mp_start(mp, init_size);
+  b->fb.bufend = b->fb.buffer + mp_avail(mp);
+}
+
+void *
+fbpool_end(struct fbpool *b)
+{
+  return mp_end(b->mp, b->fb.bptr); 
+}
+
+void
+fbpool_init(struct fbpool *b)
+{
+  bzero(b, sizeof(*b));
+  b->fb.name = "<fbpool>";
+  b->fb.spout = fbpool_spout;
+  b->fb.can_overwrite_buffer = 1;
+}
+
+#ifdef TEST
+
+int main(void)
+{
+  struct mempool *mp;
+  struct fbpool fb;
+  byte *p;
+  uns l;
+  
+  mp = mp_new(64);
+  fbpool_init(&fb);
+  fbpool_start(&fb, mp, 16);
+  for (uns i = 0; i < 1024; i++)
+    bprintf(&fb.fb, "<hello>");
+  p = fbpool_end(&fb);
+  l = mp_size(mp, p);
+  if (l != 1024 * 7)
+    ASSERT(0);
+  for (uns i = 0; i < 1024; i++)
+    if (memcmp(p + i * 7, "<hello>", 7))
+      ASSERT(0);
+  mp_delete(mp);
+  
+  return 0;
+}
+
+#endif
diff --git a/lib/fb-pool.t b/lib/fb-pool.t
new file mode 100644 (file)
index 0000000..8cf7938
--- /dev/null
@@ -0,0 +1,3 @@
+# Tests for fastbufs on memory pools
+
+Run:   obj/lib/fb-pool-t