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
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.
54 * - In some cases, the user of the bdirect interface can be allowed to modify
55 * the data in the buffer to avoid unnecessary copying. If the back-end
56 * allows such modifications, it can set can_overwrite_buffer accordingly:
57 * * 0 if no modification is allowed,
58 * * 1 if the user can modify the buffer on the condition that
59 * the modifications will be undone before calling the next
61 * * 2 if the user is allowed to overwrite the data in the buffer
62 * if bdirect_read_commit_modified() is called afterwards.
63 * In this case, the back-end must be prepared for trimming
64 * of the buffer which is done by the commit function.
68 byte is_fastbuf[0]; /* Dummy field for checking of type casts */
69 byte *bptr, *bstop; /* Access pointers */
70 byte *buffer, *bufend; /* Start and end of the buffer */
71 byte *name; /* File name for error messages */
72 sh_off_t pos; /* Position of bstop in the file */
73 int (*refill)(struct fastbuf *); /* Get a buffer with new data */
74 void (*spout)(struct fastbuf *); /* Write buffer data to the file */
75 void (*seek)(struct fastbuf *, sh_off_t, int); /* Slow path for bseek(), buffer already flushed */
76 void (*close)(struct fastbuf *); /* Close the stream */
77 int (*config)(struct fastbuf *, uns, int); /* Configure the stream */
78 int can_overwrite_buffer; /* Can the buffer be altered? (see discussion above) 0=never, 1=temporarily, 2=permanently */
81 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
83 struct fastbuf *bopen(byte *name, uns mode, uns buflen);
84 struct fastbuf *bopen_tmp(uns buflen);
85 struct fastbuf *bfdopen(int fd, uns buflen);
86 struct fastbuf *bfdopen_shared(int fd, uns buflen);
87 void bfilesync(struct fastbuf *b);
89 /* FastIO on in-memory streams */
91 struct fastbuf *fbmem_create(unsigned blocksize); /* Create stream and return its writing fastbuf */
92 struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */
94 /* FastIO on memory mapped files */
96 struct fastbuf *bopen_mm(byte *name, uns mode);
98 /* FastI on file descriptors with limit */
100 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
102 /* FastIO on static buffers */
104 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite);
105 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
107 fbbuf_count_written(struct fastbuf *f)
109 return f->bptr - f->bstop;
112 /* Configuring stream parameters */
114 int bconfig(struct fastbuf *f, uns type, int data);
116 #define BCONFIG_IS_TEMP_FILE 0
118 /* Universal functions working on all fastbuf's */
120 void bclose(struct fastbuf *f);
121 void bflush(struct fastbuf *f);
122 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
123 void bsetpos(struct fastbuf *f, sh_off_t pos);
124 void brewind(struct fastbuf *f);
125 sh_off_t bfilesize(struct fastbuf *f);
127 static inline sh_off_t btell(struct fastbuf *f)
129 return f->pos + (f->bptr - f->bstop);
132 int bgetc_slow(struct fastbuf *f);
133 static inline int bgetc(struct fastbuf *f)
135 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
138 int bpeekc_slow(struct fastbuf *f);
139 static inline int bpeekc(struct fastbuf *f)
141 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
144 static inline void bungetc(struct fastbuf *f)
149 void bputc_slow(struct fastbuf *f, uns c);
150 static inline void bputc(struct fastbuf *f, uns c)
152 if (f->bptr < f->bufend)
159 bavailr(struct fastbuf *f)
161 return f->bstop - f->bptr;
165 bavailw(struct fastbuf *f)
167 return f->bufend - f->bptr;
170 int bgetw_slow(struct fastbuf *f);
171 static inline int bgetw(struct fastbuf *f)
176 w = GET_U16(f->bptr);
181 return bgetw_slow(f);
184 u32 bgetl_slow(struct fastbuf *f);
185 static inline u32 bgetl(struct fastbuf *f)
190 l = GET_U32(f->bptr);
195 return bgetl_slow(f);
198 u64 bgetq_slow(struct fastbuf *f);
199 static inline u64 bgetq(struct fastbuf *f)
204 l = GET_U64(f->bptr);
209 return bgetq_slow(f);
212 u64 bget5_slow(struct fastbuf *f);
213 static inline u64 bget5(struct fastbuf *f)
218 l = GET_U40(f->bptr);
223 return bget5_slow(f);
226 void bputw_slow(struct fastbuf *f, uns w);
227 static inline void bputw(struct fastbuf *f, uns w)
238 void bputl_slow(struct fastbuf *f, u32 l);
239 static inline void bputl(struct fastbuf *f, u32 l)
250 void bputq_slow(struct fastbuf *f, u64 l);
251 static inline void bputq(struct fastbuf *f, u64 l)
262 void bput5_slow(struct fastbuf *f, u64 l);
263 static inline void bput5(struct fastbuf *f, u64 l)
274 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
275 static inline uns bread(struct fastbuf *f, void *b, uns l)
279 memcpy(b, f->bptr, l);
284 return bread_slow(f, b, l, 0);
287 static inline uns breadb(struct fastbuf *f, void *b, uns l)
291 memcpy(b, f->bptr, l);
296 return bread_slow(f, b, l, 1);
299 void bwrite_slow(struct fastbuf *f, void *b, uns l);
300 static inline void bwrite(struct fastbuf *f, void *b, uns l)
304 memcpy(f->bptr, b, l);
308 bwrite_slow(f, b, l);
311 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
312 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
313 byte *bgets0(struct fastbuf *f, byte *b, uns l);
316 bputs(struct fastbuf *f, byte *b)
318 bwrite(f, b, strlen(b));
322 bputs0(struct fastbuf *f, byte *b)
324 bwrite(f, b, strlen(b)+1);
328 bputsn(struct fastbuf *f, byte *b)
334 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
336 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
338 if (bavailr(f) >= l && bavailw(t) >= l)
340 memcpy(t->bptr, f->bptr, l);
345 bbcopy_slow(f, t, l);
348 int bskip_slow(struct fastbuf *f, uns len);
349 static inline int bskip(struct fastbuf *f, uns len)
351 if (bavailr(f) >= len)
357 return bskip_slow(f, len);
360 /* I/O on addr_int_t */
362 #ifdef CPU_64BIT_POINTERS
363 #define bputa(x,p) bputq(x,p)
364 #define bgeta(x) bgetq(x)
366 #define bputa(x,p) bputl(x,p)
367 #define bgeta(x) bgetl(x)
370 /* Direct I/O on buffers */
373 bdirect_read_prepare(struct fastbuf *f, byte **buf)
375 if (f->bptr == f->bstop && !f->refill(f))
377 *buf = NULL; // This is not needed, but it helps to get rid of spurious warnings
385 bdirect_read_commit(struct fastbuf *f, byte *pos)
391 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
394 f->buffer = pos; /* Avoid seeking backwards in the buffer */
398 bdirect_write_prepare(struct fastbuf *f, byte **buf)
400 if (f->bptr == f->bufend)
407 bdirect_write_commit(struct fastbuf *f, byte *pos)
412 /* Formatted output */
414 int bprintf(struct fastbuf *b, char *msg, ...) FORMAT_CHECK(printf,2,3);
415 int vbprintf(struct fastbuf *b, char *msg, va_list args);