]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
Added bskip() which skips the given number of bytes by reading.
[libucw.git] / lib / fastbuf.h
1 /*
2  *      Sherlock Library -- Fast Buffered I/O
3  *
4  *      (c) 1997--2004 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _SHERLOCK_FASTBUF_H
12 #define _SHERLOCK_FASTBUF_H
13
14 #ifndef EOF
15 #include <stdio.h>
16 #endif
17
18 #include <string.h>
19
20 #include "lib/unaligned.h"
21
22 /*
23  *  Generic buffered I/O. You supply hooks to be called for low-level operations
24  *  (swapping of buffers, seeking and closing), we do the rest.
25  *
26  *  Buffer layout when reading:
27  *
28  *  +----------------+---------------------------+
29  *  | read data      | free space                |
30  *  +----------------+---------------------------+
31  *  ^        ^        ^                           ^
32  *  buffer   bptr     bstop                       bufend
33  *
34  *  After the last character is read, bptr == bstop and buffer refill
35  *  is deferred to the next read attempt. This gives us an easy way
36  *  how to implement bungetc().
37  *
38  *  When writing:
39  *
40  *  +--------+--------------+--------------------+
41  *  | unused | written data | free space         |
42  *  +--------+--------------+--------------------+
43  *  ^         ^              ^                    ^
44  *  buffer    bstop          bptr                 bufend
45  *
46  *  Dirty tricks:
47  *
48  *    - You can mix reads and writes on the same stream, but you must
49  *      call bflush() in between and remember that the file position
50  *      points after the flushed buffer which is not necessarily the same
51  *      as after the data you've read.
52  *    - The spout/refill hooks can change not only bptr and bstop, but also
53  *      the location of the buffer; fb-mem.c takes advantage of it.
54  */
55
56 struct fastbuf {
57   byte is_fastbuf[0];                   /* Dummy field for checking of type casts */
58   byte *bptr, *bstop;                   /* Access pointers */
59   byte *buffer, *bufend;                /* Start and end of the buffer */
60   byte *name;                           /* File name for error messages */
61   sh_off_t pos;                         /* Position of bstop in the file */
62   int (*refill)(struct fastbuf *);      /* Get a buffer with new data */
63   void (*spout)(struct fastbuf *);      /* Write buffer data to the file */
64   void (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed */
65   void (*close)(struct fastbuf *);      /* Close the stream */
66   int (*config)(struct fastbuf *, uns, int);    /* Configure the stream */
67 };
68
69 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
70
71 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
72 struct fastbuf *bopen_tmp(uns buffer);
73 struct fastbuf *bfdopen(int fd, uns buffer);
74 struct fastbuf *bfdopen_shared(int fd, uns buffer);
75
76 /* FastIO on in-memory streams */
77
78 struct fastbuf *fbmem_create(unsigned blocksize);       /* Create stream and return its writing fastbuf */
79 struct fastbuf *fbmem_clone_read(struct fastbuf *);     /* Create reading fastbuf */
80
81 /* FastIO on memory mapped files */
82
83 struct fastbuf *bopen_mm(byte *name, uns mode);
84
85 /* FastI on file descriptors with limit */
86
87 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
88
89 /* FastIO on static buffers */
90
91 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size);
92 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
93 static inline uns
94 fbbuf_count_written(struct fastbuf *f)
95 {
96   return f->bptr - f->bstop;
97 }
98
99 /* Configuring stream parameters */
100
101 int bconfig(struct fastbuf *f, uns type, int data);
102
103 #define BCONFIG_IS_TEMP_FILE 0
104 #define BCONFIG_CAN_OVERWRITE 1
105   /* Specifies whether the caller is allowed to perform the following optimized
106    * 0-copy write operation:
107    *    - get the buffer by bdirect_read_prepare()
108    *    - modify the buffer, e.g. by putting \0's inside
109    *    - call bflush() to let the fastbuf know
110    * Values:
111    * 0: read-only memory
112    * 1: you can write into read-write memory, if you restore the original value
113    * 2: you can rewrite the original content */
114
115 /* Universal functions working on all fastbuf's */
116
117 void bclose(struct fastbuf *f);
118 void bflush(struct fastbuf *f);
119 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
120 void bsetpos(struct fastbuf *f, sh_off_t pos);
121 void brewind(struct fastbuf *f);
122 void bskip(struct fastbuf *f, uns len);
123
124 static inline sh_off_t btell(struct fastbuf *f)
125 {
126   return f->pos + (f->bptr - f->bstop);
127 }
128
129 int bgetc_slow(struct fastbuf *f);
130 static inline int bgetc(struct fastbuf *f)
131 {
132   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
133 }
134
135 int bpeekc_slow(struct fastbuf *f);
136 static inline int bpeekc(struct fastbuf *f)
137 {
138   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
139 }
140
141 static inline void bungetc(struct fastbuf *f)
142 {
143   f->bptr--;
144 }
145
146 void bputc_slow(struct fastbuf *f, uns c);
147 static inline void bputc(struct fastbuf *f, uns c)
148 {
149   if (f->bptr < f->bufend)
150     *f->bptr++ = c;
151   else
152     bputc_slow(f, c);
153 }
154
155 int bgetw_slow(struct fastbuf *f);
156 static inline int bgetw(struct fastbuf *f)
157 {
158   int w;
159   if (f->bptr + 2 <= f->bstop)
160     {
161       w = GET_U16(f->bptr);
162       f->bptr += 2;
163       return w;
164     }
165   else
166     return bgetw_slow(f);
167 }
168
169 u32 bgetl_slow(struct fastbuf *f);
170 static inline u32 bgetl(struct fastbuf *f)
171 {
172   u32 l;
173   if (f->bptr + 4 <= f->bstop)
174     {
175       l = GET_U32(f->bptr);
176       f->bptr += 4;
177       return l;
178     }
179   else
180     return bgetl_slow(f);
181 }
182
183 u64 bgetq_slow(struct fastbuf *f);
184 static inline u64 bgetq(struct fastbuf *f)
185 {
186   u64 l;
187   if (f->bptr + 8 <= f->bstop)
188     {
189       l = GET_U64(f->bptr);
190       f->bptr += 8;
191       return l;
192     }
193   else
194     return bgetq_slow(f);
195 }
196
197 u64 bget5_slow(struct fastbuf *f);
198 static inline u64 bget5(struct fastbuf *f)
199 {
200   u64 l;
201   if (f->bptr + 5 <= f->bstop)
202     {
203       l = GET_U40(f->bptr);
204       f->bptr += 5;
205       return l;
206     }
207   else
208     return bget5_slow(f);
209 }
210
211 void bputw_slow(struct fastbuf *f, uns w);
212 static inline void bputw(struct fastbuf *f, uns w)
213 {
214   if (f->bptr + 2 <= f->bufend)
215     {
216       PUT_U16(f->bptr, w);
217       f->bptr += 2;
218     }
219   else
220     bputw_slow(f, w);
221 }
222
223 void bputl_slow(struct fastbuf *f, u32 l);
224 static inline void bputl(struct fastbuf *f, u32 l)
225 {
226   if (f->bptr + 4 <= f->bufend)
227     {
228       PUT_U32(f->bptr, l);
229       f->bptr += 4;
230     }
231   else
232     bputl_slow(f, l);
233 }
234
235 void bputq_slow(struct fastbuf *f, u64 l);
236 static inline void bputq(struct fastbuf *f, u64 l)
237 {
238   if (f->bptr + 8 <= f->bufend)
239     {
240       PUT_U64(f->bptr, l);
241       f->bptr += 8;
242     }
243   else
244     bputq_slow(f, l);
245 }
246
247 void bput5_slow(struct fastbuf *f, u64 l);
248 static inline void bput5(struct fastbuf *f, u64 l)
249 {
250   if (f->bptr + 5 <= f->bufend)
251     {
252       PUT_U40(f->bptr, l);
253       f->bptr += 5;
254     }
255   else
256     bput5_slow(f, l);
257 }
258
259 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
260 static inline uns bread(struct fastbuf *f, void *b, uns l)
261 {
262   if (f->bptr + l <= f->bstop)
263     {
264       memcpy(b, f->bptr, l);
265       f->bptr += l;
266       return l;
267     }
268   else
269     return bread_slow(f, b, l, 0);
270 }
271
272 static inline uns breadb(struct fastbuf *f, void *b, uns l)
273 {
274   if (f->bptr + l <= f->bstop)
275     {
276       memcpy(b, f->bptr, l);
277       f->bptr += l;
278       return l;
279     }
280   else
281     return bread_slow(f, b, l, 1);
282 }
283
284 void bwrite_slow(struct fastbuf *f, void *b, uns l);
285 static inline void bwrite(struct fastbuf *f, void *b, uns l)
286 {
287   if (f->bptr + l <= f->bufend)
288     {
289       memcpy(f->bptr, b, l);
290       f->bptr += l;
291     }
292   else
293     bwrite_slow(f, b, l);
294 }
295
296 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
297 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
298 byte *bgets0(struct fastbuf *f, byte *b, uns l);
299
300 static inline void
301 bputs(struct fastbuf *f, byte *b)
302 {
303   bwrite(f, b, strlen(b));
304 }
305
306 static inline void
307 bputs0(struct fastbuf *f, byte *b)
308 {
309   bwrite(f, b, strlen(b)+1);
310 }
311
312 static inline void
313 bputsn(struct fastbuf *f, byte *b)
314 {
315   bputs(f, b);
316   bputc(f, '\n');
317 }
318
319 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
320 static inline void
321 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
322 {
323   if (f->bptr + l <= f->bstop &&
324       t->bptr + l <= t->bufend)
325     {
326       memcpy(t->bptr, f->bptr, l);
327       t->bptr += l;
328       f->bptr += l;
329     }
330   else
331     bbcopy_slow(f, t, l);
332 }
333
334 /* I/O on addr_int_t */
335
336 #ifdef CPU_64BIT_POINTERS
337 #define bputa(x,p) bputq(x,p)
338 #define bgeta(x) bgetq(x)
339 #else
340 #define bputa(x,p) bputl(x,p)
341 #define bgeta(x) bgetl(x)
342 #endif
343
344 /* Direct I/O on buffers */
345
346 static inline uns
347 bdirect_read_prepare(struct fastbuf *f, byte **buf)
348 {
349   if (f->bptr == f->bstop && !f->refill(f))
350     return 0;
351   *buf = f->bptr;
352   return f->bstop - f->bptr;
353 }
354
355 static inline void
356 bdirect_read_commit(struct fastbuf *f, byte *pos)
357 {
358   f->bptr = pos;
359 }
360
361 static inline uns
362 bdirect_write_prepare(struct fastbuf *f, byte **buf)
363 {
364   if (f->bptr == f->bufend)
365     f->spout(f);
366   *buf = f->bptr;
367   return f->bufend - f->bptr;
368 }
369
370 static inline void
371 bdirect_write_commit(struct fastbuf *f, byte *pos)
372 {
373   f->bptr = pos;
374 }
375
376 /* Formatted output */
377
378 int bprintf(struct fastbuf *b, byte *msg, ...);
379 int vbprintf(struct fastbuf *b, byte *msg, va_list args);
380
381 #endif