]> mj.ucw.cz Git - libucw.git/blob - ucw/fb-pool.c
* Fixed several bugs in fastbufs. * Implemented vdie(). * fb-grow can be allocated...
[libucw.git] / ucw / fb-pool.c
1 /*
2  *      UCW Library -- Fast Buffered I/O on Memory Pools
3  *
4  *      (c) 2007 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "ucw/lib.h"
11 #include "ucw/mempool.h"
12 #include "ucw/fastbuf.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #define FB_POOL(f) ((struct fbpool *)(f)->is_fastbuf)
18
19 static void
20 fbpool_spout(struct fastbuf *b)
21 {
22   if (b->bptr == b->bufend)
23     {
24       uns len = b->bufend - b->buffer;
25       b->bstop = b->buffer = mp_expand(FB_POOL(b)->mp);
26       b->bufend = b->buffer + mp_avail(FB_POOL(b)->mp);
27       b->bptr = b->buffer + len;
28     }
29 }
30
31 void
32 fbpool_start(struct fbpool *b, struct mempool *mp, uns init_size)
33 {
34   b->mp = mp;
35   b->fb.buffer = b->fb.bstop = b->fb.bptr = mp_start(mp, init_size);
36   b->fb.bufend = b->fb.buffer + mp_avail(mp);
37 }
38
39 void *
40 fbpool_end(struct fbpool *b)
41 {
42   return mp_end(b->mp, b->fb.bptr);
43 }
44
45 void
46 fbpool_init(struct fbpool *b)
47 {
48   bzero(b, sizeof(*b));
49   b->fb.name = "<fbpool>";
50   b->fb.spout = fbpool_spout;
51   b->fb.can_overwrite_buffer = 1;
52 }
53
54 #ifdef TEST
55
56 int main(void)
57 {
58   struct mempool *mp;
59   struct fbpool fb;
60   byte *p;
61   uns l;
62
63   mp = mp_new(64);
64   fbpool_init(&fb);
65   fbpool_start(&fb, mp, 16);
66   for (uns i = 0; i < 1024; i++)
67     bprintf(&fb.fb, "<hello>");
68   p = fbpool_end(&fb);
69   l = mp_size(mp, p);
70   if (l != 1024 * 7)
71     ASSERT(0);
72   for (uns i = 0; i < 1024; i++)
73     if (memcmp(p + i * 7, "<hello>", 7))
74       ASSERT(0);
75   mp_delete(mp);
76
77   return 0;
78 }
79
80 #endif