From 9458ba8d8926a5b6b5fd5e7aed78286c5b087fdf Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Wed, 6 Jun 2007 09:03:20 +0200 Subject: [PATCH] simple fastbuf on memory pools --- lib/Makefile | 5 ++-- lib/fastbuf.h | 12 ++++++++ lib/fb-pool.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/fb-pool.t | 3 ++ 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 lib/fb-pool.c create mode 100644 lib/fb-pool.t diff --git a/lib/Makefile b/lib/Makefile index c23ac8c5..80d1c3a2 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -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) diff --git a/lib/fastbuf.h b/lib/fastbuf.h index 85354253..1020427b 100644 --- a/lib/fastbuf.h +++ b/lib/fastbuf.h @@ -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 index 00000000..e847b7d0 --- /dev/null +++ b/lib/fb-pool.c @@ -0,0 +1,81 @@ +/* + * UCW Library -- Fast Buffered I/O on Memory Pools + * + * (c) 2007 Pavel Charvat + * + * 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 +#include + +#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 = ""; + 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, ""); + 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, "", 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 index 00000000..8cf7938e --- /dev/null +++ b/lib/fb-pool.t @@ -0,0 +1,3 @@ +# Tests for fastbufs on memory pools + +Run: obj/lib/fb-pool-t -- 2.39.2