2 * Sherlock Library -- Fast Buffered I/O
4 * (c) 1997--2000 Martin Mares <mj@ucw.cz>
12 * Generic buffered I/O on a top of buffer swapping functions.
14 * Buffer layout when reading:
16 * +----------------+---------------------------+
17 * | read data | free space |
18 * +----------------+---------------------------+
20 * buffer bptr bstop bufend
22 * After the last character is read, bptr == bstop and buffer refill
23 * is deferred to the next read attempt. This gives us an easy way
24 * how to implement bungetc().
28 * +----------------+---------------------------+
29 * | written data | free space |
30 * +----------------+---------------------------+
32 * buffer=bstop bptr bufend
36 byte *bptr, *bstop; /* Access pointers */
37 byte *buffer, *bufend; /* Start and end of the buffer */
38 byte *name; /* File name for error messages */
39 uns buflen; /* Size of the buffer */
40 sh_off_t pos; /* Position of buffer start in the file */
41 sh_off_t fdpos; /* Current position in the non-buffered file */
42 int fd; /* File descriptor, -1 if not a real file */
43 void *lldata; /* Data private to access functions below */
44 void *llpos; /* ... continued ... */
45 int (*refill)(struct fastbuf *); /* Get a buffer with new data */
46 void (*spout)(struct fastbuf *); /* Write buffer data to the file */
47 void (*seek)(struct fastbuf *, sh_off_t, int); /* Slow path for bseek(), buffer already flushed */
48 void (*close)(struct fastbuf *); /* Close the stream */
51 /* FastIO on standard files */
53 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
54 struct fastbuf *bfdopen(int fd, uns buffer);
55 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
57 /* FastIO on in-memory streams */
59 struct fastbuf *fbmem_create(unsigned blocksize); /* Create stream and return its writing fastbuf */
60 struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */
62 /* Universal functions working on all fastbuf's */
64 void bclose(struct fastbuf *f);
65 void bflush(struct fastbuf *f);
66 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
67 void bsetpos(struct fastbuf *f, sh_off_t pos);
69 extern inline sh_off_t btell(struct fastbuf *f)
71 return f->pos + (f->bptr - f->buffer);
74 int bgetc_slow(struct fastbuf *f);
75 extern inline int bgetc(struct fastbuf *f)
77 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
80 int bpeekc_slow(struct fastbuf *f);
81 extern inline int bpeekc(struct fastbuf *f)
83 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
86 extern inline void bungetc(struct fastbuf *f, byte c)
91 void bputc_slow(struct fastbuf *f, byte c);
92 extern inline void bputc(struct fastbuf *f, byte c)
94 if (f->bptr < f->bufend)
100 word bgetw_slow(struct fastbuf *f);
101 extern inline word bgetw(struct fastbuf *f)
104 if (f->bptr + 2 <= f->bstop)
107 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
110 #ifdef CPU_BIG_ENDIAN
111 w = (p[0] << 8) | p[1];
113 w = (p[1] << 8) | p[0];
120 return bgetw_slow(f);
123 u32 bgetl_slow(struct fastbuf *f);
124 extern inline u32 bgetl(struct fastbuf *f)
127 if (f->bptr + 4 <= f->bstop)
130 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
133 #ifdef CPU_BIG_ENDIAN
134 l = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
136 l = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
143 return bgetl_slow(f);
146 u64 bgetq_slow(struct fastbuf *f);
147 extern inline u64 bgetq(struct fastbuf *f)
149 if (f->bptr + 8 <= f->bstop)
152 memcpy(&l, f->bptr, 8);
157 return bgetq_slow(f);
160 u64 bget5_slow(struct fastbuf *f);
161 extern inline u64 bget5(struct fastbuf *f)
164 if (f->bptr + 5 <= f->bstop)
167 #ifdef CPU_BIG_ENDIAN
168 l = ((u64)p[0] << 32) | (u32)((p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4]);
170 l = ((u64)p[4] << 32) | (u32)((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
176 return bget5_slow(f);
179 void bputw_slow(struct fastbuf *f, word w);
180 extern inline void bputw(struct fastbuf *f, word w)
182 if (f->bptr + 2 <= f->bufend)
185 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
188 #ifdef CPU_BIG_ENDIAN
202 void bputl_slow(struct fastbuf *f, u32 l);
203 extern inline void bputl(struct fastbuf *f, u32 l)
205 if (f->bptr + 4 <= f->bufend)
208 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
211 #ifdef CPU_BIG_ENDIAN
229 void bputq_slow(struct fastbuf *f, u64 l);
230 extern inline void bputq(struct fastbuf *f, u64 l)
232 if (f->bptr + 8 <= f->bufend)
234 memcpy(f->bptr, &l, 8);
241 void bput5_slow(struct fastbuf *f, u64 l);
242 extern inline void bput5(struct fastbuf *f, u64 l)
244 if (f->bptr + 5 <= f->bufend)
248 #ifdef CPU_BIG_ENDIAN
267 void bread_slow(struct fastbuf *f, void *b, uns l);
268 extern inline void bread(struct fastbuf *f, void *b, uns l)
270 if (f->bptr + l <= f->bstop)
272 memcpy(b, f->bptr, l);
279 void bwrite_slow(struct fastbuf *f, void *b, uns l);
280 extern inline void bwrite(struct fastbuf *f, void *b, uns l)
282 if (f->bptr + l <= f->bufend)
284 memcpy(f->bptr, b, l);
288 bwrite_slow(f, b, l);
291 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
294 bputs(struct fastbuf *f, byte *b)
296 bwrite(f, b, strlen(b));
300 bputsn(struct fastbuf *f, byte *b)
306 /* Depending on compile-time configuration, we select the right function for reading/writing of file offsets */
308 #ifdef SHERLOCK_CONFIG_LARGE_DB
309 #define bgeto(f) bget5(f)
310 #define bputo(f,l) bput5(f,l)
311 #define bgetp(f) bgetq(f)
312 #define bputp(f,l) bputq(f,l)
314 #define bgeto(f) bgetl(f)
315 #define bputo(f,l) bputl(f,l)
316 #define bgetp(f) bgetl(f)
317 #define bputp(f,l) bputl(f,l)