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_update_capability(struct fastbuf *f)
189 // FB Multi is only a proxy to other fastbufs ... if any of them lacks
190 // support of any feature, FB Multi also provides no support of that feature
191 f->refill = fbmulti_refill;
192 f->seek = fbmulti_seek;
194 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
196 ASSERT(n->fb->refill);
204 fbmulti_close(struct fastbuf *f)
206 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
209 mp_delete(FB_MULTI(f)->mp);
213 fbmulti_create(uns bufsize, ...)
215 struct mempool *mp = mp_new(bufsize);
216 struct fastbuf *fb_out = mp_alloc(mp, sizeof(struct fb_multi));
217 FB_MULTI(fb_out)->mp = mp;
219 struct fastbuf *fb_in;
220 clist *subbufs = mp_alloc(mp, sizeof(clist));
222 FB_MULTI(fb_out)->subbufs = subbufs;
225 va_start(args, bufsize);
226 while (fb_in = va_arg(args, struct fastbuf *))
227 fbmulti_append(fb_out, fb_in);
231 fbmulti_update_capability(fb_out);
233 FB_MULTI(fb_out)->cur = clist_head(subbufs);
234 bsetpos(FB_MULTI(fb_out)->cur->fb, 0);
236 fbmulti_get_ptrs(fb_out);
238 // If seekable, get the length of each subbuf, the total length and boundaries
241 fbmulti_get_len(fb_out);
244 fb_out->name = FB_MULTI_NAME;
246 fb_out->close = fbmulti_close;
252 fbmulti_append(struct fastbuf *f, struct fastbuf *fb)
254 struct subbuf *sb = mp_alloc(FB_MULTI(f)->mp, sizeof(struct subbuf));
256 clist_add_tail(FB_MULTI(f)->subbufs, &(sb->n));
257 fbmulti_update_capability(f);
261 fbmulti_remove(struct fastbuf *f, struct fastbuf *fb)
267 CLIST_FOR_EACH(struct subbuf *, n, *(FB_MULTI(f)->subbufs))
270 // Move the pointers to another buffer if this one was the active.
271 if (FB_MULTI(f)->cur == n)
274 if (!fbmulti_subbuf_next(f))
276 struct subbuf *prev = clist_prev(FB_MULTI(f)->subbufs, &FB_MULTI(f)->cur->n);
280 FB_MULTI(f)->cur = prev;
286 pos -= (n->end - n->begin);
288 clist_remove(&(n->n));
289 fbmulti_update_capability(f);
295 die("Given fastbuf %p not in given fbmulti %p.", fb, f);
298 clist_init(FB_MULTI(f)->subbufs);
301 // The fbmulti is empty now, do some cleanup
302 fbmulti_update_capability(f);
304 f->buffer = f->bufend = f->bptr = f->bstop = NULL;
310 int main(int argc, char **argv)
314 fprintf(stderr, "You must specify a test (r, w, o)\n");
321 char *data[] = { "One\nLine", "Two\nLines", "Th\nreeLi\nnes\n" };
322 struct fastbuf fb[ARRAY_SIZE(data)];
323 for (uns i=0;i<ARRAY_SIZE(data);i++)
324 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
326 struct fastbuf *f = fbmulti_create(4, &fb[0], &fb[1], &fb[2], NULL);
329 while (bgets(f, buffer, 9))
337 char *data[] = { "Mnl", "ige" };
338 struct fastbuf fb[ARRAY_SIZE(data)];
339 for (uns i=0;i<ARRAY_SIZE(data);i++)
340 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
342 struct fastbuf *f = fbmulti_create(4, &fb[0], &fb[1], NULL);
344 int pos[] = {0, 3, 1, 4, 2, 5};
346 for (uns i=0;i<ARRAY_SIZE(pos);i++)
357 char *data = "Insae";
358 struct fastbuf fb[4];
359 fbbuf_init_read(&fb[0], data, 1, 0);
360 fbbuf_init_read(&fb[1], data + 1, 1, 0);
361 fbbuf_init_read(&fb[2], data + 2, 2, 0);
362 fbbuf_init_read(&fb[3], data + 4, 1, 0);
364 struct fastbuf *f = fbmulti_create(8, &fb[0], &fb[1], &fb[2], &fb[1], &fb[3], NULL);
367 while(bgets(f, buffer, 9))
375 char *data[] = { "Nested", "Data", "As", "In", "Real", "Usage", };
376 struct fastbuf fb[ARRAY_SIZE(data)];
377 for (uns i=0;i<ARRAY_SIZE(data);i++)
378 fbbuf_init_read(&fb[i], data[i], strlen(data[i]), 0);
381 fbbuf_init_read(&sp, " ", 1, 0);
384 fbbuf_init_read(&nl, "\n", 1, 0);
386 struct fastbuf *f = fbmulti_create(4,
408 while (bgets(f, buffer, 20))