]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
Added fastbuf backend for reading from file descriptors with a given limit.
[libucw.git] / lib / fastbuf.h
1 /*
2  *      Sherlock Library -- Fast Buffered I/O
3  *
4  *      (c) 1997--2002 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _SHERLOCK_FASTBUF_H
11 #define _SHERLOCK_FASTBUF_H
12
13 #ifndef EOF
14 #include <stdio.h>
15 #endif
16
17 #include <string.h>
18 #include <stdarg.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 /* Configuring stream parameters */
90
91 int bconfig(struct fastbuf *f, uns type, int data);
92
93 #define BCONFIG_IS_TEMP_FILE 0
94
95 /* Universal functions working on all fastbuf's */
96
97 void bclose(struct fastbuf *f);
98 void bflush(struct fastbuf *f);
99 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
100 void bsetpos(struct fastbuf *f, sh_off_t pos);
101
102 static inline sh_off_t btell(struct fastbuf *f)
103 {
104   return f->pos + (f->bptr - f->bstop);
105 }
106
107 int bgetc_slow(struct fastbuf *f);
108 static inline int bgetc(struct fastbuf *f)
109 {
110   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
111 }
112
113 int bpeekc_slow(struct fastbuf *f);
114 static inline int bpeekc(struct fastbuf *f)
115 {
116   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
117 }
118
119 static inline void bungetc(struct fastbuf *f)
120 {
121   f->bptr--;
122 }
123
124 void bputc_slow(struct fastbuf *f, uns c);
125 static inline void bputc(struct fastbuf *f, uns c)
126 {
127   if (f->bptr < f->bufend)
128     *f->bptr++ = c;
129   else
130     bputc_slow(f, c);
131 }
132
133 int bgetw_slow(struct fastbuf *f);
134 static inline int bgetw(struct fastbuf *f)
135 {
136   int w;
137   if (f->bptr + 2 <= f->bstop)
138     {
139       w = GET_U16(f->bptr);
140       f->bptr += 2;
141       return w;
142     }
143   else
144     return bgetw_slow(f);
145 }
146
147 u32 bgetl_slow(struct fastbuf *f);
148 static inline u32 bgetl(struct fastbuf *f)
149 {
150   u32 l;
151   if (f->bptr + 4 <= f->bstop)
152     {
153       l = GET_U32(f->bptr);
154       f->bptr += 4;
155       return l;
156     }
157   else
158     return bgetl_slow(f);
159 }
160
161 u64 bgetq_slow(struct fastbuf *f);
162 static inline u64 bgetq(struct fastbuf *f)
163 {
164   u64 l;
165   if (f->bptr + 8 <= f->bstop)
166     {
167       l = GET_U64(f->bptr);
168       f->bptr += 8;
169       return l;
170     }
171   else
172     return bgetq_slow(f);
173 }
174
175 u64 bget5_slow(struct fastbuf *f);
176 static inline u64 bget5(struct fastbuf *f)
177 {
178   u64 l;
179   if (f->bptr + 5 <= f->bstop)
180     {
181       l = GET_U40(f->bptr);
182       f->bptr += 5;
183       return l;
184     }
185   else
186     return bget5_slow(f);
187 }
188
189 void bputw_slow(struct fastbuf *f, uns w);
190 static inline void bputw(struct fastbuf *f, uns w)
191 {
192   if (f->bptr + 2 <= f->bufend)
193     {
194       PUT_U16(f->bptr, w);
195       f->bptr += 2;
196     }
197   else
198     bputw_slow(f, w);
199 }
200
201 void bputl_slow(struct fastbuf *f, u32 l);
202 static inline void bputl(struct fastbuf *f, u32 l)
203 {
204   if (f->bptr + 4 <= f->bufend)
205     {
206       PUT_U32(f->bptr, l);
207       f->bptr += 4;
208     }
209   else
210     bputl_slow(f, l);
211 }
212
213 void bputq_slow(struct fastbuf *f, u64 l);
214 static inline void bputq(struct fastbuf *f, u64 l)
215 {
216   if (f->bptr + 8 <= f->bufend)
217     {
218       PUT_U64(f->bptr, l);
219       f->bptr += 8;
220     }
221   else
222     bputq_slow(f, l);
223 }
224
225 void bput5_slow(struct fastbuf *f, u64 l);
226 static inline void bput5(struct fastbuf *f, u64 l)
227 {
228   if (f->bptr + 5 <= f->bufend)
229     {
230       PUT_U40(f->bptr, l);
231       f->bptr += 5;
232     }
233   else
234     bput5_slow(f, l);
235 }
236
237 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
238 static inline uns bread(struct fastbuf *f, void *b, uns l)
239 {
240   if (f->bptr + l <= f->bstop)
241     {
242       memcpy(b, f->bptr, l);
243       f->bptr += l;
244       return l;
245     }
246   else
247     return bread_slow(f, b, l, 0);
248 }
249
250 static inline uns breadb(struct fastbuf *f, void *b, uns l)
251 {
252   if (f->bptr + l <= f->bstop)
253     {
254       memcpy(b, f->bptr, l);
255       f->bptr += l;
256       return l;
257     }
258   else
259     return bread_slow(f, b, l, 1);
260 }
261
262 void bwrite_slow(struct fastbuf *f, void *b, uns l);
263 static inline void bwrite(struct fastbuf *f, void *b, uns l)
264 {
265   if (f->bptr + l <= f->bufend)
266     {
267       memcpy(f->bptr, b, l);
268       f->bptr += l;
269     }
270   else
271     bwrite_slow(f, b, l);
272 }
273
274 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
275 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
276 byte *bgets0(struct fastbuf *f, byte *b, uns l);
277
278 static inline void
279 bputs(struct fastbuf *f, byte *b)
280 {
281   bwrite(f, b, strlen(b));
282 }
283
284 static inline void
285 bputs0(struct fastbuf *f, byte *b)
286 {
287   bwrite(f, b, strlen(b)+1);
288 }
289
290 static inline void
291 bputsn(struct fastbuf *f, byte *b)
292 {
293   bputs(f, b);
294   bputc(f, '\n');
295 }
296
297 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
298 static inline void
299 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
300 {
301   if (f->bptr + l <= f->bstop &&
302       t->bptr + l <= t->bufend)
303     {
304       memcpy(t->bptr, f->bptr, l);
305       t->bptr += l;
306       f->bptr += l;
307     }
308   else
309     bbcopy_slow(f, t, l);
310 }
311
312 /* I/O on addr_int_t */
313
314 #ifdef CPU_64BIT_POINTERS
315 #define bputa(x,p) bputq(x,p)
316 #define bgeta(x) bgetq(x)
317 #else
318 #define bputa(x,p) bputl(x,p)
319 #define bgeta(x) bgetl(x)
320 #endif
321
322 /* Direct I/O on buffers */
323
324 static inline uns
325 bdirect_read_prepare(struct fastbuf *f, byte **buf)
326 {
327   if (f->bptr == f->bstop && !f->refill(f))
328     return 0;
329   *buf = f->bptr;
330   return f->bstop - f->bptr;
331 }
332
333 static inline void
334 bdirect_read_commit(struct fastbuf *f, byte *pos)
335 {
336   f->bptr = pos;
337 }
338
339 static inline uns
340 bdirect_write_prepare(struct fastbuf *f, byte **buf)
341 {
342   if (f->bptr == f->bufend)
343     f->spout(f);
344   *buf = f->bptr;
345   return f->bufend - f->bptr;
346 }
347
348 static inline void
349 bdirect_write_commit(struct fastbuf *f, byte *pos)
350 {
351   f->bptr = pos;
352 }
353
354 /* Formatted output */
355
356 int bprintf(struct fastbuf *b, byte *msg, ...);
357 int vbprintf(struct fastbuf *b, byte *msg, va_list args);
358
359 #endif