2 * Sherlock Library -- Fast Buffered I/O
4 * (c) 1997--2000 Martin Mares <mj@ucw.cz>
7 #ifndef _SHERLOCK_FASTBUF_H
8 #define _SHERLOCK_FASTBUF_H
14 #include "lib/unaligned.h"
17 * Generic buffered I/O on a top of buffer swapping functions.
19 * Buffer layout when reading:
21 * +----------------+---------------------------+
22 * | read data | free space |
23 * +----------------+---------------------------+
25 * buffer bptr bstop bufend
27 * After the last character is read, bptr == bstop and buffer refill
28 * is deferred to the next read attempt. This gives us an easy way
29 * how to implement bungetc().
33 * +----------------+---------------------------+
34 * | written data | free space |
35 * +----------------+---------------------------+
37 * buffer=bstop bptr bufend
41 byte *bptr, *bstop; /* Access pointers */
42 byte *buffer, *bufend; /* Start and end of the buffer */
43 byte *name; /* File name for error messages */
44 uns buflen; /* Size of the buffer */
45 sh_off_t pos; /* Position of buffer start in the file */
46 sh_off_t fdpos; /* Current position in the non-buffered file */
47 int fd; /* File descriptor, -1 if not a real file */
48 int is_temp_file; /* Is a temporary file, delete on close */
49 void *lldata; /* Data private to access functions below */
50 void *llpos; /* ... continued ... */
51 int (*refill)(struct fastbuf *); /* Get a buffer with new data */
52 void (*spout)(struct fastbuf *); /* Write buffer data to the file */
53 void (*seek)(struct fastbuf *, sh_off_t, int); /* Slow path for bseek(), buffer already flushed */
54 void (*close)(struct fastbuf *); /* Close the stream */
57 /* FastIO on standard files */
59 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
60 struct fastbuf *bfdopen(int fd, uns buffer);
61 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
63 /* FastIO on in-memory streams */
65 struct fastbuf *fbmem_create(unsigned blocksize); /* Create stream and return its writing fastbuf */
66 struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */
68 /* Universal functions working on all fastbuf's */
70 void bclose(struct fastbuf *f);
71 void bflush(struct fastbuf *f);
72 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
73 void bsetpos(struct fastbuf *f, sh_off_t pos);
75 static inline sh_off_t btell(struct fastbuf *f)
77 return f->pos + (f->bptr - f->buffer);
80 int bgetc_slow(struct fastbuf *f);
81 static inline int bgetc(struct fastbuf *f)
83 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
86 int bpeekc_slow(struct fastbuf *f);
87 static inline int bpeekc(struct fastbuf *f)
89 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
92 static inline void bungetc(struct fastbuf *f, byte c)
97 void bputc_slow(struct fastbuf *f, byte c);
98 static inline void bputc(struct fastbuf *f, byte c)
100 if (f->bptr < f->bufend)
106 word bgetw_slow(struct fastbuf *f);
107 static inline word bgetw(struct fastbuf *f)
110 if (f->bptr + 2 <= f->bstop)
112 w = GET_U16(f->bptr);
117 return bgetw_slow(f);
120 u32 bgetl_slow(struct fastbuf *f);
121 static inline u32 bgetl(struct fastbuf *f)
124 if (f->bptr + 4 <= f->bstop)
126 l = GET_U32(f->bptr);
131 return bgetl_slow(f);
134 u64 bgetq_slow(struct fastbuf *f);
135 static inline u64 bgetq(struct fastbuf *f)
138 if (f->bptr + 8 <= f->bstop)
140 l = GET_U64(f->bptr);
145 return bgetq_slow(f);
148 u64 bget5_slow(struct fastbuf *f);
149 static inline u64 bget5(struct fastbuf *f)
152 if (f->bptr + 5 <= f->bstop)
154 l = GET_U40(f->bptr);
159 return bget5_slow(f);
162 void bputw_slow(struct fastbuf *f, word w);
163 static inline void bputw(struct fastbuf *f, word w)
165 if (f->bptr + 2 <= f->bufend)
174 void bputl_slow(struct fastbuf *f, u32 l);
175 static inline void bputl(struct fastbuf *f, u32 l)
177 if (f->bptr + 4 <= f->bufend)
186 void bputq_slow(struct fastbuf *f, u64 l);
187 static inline void bputq(struct fastbuf *f, u64 l)
189 if (f->bptr + 8 <= f->bufend)
198 void bput5_slow(struct fastbuf *f, u64 l);
199 static inline void bput5(struct fastbuf *f, u64 l)
201 if (f->bptr + 5 <= f->bufend)
210 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
211 static inline uns bread(struct fastbuf *f, void *b, uns l)
213 if (f->bptr + l <= f->bstop)
215 memcpy(b, f->bptr, l);
220 return bread_slow(f, b, l, 0);
223 static inline uns breadb(struct fastbuf *f, void *b, uns l)
225 if (f->bptr + l <= f->bstop)
227 memcpy(b, f->bptr, l);
232 return bread_slow(f, b, l, 1);
235 void bwrite_slow(struct fastbuf *f, void *b, uns l);
236 static inline void bwrite(struct fastbuf *f, void *b, uns l)
238 if (f->bptr + l <= f->bufend)
240 memcpy(f->bptr, b, l);
244 bwrite_slow(f, b, l);
247 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
248 byte *bgets0(struct fastbuf *f, byte *b, uns l);
251 bputs(struct fastbuf *f, byte *b)
253 bwrite(f, b, strlen(b));
257 bputs0(struct fastbuf *f, byte *b)
259 bwrite(f, b, strlen(b)+1);
263 bputsn(struct fastbuf *f, byte *b)
269 /* I/O on addr_int_t */
271 #ifdef CPU_64BIT_POINTERS
272 #define bputa(x,p) bputq(x,p)
273 #define bgeta(x) bgetq(x)
275 #define bputa(x,p) bputl(x,p)
276 #define bgeta(x) bgetl(x)
279 /* Direct I/O on buffers */
281 int bdirect_read(struct fastbuf *f, byte **buf);
282 int bdirect_write_prepare(struct fastbuf *f, byte **buf);
283 void bdirect_write_commit(struct fastbuf *f, byte *pos);