2 * UCW Library -- Fast Buffered I/O on itself
4 * (c) 2012 Jan Moskyto Matejka <mq@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/clists.h>
12 #include <ucw/fastbuf.h>
13 #include <ucw/mempool.h>
17 #define FB_MULTI_NAME "<multi>"
27 #define FB_MULTI(f) ((struct fb_multi *)(f))
31 ucw_off_t begin, end, offset;
36 fbmulti_subbuf_get_end(struct subbuf *s)
39 bseek(s->fb, 0, SEEK_END);
40 s->end = s->begin + btell(s->fb);
44 fbmulti_get_ptrs(struct fastbuf *f)
46 f->buffer = FB_MULTI(f)->cur->fb->buffer;
47 f->bptr = FB_MULTI(f)->cur->fb->bptr;
48 f->bstop = FB_MULTI(f)->cur->fb->bstop;
49 f->bufend = FB_MULTI(f)->cur->fb->bufend;
50 f->pos = FB_MULTI(f)->cur->begin + FB_MULTI(f)->cur->fb->pos - FB_MULTI(f)->cur->offset;
54 fbmulti_set_ptrs(struct fastbuf *f)
56 FB_MULTI(f)->cur->fb->bptr = f->bptr;
60 fbmulti_subbuf_next(struct fastbuf *f)
62 struct subbuf *next = clist_next(FB_MULTI(f)->subbufs, &FB_MULTI(f)->cur->n);
66 // Check the end of current buf
69 FB_MULTI(f)->cur->fb->seek(FB_MULTI(f)->cur->fb, FB_MULTI(f)->cur->end - FB_MULTI(f)->cur->begin, SEEK_SET);
71 ASSERT(FB_MULTI(f)->cur->end == f->pos);
74 FB_MULTI(f)->cur->end = f->pos;
76 // Set the beginning of the next buf
85 next->offset = btell(next->fb);
88 next->begin = FB_MULTI(f)->cur->end;
91 FB_MULTI(f)->cur = next;
98 fbmulti_subbuf_prev(struct fastbuf *f)
100 // Called only when seeking, assuming everything seekable
101 struct subbuf *prev = clist_prev(FB_MULTI(f)->subbufs, &FB_MULTI(f)->cur->n);
102 ASSERT(prev != NULL);
104 // Set pos to beginning, flush offset
105 bsetpos(prev->fb, 0);
109 FB_MULTI(f)->cur = prev;
116 fbmulti_refill(struct fastbuf *f)
120 uns len = FB_MULTI(f)->cur->fb->refill(FB_MULTI(f)->cur->fb);
127 // Current buf returned EOF
128 // Update the information on end of this buffer
129 fbmulti_subbuf_get_end(FB_MULTI(f)->cur);
131 // Take the next one if exists and redo
132 if (fbmulti_subbuf_next(f))
133 return fbmulti_refill(f);
139 fbmulti_get_len(struct fastbuf *f)
141 ucw_off_t pos = btell(f);
143 FB_MULTI(f)->len = 0;
145 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
147 n->begin = FB_MULTI(f)->len;
148 fbmulti_subbuf_get_end(n);
149 FB_MULTI(f)->len = n->end;
151 f->seek(f, pos, SEEK_SET); // XXX: f->seek is needed here instead of bsetpos as the FE assumptions about f's state may be completely wrong.
155 fbmulti_seek(struct fastbuf *f, ucw_off_t pos, int whence)
161 if (pos > FB_MULTI(f)->len)
162 bthrow(f, "seek", "Seek out of range");
164 while (pos > FB_MULTI(f)->cur->end) // Moving forward
165 ASSERT(fbmulti_subbuf_next(f));
167 while (pos < FB_MULTI(f)->cur->begin) // Moving backwards
168 ASSERT(fbmulti_subbuf_prev(f));
170 // Now cur is the right buffer.
171 FB_MULTI(f)->cur->fb->seek(FB_MULTI(f)->cur->fb, (pos - FB_MULTI(f)->cur->begin), SEEK_SET);
178 return fbmulti_seek(f, FB_MULTI(f)->len+pos, SEEK_SET);
187 fbmulti_close(struct fastbuf *f)
189 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
192 mp_delete(FB_MULTI(f)->mp);
196 fbmulti_create(uns bufsize, ...)
198 struct mempool *mp = mp_new(bufsize);
199 struct fastbuf *fb_out = mp_alloc(mp, sizeof(struct fb_multi));
200 FB_MULTI(fb_out)->mp = mp;
202 struct fastbuf *fb_in;
203 clist *subbufs = mp_alloc(mp, sizeof(clist));
205 FB_MULTI(fb_out)->subbufs = subbufs;
208 va_start(args, bufsize);
209 while (fb_in = va_arg(args, struct fastbuf *))
210 fbmulti_append(fb_out, fb_in);
214 fbmulti_update_capability(fb_out);
216 FB_MULTI(fb_out)->cur = clist_head(subbufs);
217 bsetpos(FB_MULTI(fb_out)->cur->fb, 0);
219 fbmulti_get_ptrs(fb_out);
221 // If seekable, get the length of each subbuf, the total length and boundaries
224 fbmulti_get_len(fb_out);
227 fb_out->name = FB_MULTI_NAME;
228 f->refill = fbmulti_refill;
229 f->seek = fbmulti_seek;
231 fb_out->close = fbmulti_close;
237 fbmulti_append(struct fastbuf *f, struct fastbuf *fb)
243 struct subbuf *sb = mp_alloc(FB_MULTI(f)->mp, sizeof(struct subbuf));
245 clist_add_tail(FB_MULTI(f)->subbufs, &(sb->n));
246 fbmulti_update_capability(f);
250 fbmulti_remove(struct fastbuf *f, struct fastbuf *fb)
256 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
259 // Move the pointers to another buffer if this one was the active.
260 if (FB_MULTI(f)->cur == n)
263 if (!fbmulti_subbuf_next(f))
265 struct subbuf *prev = clist_prev(FB_MULTI(f)->subbufs, &FB_MULTI(f)->cur->n);
269 FB_MULTI(f)->cur = prev;
275 pos -= (n->end - n->begin);
277 clist_remove(&(n->n));
278 fbmulti_update_capability(f);
284 die("Given fastbuf %p not in given fbmulti %p.", fb, f);
287 clist_init(FB_MULTI(f)->subbufs);
290 // The fbmulti is empty now, do some cleanup
291 fbmulti_update_capability(f);
293 f->buffer = f->bufend = f->bptr = f->bstop = NULL;
299 int main(int argc, char **argv)
303 fprintf(stderr, "You must specify a test (r, w, o)\n");
310 char *data[] = { "One\nLine", "Two\nLines", "Th\nreeLi\nnes\n" };
311 struct fastbuf fb[ARRAY_SIZE(data)];
312 for (uns i=0;i<ARRAY_SIZE(data);i++)
313 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
315 struct fastbuf *f = fbmulti_create(4, &fb[0], &fb[1], &fb[2], NULL);
318 while (bgets(f, buffer, 9))
326 char *data[] = { "Mnl", "ige" };
327 struct fastbuf fb[ARRAY_SIZE(data)];
328 for (uns i=0;i<ARRAY_SIZE(data);i++)
329 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
331 struct fastbuf *f = fbmulti_create(4, &fb[0], &fb[1], NULL);
333 int pos[] = {0, 3, 1, 4, 2, 5};
335 for (uns i=0;i<ARRAY_SIZE(pos);i++)
346 char *data = "Insae";
347 struct fastbuf fb[4];
348 fbbuf_init_read(&fb[0], data, 1, 0);
349 fbbuf_init_read(&fb[1], data + 1, 1, 0);
350 fbbuf_init_read(&fb[2], data + 2, 2, 0);
351 fbbuf_init_read(&fb[3], data + 4, 1, 0);
353 struct fastbuf *f = fbmulti_create(8, &fb[0], &fb[1], &fb[2], &fb[1], &fb[3], NULL);
356 while(bgets(f, buffer, 9))
364 char *data[] = { "Nested", "Data", "As", "In", "Real", "Usage", };
365 struct fastbuf fb[ARRAY_SIZE(data)];
366 for (uns i=0;i<ARRAY_SIZE(data);i++)
367 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
370 fbbuf_init_read(&sp, " ", 1, 0);
373 fbbuf_init_read(&nl, "\n", 1, 0);
375 struct fastbuf *f = fbmulti_create(4,
397 while (bgets(f, buffer, 20))