]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
fb-direct: Implemented temporary 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   void (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed */
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 /* FastIO on files opened with O_DIRECT (see fb-direct.c for description) */
104
105 struct asio_queue;
106 struct fastbuf *fbdir_open(byte *name, uns mode, struct asio_queue *io_queue);
107 struct fastbuf *fbdir_open_try(byte *name, uns mode, struct asio_queue *io_queue);
108 struct fastbuf *fbdir_open_fd(int fd, struct asio_queue *io_queue);
109 struct fastbuf *fbdir_open_tmp(struct asio_queue *io_queue);
110
111 /* FastI on file descriptors with limit */
112
113 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
114
115 /* FastIO on static buffers */
116
117 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite);
118 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
119 static inline uns
120 fbbuf_count_written(struct fastbuf *f)
121 {
122   return f->bptr - f->bstop;
123 }
124
125 /* FastIO on recyclable growing buffers */
126
127 struct fastbuf *fbgrow_create(unsigned basic_size);
128 void fbgrow_reset(struct fastbuf *b);                   /* Reset stream and prepare for writing */
129 void fbgrow_rewind(struct fastbuf *b);                  /* Prepare for reading */
130
131 /* FastO with atomic writes for multi-threaded programs */
132
133 struct fb_atomic {
134   struct fastbuf fb;
135   struct fb_atomic_file *af;
136   byte *expected_max_bptr;
137   uns slack_size;
138 };
139 #define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
140
141 struct fastbuf *fbatomic_open(byte *name, struct fastbuf *master, uns bufsize, int record_len);
142 void fbatomic_internal_write(struct fastbuf *b);
143
144 static inline void
145 fbatomic_commit(struct fastbuf *b)
146 {
147   if (b->bptr >= ((struct fb_atomic *)b)->expected_max_bptr)
148     fbatomic_internal_write(b);
149 }
150
151 /* Configuring stream parameters */
152
153 int bconfig(struct fastbuf *f, uns type, int data);
154
155 #define BCONFIG_IS_TEMP_FILE 0
156
157 /* Universal functions working on all fastbuf's */
158
159 void bclose(struct fastbuf *f);
160 void bflush(struct fastbuf *f);
161 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
162 void bsetpos(struct fastbuf *f, sh_off_t pos);
163 void brewind(struct fastbuf *f);
164 sh_off_t bfilesize(struct fastbuf *f);
165
166 static inline sh_off_t btell(struct fastbuf *f)
167 {
168   return f->pos + (f->bptr - f->bstop);
169 }
170
171 int bgetc_slow(struct fastbuf *f);
172 static inline int bgetc(struct fastbuf *f)
173 {
174   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
175 }
176
177 int bpeekc_slow(struct fastbuf *f);
178 static inline int bpeekc(struct fastbuf *f)
179 {
180   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
181 }
182
183 static inline void bungetc(struct fastbuf *f)
184 {
185   f->bptr--;
186 }
187
188 void bputc_slow(struct fastbuf *f, uns c);
189 static inline void bputc(struct fastbuf *f, uns c)
190 {
191   if (f->bptr < f->bufend)
192     *f->bptr++ = c;
193   else
194     bputc_slow(f, c);
195 }
196
197 static inline uns
198 bavailr(struct fastbuf *f)
199 {
200   return f->bstop - f->bptr;
201 }
202
203 static inline uns
204 bavailw(struct fastbuf *f)
205 {
206   return f->bufend - f->bptr;
207 }
208
209 int bgetw_slow(struct fastbuf *f);
210 static inline int bgetw(struct fastbuf *f)
211 {
212   int w;
213   if (bavailr(f) >= 2)
214     {
215       w = GET_U16(f->bptr);
216       f->bptr += 2;
217       return w;
218     }
219   else
220     return bgetw_slow(f);
221 }
222
223 u32 bgetl_slow(struct fastbuf *f);
224 static inline u32 bgetl(struct fastbuf *f)
225 {
226   u32 l;
227   if (bavailr(f) >= 4)
228     {
229       l = GET_U32(f->bptr);
230       f->bptr += 4;
231       return l;
232     }
233   else
234     return bgetl_slow(f);
235 }
236
237 u64 bgetq_slow(struct fastbuf *f);
238 static inline u64 bgetq(struct fastbuf *f)
239 {
240   u64 l;
241   if (bavailr(f) >= 8)
242     {
243       l = GET_U64(f->bptr);
244       f->bptr += 8;
245       return l;
246     }
247   else
248     return bgetq_slow(f);
249 }
250
251 u64 bget5_slow(struct fastbuf *f);
252 static inline u64 bget5(struct fastbuf *f)
253 {
254   u64 l;
255   if (bavailr(f) >= 5)
256     {
257       l = GET_U40(f->bptr);
258       f->bptr += 5;
259       return l;
260     }
261   else
262     return bget5_slow(f);
263 }
264
265 void bputw_slow(struct fastbuf *f, uns w);
266 static inline void bputw(struct fastbuf *f, uns w)
267 {
268   if (bavailw(f) >= 2)
269     {
270       PUT_U16(f->bptr, w);
271       f->bptr += 2;
272     }
273   else
274     bputw_slow(f, w);
275 }
276
277 void bputl_slow(struct fastbuf *f, u32 l);
278 static inline void bputl(struct fastbuf *f, u32 l)
279 {
280   if (bavailw(f) >= 4)
281     {
282       PUT_U32(f->bptr, l);
283       f->bptr += 4;
284     }
285   else
286     bputl_slow(f, l);
287 }
288
289 void bputq_slow(struct fastbuf *f, u64 l);
290 static inline void bputq(struct fastbuf *f, u64 l)
291 {
292   if (bavailw(f) >= 8)
293     {
294       PUT_U64(f->bptr, l);
295       f->bptr += 8;
296     }
297   else
298     bputq_slow(f, l);
299 }
300
301 void bput5_slow(struct fastbuf *f, u64 l);
302 static inline void bput5(struct fastbuf *f, u64 l)
303 {
304   if (bavailw(f) >= 5)
305     {
306       PUT_U40(f->bptr, l);
307       f->bptr += 5;
308     }
309   else
310     bput5_slow(f, l);
311 }
312
313 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
314 static inline uns bread(struct fastbuf *f, void *b, uns l)
315 {
316   if (bavailr(f) >= l)
317     {
318       memcpy(b, f->bptr, l);
319       f->bptr += l;
320       return l;
321     }
322   else
323     return bread_slow(f, b, l, 0);
324 }
325
326 static inline uns breadb(struct fastbuf *f, void *b, uns l)
327 {
328   if (bavailr(f) >= l)
329     {
330       memcpy(b, f->bptr, l);
331       f->bptr += l;
332       return l;
333     }
334   else
335     return bread_slow(f, b, l, 1);
336 }
337
338 void bwrite_slow(struct fastbuf *f, void *b, uns l);
339 static inline void bwrite(struct fastbuf *f, void *b, uns l)
340 {
341   if (bavailw(f) >= l)
342     {
343       memcpy(f->bptr, b, l);
344       f->bptr += l;
345     }
346   else
347     bwrite_slow(f, b, l);
348 }
349
350 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
351 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
352 byte *bgets0(struct fastbuf *f, byte *b, uns l);
353
354 struct mempool;
355 struct bb_t;
356 uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit);
357 byte *bgets_mp(struct fastbuf *f, struct mempool *mp);
358
359 struct bgets_stk_struct {
360   struct fastbuf *f;
361   byte *old_buf, *cur_buf, *src;
362   uns old_len, cur_len, src_len;
363 };
364 void bgets_stk_init(struct bgets_stk_struct *s);
365 void bgets_stk_step(struct bgets_stk_struct *s);
366 #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; })
367
368 static inline void
369 bputs(struct fastbuf *f, byte *b)
370 {
371   bwrite(f, b, strlen(b));
372 }
373
374 static inline void
375 bputs0(struct fastbuf *f, byte *b)
376 {
377   bwrite(f, b, strlen(b)+1);
378 }
379
380 static inline void
381 bputsn(struct fastbuf *f, byte *b)
382 {
383   bputs(f, b);
384   bputc(f, '\n');
385 }
386
387 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
388 static inline void
389 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
390 {
391   if (bavailr(f) >= l && bavailw(t) >= l)
392     {
393       memcpy(t->bptr, f->bptr, l);
394       t->bptr += l;
395       f->bptr += l;
396     }
397   else
398     bbcopy_slow(f, t, l);
399 }
400
401 int bskip_slow(struct fastbuf *f, uns len);
402 static inline int bskip(struct fastbuf *f, uns len)
403 {
404   if (bavailr(f) >= len)
405     {
406       f->bptr += len;
407       return 1;
408     }
409   else
410     return bskip_slow(f, len);
411 }
412
413 /* I/O on addr_int_t */
414
415 #ifdef CPU_64BIT_POINTERS
416 #define bputa(x,p) bputq(x,p)
417 #define bgeta(x) bgetq(x)
418 #else
419 #define bputa(x,p) bputl(x,p)
420 #define bgeta(x) bgetl(x)
421 #endif
422
423 /* Direct I/O on buffers */
424
425 static inline uns
426 bdirect_read_prepare(struct fastbuf *f, byte **buf)
427 {
428   if (f->bptr == f->bstop && !f->refill(f))
429     {
430       *buf = NULL;  // This is not needed, but it helps to get rid of spurious warnings
431       return 0;
432     }
433   *buf = f->bptr;
434   return bavailr(f);
435 }
436
437 static inline void
438 bdirect_read_commit(struct fastbuf *f, byte *pos)
439 {
440   f->bptr = pos;
441 }
442
443 static inline void
444 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
445 {
446   f->bptr = pos;
447   f->buffer = pos;      /* Avoid seeking backwards in the buffer */
448 }
449
450 static inline uns
451 bdirect_write_prepare(struct fastbuf *f, byte **buf)
452 {
453   if (f->bptr == f->bufend)
454     f->spout(f);
455   *buf = f->bptr;
456   return bavailw(f);
457 }
458
459 static inline void
460 bdirect_write_commit(struct fastbuf *f, byte *pos)
461 {
462   f->bptr = pos;
463 }
464
465 /* Formatted output */
466
467 int bprintf(struct fastbuf *b, char *msg, ...) FORMAT_CHECK(printf,2,3);
468 int vbprintf(struct fastbuf *b, char *msg, va_list args);
469
470 #endif