2 * Sherlock 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 _SHERLOCK_FASTBUF_H
12 #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 /* FastI on file descriptors with limit */
87 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
89 /* FastIO on static buffers */
91 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size);
92 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
94 fbbuf_count_written(struct fastbuf *f)
96 return f->bptr - f->bstop;
99 /* Configuring stream parameters */
101 int bconfig(struct fastbuf *f, uns type, int data);
103 #define BCONFIG_IS_TEMP_FILE 0
104 #define BCONFIG_CAN_OVERWRITE 1
105 /* Specified whether the caller is allowed to perform the following optimized
106 * 0-copy write operation:
107 * - get the buffer by bdirect_read_prepare()
108 * - modify the buffer, e.g. by putting \0's inside
109 * - call bflush() to let the fastbuf know
110 * 0: read-only memory
111 * 1: you can write into read-write memory, if you restore the original value
112 * 2: you can rewrite the original content */
114 /* Universal functions working on all fastbuf's */
116 void bclose(struct fastbuf *f);
117 void bflush(struct fastbuf *f);
118 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
119 void bsetpos(struct fastbuf *f, sh_off_t pos);
120 void brewind(struct fastbuf *f);
122 static inline sh_off_t btell(struct fastbuf *f)
124 return f->pos + (f->bptr - f->bstop);
127 int bgetc_slow(struct fastbuf *f);
128 static inline int bgetc(struct fastbuf *f)
130 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
133 int bpeekc_slow(struct fastbuf *f);
134 static inline int bpeekc(struct fastbuf *f)
136 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
139 static inline void bungetc(struct fastbuf *f)
144 void bputc_slow(struct fastbuf *f, uns c);
145 static inline void bputc(struct fastbuf *f, uns c)
147 if (f->bptr < f->bufend)
153 int bgetw_slow(struct fastbuf *f);
154 static inline int bgetw(struct fastbuf *f)
157 if (f->bptr + 2 <= f->bstop)
159 w = GET_U16(f->bptr);
164 return bgetw_slow(f);
167 u32 bgetl_slow(struct fastbuf *f);
168 static inline u32 bgetl(struct fastbuf *f)
171 if (f->bptr + 4 <= f->bstop)
173 l = GET_U32(f->bptr);
178 return bgetl_slow(f);
181 u64 bgetq_slow(struct fastbuf *f);
182 static inline u64 bgetq(struct fastbuf *f)
185 if (f->bptr + 8 <= f->bstop)
187 l = GET_U64(f->bptr);
192 return bgetq_slow(f);
195 u64 bget5_slow(struct fastbuf *f);
196 static inline u64 bget5(struct fastbuf *f)
199 if (f->bptr + 5 <= f->bstop)
201 l = GET_U40(f->bptr);
206 return bget5_slow(f);
209 void bputw_slow(struct fastbuf *f, uns w);
210 static inline void bputw(struct fastbuf *f, uns w)
212 if (f->bptr + 2 <= f->bufend)
221 void bputl_slow(struct fastbuf *f, u32 l);
222 static inline void bputl(struct fastbuf *f, u32 l)
224 if (f->bptr + 4 <= f->bufend)
233 void bputq_slow(struct fastbuf *f, u64 l);
234 static inline void bputq(struct fastbuf *f, u64 l)
236 if (f->bptr + 8 <= f->bufend)
245 void bput5_slow(struct fastbuf *f, u64 l);
246 static inline void bput5(struct fastbuf *f, u64 l)
248 if (f->bptr + 5 <= f->bufend)
257 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
258 static inline uns bread(struct fastbuf *f, void *b, uns l)
260 if (f->bptr + l <= f->bstop)
262 memcpy(b, f->bptr, l);
267 return bread_slow(f, b, l, 0);
270 static inline uns breadb(struct fastbuf *f, void *b, uns l)
272 if (f->bptr + l <= f->bstop)
274 memcpy(b, f->bptr, l);
279 return bread_slow(f, b, l, 1);
282 void bwrite_slow(struct fastbuf *f, void *b, uns l);
283 static inline void bwrite(struct fastbuf *f, void *b, uns l)
285 if (f->bptr + l <= f->bufend)
287 memcpy(f->bptr, b, l);
291 bwrite_slow(f, b, l);
294 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
295 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
296 byte *bgets0(struct fastbuf *f, byte *b, uns l);
299 bputs(struct fastbuf *f, byte *b)
301 bwrite(f, b, strlen(b));
305 bputs0(struct fastbuf *f, byte *b)
307 bwrite(f, b, strlen(b)+1);
311 bputsn(struct fastbuf *f, byte *b)
317 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
319 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
321 if (f->bptr + l <= f->bstop &&
322 t->bptr + l <= t->bufend)
324 memcpy(t->bptr, f->bptr, l);
329 bbcopy_slow(f, t, l);
332 /* I/O on addr_int_t */
334 #ifdef CPU_64BIT_POINTERS
335 #define bputa(x,p) bputq(x,p)
336 #define bgeta(x) bgetq(x)
338 #define bputa(x,p) bputl(x,p)
339 #define bgeta(x) bgetl(x)
342 /* Direct I/O on buffers */
345 bdirect_read_prepare(struct fastbuf *f, byte **buf)
347 if (f->bptr == f->bstop && !f->refill(f))
350 return f->bstop - f->bptr;
354 bdirect_read_commit(struct fastbuf *f, byte *pos)
360 bdirect_write_prepare(struct fastbuf *f, byte **buf)
362 if (f->bptr == f->bufend)
365 return f->bufend - f->bptr;
369 bdirect_write_commit(struct fastbuf *f, byte *pos)
374 /* Formatted output */
376 int bprintf(struct fastbuf *b, byte *msg, ...);
377 int vbprintf(struct fastbuf *b, byte *msg, va_list args);