]> mj.ucw.cz Git - libucw.git/blob - ucw/fb-pool.c
Better names for direct IO switches
[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->buffer = mp_expand(FB_POOL(b)->mp);
26       b->bufend = b->buffer + mp_avail(FB_POOL(b)->mp);
27       b->bstop = b->buffer;
28       b->bptr = b->buffer + len;
29     }
30 }
31
32 void
33 fbpool_start(struct fbpool *b, struct mempool *mp, uns init_size)
34 {
35   b->mp = mp;
36   b->fb.buffer = b->fb.bstop = b->fb.bptr = mp_start(mp, init_size);
37   b->fb.bufend = b->fb.buffer + mp_avail(mp);
38 }
39
40 void *
41 fbpool_end(struct fbpool *b)
42 {
43   return mp_end(b->mp, b->fb.bptr); 
44 }
45
46 void
47 fbpool_init(struct fbpool *b)
48 {
49   bzero(b, sizeof(*b));
50   b->fb.name = "<fbpool>";
51   b->fb.spout = fbpool_spout;
52   b->fb.can_overwrite_buffer = 1;
53 }
54
55 #ifdef TEST
56
57 int main(void)
58 {
59   struct mempool *mp;
60   struct fbpool fb;
61   byte *p;
62   uns l;
63   
64   mp = mp_new(64);
65   fbpool_init(&fb);
66   fbpool_start(&fb, mp, 16);
67   for (uns i = 0; i < 1024; i++)
68     bprintf(&fb.fb, "<hello>");
69   p = fbpool_end(&fb);
70   l = mp_size(mp, p);
71   if (l != 1024 * 7)
72     ASSERT(0);
73   for (uns i = 0; i < 1024; i++)
74     if (memcmp(p + i * 7, "<hello>", 7))
75       ASSERT(0);
76   mp_delete(mp);
77   
78   return 0;
79 }
80
81 #endif