2 * UCW Library -- Fast Buffered I/O
4 * (c) 1997--2004 Martin Mares <mj@ucw.cz>
5 * (c) 2004 Robert Spalek <robert@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
11 #ifndef _UCW_FASTBUF_H
12 #define _UCW_FASTBUF_H
21 #include "lib/unaligned.h"
24 * Generic buffered I/O. You supply hooks to be called for low-level operations
25 * (swapping of buffers, seeking and closing), we do the rest.
27 * Buffer layout when reading:
29 * +----------------+---------------------------+
30 * | read data | free space |
31 * +----------------+---------------------------+
33 * buffer bptr bstop bufend
35 * After the last character is read, bptr == bstop and buffer refill
36 * is deferred to the next read attempt. This gives us an easy way
37 * how to implement bungetc().
41 * +--------+--------------+--------------------+
42 * | unused | written data | free space |
43 * +--------+--------------+--------------------+
45 * buffer bstop bptr bufend
49 * - You can mix reads and writes on the same stream, but you must
50 * call bflush() in between and remember that the file position
51 * points after the flushed buffer which is not necessarily the same
52 * as after the data you've read.
53 * - The spout/refill hooks can change not only bptr and bstop, but also
54 * the location of the buffer; fb-mem.c takes advantage of it.
55 * - In some cases, the user of the bdirect interface can be allowed to modify
56 * the data in the buffer to avoid unnecessary copying. If the back-end
57 * allows such modifications, it can set can_overwrite_buffer accordingly:
58 * * 0 if no modification is allowed,
59 * * 1 if the user can modify the buffer on the condition that
60 * the modifications will be undone before calling the next
62 * * 2 if the user is allowed to overwrite the data in the buffer
63 * if bdirect_read_commit_modified() is called afterwards.
64 * In this case, the back-end must be prepared for trimming
65 * of the buffer which is done by the commit function.
69 byte is_fastbuf[0]; /* Dummy field for checking of type casts */
70 byte *bptr, *bstop; /* Access pointers */
71 byte *buffer, *bufend; /* Start and end of the buffer */
72 byte *name; /* File name for error messages */
73 sh_off_t pos; /* Position of bstop in the file */
74 int (*refill)(struct fastbuf *); /* Get a buffer with new data */
75 void (*spout)(struct fastbuf *); /* Write buffer data to the file */
76 void (*seek)(struct fastbuf *, sh_off_t, int); /* Slow path for bseek(), buffer already flushed */
77 void (*close)(struct fastbuf *); /* Close the stream */
78 int (*config)(struct fastbuf *, uns, int); /* Configure the stream */
79 int can_overwrite_buffer; /* Can the buffer be altered? (see discussion above) 0=never, 1=temporarily, 2=permanently */
82 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
84 struct fastbuf *bopen(byte *name, uns mode, uns buflen);
85 struct fastbuf *bopen_try(byte *name, uns mode, uns buflen);
86 struct fastbuf *bopen_tmp(uns buflen);
87 struct fastbuf *bfdopen(int fd, uns buflen);
88 struct fastbuf *bfdopen_shared(int fd, uns buflen);
89 void bfilesync(struct fastbuf *b);
91 /* FastIO on in-memory streams */
93 struct fastbuf *fbmem_create(unsigned blocksize); /* Create stream and return its writing fastbuf */
94 struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */
96 /* FastIO on memory mapped files */
98 struct fastbuf *bopen_mm(byte *name, uns mode);
100 /* FastIO on files opened with O_DIRECT (see fb-direct.c for description) */
103 struct fastbuf *fbdir_open(byte *name, uns mode, struct asio_queue *io_queue);
104 struct fastbuf *fbdir_open_try(byte *name, uns mode, struct asio_queue *io_queue);
105 struct fastbuf *fbdir_open_fd(int fd, struct asio_queue *io_queue);
106 struct fastbuf *fbdir_open_tmp(int fd, struct asio_queue *io_queue);
108 /* FastI on file descriptors with limit */
110 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
112 /* FastIO on static buffers */
114 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite);
115 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
117 fbbuf_count_written(struct fastbuf *f)
119 return f->bptr - f->bstop;
122 /* FastIO on recyclable growing buffers */
124 struct fastbuf *fbgrow_create(unsigned basic_size);
125 void fbgrow_reset(struct fastbuf *b); /* Reset stream and prepare for writing */
126 void fbgrow_rewind(struct fastbuf *b); /* Prepare for reading */
128 /* FastO with atomic writes for multi-threaded programs */
132 struct fb_atomic_file *af;
133 byte *expected_max_bptr;
136 #define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
138 struct fastbuf *fbatomic_open(byte *name, struct fastbuf *master, uns bufsize, int record_len);
139 void fbatomic_internal_write(struct fastbuf *b);
142 fbatomic_commit(struct fastbuf *b)
144 if (b->bptr >= ((struct fb_atomic *)b)->expected_max_bptr)
145 fbatomic_internal_write(b);
148 /* Configuring stream parameters */
150 int bconfig(struct fastbuf *f, uns type, int data);
152 #define BCONFIG_IS_TEMP_FILE 0
154 /* Universal functions working on all fastbuf's */
156 void bclose(struct fastbuf *f);
157 void bflush(struct fastbuf *f);
158 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
159 void bsetpos(struct fastbuf *f, sh_off_t pos);
160 void brewind(struct fastbuf *f);
161 sh_off_t bfilesize(struct fastbuf *f);
163 static inline sh_off_t btell(struct fastbuf *f)
165 return f->pos + (f->bptr - f->bstop);
168 int bgetc_slow(struct fastbuf *f);
169 static inline int bgetc(struct fastbuf *f)
171 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
174 int bpeekc_slow(struct fastbuf *f);
175 static inline int bpeekc(struct fastbuf *f)
177 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
180 static inline void bungetc(struct fastbuf *f)
185 void bputc_slow(struct fastbuf *f, uns c);
186 static inline void bputc(struct fastbuf *f, uns c)
188 if (f->bptr < f->bufend)
195 bavailr(struct fastbuf *f)
197 return f->bstop - f->bptr;
201 bavailw(struct fastbuf *f)
203 return f->bufend - f->bptr;
206 int bgetw_slow(struct fastbuf *f);
207 static inline int bgetw(struct fastbuf *f)
212 w = GET_U16(f->bptr);
217 return bgetw_slow(f);
220 u32 bgetl_slow(struct fastbuf *f);
221 static inline u32 bgetl(struct fastbuf *f)
226 l = GET_U32(f->bptr);
231 return bgetl_slow(f);
234 u64 bgetq_slow(struct fastbuf *f);
235 static inline u64 bgetq(struct fastbuf *f)
240 l = GET_U64(f->bptr);
245 return bgetq_slow(f);
248 u64 bget5_slow(struct fastbuf *f);
249 static inline u64 bget5(struct fastbuf *f)
254 l = GET_U40(f->bptr);
259 return bget5_slow(f);
262 void bputw_slow(struct fastbuf *f, uns w);
263 static inline void bputw(struct fastbuf *f, uns w)
274 void bputl_slow(struct fastbuf *f, u32 l);
275 static inline void bputl(struct fastbuf *f, u32 l)
286 void bputq_slow(struct fastbuf *f, u64 l);
287 static inline void bputq(struct fastbuf *f, u64 l)
298 void bput5_slow(struct fastbuf *f, u64 l);
299 static inline void bput5(struct fastbuf *f, u64 l)
310 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
311 static inline uns bread(struct fastbuf *f, void *b, uns l)
315 memcpy(b, f->bptr, l);
320 return bread_slow(f, b, l, 0);
323 static inline uns breadb(struct fastbuf *f, void *b, uns l)
327 memcpy(b, f->bptr, l);
332 return bread_slow(f, b, l, 1);
335 void bwrite_slow(struct fastbuf *f, void *b, uns l);
336 static inline void bwrite(struct fastbuf *f, void *b, uns l)
340 memcpy(f->bptr, b, l);
344 bwrite_slow(f, b, l);
347 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
348 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
349 byte *bgets0(struct fastbuf *f, byte *b, uns l);
353 uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit);
354 byte *bgets_mp(struct fastbuf *f, struct mempool *mp);
356 struct bgets_stk_struct {
358 byte *old_buf, *cur_buf, *src;
359 uns old_len, cur_len, src_len;
361 void bgets_stk_init(struct bgets_stk_struct *s);
362 void bgets_stk_step(struct bgets_stk_struct *s);
363 #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; })
366 bputs(struct fastbuf *f, byte *b)
368 bwrite(f, b, strlen(b));
372 bputs0(struct fastbuf *f, byte *b)
374 bwrite(f, b, strlen(b)+1);
378 bputsn(struct fastbuf *f, byte *b)
384 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
386 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
388 if (bavailr(f) >= l && bavailw(t) >= l)
390 memcpy(t->bptr, f->bptr, l);
395 bbcopy_slow(f, t, l);
398 int bskip_slow(struct fastbuf *f, uns len);
399 static inline int bskip(struct fastbuf *f, uns len)
401 if (bavailr(f) >= len)
407 return bskip_slow(f, len);
410 /* I/O on addr_int_t */
412 #ifdef CPU_64BIT_POINTERS
413 #define bputa(x,p) bputq(x,p)
414 #define bgeta(x) bgetq(x)
416 #define bputa(x,p) bputl(x,p)
417 #define bgeta(x) bgetl(x)
420 /* Direct I/O on buffers */
423 bdirect_read_prepare(struct fastbuf *f, byte **buf)
425 if (f->bptr == f->bstop && !f->refill(f))
427 *buf = NULL; // This is not needed, but it helps to get rid of spurious warnings
435 bdirect_read_commit(struct fastbuf *f, byte *pos)
441 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
444 f->buffer = pos; /* Avoid seeking backwards in the buffer */
448 bdirect_write_prepare(struct fastbuf *f, byte **buf)
450 if (f->bptr == f->bufend)
457 bdirect_write_commit(struct fastbuf *f, byte *pos)
462 /* Formatted output */
464 int bprintf(struct fastbuf *b, char *msg, ...) FORMAT_CHECK(printf,2,3);
465 int vbprintf(struct fastbuf *b, char *msg, va_list args);