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
16 #include "lib/unaligned.h"
19 * Generic buffered I/O on a top of buffer swapping functions.
21 * Buffer layout when reading:
23 * +----------------+---------------------------+
24 * | read data | free space |
25 * +----------------+---------------------------+
27 * buffer bptr bstop bufend
29 * After the last character is read, bptr == bstop and buffer refill
30 * is deferred to the next read attempt. This gives us an easy way
31 * how to implement bungetc().
35 * +----------------+---------------------------+
36 * | written data | free space |
37 * +----------------+---------------------------+
39 * buffer=bstop bptr bufend
43 byte *bptr, *bstop; /* Access pointers */
44 byte *buffer, *bufend; /* Start and end of the buffer */
45 byte *name; /* File name for error messages */
46 uns buflen; /* Size of the buffer */
47 sh_off_t pos; /* Position of buffer start in the file */
48 sh_off_t fdpos; /* Current position in the non-buffered file */
49 int fd; /* File descriptor, -1 if not a real file */
50 int is_temp_file; /* Is a temporary file, delete on close */
51 void *lldata; /* Data private to access functions below */
52 void *llpos; /* ... continued ... */
53 int (*refill)(struct fastbuf *); /* Get a buffer with new data */
54 void (*spout)(struct fastbuf *); /* Write buffer data to the file */
55 void (*seek)(struct fastbuf *, sh_off_t, int); /* Slow path for bseek(), buffer already flushed */
56 void (*close)(struct fastbuf *); /* Close the stream */
59 /* FastIO on standard files */
61 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
62 struct fastbuf *bfdopen(int fd, uns buffer);
63 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
65 /* FastIO on in-memory streams */
67 struct fastbuf *fbmem_create(unsigned blocksize); /* Create stream and return its writing fastbuf */
68 struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */
70 /* Universal functions working on all fastbuf's */
72 void bclose(struct fastbuf *f);
73 void bflush(struct fastbuf *f);
74 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
75 void bsetpos(struct fastbuf *f, sh_off_t pos);
77 static inline sh_off_t btell(struct fastbuf *f)
79 return f->pos + (f->bptr - f->buffer);
82 int bgetc_slow(struct fastbuf *f);
83 static inline int bgetc(struct fastbuf *f)
85 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
88 int bpeekc_slow(struct fastbuf *f);
89 static inline int bpeekc(struct fastbuf *f)
91 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
94 static inline void bungetc(struct fastbuf *f, byte c)
99 void bputc_slow(struct fastbuf *f, byte c);
100 static inline void bputc(struct fastbuf *f, byte c)
102 if (f->bptr < f->bufend)
108 int bgetw_slow(struct fastbuf *f);
109 static inline int bgetw(struct fastbuf *f)
112 if (f->bptr + 2 <= f->bstop)
114 w = GET_U16(f->bptr);
119 return bgetw_slow(f);
122 u32 bgetl_slow(struct fastbuf *f);
123 static inline u32 bgetl(struct fastbuf *f)
126 if (f->bptr + 4 <= f->bstop)
128 l = GET_U32(f->bptr);
133 return bgetl_slow(f);
136 u64 bgetq_slow(struct fastbuf *f);
137 static inline u64 bgetq(struct fastbuf *f)
140 if (f->bptr + 8 <= f->bstop)
142 l = GET_U64(f->bptr);
147 return bgetq_slow(f);
150 u64 bget5_slow(struct fastbuf *f);
151 static inline u64 bget5(struct fastbuf *f)
154 if (f->bptr + 5 <= f->bstop)
156 l = GET_U40(f->bptr);
161 return bget5_slow(f);
164 void bputw_slow(struct fastbuf *f, word w);
165 static inline void bputw(struct fastbuf *f, word w)
167 if (f->bptr + 2 <= f->bufend)
176 void bputl_slow(struct fastbuf *f, u32 l);
177 static inline void bputl(struct fastbuf *f, u32 l)
179 if (f->bptr + 4 <= f->bufend)
188 void bputq_slow(struct fastbuf *f, u64 l);
189 static inline void bputq(struct fastbuf *f, u64 l)
191 if (f->bptr + 8 <= f->bufend)
200 void bput5_slow(struct fastbuf *f, u64 l);
201 static inline void bput5(struct fastbuf *f, u64 l)
203 if (f->bptr + 5 <= f->bufend)
212 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
213 static inline uns bread(struct fastbuf *f, void *b, uns l)
215 if (f->bptr + l <= f->bstop)
217 memcpy(b, f->bptr, l);
222 return bread_slow(f, b, l, 0);
225 static inline uns breadb(struct fastbuf *f, void *b, uns l)
227 if (f->bptr + l <= f->bstop)
229 memcpy(b, f->bptr, l);
234 return bread_slow(f, b, l, 1);
237 void bwrite_slow(struct fastbuf *f, void *b, uns l);
238 static inline void bwrite(struct fastbuf *f, void *b, uns l)
240 if (f->bptr + l <= f->bufend)
242 memcpy(f->bptr, b, l);
246 bwrite_slow(f, b, l);
249 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
250 byte *bgets0(struct fastbuf *f, byte *b, uns l);
253 bputs(struct fastbuf *f, byte *b)
255 bwrite(f, b, strlen(b));
259 bputs0(struct fastbuf *f, byte *b)
261 bwrite(f, b, strlen(b)+1);
265 bputsn(struct fastbuf *f, byte *b)
271 /* I/O on addr_int_t */
273 #ifdef CPU_64BIT_POINTERS
274 #define bputa(x,p) bputq(x,p)
275 #define bgeta(x) bgetq(x)
277 #define bputa(x,p) bputl(x,p)
278 #define bgeta(x) bgetl(x)
281 /* Direct I/O on buffers */
283 int bdirect_read(struct fastbuf *f, byte **buf);
284 int bdirect_write_prepare(struct fastbuf *f, byte **buf);
285 void bdirect_write_commit(struct fastbuf *f, byte *pos);