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"
25 * Generic buffered I/O. You supply hooks to be called for low-level operations
26 * (swapping of buffers, seeking and closing), we do the rest.
28 * Buffer layout when reading:
30 * +----------------+---------------------------+
31 * | read data | free space |
32 * +----------------+---------------------------+
34 * buffer bptr bstop bufend
36 * After the last character is read, bptr == bstop and buffer refill
37 * is deferred to the next read attempt. This gives us an easy way
38 * how to implement bungetc().
42 * +--------+--------------+--------------------+
43 * | unused | written data | free space |
44 * +--------+--------------+--------------------+
46 * buffer bstop bptr bufend
50 * - You can mix reads and writes on the same stream, but you must
51 * call bflush() in between and remember that the file position
52 * points after the flushed buffer which is not necessarily the same
53 * as after the data you've read.
54 * - The spout/refill hooks can change not only bptr and bstop, but also
55 * the location of the buffer; fb-mem.c takes advantage of it.
56 * - In some cases, the user of the bdirect interface can be allowed to modify
57 * the data in the buffer to avoid unnecessary copying. If the back-end
58 * allows such modifications, it can set can_overwrite_buffer accordingly:
59 * * 0 if no modification is allowed,
60 * * 1 if the user can modify the buffer on the condition that
61 * the modifications will be undone before calling the next
63 * * 2 if the user is allowed to overwrite the data in the buffer
64 * if bdirect_read_commit_modified() is called afterwards.
65 * In this case, the back-end must be prepared for trimming
66 * of the buffer which is done by the commit function.
70 byte is_fastbuf[0]; /* Dummy field for checking of type casts */
71 byte *bptr, *bstop; /* Access pointers */
72 byte *buffer, *bufend; /* Start and end of the buffer */
73 byte *name; /* File name for error messages */
74 sh_off_t pos; /* Position of bstop in the file */
75 int (*refill)(struct fastbuf *); /* Get a buffer with new data */
76 void (*spout)(struct fastbuf *); /* Write buffer data to the file */
77 void (*seek)(struct fastbuf *, sh_off_t, int); /* Slow path for bseek(), buffer already flushed */
78 void (*close)(struct fastbuf *); /* Close the stream */
79 int (*config)(struct fastbuf *, uns, int); /* Configure the stream */
80 int can_overwrite_buffer; /* Can the buffer be altered? (see discussion above) 0=never, 1=temporarily, 2=permanently */
83 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
85 struct fastbuf *bopen(byte *name, uns mode, uns buflen);
86 struct fastbuf *bopen_try(byte *name, uns mode, uns buflen);
87 struct fastbuf *bopen_tmp(uns buflen);
88 struct fastbuf *bfdopen(int fd, uns buflen);
89 struct fastbuf *bfdopen_shared(int fd, uns buflen);
90 void bfilesync(struct fastbuf *b);
92 /* FastIO on in-memory streams */
94 struct fastbuf *fbmem_create(unsigned blocksize); /* Create stream and return its writing fastbuf */
95 struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */
97 /* FastIO on memory mapped files */
99 struct fastbuf *bopen_mm(byte *name, uns mode);
101 /* FastI on file descriptors with limit */
103 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
105 /* FastIO on static buffers */
107 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite);
108 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
110 fbbuf_count_written(struct fastbuf *f)
112 return f->bptr - f->bstop;
115 /* FastIO on recyclable growing buffers */
117 struct fastbuf *fbgrow_create(unsigned basic_size);
118 void fbgrow_reset(struct fastbuf *b); /* Reset stream and prepare for writing */
119 void fbgrow_rewind(struct fastbuf *b); /* Prepare for reading */
121 /* FastO with atomic writes for multi-threaded programs */
125 struct fb_atomic_file *af;
126 byte *expected_max_bptr;
129 #define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
131 struct fastbuf *fbatomic_open(byte *name, struct fastbuf *master, uns bufsize, int record_len);
132 void fbatomic_internal_write(struct fastbuf *b);
135 fbatomic_commit(struct fastbuf *b)
137 if (b->bptr >= ((struct fb_atomic *)b)->expected_max_bptr)
138 fbatomic_internal_write(b);
141 /* Configuring stream parameters */
143 int bconfig(struct fastbuf *f, uns type, int data);
145 #define BCONFIG_IS_TEMP_FILE 0
147 /* Universal functions working on all fastbuf's */
149 void bclose(struct fastbuf *f);
150 void bflush(struct fastbuf *f);
151 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
152 void bsetpos(struct fastbuf *f, sh_off_t pos);
153 void brewind(struct fastbuf *f);
154 sh_off_t bfilesize(struct fastbuf *f);
156 static inline sh_off_t btell(struct fastbuf *f)
158 return f->pos + (f->bptr - f->bstop);
161 int bgetc_slow(struct fastbuf *f);
162 static inline int bgetc(struct fastbuf *f)
164 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
167 int bpeekc_slow(struct fastbuf *f);
168 static inline int bpeekc(struct fastbuf *f)
170 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
173 static inline void bungetc(struct fastbuf *f)
178 void bputc_slow(struct fastbuf *f, uns c);
179 static inline void bputc(struct fastbuf *f, uns c)
181 if (f->bptr < f->bufend)
188 bavailr(struct fastbuf *f)
190 return f->bstop - f->bptr;
194 bavailw(struct fastbuf *f)
196 return f->bufend - f->bptr;
199 int bgetw_slow(struct fastbuf *f);
200 static inline int bgetw(struct fastbuf *f)
205 w = GET_U16(f->bptr);
210 return bgetw_slow(f);
213 u32 bgetl_slow(struct fastbuf *f);
214 static inline u32 bgetl(struct fastbuf *f)
219 l = GET_U32(f->bptr);
224 return bgetl_slow(f);
227 u64 bgetq_slow(struct fastbuf *f);
228 static inline u64 bgetq(struct fastbuf *f)
233 l = GET_U64(f->bptr);
238 return bgetq_slow(f);
241 u64 bget5_slow(struct fastbuf *f);
242 static inline u64 bget5(struct fastbuf *f)
247 l = GET_U40(f->bptr);
252 return bget5_slow(f);
255 void bputw_slow(struct fastbuf *f, uns w);
256 static inline void bputw(struct fastbuf *f, uns w)
267 void bputl_slow(struct fastbuf *f, u32 l);
268 static inline void bputl(struct fastbuf *f, u32 l)
279 void bputq_slow(struct fastbuf *f, u64 l);
280 static inline void bputq(struct fastbuf *f, u64 l)
291 void bput5_slow(struct fastbuf *f, u64 l);
292 static inline void bput5(struct fastbuf *f, u64 l)
303 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
304 static inline uns bread(struct fastbuf *f, void *b, uns l)
308 memcpy(b, f->bptr, l);
313 return bread_slow(f, b, l, 0);
316 static inline uns breadb(struct fastbuf *f, void *b, uns l)
320 memcpy(b, f->bptr, l);
325 return bread_slow(f, b, l, 1);
328 void bwrite_slow(struct fastbuf *f, void *b, uns l);
329 static inline void bwrite(struct fastbuf *f, void *b, uns l)
333 memcpy(f->bptr, b, l);
337 bwrite_slow(f, b, l);
340 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
341 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
342 byte *bgets0(struct fastbuf *f, byte *b, uns l);
345 uns bgets_bb(struct fastbuf *f, bb_t *b, uns limit);
346 byte *bgets_mp(struct fastbuf *f, struct mempool *mp);
348 struct bgets_stk_struct {
350 byte *old_buf, *cur_buf, *src;
351 uns old_len, cur_len, src_len;
353 void bgets_stk_init(struct bgets_stk_struct *s);
354 void bgets_stk_step(struct bgets_stk_struct *s);
355 #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; })
358 bputs(struct fastbuf *f, byte *b)
360 bwrite(f, b, strlen(b));
364 bputs0(struct fastbuf *f, byte *b)
366 bwrite(f, b, strlen(b)+1);
370 bputsn(struct fastbuf *f, byte *b)
376 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
378 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
380 if (bavailr(f) >= l && bavailw(t) >= l)
382 memcpy(t->bptr, f->bptr, l);
387 bbcopy_slow(f, t, l);
390 int bskip_slow(struct fastbuf *f, uns len);
391 static inline int bskip(struct fastbuf *f, uns len)
393 if (bavailr(f) >= len)
399 return bskip_slow(f, len);
402 /* I/O on addr_int_t */
404 #ifdef CPU_64BIT_POINTERS
405 #define bputa(x,p) bputq(x,p)
406 #define bgeta(x) bgetq(x)
408 #define bputa(x,p) bputl(x,p)
409 #define bgeta(x) bgetl(x)
412 /* Direct I/O on buffers */
415 bdirect_read_prepare(struct fastbuf *f, byte **buf)
417 if (f->bptr == f->bstop && !f->refill(f))
419 *buf = NULL; // This is not needed, but it helps to get rid of spurious warnings
427 bdirect_read_commit(struct fastbuf *f, byte *pos)
433 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
436 f->buffer = pos; /* Avoid seeking backwards in the buffer */
440 bdirect_write_prepare(struct fastbuf *f, byte **buf)
442 if (f->bptr == f->bufend)
449 bdirect_write_commit(struct fastbuf *f, byte *pos)
454 /* Formatted output */
456 int bprintf(struct fastbuf *b, char *msg, ...) FORMAT_CHECK(printf,2,3);
457 int vbprintf(struct fastbuf *b, char *msg, va_list args);