2 * Sherlock Library -- Fast Buffered I/O
4 * (c) 1997--2000 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 on a top of buffer swapping functions.
24 * Buffer layout when reading:
26 * +----------------+---------------------------+
27 * | read data | free space |
28 * +----------------+---------------------------+
30 * buffer bptr bstop bufend
32 * After the last character is read, bptr == bstop and buffer refill
33 * is deferred to the next read attempt. This gives us an easy way
34 * how to implement bungetc().
38 * +----------------+---------------------------+
39 * | written data | free space |
40 * +----------------+---------------------------+
42 * buffer=bstop bptr bufend
46 byte *bptr, *bstop; /* Access pointers */
47 byte *buffer, *bufend; /* Start and end of the buffer */
48 byte *name; /* File name for error messages */
49 uns buflen; /* Size of the buffer */
50 sh_off_t pos; /* Position of buffer start in the file */
51 sh_off_t fdpos; /* Current position in the non-buffered file */
52 int fd; /* File descriptor, -1 if not a real file */
53 int is_temp_file; /* Is a temporary file, delete on close */
54 void *lldata; /* Data private to access functions below */
55 void *llpos; /* ... continued ... */
56 int (*refill)(struct fastbuf *); /* Get a buffer with new data */
57 void (*spout)(struct fastbuf *); /* Write buffer data to the file */
58 void (*seek)(struct fastbuf *, sh_off_t, int); /* Slow path for bseek(), buffer already flushed */
59 void (*close)(struct fastbuf *); /* Close the stream */
62 /* FastIO on standard files */
64 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
65 struct fastbuf *bopen_tmp(uns buffer);
66 struct fastbuf *bfdopen(int fd, uns buffer);
67 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
69 /* FastIO on in-memory streams */
71 struct fastbuf *fbmem_create(unsigned blocksize); /* Create stream and return its writing fastbuf */
72 struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */
74 /* Universal functions working on all fastbuf's */
76 void bclose(struct fastbuf *f);
77 void bflush(struct fastbuf *f);
78 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
79 void bsetpos(struct fastbuf *f, sh_off_t pos);
81 static inline sh_off_t btell(struct fastbuf *f)
83 return f->pos + (f->bptr - f->buffer);
86 int bgetc_slow(struct fastbuf *f);
87 static inline int bgetc(struct fastbuf *f)
89 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
92 int bpeekc_slow(struct fastbuf *f);
93 static inline int bpeekc(struct fastbuf *f)
95 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
98 static inline void bungetc(struct fastbuf *f)
103 void bputc_slow(struct fastbuf *f, uns c);
104 static inline void bputc(struct fastbuf *f, uns c)
106 if (f->bptr < f->bufend)
112 int bgetw_slow(struct fastbuf *f);
113 static inline int bgetw(struct fastbuf *f)
116 if (f->bptr + 2 <= f->bstop)
118 w = GET_U16(f->bptr);
123 return bgetw_slow(f);
126 u32 bgetl_slow(struct fastbuf *f);
127 static inline u32 bgetl(struct fastbuf *f)
130 if (f->bptr + 4 <= f->bstop)
132 l = GET_U32(f->bptr);
137 return bgetl_slow(f);
140 u64 bgetq_slow(struct fastbuf *f);
141 static inline u64 bgetq(struct fastbuf *f)
144 if (f->bptr + 8 <= f->bstop)
146 l = GET_U64(f->bptr);
151 return bgetq_slow(f);
154 u64 bget5_slow(struct fastbuf *f);
155 static inline u64 bget5(struct fastbuf *f)
158 if (f->bptr + 5 <= f->bstop)
160 l = GET_U40(f->bptr);
165 return bget5_slow(f);
168 void bputw_slow(struct fastbuf *f, uns w);
169 static inline void bputw(struct fastbuf *f, uns w)
171 if (f->bptr + 2 <= f->bufend)
180 void bputl_slow(struct fastbuf *f, u32 l);
181 static inline void bputl(struct fastbuf *f, u32 l)
183 if (f->bptr + 4 <= f->bufend)
192 void bputq_slow(struct fastbuf *f, u64 l);
193 static inline void bputq(struct fastbuf *f, u64 l)
195 if (f->bptr + 8 <= f->bufend)
204 void bput5_slow(struct fastbuf *f, u64 l);
205 static inline void bput5(struct fastbuf *f, u64 l)
207 if (f->bptr + 5 <= f->bufend)
216 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
217 static inline uns bread(struct fastbuf *f, void *b, uns l)
219 if (f->bptr + l <= f->bstop)
221 memcpy(b, f->bptr, l);
226 return bread_slow(f, b, l, 0);
229 static inline uns breadb(struct fastbuf *f, void *b, uns l)
231 if (f->bptr + l <= f->bstop)
233 memcpy(b, f->bptr, l);
238 return bread_slow(f, b, l, 1);
241 void bwrite_slow(struct fastbuf *f, void *b, uns l);
242 static inline void bwrite(struct fastbuf *f, void *b, uns l)
244 if (f->bptr + l <= f->bufend)
246 memcpy(f->bptr, b, l);
250 bwrite_slow(f, b, l);
253 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
254 byte *bgets0(struct fastbuf *f, byte *b, uns l);
257 bputs(struct fastbuf *f, byte *b)
259 bwrite(f, b, strlen(b));
263 bputs0(struct fastbuf *f, byte *b)
265 bwrite(f, b, strlen(b)+1);
269 bputsn(struct fastbuf *f, byte *b)
275 /* I/O on addr_int_t */
277 #ifdef CPU_64BIT_POINTERS
278 #define bputa(x,p) bputq(x,p)
279 #define bgeta(x) bgetq(x)
281 #define bputa(x,p) bputl(x,p)
282 #define bgeta(x) bgetl(x)
285 /* Direct I/O on buffers */
287 int bdirect_read(struct fastbuf *f, byte **buf);
288 int bdirect_write_prepare(struct fastbuf *f, byte **buf);
289 void bdirect_write_commit(struct fastbuf *f, byte *pos);