]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
Fastbufs now work better on unseekable files.
[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 #include <alloca.h>
20
21 #include "lib/unaligned.h"
22
23 /*
24  *  Generic buffered I/O. You supply hooks to be called for low-level operations
25  *  (swapping of buffers, seeking and closing), we do the rest.
26  *
27  *  Buffer layout when reading:
28  *
29  *  +----------------+---------------------------+
30  *  | read data      | free space                |
31  *  +----------------+---------------------------+
32  *  ^        ^        ^                           ^
33  *  buffer   bptr     bstop                       bufend
34  *
35  *  After the last character is read, bptr == bstop and buffer refill
36  *  is deferred to the next read attempt. This gives us an easy way
37  *  how to implement bungetc().
38  *
39  *  When writing:
40  *
41  *  +--------+--------------+--------------------+
42  *  | unused | written data | free space         |
43  *  +--------+--------------+--------------------+
44  *  ^         ^              ^                    ^
45  *  buffer    bstop          bptr                 bufend
46  *
47  *  Dirty tricks:
48  *
49  *    - You can mix reads and writes on the same stream, but you must
50  *      call bflush() in between and remember that the file position
51  *      points after the flushed buffer which is not necessarily the same
52  *      as after the data you've read.
53  *    - The spout/refill hooks can change not only bptr and bstop, but also
54  *      the location of the buffer; fb-mem.c takes advantage of it.
55  *    - In some cases, the user of the bdirect interface can be allowed to modify
56  *      the data in the buffer to avoid unnecessary copying. If the back-end
57  *      allows such modifications, it can set can_overwrite_buffer accordingly:
58  *              *  0 if no modification is allowed,
59  *              *  1 if the user can modify the buffer on the condition that
60  *                   the modifications will be undone before calling the next
61  *                   fastbuf operation
62  *              *  2 if the user is allowed to overwrite the data in the buffer
63  *                   if bdirect_read_commit_modified() is called afterwards.
64  *                   In this case, the back-end must be prepared for trimming
65  *                   of the buffer which is done by the commit function.
66  */
67
68 struct fastbuf {
69   byte is_fastbuf[0];                   /* Dummy field for checking of type casts */
70   byte *bptr, *bstop;                   /* Access pointers */
71   byte *buffer, *bufend;                /* Start and end of the buffer */
72   byte *name;                           /* File name for error messages */
73   sh_off_t pos;                         /* Position of bstop in the file */
74   int (*refill)(struct fastbuf *);      /* Get a buffer with new data */
75   void (*spout)(struct fastbuf *);      /* Write buffer data to the file */
76   int (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed; returns success */
77   void (*close)(struct fastbuf *);      /* Close the stream */
78   int (*config)(struct fastbuf *, uns, int);    /* Configure the stream */
79   int can_overwrite_buffer;             /* Can the buffer be altered? (see discussion above) 0=never, 1=temporarily, 2=permanently */
80 };
81
82 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
83
84 struct fastbuf *bopen(byte *name, uns mode, uns buflen);
85 struct fastbuf *bopen_try(byte *name, uns mode, uns buflen);
86 struct fastbuf *bopen_tmp(uns buflen);
87 struct fastbuf *bfdopen(int fd, uns buflen);
88 struct fastbuf *bfdopen_shared(int fd, uns buflen);
89 void bfilesync(struct fastbuf *b);
90
91 #define TEMP_FILE_NAME_LEN 256
92 void temp_file_name(byte *name);
93
94 /* FastIO on in-memory streams */
95
96 struct fastbuf *fbmem_create(unsigned blocksize);       /* Create stream and return its writing fastbuf */
97 struct fastbuf *fbmem_clone_read(struct fastbuf *);     /* Create reading fastbuf */
98
99 /* FastIO on memory mapped files */
100
101 struct fastbuf *bopen_mm(byte *name, uns mode);
102
103 /* FastI on file descriptors with limit */
104
105 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
106
107 /* FastIO on static buffers */
108
109 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite);
110 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
111 static inline uns
112 fbbuf_count_written(struct fastbuf *f)
113 {
114   return f->bptr - f->bstop;
115 }
116
117 /* FastIO on recyclable growing buffers */
118
119 struct fastbuf *fbgrow_create(unsigned basic_size);
120 void fbgrow_reset(struct fastbuf *b);                   /* Reset stream and prepare for writing */
121 void fbgrow_rewind(struct fastbuf *b);                  /* Prepare for reading */
122
123 /* FastO with atomic writes for multi-threaded programs */
124
125 struct fb_atomic {
126   struct fastbuf fb;
127   struct fb_atomic_file *af;
128   byte *expected_max_bptr;
129   uns slack_size;
130 };
131 #define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
132
133 struct fastbuf *fbatomic_open(byte *name, struct fastbuf *master, uns bufsize, int record_len);
134 void fbatomic_internal_write(struct fastbuf *b);
135
136 static inline void
137 fbatomic_commit(struct fastbuf *b)
138 {
139   if (b->bptr >= ((struct fb_atomic *)b)->expected_max_bptr)
140     fbatomic_internal_write(b);
141 }
142
143 /* Configuring stream parameters */
144
145 int bconfig(struct fastbuf *f, uns type, int data);
146
147 #define BCONFIG_IS_TEMP_FILE 0
148
149 /* Universal functions working on all fastbuf's */
150
151 void bclose(struct fastbuf *f);
152 void bflush(struct fastbuf *f);
153 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
154 void bsetpos(struct fastbuf *f, sh_off_t pos);
155 void brewind(struct fastbuf *f);
156 sh_off_t bfilesize(struct fastbuf *f);          // -1 if not seekable
157
158 static inline sh_off_t btell(struct fastbuf *f)
159 {
160   return f->pos + (f->bptr - f->bstop);
161 }
162
163 int bgetc_slow(struct fastbuf *f);
164 static inline int bgetc(struct fastbuf *f)
165 {
166   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
167 }
168
169 int bpeekc_slow(struct fastbuf *f);
170 static inline int bpeekc(struct fastbuf *f)
171 {
172   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
173 }
174
175 static inline void bungetc(struct fastbuf *f)
176 {
177   f->bptr--;
178 }
179
180 void bputc_slow(struct fastbuf *f, uns c);
181 static inline void bputc(struct fastbuf *f, uns c)
182 {
183   if (f->bptr < f->bufend)
184     *f->bptr++ = c;
185   else
186     bputc_slow(f, c);
187 }
188
189 static inline uns
190 bavailr(struct fastbuf *f)
191 {
192   return f->bstop - f->bptr;
193 }
194
195 static inline uns
196 bavailw(struct fastbuf *f)
197 {
198   return f->bufend - f->bptr;
199 }
200
201 int bgetw_slow(struct fastbuf *f);
202 static inline int bgetw(struct fastbuf *f)
203 {
204   int w;
205   if (bavailr(f) >= 2)
206     {
207       w = GET_U16(f->bptr);
208       f->bptr += 2;
209       return w;
210     }
211   else
212     return bgetw_slow(f);
213 }
214
215 u32 bgetl_slow(struct fastbuf *f);
216 static inline u32 bgetl(struct fastbuf *f)
217 {
218   u32 l;
219   if (bavailr(f) >= 4)
220     {
221       l = GET_U32(f->bptr);
222       f->bptr += 4;
223       return l;
224     }
225   else
226     return bgetl_slow(f);
227 }
228
229 u64 bgetq_slow(struct fastbuf *f);
230 static inline u64 bgetq(struct fastbuf *f)
231 {
232   u64 l;
233   if (bavailr(f) >= 8)
234     {
235       l = GET_U64(f->bptr);
236       f->bptr += 8;
237       return l;
238     }
239   else
240     return bgetq_slow(f);
241 }
242
243 u64 bget5_slow(struct fastbuf *f);
244 static inline u64 bget5(struct fastbuf *f)
245 {
246   u64 l;
247   if (bavailr(f) >= 5)
248     {
249       l = GET_U40(f->bptr);
250       f->bptr += 5;
251       return l;
252     }
253   else
254     return bget5_slow(f);
255 }
256
257 void bputw_slow(struct fastbuf *f, uns w);
258 static inline void bputw(struct fastbuf *f, uns w)
259 {
260   if (bavailw(f) >= 2)
261     {
262       PUT_U16(f->bptr, w);
263       f->bptr += 2;
264     }
265   else
266     bputw_slow(f, w);
267 }
268
269 void bputl_slow(struct fastbuf *f, u32 l);
270 static inline void bputl(struct fastbuf *f, u32 l)
271 {
272   if (bavailw(f) >= 4)
273     {
274       PUT_U32(f->bptr, l);
275       f->bptr += 4;
276     }
277   else
278     bputl_slow(f, l);
279 }
280
281 void bputq_slow(struct fastbuf *f, u64 l);
282 static inline void bputq(struct fastbuf *f, u64 l)
283 {
284   if (bavailw(f) >= 8)
285     {
286       PUT_U64(f->bptr, l);
287       f->bptr += 8;
288     }
289   else
290     bputq_slow(f, l);
291 }
292
293 void bput5_slow(struct fastbuf *f, u64 l);
294 static inline void bput5(struct fastbuf *f, u64 l)
295 {
296   if (bavailw(f) >= 5)
297     {
298       PUT_U40(f->bptr, l);
299       f->bptr += 5;
300     }
301   else
302     bput5_slow(f, l);
303 }
304
305 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
306 static inline uns bread(struct fastbuf *f, void *b, uns l)
307 {
308   if (bavailr(f) >= l)
309     {
310       memcpy(b, f->bptr, l);
311       f->bptr += l;
312       return l;
313     }
314   else
315     return bread_slow(f, b, l, 0);
316 }
317
318 static inline uns breadb(struct fastbuf *f, void *b, uns l)
319 {
320   if (bavailr(f) >= l)
321     {
322       memcpy(b, f->bptr, l);
323       f->bptr += l;
324       return l;
325     }
326   else
327     return bread_slow(f, b, l, 1);
328 }
329
330 void bwrite_slow(struct fastbuf *f, void *b, uns l);
331 static inline void bwrite(struct fastbuf *f, void *b, uns l)
332 {
333   if (bavailw(f) >= l)
334     {
335       memcpy(f->bptr, b, l);
336       f->bptr += l;
337     }
338   else
339     bwrite_slow(f, b, l);
340 }
341
342 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
343 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
344 byte *bgets0(struct fastbuf *f, byte *b, uns l);
345
346 struct mempool;
347 struct bb_t;
348 uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit);
349 byte *bgets_mp(struct fastbuf *f, struct mempool *mp);
350
351 struct bgets_stk_struct {
352   struct fastbuf *f;
353   byte *old_buf, *cur_buf, *src;
354   uns old_len, cur_len, src_len;
355 };
356 void bgets_stk_init(struct bgets_stk_struct *s);
357 void bgets_stk_step(struct bgets_stk_struct *s);
358 #define bgets_stk(fb) ({ struct bgets_stk_struct _s; _s.f = (fb); for (bgets_stk_init(&_s); _s.cur_len; _s.cur_buf = alloca(_s.cur_len), bgets_stk_step(&_s)); _s.cur_buf; })
359
360 static inline void
361 bputs(struct fastbuf *f, byte *b)
362 {
363   bwrite(f, b, strlen(b));
364 }
365
366 static inline void
367 bputs0(struct fastbuf *f, byte *b)
368 {
369   bwrite(f, b, strlen(b)+1);
370 }
371
372 static inline void
373 bputsn(struct fastbuf *f, byte *b)
374 {
375   bputs(f, b);
376   bputc(f, '\n');
377 }
378
379 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
380 static inline void
381 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
382 {
383   if (bavailr(f) >= l && bavailw(t) >= l)
384     {
385       memcpy(t->bptr, f->bptr, l);
386       t->bptr += l;
387       f->bptr += l;
388     }
389   else
390     bbcopy_slow(f, t, l);
391 }
392
393 int bskip_slow(struct fastbuf *f, uns len);
394 static inline int bskip(struct fastbuf *f, uns len)
395 {
396   if (bavailr(f) >= len)
397     {
398       f->bptr += len;
399       return 1;
400     }
401   else
402     return bskip_slow(f, len);
403 }
404
405 /* I/O on uintptr_t */
406
407 #ifdef CPU_64BIT_POINTERS
408 #define bputa(x,p) bputq(x,p)
409 #define bgeta(x) bgetq(x)
410 #else
411 #define bputa(x,p) bputl(x,p)
412 #define bgeta(x) bgetl(x)
413 #endif
414
415 /* Direct I/O on buffers */
416
417 static inline uns
418 bdirect_read_prepare(struct fastbuf *f, byte **buf)
419 {
420   if (f->bptr == f->bstop && !f->refill(f))
421     {
422       *buf = NULL;  // This is not needed, but it helps to get rid of spurious warnings
423       return 0;
424     }
425   *buf = f->bptr;
426   return bavailr(f);
427 }
428
429 static inline void
430 bdirect_read_commit(struct fastbuf *f, byte *pos)
431 {
432   f->bptr = pos;
433 }
434
435 static inline void
436 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
437 {
438   f->bptr = pos;
439   f->buffer = pos;      /* Avoid seeking backwards in the buffer */
440 }
441
442 static inline uns
443 bdirect_write_prepare(struct fastbuf *f, byte **buf)
444 {
445   if (f->bptr == f->bufend)
446     f->spout(f);
447   *buf = f->bptr;
448   return bavailw(f);
449 }
450
451 static inline void
452 bdirect_write_commit(struct fastbuf *f, byte *pos)
453 {
454   f->bptr = pos;
455 }
456
457 /* Formatted output */
458
459 int bprintf(struct fastbuf *b, char *msg, ...) FORMAT_CHECK(printf,2,3);
460 int vbprintf(struct fastbuf *b, char *msg, va_list args);
461
462 #endif