2 * Sherlock Library -- Fast Buffered I/O
4 * (c) 1997--2002 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 #ifndef _SHERLOCK_FASTBUF_H
11 #define _SHERLOCK_FASTBUF_H
20 #include "lib/unaligned.h"
23 * Generic buffered I/O. You supply hooks to be called for low-level operations
24 * (swapping of buffers, seeking and closing), we do the rest.
26 * Buffer layout when reading:
28 * +----------------+---------------------------+
29 * | read data | free space |
30 * +----------------+---------------------------+
32 * buffer bptr bstop bufend
34 * After the last character is read, bptr == bstop and buffer refill
35 * is deferred to the next read attempt. This gives us an easy way
36 * how to implement bungetc().
40 * +--------+--------------+--------------------+
41 * | unused | written data | free space |
42 * +--------+--------------+--------------------+
44 * buffer bstop bptr bufend
48 * - You can mix reads and writes on the same stream, but you must
49 * call bflush() in between and remember that the file position
50 * points after the flushed buffer which is not necessarily the same
51 * as after the data you've read.
52 * - The spout/refill hooks can change not only bptr and bstop, but also
53 * the location of the buffer; fb-mem.c takes advantage of it.
57 byte is_fastbuf[0]; /* Dummy field for checking of type casts */
58 byte *bptr, *bstop; /* Access pointers */
59 byte *buffer, *bufend; /* Start and end of the buffer */
60 byte *name; /* File name for error messages */
61 sh_off_t pos; /* Position of bstop in the file */
62 int (*refill)(struct fastbuf *); /* Get a buffer with new data */
63 void (*spout)(struct fastbuf *); /* Write buffer data to the file */
64 void (*seek)(struct fastbuf *, sh_off_t, int); /* Slow path for bseek(), buffer already flushed */
65 void (*close)(struct fastbuf *); /* Close the stream */
66 int (*config)(struct fastbuf *, uns, int); /* Configure the stream */
69 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
71 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
72 struct fastbuf *bopen_tmp(uns buffer);
73 struct fastbuf *bfdopen(int fd, uns buffer);
74 struct fastbuf *bfdopen_shared(int fd, uns buffer);
76 /* FastIO on in-memory streams */
78 struct fastbuf *fbmem_create(unsigned blocksize); /* Create stream and return its writing fastbuf */
79 struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */
81 /* FastIO on memory mapped files */
83 struct fastbuf *bopen_mm(byte *name, uns mode);
85 /* Configuring stream parameters */
87 int bconfig(struct fastbuf *f, uns type, int data);
89 #define BCONFIG_IS_TEMP_FILE 0
91 /* Universal functions working on all fastbuf's */
93 void bclose(struct fastbuf *f);
94 void bflush(struct fastbuf *f);
95 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
96 void bsetpos(struct fastbuf *f, sh_off_t pos);
98 static inline sh_off_t btell(struct fastbuf *f)
100 return f->pos + (f->bptr - f->bstop);
103 int bgetc_slow(struct fastbuf *f);
104 static inline int bgetc(struct fastbuf *f)
106 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
109 int bpeekc_slow(struct fastbuf *f);
110 static inline int bpeekc(struct fastbuf *f)
112 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
115 static inline void bungetc(struct fastbuf *f)
120 void bputc_slow(struct fastbuf *f, uns c);
121 static inline void bputc(struct fastbuf *f, uns c)
123 if (f->bptr < f->bufend)
129 int bgetw_slow(struct fastbuf *f);
130 static inline int bgetw(struct fastbuf *f)
133 if (f->bptr + 2 <= f->bstop)
135 w = GET_U16(f->bptr);
140 return bgetw_slow(f);
143 u32 bgetl_slow(struct fastbuf *f);
144 static inline u32 bgetl(struct fastbuf *f)
147 if (f->bptr + 4 <= f->bstop)
149 l = GET_U32(f->bptr);
154 return bgetl_slow(f);
157 u64 bgetq_slow(struct fastbuf *f);
158 static inline u64 bgetq(struct fastbuf *f)
161 if (f->bptr + 8 <= f->bstop)
163 l = GET_U64(f->bptr);
168 return bgetq_slow(f);
171 u64 bget5_slow(struct fastbuf *f);
172 static inline u64 bget5(struct fastbuf *f)
175 if (f->bptr + 5 <= f->bstop)
177 l = GET_U40(f->bptr);
182 return bget5_slow(f);
185 void bputw_slow(struct fastbuf *f, uns w);
186 static inline void bputw(struct fastbuf *f, uns w)
188 if (f->bptr + 2 <= f->bufend)
197 void bputl_slow(struct fastbuf *f, u32 l);
198 static inline void bputl(struct fastbuf *f, u32 l)
200 if (f->bptr + 4 <= f->bufend)
209 void bputq_slow(struct fastbuf *f, u64 l);
210 static inline void bputq(struct fastbuf *f, u64 l)
212 if (f->bptr + 8 <= f->bufend)
221 void bput5_slow(struct fastbuf *f, u64 l);
222 static inline void bput5(struct fastbuf *f, u64 l)
224 if (f->bptr + 5 <= f->bufend)
233 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
234 static inline uns bread(struct fastbuf *f, void *b, uns l)
236 if (f->bptr + l <= f->bstop)
238 memcpy(b, f->bptr, l);
243 return bread_slow(f, b, l, 0);
246 static inline uns breadb(struct fastbuf *f, void *b, uns l)
248 if (f->bptr + l <= f->bstop)
250 memcpy(b, f->bptr, l);
255 return bread_slow(f, b, l, 1);
258 void bwrite_slow(struct fastbuf *f, void *b, uns l);
259 static inline void bwrite(struct fastbuf *f, void *b, uns l)
261 if (f->bptr + l <= f->bufend)
263 memcpy(f->bptr, b, l);
267 bwrite_slow(f, b, l);
270 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
271 byte *bgets0(struct fastbuf *f, byte *b, uns l);
274 bputs(struct fastbuf *f, byte *b)
276 bwrite(f, b, strlen(b));
280 bputs0(struct fastbuf *f, byte *b)
282 bwrite(f, b, strlen(b)+1);
286 bputsn(struct fastbuf *f, byte *b)
292 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
294 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
296 if (f->bptr + l <= f->bstop &&
297 t->bptr + l <= t->bufend)
299 memcpy(t->bptr, f->bptr, l);
304 bbcopy_slow(f, t, l);
307 /* I/O on addr_int_t */
309 #ifdef CPU_64BIT_POINTERS
310 #define bputa(x,p) bputq(x,p)
311 #define bgeta(x) bgetq(x)
313 #define bputa(x,p) bputl(x,p)
314 #define bgeta(x) bgetl(x)
317 /* Direct I/O on buffers */
320 bdirect_read_prepare(struct fastbuf *f, byte **buf)
322 if (f->bptr == f->bstop && !f->refill(f))
325 return f->bstop - f->bptr;
329 bdirect_read_commit(struct fastbuf *f, byte *pos)
335 bdirect_write_prepare(struct fastbuf *f, byte **buf)
337 if (f->bptr == f->bufend)
340 return f->bufend - f->bptr;
344 bdirect_write_commit(struct fastbuf *f, byte *pos)
349 /* Formatted output */
351 int bprintf(struct fastbuf *b, byte *msg, ...);
352 int vbprintf(struct fastbuf *b, byte *msg, va_list args);