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
15 * Generic buffered I/O on a top of buffer swapping functions.
17 * Buffer layout when reading:
19 * +----------------+---------------------------+
20 * | read data | free space |
21 * +----------------+---------------------------+
23 * buffer bptr bstop bufend
25 * After the last character is read, bptr == bstop and buffer refill
26 * is deferred to the next read attempt. This gives us an easy way
27 * how to implement bungetc().
31 * +----------------+---------------------------+
32 * | written data | free space |
33 * +----------------+---------------------------+
35 * buffer=bstop bptr bufend
39 byte *bptr, *bstop; /* Access pointers */
40 byte *buffer, *bufend; /* Start and end of the buffer */
41 byte *name; /* File name for error messages */
42 uns buflen; /* Size of the buffer */
43 sh_off_t pos; /* Position of buffer start in the file */
44 sh_off_t fdpos; /* Current position in the non-buffered file */
45 int fd; /* File descriptor, -1 if not a real file */
46 void *lldata; /* Data private to access functions below */
47 void *llpos; /* ... continued ... */
48 int (*refill)(struct fastbuf *); /* Get a buffer with new data */
49 void (*spout)(struct fastbuf *); /* Write buffer data to the file */
50 void (*seek)(struct fastbuf *, sh_off_t, int); /* Slow path for bseek(), buffer already flushed */
51 void (*close)(struct fastbuf *); /* Close the stream */
54 /* FastIO on standard files */
56 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
57 struct fastbuf *bfdopen(int fd, uns buffer);
58 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
60 /* FastIO on in-memory streams */
62 struct fastbuf *fbmem_create(unsigned blocksize); /* Create stream and return its writing fastbuf */
63 struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */
65 /* Universal functions working on all fastbuf's */
67 void bclose(struct fastbuf *f);
68 void bflush(struct fastbuf *f);
69 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
70 void bsetpos(struct fastbuf *f, sh_off_t pos);
72 static inline sh_off_t btell(struct fastbuf *f)
74 return f->pos + (f->bptr - f->buffer);
77 int bgetc_slow(struct fastbuf *f);
78 static inline int bgetc(struct fastbuf *f)
80 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
83 int bpeekc_slow(struct fastbuf *f);
84 static inline int bpeekc(struct fastbuf *f)
86 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
89 static inline void bungetc(struct fastbuf *f, byte c)
94 void bputc_slow(struct fastbuf *f, byte c);
95 static inline void bputc(struct fastbuf *f, byte c)
97 if (f->bptr < f->bufend)
103 word bgetw_slow(struct fastbuf *f);
104 static inline word bgetw(struct fastbuf *f)
107 if (f->bptr + 2 <= f->bstop)
110 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
113 #ifdef CPU_BIG_ENDIAN
114 w = (p[0] << 8) | p[1];
116 w = (p[1] << 8) | p[0];
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)
133 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
136 #ifdef CPU_BIG_ENDIAN
137 l = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
139 l = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
146 return bgetl_slow(f);
149 u64 bgetq_slow(struct fastbuf *f);
150 static inline u64 bgetq(struct fastbuf *f)
152 if (f->bptr + 8 <= f->bstop)
155 memcpy(&l, f->bptr, 8);
160 return bgetq_slow(f);
163 u64 bget5_slow(struct fastbuf *f);
164 static inline u64 bget5(struct fastbuf *f)
167 if (f->bptr + 5 <= f->bstop)
170 #ifdef CPU_BIG_ENDIAN
171 l = ((u64)p[0] << 32) | (u32)((p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4]);
173 l = ((u64)p[4] << 32) | (u32)((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
179 return bget5_slow(f);
182 void bputw_slow(struct fastbuf *f, word w);
183 static inline void bputw(struct fastbuf *f, word w)
185 if (f->bptr + 2 <= f->bufend)
188 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
191 #ifdef CPU_BIG_ENDIAN
205 void bputl_slow(struct fastbuf *f, u32 l);
206 static inline void bputl(struct fastbuf *f, u32 l)
208 if (f->bptr + 4 <= f->bufend)
211 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
214 #ifdef CPU_BIG_ENDIAN
232 void bputq_slow(struct fastbuf *f, u64 l);
233 static inline void bputq(struct fastbuf *f, u64 l)
235 if (f->bptr + 8 <= f->bufend)
237 memcpy(f->bptr, &l, 8);
244 void bput5_slow(struct fastbuf *f, u64 l);
245 static inline void bput5(struct fastbuf *f, u64 l)
247 if (f->bptr + 5 <= f->bufend)
251 #ifdef CPU_BIG_ENDIAN
270 uns bread_slow(struct fastbuf *f, void *b, uns l);
271 static inline uns bread(struct fastbuf *f, void *b, uns l)
273 if (f->bptr + l <= f->bstop)
275 memcpy(b, f->bptr, l);
280 return bread_slow(f, b, l);
283 void bwrite_slow(struct fastbuf *f, void *b, uns l);
284 static inline void bwrite(struct fastbuf *f, void *b, uns l)
286 if (f->bptr + l <= f->bufend)
288 memcpy(f->bptr, b, l);
292 bwrite_slow(f, b, l);
295 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
298 bputs(struct fastbuf *f, byte *b)
300 bwrite(f, b, strlen(b));
304 bputsn(struct fastbuf *f, byte *b)
310 /* Direct I/O on buffers */
312 int bdirect_read(struct fastbuf *f, byte **buf);
313 int bdirect_write_prepare(struct fastbuf *f, byte **buf);
314 void bdirect_write_commit(struct fastbuf *f, byte *pos);
316 /* Depending on compile-time configuration, we select the right function for reading/writing of file offsets */
318 #ifdef SHERLOCK_CONFIG_LARGE_DB
319 #define bgeto(f) bget5(f)
320 #define bputo(f,l) bput5(f,l)
321 #define bgetp(f) bgetq(f)
322 #define bputp(f,l) bputq(f,l)
324 #define bgeto(f) bgetl(f)
325 #define bputo(f,l) bputl(f,l)
326 #define bgetp(f) bgetl(f)
327 #define bputp(f,l) bputl(f,l)