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 int is_temp_file; /* Is a temporary file, delete on close */
47 void *lldata; /* Data private to access functions below */
48 void *llpos; /* ... continued ... */
49 int (*refill)(struct fastbuf *); /* Get a buffer with new data */
50 void (*spout)(struct fastbuf *); /* Write buffer data to the file */
51 void (*seek)(struct fastbuf *, sh_off_t, int); /* Slow path for bseek(), buffer already flushed */
52 void (*close)(struct fastbuf *); /* Close the stream */
55 /* FastIO on standard files */
57 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
58 struct fastbuf *bfdopen(int fd, uns buffer);
59 void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l);
61 /* FastIO on in-memory streams */
63 struct fastbuf *fbmem_create(unsigned blocksize); /* Create stream and return its writing fastbuf */
64 struct fastbuf *fbmem_clone_read(struct fastbuf *); /* Create reading fastbuf */
66 /* Universal functions working on all fastbuf's */
68 void bclose(struct fastbuf *f);
69 void bflush(struct fastbuf *f);
70 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
71 void bsetpos(struct fastbuf *f, sh_off_t pos);
73 static inline sh_off_t btell(struct fastbuf *f)
75 return f->pos + (f->bptr - f->buffer);
78 int bgetc_slow(struct fastbuf *f);
79 static inline int bgetc(struct fastbuf *f)
81 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
84 int bpeekc_slow(struct fastbuf *f);
85 static inline int bpeekc(struct fastbuf *f)
87 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
90 static inline void bungetc(struct fastbuf *f, byte c)
95 void bputc_slow(struct fastbuf *f, byte c);
96 static inline void bputc(struct fastbuf *f, byte c)
98 if (f->bptr < f->bufend)
104 word bgetw_slow(struct fastbuf *f);
105 static inline word bgetw(struct fastbuf *f)
108 if (f->bptr + 2 <= f->bstop)
111 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
114 #ifdef CPU_BIG_ENDIAN
115 w = (p[0] << 8) | p[1];
117 w = (p[1] << 8) | p[0];
124 return bgetw_slow(f);
127 u32 bgetl_slow(struct fastbuf *f);
128 static inline u32 bgetl(struct fastbuf *f)
131 if (f->bptr + 4 <= f->bstop)
134 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
137 #ifdef CPU_BIG_ENDIAN
138 l = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
140 l = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
147 return bgetl_slow(f);
150 u64 bgetq_slow(struct fastbuf *f);
151 static inline u64 bgetq(struct fastbuf *f)
153 if (f->bptr + 8 <= f->bstop)
156 memcpy(&l, f->bptr, 8);
161 return bgetq_slow(f);
164 u64 bget5_slow(struct fastbuf *f);
165 static inline u64 bget5(struct fastbuf *f)
168 if (f->bptr + 5 <= f->bstop)
171 #ifdef CPU_BIG_ENDIAN
172 l = ((u64)p[0] << 32) | (u32)((p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4]);
174 l = ((u64)p[4] << 32) | (u32)((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
180 return bget5_slow(f);
183 void bputw_slow(struct fastbuf *f, word w);
184 static inline void bputw(struct fastbuf *f, word w)
186 if (f->bptr + 2 <= f->bufend)
189 #ifdef CPU_CAN_DO_UNALIGNED_WORDS
192 #ifdef CPU_BIG_ENDIAN
206 void bputl_slow(struct fastbuf *f, u32 l);
207 static inline void bputl(struct fastbuf *f, u32 l)
209 if (f->bptr + 4 <= f->bufend)
212 #ifdef CPU_CAN_DO_UNALIGNED_LONGS
215 #ifdef CPU_BIG_ENDIAN
233 void bputq_slow(struct fastbuf *f, u64 l);
234 static inline void bputq(struct fastbuf *f, u64 l)
236 if (f->bptr + 8 <= f->bufend)
238 memcpy(f->bptr, &l, 8);
245 void bput5_slow(struct fastbuf *f, u64 l);
246 static inline void bput5(struct fastbuf *f, u64 l)
248 if (f->bptr + 5 <= f->bufend)
252 #ifdef CPU_BIG_ENDIAN
271 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
272 static inline uns bread(struct fastbuf *f, void *b, uns l)
274 if (f->bptr + l <= f->bstop)
276 memcpy(b, f->bptr, l);
281 return bread_slow(f, b, l, 0);
284 static inline uns breadb(struct fastbuf *f, void *b, uns l)
286 if (f->bptr + l <= f->bstop)
288 memcpy(b, f->bptr, l);
293 return bread_slow(f, b, l, 1);
296 void bwrite_slow(struct fastbuf *f, void *b, uns l);
297 static inline void bwrite(struct fastbuf *f, void *b, uns l)
299 if (f->bptr + l <= f->bufend)
301 memcpy(f->bptr, b, l);
305 bwrite_slow(f, b, l);
308 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
309 byte *bgets0(struct fastbuf *f, byte *b, uns l);
312 bputs(struct fastbuf *f, byte *b)
314 bwrite(f, b, strlen(b));
318 bputs0(struct fastbuf *f, byte *b)
320 bwrite(f, b, strlen(b)+1);
324 bputsn(struct fastbuf *f, byte *b)
330 /* Direct I/O on buffers */
332 int bdirect_read(struct fastbuf *f, byte **buf);
333 int bdirect_write_prepare(struct fastbuf *f, byte **buf);
334 void bdirect_write_commit(struct fastbuf *f, byte *pos);
336 /* Depending on compile-time configuration, we select the right function for reading/writing of file offsets */
338 #ifdef SHERLOCK_CONFIG_LARGE_DB
339 #define bgeto(f) bget5(f)
340 #define bputo(f,l) bput5(f,l)
341 #define bgetp(f) bgetq(f)
342 #define bputp(f,l) bputq(f,l)
344 #define bgeto(f) bgetl(f)
345 #define bputo(f,l) bputl(f,l)
346 #define bgetp(f) bgetl(f)
347 #define bputp(f,l) bputl(f,l)