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
19 #include "lib/unaligned.h"
22 * Generic buffered I/O. You supply hooks to be called for low-level operations
23 * (swapping of buffers, seeking and closing), we do the rest.
25 * Buffer layout when reading:
27 * +----------------+---------------------------+
28 * | read data | free space |
29 * +----------------+---------------------------+
31 * buffer bptr bstop bufend
33 * After the last character is read, bptr == bstop and buffer refill
34 * is deferred to the next read attempt. This gives us an easy way
35 * how to implement bungetc().
39 * +----------------+---------------------------+
40 * | written data | free space |
41 * +----------------+---------------------------+
43 * buffer=bstop bptr bufend
47 * - You can mix reads and writes on the same stream, but you must
48 * call bflush() in between and remember that the file position
49 * points after the flushed buffer which is not necessarily the same
50 * as after the data you've read.
51 * - The spout/refill hooks can change not only bptr and bstop, but also
52 * the location of the buffer; fb-mem.c takes advantage of it.
56 byte is_fastbuf[0]; /* Dummy field for checking of type casts */
57 byte *bptr, *bstop; /* Access pointers */
58 byte *buffer, *bufend; /* Start and end of the buffer */
59 byte *name; /* File name for error messages */
60 sh_off_t pos; /* Position of bstop in the file */
61 int (*refill)(struct fastbuf *); /* Get a buffer with new data */
62 void (*spout)(struct fastbuf *); /* Write buffer data to the file */
63 void (*seek)(struct fastbuf *, sh_off_t, int); /* Slow path for bseek(), buffer already flushed */
64 void (*close)(struct fastbuf *); /* Close the stream */
67 /* FastIO on standard files */
71 int fd; /* File descriptor, -1 if not a real file */
72 int is_temp_file; /* 0=normal file, 1=temporary file, delete on close, -1=shared FD */
74 #define FB_FILE(f) ((struct fb_file *)(f)->is_fastbuf)
76 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
77 struct fastbuf *bopen_tmp(uns buffer);
78 struct fastbuf *bfdopen(int fd, uns buffer);
79 struct fastbuf *bfdopen_shared(int fd, uns buffer);
80 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
81 #define FB_IS_TEMP_FILE(f) FB_FILE(f)->is_temp_file
83 /* FastIO on in-memory streams */
85 struct fastbuf *fbmem_create(unsigned blocksize); /* Create stream and return its writing fastbuf */
86 struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */
88 /* Universal functions working on all fastbuf's */
90 void bclose(struct fastbuf *f);
91 void bflush(struct fastbuf *f);
92 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
93 void bsetpos(struct fastbuf *f, sh_off_t pos);
95 static inline sh_off_t btell(struct fastbuf *f)
97 return f->pos + (f->bptr - f->bstop);
100 int bgetc_slow(struct fastbuf *f);
101 static inline int bgetc(struct fastbuf *f)
103 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
106 int bpeekc_slow(struct fastbuf *f);
107 static inline int bpeekc(struct fastbuf *f)
109 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
112 static inline void bungetc(struct fastbuf *f)
117 void bputc_slow(struct fastbuf *f, uns c);
118 static inline void bputc(struct fastbuf *f, uns c)
120 if (f->bptr < f->bufend)
126 int bgetw_slow(struct fastbuf *f);
127 static inline int bgetw(struct fastbuf *f)
130 if (f->bptr + 2 <= f->bstop)
132 w = GET_U16(f->bptr);
137 return bgetw_slow(f);
140 u32 bgetl_slow(struct fastbuf *f);
141 static inline u32 bgetl(struct fastbuf *f)
144 if (f->bptr + 4 <= f->bstop)
146 l = GET_U32(f->bptr);
151 return bgetl_slow(f);
154 u64 bgetq_slow(struct fastbuf *f);
155 static inline u64 bgetq(struct fastbuf *f)
158 if (f->bptr + 8 <= f->bstop)
160 l = GET_U64(f->bptr);
165 return bgetq_slow(f);
168 u64 bget5_slow(struct fastbuf *f);
169 static inline u64 bget5(struct fastbuf *f)
172 if (f->bptr + 5 <= f->bstop)
174 l = GET_U40(f->bptr);
179 return bget5_slow(f);
182 void bputw_slow(struct fastbuf *f, uns w);
183 static inline void bputw(struct fastbuf *f, uns w)
185 if (f->bptr + 2 <= f->bufend)
194 void bputl_slow(struct fastbuf *f, u32 l);
195 static inline void bputl(struct fastbuf *f, u32 l)
197 if (f->bptr + 4 <= f->bufend)
206 void bputq_slow(struct fastbuf *f, u64 l);
207 static inline void bputq(struct fastbuf *f, u64 l)
209 if (f->bptr + 8 <= f->bufend)
218 void bput5_slow(struct fastbuf *f, u64 l);
219 static inline void bput5(struct fastbuf *f, u64 l)
221 if (f->bptr + 5 <= f->bufend)
230 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
231 static inline uns bread(struct fastbuf *f, void *b, uns l)
233 if (f->bptr + l <= f->bstop)
235 memcpy(b, f->bptr, l);
240 return bread_slow(f, b, l, 0);
243 static inline uns breadb(struct fastbuf *f, void *b, uns l)
245 if (f->bptr + l <= f->bstop)
247 memcpy(b, f->bptr, l);
252 return bread_slow(f, b, l, 1);
255 void bwrite_slow(struct fastbuf *f, void *b, uns l);
256 static inline void bwrite(struct fastbuf *f, void *b, uns l)
258 if (f->bptr + l <= f->bufend)
260 memcpy(f->bptr, b, l);
264 bwrite_slow(f, b, l);
267 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
268 byte *bgets0(struct fastbuf *f, byte *b, uns l);
271 bputs(struct fastbuf *f, byte *b)
273 bwrite(f, b, strlen(b));
277 bputs0(struct fastbuf *f, byte *b)
279 bwrite(f, b, strlen(b)+1);
283 bputsn(struct fastbuf *f, byte *b)
289 /* I/O on addr_int_t */
291 #ifdef CPU_64BIT_POINTERS
292 #define bputa(x,p) bputq(x,p)
293 #define bgeta(x) bgetq(x)
295 #define bputa(x,p) bputl(x,p)
296 #define bgeta(x) bgetl(x)
299 /* Direct I/O on buffers */
301 int bdirect_read_prepare(struct fastbuf *f, byte **buf);
302 void bdirect_read_commit(struct fastbuf *f, byte *pos);
303 int bdirect_write_prepare(struct fastbuf *f, byte **buf);
304 void bdirect_write_commit(struct fastbuf *f, byte *pos);