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 static inline sh_off_t btell(struct fastbuf *f)
71 return f->pos + (f->bptr - f->buffer);
74 int bgetc_slow(struct fastbuf *f);
75 static 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 static inline int bpeekc(struct fastbuf *f)
83 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
86 static inline void bungetc(struct fastbuf *f, byte c)
91 void bputc_slow(struct fastbuf *f, byte c);
92 static inline void bputc(struct fastbuf *f, byte c)
94 if (f->bptr < f->bufend)
100 word bgetw_slow(struct fastbuf *f);
101 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static inline void bput5(struct fastbuf *f, u64 l)
244 if (f->bptr + 5 <= f->bufend)
248 #ifdef CPU_BIG_ENDIAN
267 uns bread_slow(struct fastbuf *f, void *b, uns l);
268 static inline uns bread(struct fastbuf *f, void *b, uns l)
270 if (f->bptr + l <= f->bstop)
272 memcpy(b, f->bptr, l);
277 return bread_slow(f, b, l);
280 void bwrite_slow(struct fastbuf *f, void *b, uns l);
281 static inline void bwrite(struct fastbuf *f, void *b, uns l)
283 if (f->bptr + l <= f->bufend)
285 memcpy(f->bptr, b, l);
289 bwrite_slow(f, b, l);
292 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
295 bputs(struct fastbuf *f, byte *b)
297 bwrite(f, b, strlen(b));
301 bputsn(struct fastbuf *f, byte *b)
307 /* Depending on compile-time configuration, we select the right function for reading/writing of file offsets */
309 #ifdef SHERLOCK_CONFIG_LARGE_DB
310 #define bgeto(f) bget5(f)
311 #define bputo(f,l) bput5(f,l)
312 #define bgetp(f) bgetq(f)
313 #define bputp(f,l) bputq(f,l)
315 #define bgeto(f) bgetl(f)
316 #define bputo(f,l) bputl(f,l)
317 #define bgetp(f) bgetl(f)
318 #define bputp(f,l) bputl(f,l)