2 * UCW Library -- Fast Buffered I/O
4 * (c) 1997--2007 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
17 #include "lib/unaligned.h"
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.
23 * Buffer layout when reading:
25 * +----------------+---------------------------+
26 * | read data | free space |
27 * +----------------+---------------------------+
29 * buffer bptr bstop bufend
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().
37 * +--------+--------------+--------------------+
38 * | unused | written data | free space |
39 * +--------+--------------+--------------------+
41 * buffer bstop bptr bufend
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
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.
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 */
78 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
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);
87 #define TEMP_FILE_NAME_LEN 256
88 void temp_file_name(byte *name);
90 /* FastIO on in-memory streams */
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 */
95 /* FastIO on memory mapped files */
97 struct fastbuf *bopen_mm(byte *name, uns mode);
99 /* FastI on file descriptors with limit */
101 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
103 /* FastIO on static buffers */
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);
108 fbbuf_count_written(struct fastbuf *f)
110 return f->bptr - f->bstop;
113 /* FastIO on recyclable growing buffers */
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 */
119 /* FastO with atomic writes for multi-threaded programs */
123 struct fb_atomic_file *af;
124 byte *expected_max_bptr;
127 #define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
129 struct fastbuf *fbatomic_open(byte *name, struct fastbuf *master, uns bufsize, int record_len);
130 void fbatomic_internal_write(struct fastbuf *b);
133 fbatomic_commit(struct fastbuf *b)
135 if (b->bptr >= ((struct fb_atomic *)b)->expected_max_bptr)
136 fbatomic_internal_write(b);
139 /* Configuring stream parameters */
141 int bconfig(struct fastbuf *f, uns type, int data);
143 #define BCONFIG_IS_TEMP_FILE 0
145 /* Universal functions working on all fastbuf's */
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
154 static inline sh_off_t btell(struct fastbuf *f)
156 return f->pos + (f->bptr - f->bstop);
159 int bgetc_slow(struct fastbuf *f);
160 static inline int bgetc(struct fastbuf *f)
162 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
165 int bpeekc_slow(struct fastbuf *f);
166 static inline int bpeekc(struct fastbuf *f)
168 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
171 static inline void bungetc(struct fastbuf *f)
176 void bputc_slow(struct fastbuf *f, uns c);
177 static inline void bputc(struct fastbuf *f, uns c)
179 if (f->bptr < f->bufend)
186 bavailr(struct fastbuf *f)
188 return f->bstop - f->bptr;
192 bavailw(struct fastbuf *f)
194 return f->bufend - f->bptr;
197 int bgetw_slow(struct fastbuf *f);
198 static inline int bgetw(struct fastbuf *f)
203 w = GET_U16(f->bptr);
208 return bgetw_slow(f);
211 u32 bgetl_slow(struct fastbuf *f);
212 static inline u32 bgetl(struct fastbuf *f)
217 l = GET_U32(f->bptr);
222 return bgetl_slow(f);
225 u64 bgetq_slow(struct fastbuf *f);
226 static inline u64 bgetq(struct fastbuf *f)
231 l = GET_U64(f->bptr);
236 return bgetq_slow(f);
239 u64 bget5_slow(struct fastbuf *f);
240 static inline u64 bget5(struct fastbuf *f)
245 l = GET_U40(f->bptr);
250 return bget5_slow(f);
253 void bputw_slow(struct fastbuf *f, uns w);
254 static inline void bputw(struct fastbuf *f, uns w)
265 void bputl_slow(struct fastbuf *f, u32 l);
266 static inline void bputl(struct fastbuf *f, u32 l)
277 void bputq_slow(struct fastbuf *f, u64 l);
278 static inline void bputq(struct fastbuf *f, u64 l)
289 void bput5_slow(struct fastbuf *f, u64 l);
290 static inline void bput5(struct fastbuf *f, u64 l)
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)
306 memcpy(b, f->bptr, l);
311 return bread_slow(f, b, l, 0);
314 static inline uns breadb(struct fastbuf *f, void *b, uns l)
318 memcpy(b, f->bptr, l);
323 return bread_slow(f, b, l, 1);
326 void bwrite_slow(struct fastbuf *f, void *b, uns l);
327 static inline void bwrite(struct fastbuf *f, void *b, uns l)
331 memcpy(f->bptr, b, l);
335 bwrite_slow(f, b, l);
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);
344 uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit);
345 byte *bgets_mp(struct fastbuf *f, struct mempool *mp);
347 struct bgets_stk_struct {
349 byte *old_buf, *cur_buf, *src;
350 uns old_len, cur_len, src_len;
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; })
357 bputs(struct fastbuf *f, byte *b)
359 bwrite(f, b, strlen(b));
363 bputs0(struct fastbuf *f, byte *b)
365 bwrite(f, b, strlen(b)+1);
369 bputsn(struct fastbuf *f, byte *b)
375 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
377 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
379 if (bavailr(f) >= l && bavailw(t) >= l)
381 memcpy(t->bptr, f->bptr, l);
386 bbcopy_slow(f, t, l);
389 int bskip_slow(struct fastbuf *f, uns len);
390 static inline int bskip(struct fastbuf *f, uns len)
392 if (bavailr(f) >= len)
398 return bskip_slow(f, len);
401 /* I/O on uintptr_t */
403 #ifdef CPU_64BIT_POINTERS
404 #define bputa(x,p) bputq(x,p)
405 #define bgeta(x) bgetq(x)
407 #define bputa(x,p) bputl(x,p)
408 #define bgeta(x) bgetl(x)
411 /* Direct I/O on buffers */
414 bdirect_read_prepare(struct fastbuf *f, byte **buf)
416 if (f->bptr == f->bstop && !f->refill(f))
418 *buf = NULL; // This is not needed, but it helps to get rid of spurious warnings
426 bdirect_read_commit(struct fastbuf *f, byte *pos)
432 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
435 f->buffer = pos; /* Avoid seeking backwards in the buffer */
439 bdirect_write_prepare(struct fastbuf *f, byte **buf)
441 if (f->bptr == f->bufend)
448 bdirect_write_commit(struct fastbuf *f, byte *pos)
453 /* Formatted output */
455 int bprintf(struct fastbuf *b, char *msg, ...) FORMAT_CHECK(printf,2,3);
456 int vbprintf(struct fastbuf *b, char *msg, va_list args);