]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
lib/fastbuf.h no longer includes <stdio.h>.
[libucw.git] / lib / fastbuf.h
1 /*
2  *      UCW Library -- Fast Buffered I/O
3  *
4  *      (c) 1997--2007 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_FASTBUF_H
12 #define _UCW_FASTBUF_H
13
14 #include <string.h>
15 #include <alloca.h>
16
17 #include "lib/unaligned.h"
18
19 /*
20  *  Generic buffered I/O. You supply hooks to be called for low-level operations
21  *  (swapping of buffers, seeking and closing), we do the rest.
22  *
23  *  Buffer layout when reading:
24  *
25  *  +----------------+---------------------------+
26  *  | read data      | free space                |
27  *  +----------------+---------------------------+
28  *  ^        ^        ^                           ^
29  *  buffer   bptr     bstop                       bufend
30  *
31  *  After the last character is read, bptr == bstop and buffer refill
32  *  is deferred to the next read attempt. This gives us an easy way
33  *  how to implement bungetc().
34  *
35  *  When writing:
36  *
37  *  +--------+--------------+--------------------+
38  *  | unused | written data | free space         |
39  *  +--------+--------------+--------------------+
40  *  ^         ^              ^                    ^
41  *  buffer    bstop          bptr                 bufend
42  *
43  *  Dirty tricks:
44  *
45  *    - You can mix reads and writes on the same stream, but you must
46  *      call bflush() in between and remember that the file position
47  *      points after the flushed buffer which is not necessarily the same
48  *      as after the data you've read.
49  *    - The spout/refill hooks can change not only bptr and bstop, but also
50  *      the location of the buffer; fb-mem.c takes advantage of it.
51  *    - In some cases, the user of the bdirect interface can be allowed to modify
52  *      the data in the buffer to avoid unnecessary copying. If the back-end
53  *      allows such modifications, it can set can_overwrite_buffer accordingly:
54  *              *  0 if no modification is allowed,
55  *              *  1 if the user can modify the buffer on the condition that
56  *                   the modifications will be undone before calling the next
57  *                   fastbuf operation
58  *              *  2 if the user is allowed to overwrite the data in the buffer
59  *                   if bdirect_read_commit_modified() is called afterwards.
60  *                   In this case, the back-end must be prepared for trimming
61  *                   of the buffer which is done by the commit function.
62  */
63
64 struct fastbuf {
65   byte is_fastbuf[0];                   /* Dummy field for checking of type casts */
66   byte *bptr, *bstop;                   /* Access pointers */
67   byte *buffer, *bufend;                /* Start and end of the buffer */
68   byte *name;                           /* File name for error messages */
69   sh_off_t pos;                         /* Position of bstop in the file */
70   int (*refill)(struct fastbuf *);      /* Get a buffer with new data */
71   void (*spout)(struct fastbuf *);      /* Write buffer data to the file */
72   int (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed; returns success */
73   void (*close)(struct fastbuf *);      /* Close the stream */
74   int (*config)(struct fastbuf *, uns, int);    /* Configure the stream */
75   int can_overwrite_buffer;             /* Can the buffer be altered? (see discussion above) 0=never, 1=temporarily, 2=permanently */
76 };
77
78 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
79
80 struct fastbuf *bopen(byte *name, uns mode, uns buflen);
81 struct fastbuf *bopen_try(byte *name, uns mode, uns buflen);
82 struct fastbuf *bopen_tmp(uns buflen);
83 struct fastbuf *bfdopen(int fd, uns buflen);
84 struct fastbuf *bfdopen_shared(int fd, uns buflen);
85 void bfilesync(struct fastbuf *b);
86
87 #define TEMP_FILE_NAME_LEN 256
88 void temp_file_name(byte *name);
89
90 /* FastIO on in-memory streams */
91
92 struct fastbuf *fbmem_create(unsigned blocksize);       /* Create stream and return its writing fastbuf */
93 struct fastbuf *fbmem_clone_read(struct fastbuf *);     /* Create reading fastbuf */
94
95 /* FastIO on memory mapped files */
96
97 struct fastbuf *bopen_mm(byte *name, uns mode);
98
99 /* FastI on file descriptors with limit */
100
101 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
102
103 /* FastIO on static buffers */
104
105 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite);
106 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
107 static inline uns
108 fbbuf_count_written(struct fastbuf *f)
109 {
110   return f->bptr - f->bstop;
111 }
112
113 /* FastIO on recyclable growing buffers */
114
115 struct fastbuf *fbgrow_create(unsigned basic_size);
116 void fbgrow_reset(struct fastbuf *b);                   /* Reset stream and prepare for writing */
117 void fbgrow_rewind(struct fastbuf *b);                  /* Prepare for reading */
118
119 /* FastO with atomic writes for multi-threaded programs */
120
121 struct fb_atomic {
122   struct fastbuf fb;
123   struct fb_atomic_file *af;
124   byte *expected_max_bptr;
125   uns slack_size;
126 };
127 #define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
128
129 struct fastbuf *fbatomic_open(byte *name, struct fastbuf *master, uns bufsize, int record_len);
130 void fbatomic_internal_write(struct fastbuf *b);
131
132 static inline void
133 fbatomic_commit(struct fastbuf *b)
134 {
135   if (b->bptr >= ((struct fb_atomic *)b)->expected_max_bptr)
136     fbatomic_internal_write(b);
137 }
138
139 /* Configuring stream parameters */
140
141 int bconfig(struct fastbuf *f, uns type, int data);
142
143 #define BCONFIG_IS_TEMP_FILE 0
144
145 /* Universal functions working on all fastbuf's */
146
147 void bclose(struct fastbuf *f);
148 void bflush(struct fastbuf *f);
149 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
150 void bsetpos(struct fastbuf *f, sh_off_t pos);
151 void brewind(struct fastbuf *f);
152 sh_off_t bfilesize(struct fastbuf *f);          // -1 if not seekable
153
154 static inline sh_off_t btell(struct fastbuf *f)
155 {
156   return f->pos + (f->bptr - f->bstop);
157 }
158
159 int bgetc_slow(struct fastbuf *f);
160 static inline int bgetc(struct fastbuf *f)
161 {
162   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
163 }
164
165 int bpeekc_slow(struct fastbuf *f);
166 static inline int bpeekc(struct fastbuf *f)
167 {
168   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
169 }
170
171 static inline void bungetc(struct fastbuf *f)
172 {
173   f->bptr--;
174 }
175
176 void bputc_slow(struct fastbuf *f, uns c);
177 static inline void bputc(struct fastbuf *f, uns c)
178 {
179   if (f->bptr < f->bufend)
180     *f->bptr++ = c;
181   else
182     bputc_slow(f, c);
183 }
184
185 static inline uns
186 bavailr(struct fastbuf *f)
187 {
188   return f->bstop - f->bptr;
189 }
190
191 static inline uns
192 bavailw(struct fastbuf *f)
193 {
194   return f->bufend - f->bptr;
195 }
196
197 int bgetw_slow(struct fastbuf *f);
198 static inline int bgetw(struct fastbuf *f)
199 {
200   int w;
201   if (bavailr(f) >= 2)
202     {
203       w = GET_U16(f->bptr);
204       f->bptr += 2;
205       return w;
206     }
207   else
208     return bgetw_slow(f);
209 }
210
211 u32 bgetl_slow(struct fastbuf *f);
212 static inline u32 bgetl(struct fastbuf *f)
213 {
214   u32 l;
215   if (bavailr(f) >= 4)
216     {
217       l = GET_U32(f->bptr);
218       f->bptr += 4;
219       return l;
220     }
221   else
222     return bgetl_slow(f);
223 }
224
225 u64 bgetq_slow(struct fastbuf *f);
226 static inline u64 bgetq(struct fastbuf *f)
227 {
228   u64 l;
229   if (bavailr(f) >= 8)
230     {
231       l = GET_U64(f->bptr);
232       f->bptr += 8;
233       return l;
234     }
235   else
236     return bgetq_slow(f);
237 }
238
239 u64 bget5_slow(struct fastbuf *f);
240 static inline u64 bget5(struct fastbuf *f)
241 {
242   u64 l;
243   if (bavailr(f) >= 5)
244     {
245       l = GET_U40(f->bptr);
246       f->bptr += 5;
247       return l;
248     }
249   else
250     return bget5_slow(f);
251 }
252
253 void bputw_slow(struct fastbuf *f, uns w);
254 static inline void bputw(struct fastbuf *f, uns w)
255 {
256   if (bavailw(f) >= 2)
257     {
258       PUT_U16(f->bptr, w);
259       f->bptr += 2;
260     }
261   else
262     bputw_slow(f, w);
263 }
264
265 void bputl_slow(struct fastbuf *f, u32 l);
266 static inline void bputl(struct fastbuf *f, u32 l)
267 {
268   if (bavailw(f) >= 4)
269     {
270       PUT_U32(f->bptr, l);
271       f->bptr += 4;
272     }
273   else
274     bputl_slow(f, l);
275 }
276
277 void bputq_slow(struct fastbuf *f, u64 l);
278 static inline void bputq(struct fastbuf *f, u64 l)
279 {
280   if (bavailw(f) >= 8)
281     {
282       PUT_U64(f->bptr, l);
283       f->bptr += 8;
284     }
285   else
286     bputq_slow(f, l);
287 }
288
289 void bput5_slow(struct fastbuf *f, u64 l);
290 static inline void bput5(struct fastbuf *f, u64 l)
291 {
292   if (bavailw(f) >= 5)
293     {
294       PUT_U40(f->bptr, l);
295       f->bptr += 5;
296     }
297   else
298     bput5_slow(f, l);
299 }
300
301 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
302 static inline uns bread(struct fastbuf *f, void *b, uns l)
303 {
304   if (bavailr(f) >= l)
305     {
306       memcpy(b, f->bptr, l);
307       f->bptr += l;
308       return l;
309     }
310   else
311     return bread_slow(f, b, l, 0);
312 }
313
314 static inline uns breadb(struct fastbuf *f, void *b, uns l)
315 {
316   if (bavailr(f) >= l)
317     {
318       memcpy(b, f->bptr, l);
319       f->bptr += l;
320       return l;
321     }
322   else
323     return bread_slow(f, b, l, 1);
324 }
325
326 void bwrite_slow(struct fastbuf *f, void *b, uns l);
327 static inline void bwrite(struct fastbuf *f, void *b, uns l)
328 {
329   if (bavailw(f) >= l)
330     {
331       memcpy(f->bptr, b, l);
332       f->bptr += l;
333     }
334   else
335     bwrite_slow(f, b, l);
336 }
337
338 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
339 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
340 byte *bgets0(struct fastbuf *f, byte *b, uns l);
341
342 struct mempool;
343 struct bb_t;
344 uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit);
345 byte *bgets_mp(struct fastbuf *f, struct mempool *mp);
346
347 struct bgets_stk_struct {
348   struct fastbuf *f;
349   byte *old_buf, *cur_buf, *src;
350   uns old_len, cur_len, src_len;
351 };
352 void bgets_stk_init(struct bgets_stk_struct *s);
353 void bgets_stk_step(struct bgets_stk_struct *s);
354 #define bgets_stk(fb) ({ struct bgets_stk_struct _s; _s.f = (fb); for (bgets_stk_init(&_s); _s.cur_len; _s.cur_buf = alloca(_s.cur_len), bgets_stk_step(&_s)); _s.cur_buf; })
355
356 static inline void
357 bputs(struct fastbuf *f, byte *b)
358 {
359   bwrite(f, b, strlen(b));
360 }
361
362 static inline void
363 bputs0(struct fastbuf *f, byte *b)
364 {
365   bwrite(f, b, strlen(b)+1);
366 }
367
368 static inline void
369 bputsn(struct fastbuf *f, byte *b)
370 {
371   bputs(f, b);
372   bputc(f, '\n');
373 }
374
375 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
376 static inline void
377 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
378 {
379   if (bavailr(f) >= l && bavailw(t) >= l)
380     {
381       memcpy(t->bptr, f->bptr, l);
382       t->bptr += l;
383       f->bptr += l;
384     }
385   else
386     bbcopy_slow(f, t, l);
387 }
388
389 int bskip_slow(struct fastbuf *f, uns len);
390 static inline int bskip(struct fastbuf *f, uns len)
391 {
392   if (bavailr(f) >= len)
393     {
394       f->bptr += len;
395       return 1;
396     }
397   else
398     return bskip_slow(f, len);
399 }
400
401 /* I/O on uintptr_t */
402
403 #ifdef CPU_64BIT_POINTERS
404 #define bputa(x,p) bputq(x,p)
405 #define bgeta(x) bgetq(x)
406 #else
407 #define bputa(x,p) bputl(x,p)
408 #define bgeta(x) bgetl(x)
409 #endif
410
411 /* Direct I/O on buffers */
412
413 static inline uns
414 bdirect_read_prepare(struct fastbuf *f, byte **buf)
415 {
416   if (f->bptr == f->bstop && !f->refill(f))
417     {
418       *buf = NULL;  // This is not needed, but it helps to get rid of spurious warnings
419       return 0;
420     }
421   *buf = f->bptr;
422   return bavailr(f);
423 }
424
425 static inline void
426 bdirect_read_commit(struct fastbuf *f, byte *pos)
427 {
428   f->bptr = pos;
429 }
430
431 static inline void
432 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
433 {
434   f->bptr = pos;
435   f->buffer = pos;      /* Avoid seeking backwards in the buffer */
436 }
437
438 static inline uns
439 bdirect_write_prepare(struct fastbuf *f, byte **buf)
440 {
441   if (f->bptr == f->bufend)
442     f->spout(f);
443   *buf = f->bptr;
444   return bavailw(f);
445 }
446
447 static inline void
448 bdirect_write_commit(struct fastbuf *f, byte *pos)
449 {
450   f->bptr = pos;
451 }
452
453 /* Formatted output */
454
455 int bprintf(struct fastbuf *b, char *msg, ...) FORMAT_CHECK(printf,2,3);
456 int vbprintf(struct fastbuf *b, char *msg, va_list args);
457
458 #endif