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