]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
Merge with git+ssh://cvs.ucw.cz/projects/sherlock/GIT/sherlock.git
[libucw.git] / lib / fastbuf.h
1 /*
2  *      UCW 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 _UCW_FASTBUF_H
12 #define _UCW_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  *    - In some cases, the user of the bdirect interface can be allowed to modify
55  *      the data in the buffer to avoid unnecessary copying. If the back-end
56  *      allows such modifications, it can set can_overwrite_buffer accordingly:
57  *              *  0 if no modification is allowed,
58  *              *  1 if the user can modify the buffer on the condition that
59  *                   the modifications will be undone before calling the next
60  *                   fastbuf operation
61  *              *  2 if the user is allowed to overwrite the data in the buffer
62  *                   if bdirect_read_commit_modified() is called afterwards.
63  *                   In this case, the back-end must be prepared for trimming
64  *                   of the buffer which is done by the commit function.
65  */
66
67 struct fastbuf {
68   byte is_fastbuf[0];                   /* Dummy field for checking of type casts */
69   byte *bptr, *bstop;                   /* Access pointers */
70   byte *buffer, *bufend;                /* Start and end of the buffer */
71   byte *name;                           /* File name for error messages */
72   sh_off_t pos;                         /* Position of bstop in the file */
73   int (*refill)(struct fastbuf *);      /* Get a buffer with new data */
74   void (*spout)(struct fastbuf *);      /* Write buffer data to the file */
75   void (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed */
76   void (*close)(struct fastbuf *);      /* Close the stream */
77   int (*config)(struct fastbuf *, uns, int);    /* Configure the stream */
78   int can_overwrite_buffer;             /* Can the buffer be altered? (see discussion above) 0=never, 1=temporarily, 2=permanently */
79 };
80
81 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
82
83 struct fastbuf *bopen(byte *name, uns mode, uns buflen);
84 struct fastbuf *bopen_try(byte *name, uns mode, uns buflen);
85 struct fastbuf *bopen_tmp(uns buflen);
86 struct fastbuf *bfdopen(int fd, uns buflen);
87 struct fastbuf *bfdopen_shared(int fd, uns buflen);
88 void bfilesync(struct fastbuf *b);
89
90 /* FastIO on in-memory streams */
91
92 struct fastbuf *fbmem_create(unsigned blocksize);       /* Create stream and return its writing fastbuf */
93 struct fastbuf *fbmem_clone_read(struct fastbuf *);     /* Create reading fastbuf */
94
95 /* FastIO on memory mapped files */
96
97 struct fastbuf *bopen_mm(byte *name, uns mode);
98
99 /* FastI on file descriptors with limit */
100
101 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
102
103 /* FastIO on static buffers */
104
105 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite);
106 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
107 static inline uns
108 fbbuf_count_written(struct fastbuf *f)
109 {
110   return f->bptr - f->bstop;
111 }
112
113 /* FastIO on recyclable growing buffers */
114
115 struct fastbuf *fbgrow_create(unsigned basic_size);
116 void fbgrow_reset(struct fastbuf *b);                   /* Reset stream and prepare for writing */
117 void fbgrow_rewind(struct fastbuf *b);                  /* Prepare for reading */
118
119 /* Configuring stream parameters */
120
121 int bconfig(struct fastbuf *f, uns type, int data);
122
123 #define BCONFIG_IS_TEMP_FILE 0
124
125 /* Universal functions working on all fastbuf's */
126
127 void bclose(struct fastbuf *f);
128 void bflush(struct fastbuf *f);
129 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
130 void bsetpos(struct fastbuf *f, sh_off_t pos);
131 void brewind(struct fastbuf *f);
132 sh_off_t bfilesize(struct fastbuf *f);
133
134 static inline sh_off_t btell(struct fastbuf *f)
135 {
136   return f->pos + (f->bptr - f->bstop);
137 }
138
139 int bgetc_slow(struct fastbuf *f);
140 static inline int bgetc(struct fastbuf *f)
141 {
142   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
143 }
144
145 int bpeekc_slow(struct fastbuf *f);
146 static inline int bpeekc(struct fastbuf *f)
147 {
148   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
149 }
150
151 static inline void bungetc(struct fastbuf *f)
152 {
153   f->bptr--;
154 }
155
156 void bputc_slow(struct fastbuf *f, uns c);
157 static inline void bputc(struct fastbuf *f, uns c)
158 {
159   if (f->bptr < f->bufend)
160     *f->bptr++ = c;
161   else
162     bputc_slow(f, c);
163 }
164
165 static inline uns
166 bavailr(struct fastbuf *f)
167 {
168   return f->bstop - f->bptr;
169 }
170
171 static inline uns
172 bavailw(struct fastbuf *f)
173 {
174   return f->bufend - f->bptr;
175 }
176
177 int bgetw_slow(struct fastbuf *f);
178 static inline int bgetw(struct fastbuf *f)
179 {
180   int w;
181   if (bavailr(f) >= 2)
182     {
183       w = GET_U16(f->bptr);
184       f->bptr += 2;
185       return w;
186     }
187   else
188     return bgetw_slow(f);
189 }
190
191 u32 bgetl_slow(struct fastbuf *f);
192 static inline u32 bgetl(struct fastbuf *f)
193 {
194   u32 l;
195   if (bavailr(f) >= 4)
196     {
197       l = GET_U32(f->bptr);
198       f->bptr += 4;
199       return l;
200     }
201   else
202     return bgetl_slow(f);
203 }
204
205 u64 bgetq_slow(struct fastbuf *f);
206 static inline u64 bgetq(struct fastbuf *f)
207 {
208   u64 l;
209   if (bavailr(f) >= 8)
210     {
211       l = GET_U64(f->bptr);
212       f->bptr += 8;
213       return l;
214     }
215   else
216     return bgetq_slow(f);
217 }
218
219 u64 bget5_slow(struct fastbuf *f);
220 static inline u64 bget5(struct fastbuf *f)
221 {
222   u64 l;
223   if (bavailr(f) >= 5)
224     {
225       l = GET_U40(f->bptr);
226       f->bptr += 5;
227       return l;
228     }
229   else
230     return bget5_slow(f);
231 }
232
233 void bputw_slow(struct fastbuf *f, uns w);
234 static inline void bputw(struct fastbuf *f, uns w)
235 {
236   if (bavailw(f) >= 2)
237     {
238       PUT_U16(f->bptr, w);
239       f->bptr += 2;
240     }
241   else
242     bputw_slow(f, w);
243 }
244
245 void bputl_slow(struct fastbuf *f, u32 l);
246 static inline void bputl(struct fastbuf *f, u32 l)
247 {
248   if (bavailw(f) >= 4)
249     {
250       PUT_U32(f->bptr, l);
251       f->bptr += 4;
252     }
253   else
254     bputl_slow(f, l);
255 }
256
257 void bputq_slow(struct fastbuf *f, u64 l);
258 static inline void bputq(struct fastbuf *f, u64 l)
259 {
260   if (bavailw(f) >= 8)
261     {
262       PUT_U64(f->bptr, l);
263       f->bptr += 8;
264     }
265   else
266     bputq_slow(f, l);
267 }
268
269 void bput5_slow(struct fastbuf *f, u64 l);
270 static inline void bput5(struct fastbuf *f, u64 l)
271 {
272   if (bavailw(f) >= 5)
273     {
274       PUT_U40(f->bptr, l);
275       f->bptr += 5;
276     }
277   else
278     bput5_slow(f, l);
279 }
280
281 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
282 static inline uns bread(struct fastbuf *f, void *b, uns l)
283 {
284   if (bavailr(f) >= l)
285     {
286       memcpy(b, f->bptr, l);
287       f->bptr += l;
288       return l;
289     }
290   else
291     return bread_slow(f, b, l, 0);
292 }
293
294 static inline uns breadb(struct fastbuf *f, void *b, uns l)
295 {
296   if (bavailr(f) >= l)
297     {
298       memcpy(b, f->bptr, l);
299       f->bptr += l;
300       return l;
301     }
302   else
303     return bread_slow(f, b, l, 1);
304 }
305
306 void bwrite_slow(struct fastbuf *f, void *b, uns l);
307 static inline void bwrite(struct fastbuf *f, void *b, uns l)
308 {
309   if (bavailw(f) >= l)
310     {
311       memcpy(f->bptr, b, l);
312       f->bptr += l;
313     }
314   else
315     bwrite_slow(f, b, l);
316 }
317
318 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
319 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
320 byte *bgets0(struct fastbuf *f, byte *b, uns l);
321
322 static inline void
323 bputs(struct fastbuf *f, byte *b)
324 {
325   bwrite(f, b, strlen(b));
326 }
327
328 static inline void
329 bputs0(struct fastbuf *f, byte *b)
330 {
331   bwrite(f, b, strlen(b)+1);
332 }
333
334 static inline void
335 bputsn(struct fastbuf *f, byte *b)
336 {
337   bputs(f, b);
338   bputc(f, '\n');
339 }
340
341 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
342 static inline void
343 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
344 {
345   if (bavailr(f) >= l && bavailw(t) >= l)
346     {
347       memcpy(t->bptr, f->bptr, l);
348       t->bptr += l;
349       f->bptr += l;
350     }
351   else
352     bbcopy_slow(f, t, l);
353 }
354
355 int bskip_slow(struct fastbuf *f, uns len);
356 static inline int bskip(struct fastbuf *f, uns len)
357 {
358   if (bavailr(f) >= len)
359     {
360       f->bptr += len;
361       return 1;
362     }
363   else
364     return bskip_slow(f, len);
365 }
366
367 /* I/O on addr_int_t */
368
369 #ifdef CPU_64BIT_POINTERS
370 #define bputa(x,p) bputq(x,p)
371 #define bgeta(x) bgetq(x)
372 #else
373 #define bputa(x,p) bputl(x,p)
374 #define bgeta(x) bgetl(x)
375 #endif
376
377 /* Direct I/O on buffers */
378
379 static inline uns
380 bdirect_read_prepare(struct fastbuf *f, byte **buf)
381 {
382   if (f->bptr == f->bstop && !f->refill(f))
383     {
384       *buf = NULL;  // This is not needed, but it helps to get rid of spurious warnings
385       return 0;
386     }
387   *buf = f->bptr;
388   return bavailr(f);
389 }
390
391 static inline void
392 bdirect_read_commit(struct fastbuf *f, byte *pos)
393 {
394   f->bptr = pos;
395 }
396
397 static inline void
398 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
399 {
400   f->bptr = pos;
401   f->buffer = pos;      /* Avoid seeking backwards in the buffer */
402 }
403
404 static inline uns
405 bdirect_write_prepare(struct fastbuf *f, byte **buf)
406 {
407   if (f->bptr == f->bufend)
408     f->spout(f);
409   *buf = f->bptr;
410   return bavailw(f);
411 }
412
413 static inline void
414 bdirect_write_commit(struct fastbuf *f, byte *pos)
415 {
416   f->bptr = pos;
417 }
418
419 /* Formatted output */
420
421 int bprintf(struct fastbuf *b, char *msg, ...) FORMAT_CHECK(printf,2,3);
422 int vbprintf(struct fastbuf *b, char *msg, va_list args);
423
424 #endif