]> mj.ucw.cz Git - libucw.git/blob - ucw/fastbuf.h
c4f7cd90cada9efa1bf15e14ad9ac0b311b8b8c0
[libucw.git] / ucw / fastbuf.h
1 /*
2  *      UCW Library -- Fast Buffered I/O
3  *
4  *      (c) 1997--2008 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 #include <string.h>
15 #include <alloca.h>
16
17 /***
18  * === Internal structure [[internal]]
19  *
20  * Generally speaking, a fastbuf consists of a buffer and a set of callbacks.
21  * All front-end functions operate on the buffer and if the buffer becomes
22  * empty or fills up, they ask the corresponding callback to solve the
23  * situation. Back-ends then differ just in the definition of the callbacks.
24  * The state of the fastbuf is represented by `struct fastbuf`, which
25  * is a simple structure describing the state of the buffer and pointers
26  * to the callback functions.
27  *
28  * When we are reading from the fastbuf, the buffer is laid out as follows:
29  *
30  *  +----------------+---------------------------+
31  *  | read data      | free space                |
32  *  +----------------+---------------------------+
33  *  ^        ^        ^                           ^
34  *  buffer   bptr     bstop                       bufend
35  *
36  * Here `bptr` points to the next character to be read. After the last character is
37  * read, `bptr == bstop` and the `refill` callback gets called upon the next read
38  * attempt to bring further data. This gives us an easy way how to implement bungetc().
39  *
40  * When writing, the situation looks like:
41  *
42  *  +--------+--------------+--------------------+
43  *  | unused | written data | free space         |
44  *  +--------+--------------+--------------------+
45  *  ^         ^              ^                    ^
46  *  buffer    bstop          bptr                 bufend
47  *
48  * In this case, the `bptr` points at the position where the next character
49  * will be written to. When we want to write, but `bptr == bufend`, we call
50  * the `spout` hook to flush the data and get an empty buffer.
51  *
52  * Several dirty tricks can be played:
53  *
54  *    - The `spout`/`refill` hooks can change not only `bptr` and `bstop`, but also
55  *      the location and size of the buffer; the fb-mem back-end 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 `fastbuf->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
70 /**
71  * This structure contains the state of the fastbuf. See the discussion above
72  * for how it works.
73  **/
74 struct fastbuf {
75   byte is_fastbuf[0];                           /* Dummy field for checking of type casts */
76   byte *bptr, *bstop;                           /* State of the buffer */
77   byte *buffer, *bufend;                        /* Start and end of the buffer */
78   char *name;                                   /* File name (used for error messages) */
79   ucw_off_t pos;                                /* Position of bstop in the file */
80   int (*refill)(struct fastbuf *);              /* Get a buffer with new data, returns 0 on EOF */
81   void (*spout)(struct fastbuf *);              /* Write buffer data to the file */
82   int (*seek)(struct fastbuf *, ucw_off_t, int);/* Slow path for bseek(), buffer already flushed; returns success */
83   void (*close)(struct fastbuf *);              /* Close the stream */
84   int (*config)(struct fastbuf *, uns, int);    /* Configure the stream */
85   int can_overwrite_buffer;                     /* Can the buffer be altered? 0=never, 1=temporarily, 2=permanently */
86 };
87
88 /***
89  * === Fastbuf on files [[fbparam]]
90  *
91  * If you want to use fastbufs to access files, you can choose one of several
92  * back-ends and set their parameters.
93  ***/
94
95 /**
96  * Back-end types
97  */
98 enum fb_type {
99   FB_STD,                               /* Standard buffered I/O */
100   FB_DIRECT,                            /* Direct I/O bypassing system caches (see fb-direct.c for a description) */
101   FB_MMAP                               /* Memory mapped files */
102 };
103
104 /**
105  * When you open a file fastbuf, you can use this structure to select a back-end
106  * and set its parameters. If you want just an "ordinary" file stream, you can
107  * happily pass NULL instead and the defaults from the configuration file (or
108  * hard-wired defaults if no config file has been read) will be used.
109  */
110 struct fb_params {
111   enum fb_type type;                    /* The chosen back-end */
112   uns buffer_size;                      /* 0 for default size */
113   uns keep_back_buf;                    /* FB_STD: optimize for bi-directional access */
114   uns read_ahead;                       /* FB_DIRECT options */
115   uns write_back;
116   struct asio_queue *asio;
117 };
118
119 struct cf_section;
120 extern struct cf_section fbpar_cf;      /** Configuration section with which you can fill the `fb_params` **/
121 extern struct fb_params fbpar_def;      /** The default `fb_params` **/
122
123 /**
124  * Opens a file with file mode @mode (see the man page of open()).
125  * Use @params to select the fastbuf back-end and its parameters or
126  * pass NULL if you are fine with defaults.
127  *
128  * Dies if the file does not exist.
129  **/
130 struct fastbuf *bopen_file(const char *name, int mode, struct fb_params *params);
131 struct fastbuf *bopen_file_try(const char *name, int mode, struct fb_params *params); /** Like bopen_file(), but returns NULL on failure. **/
132
133 /**
134  * Opens a temporary file.
135  * It is placed with other temp files and it is deleted when closed.
136  * Again, use NULL for @params if you want the defaults.
137  **/
138 struct fastbuf *bopen_tmp_file(struct fb_params *params);
139
140 /**
141  * Creates a fastbuf from a file descriptor @fd and sets its filename
142  * to @name (the name is used only in error messages).
143  * When the fastbuf is closed, the fd is closed as well. You can override
144  * this behavior by calling bconfig().
145  */
146 struct fastbuf *bopen_fd_name(int fd, struct fb_params *params, const char *name);
147 static inline struct fastbuf *bopen_fd(int fd, struct fb_params *params) /** Same as above, but with an auto-generated filename. **/
148 {
149   return bopen_fd_name(fd, params, NULL);
150 }
151
152 /**
153  * Flushes all buffers and makes sure that they are written to the disk.
154  **/
155 void bfilesync(struct fastbuf *b);
156
157 /***
158  * === Fastbufs on regular files [[fbfile]]
159  *
160  * If you want to use the `FB_STD` back-end and not worry about setting
161  * up any parameters, there is a couple of shortcuts.
162  ***/
163
164 struct fastbuf *bopen(const char *name, uns mode, uns buflen);          /** Equivalent to bopen_file() with `FB_STD` back-end. **/
165 struct fastbuf *bopen_try(const char *name, uns mode, uns buflen);      /** Equivalent to bopen_file_try() with `FB_STD` back-end. **/
166 struct fastbuf *bopen_tmp(uns buflen);                                  /** Equivalent to bopen_tmp_file() with `FB_STD` back-end. **/
167 struct fastbuf *bfdopen(int fd, uns buflen);                            /** Equivalent to bopen_fd() with `FB_STD` back-end. **/
168 struct fastbuf *bfdopen_shared(int fd, uns buflen);                     /** Like bfdopen(), but it does not close the @fd on bclose(). **/
169
170 /***
171  * === Temporary files [[fbtemp]]
172  *
173  * Usually, bopen_tmp_file() is the best way how to come to a temporary file.
174  * However, in some specific cases you can need more, so there is also a set
175  * of more general functions.
176  ***/
177
178 #define TEMP_FILE_NAME_LEN 256
179
180 /**
181  * Generates a temporary filename and stores it to the @name_buf (of size
182  * at least * `TEMP_FILE_NAME_LEN`). If @open_flags are not NULL, flags that
183  * should be OR-ed with other flags to open() will be stored there.
184  *
185  * The location and style of temporary files is controlled by the configuration.
186  * By default, the system temp directory (`$TMPDIR` or `/tmp`) is used.
187  *
188  * If the location is a publicly writeable directory (like `/tmp`), the
189  * generated filename cannot be guaranteed to be unique, so @open_flags
190  * will include `O_EXCL` and you have to check the result of open() and
191  * iterate if needed.
192  *
193  * This function is not specific to fastbufs, it can be used separately.
194  **/
195 void temp_file_name(char *name_buf, int *open_flags);
196
197 /**
198  * Opens a temporary file and returns its file descriptor.
199  * You specify the file @mode and @open_flags passed to open().
200  *
201  * If the @name_buf (of at last `TEMP_FILE_NAME_LEN` chars) is not NULL,
202  * the filename is also stored in it.
203  *
204  * This function is not specific to fastbufs, it can be used separately.
205  */
206 int open_tmp(char *name_buf, int open_flags, int mode);
207
208 /**
209  * Sometimes, a file is created as temporary and then moved to a stable
210  * location. This function takes a fastbuf created by bopen_tmp_file()
211  * or bopen_tmp(), marks it as permanent, closes it and renames it to
212  * @name.
213  *
214  * Please note that it assumes that the temporary file and the @name
215  * are on the same volume (otherwise, rename() fails), so you might
216  * want to configure a special location for the temporary files
217  * beforehand.
218  */
219 void bfix_tmp_file(struct fastbuf *fb, const char *name);
220
221 /* Internal functions of some file back-ends */
222
223 struct fastbuf *bfdopen_internal(int fd, const char *name, uns buflen);
224 struct fastbuf *bfmmopen_internal(int fd, const char *name, uns mode);
225
226 extern uns fbdir_cheat;
227 struct asio_queue;
228 struct fastbuf *fbdir_open_fd_internal(int fd, const char *name, struct asio_queue *io_queue, uns buffer_size, uns read_ahead, uns write_back);
229
230 void bclose_file_helper(struct fastbuf *f, int fd, int is_temp_file);
231
232 /***
233  * === Fastbufs on file fragments [[fblim]]
234  *
235  * The `fblim` back-end reads from a file handle, but at most a given
236  * number of bytes. This is frequently used for reading from sockets.
237  ***/
238
239 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit); /** Create a fastbuf which reads at most @limit bytes from @fd. **/
240
241 /***
242  * === Fastbufs on in-memory streams [[fbmem]]
243  *
244  * The `fbmem` back-end keeps the whole contents of the stream
245  * in memory (as a linked list of memory blocks, so address space
246  * fragmentation is avoided).
247  *
248  * First, you use fbmem_create() to create the stream and the fastbuf
249  * used for writing to it. Then you can call fbmem_clone_read() to get
250  * an arbitrary number of fastbuf for reading from the stream.
251  ***/
252
253 struct fastbuf *fbmem_create(uns blocksize);            /** Create stream and return its writing fastbuf. **/
254 struct fastbuf *fbmem_clone_read(struct fastbuf *f);    /** Given a writing fastbuf, create a new reading fastbuf. **/
255
256 /***
257  * === Fastbufs on static buffers [[fbbuf]]
258  *
259  * The `fbbuf` back-end stores the stream in a given block of memory.
260  * This is useful for parsing and generating of complex data structures.
261  ***/
262
263 /**
264  * Creates a read-only fastbuf that takes its data from a given buffer.
265  * The fastbuf structure is allocated by the caller and pointed to by @f.
266  * The @buffer and @size specify the location and size of the buffer.
267  *
268  * In some cases, the front-ends can take advantage of rewriting the contents
269  * of the buffer temporarily. In this case, set @can_overwrite as described
270  * in <<internal,Internals>>. If you do not care, keep @can_overwrite zero.
271  *
272  * It is not possible to close this fastbuf.
273  */
274 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite);
275
276 /**
277  * Creates a write-only fastbuf which writes into a provided memory buffer.
278  * The fastbuf structure is allocated by the caller and pointed to by @f.
279  * An attempt to write behind the end of the buffer dies.
280  *
281  * Data are written directly into the buffer, so it is not necessary to call bflush()
282  * at any moment.
283  *
284  * It is not possible to close this fastbuf.
285  */
286 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
287
288 static inline uns fbbuf_count_written(struct fastbuf *f) /** Calculates, how many bytes were already written into the buffer. **/
289 {
290   return f->bptr - f->bstop;
291 }
292
293 /***
294  * === Fastbuf on recyclable growing buffers [[fbgrow]]
295  *
296  * The `fbgrow` back-end keeps the stream in a contiguous buffer stored in the
297  * main memory, but unlike <<fbmem,`fbmem`>>, the buffer does not have a fixed
298  * size and it is expanded to accomodate all data.
299  *
300  * At every moment, you can use `fastbuf->buffer` to gain access to the stream.
301  ***/
302
303 struct fastbuf *fbgrow_create(unsigned basic_size);     /** Create the growing buffer pre-allocated to @basic_size bytes. **/
304 void fbgrow_reset(struct fastbuf *b);                   /** Reset stream and prepare for writing. **/
305 void fbgrow_rewind(struct fastbuf *b);                  /** Prepare for reading (of already written data). **/
306
307 /***
308  * === Fastbuf on memory pools [[fbpool]]
309  *
310  * The write-only `fbpool` back-end also keeps the stream in a contiguous
311  * buffer, but this time the buffer is allocated from within a memory pool.
312  ***/
313
314 struct mempool;
315 struct fbpool { /** Structure for fastbufs & mempools. **/
316   struct fastbuf fb;
317   struct mempool *mp;
318 };
319
320 /**
321  * Initialize a new `fbpool`. The structure is allocated by the caller.
322  **/
323 void fbpool_init(struct fbpool *fb);    /** Initialize a new mempool fastbuf. **/
324 /**
325  * Start a new continuous block and prepare for writing (see mp_start()).
326  * Provide the memory pool you want to use for this block as @mp.
327  **/
328 void fbpool_start(struct fbpool *fb, struct mempool *mp, uns init_size);
329 /**
330  * Close the block and return the address of its start (see mp_end()).
331  * The length can be determined by calling mp_size(mp, ptr).
332  **/
333 void *fbpool_end(struct fbpool *fb);
334
335 /***
336  * === Atomic files for multi-threaded programs [[fbatomic]]
337  *
338  * This fastbuf backend is designed for cases when several threads
339  * of a single program append records to a common file and while the
340  * record can mix in an arbitrary way, the bytes inside a single
341  * record must remain uninterrupted.
342  *
343  * In case of files with fixed record size, we just allocate the
344  * buffer to hold a whole number of records and take advantage
345  * of the atomicity of the write() system call.
346  *
347  * With variable-sized records, we need another solution: when
348  * writing a record, we keep the fastbuf in a locked state, which
349  * prevents buffer flushing (and if the buffer becomes full, we extend it),
350  * and we wait for an explicit commit operation which write()s the buffer
351  * if the free space in the buffer falls below the expected maximum record
352  * length.
353  *
354  * Please note that initialization of the clones is not thread-safe,
355  * so you have to serialize it yourself.
356  ***/
357
358 struct fb_atomic {
359   struct fastbuf fb;
360   struct fb_atomic_file *af;
361   byte *expected_max_bptr;
362   uns slack_size;
363 };
364 #define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
365
366 /**
367  * Open an atomic fastbuf.
368  * If @master is NULL, the file @name is opened. If it is non-null,
369  * a new clone of an existing atomic fastbuf is created.
370  *
371  * If the file has fixed record length, just set @record_len to it.
372  * Otherwise set @record_len to the expected maximum record length
373  * with a negative sign (you need not fit in this length, but as long
374  * as you do, the fastbuf is more efficient) and call fbatomic_commit()
375  * after each record.
376  *
377  * You can specify @record_len, if it is known (for optimisations).
378  *
379  * The file is closed when all fastbufs using it are closed.
380  **/
381 struct fastbuf *fbatomic_open(const char *name, struct fastbuf *master, uns bufsize, int record_len);
382 void fbatomic_internal_write(struct fastbuf *b);
383
384 /**
385  * Declare that you have finished writing a record. This is required only
386  * if a fixed record size was not specified.
387  **/
388 static inline void fbatomic_commit(struct fastbuf *b)
389 {
390   if (b->bptr >= ((struct fb_atomic *)b)->expected_max_bptr)
391     fbatomic_internal_write(b);
392 }
393
394 /*** === Configuring stream parameters [[bconfig]] ***/
395
396 enum bconfig_type {                     /** Parameters that could be configured. **/
397   BCONFIG_IS_TEMP_FILE,                 /* 0=normal file, 1=temporary file, 2=shared fd */
398   BCONFIG_KEEP_BACK_BUF,                /* Optimize for bi-directional access */
399 };
400
401 int bconfig(struct fastbuf *f, uns type, int data); /** Configure a fastbuf. Returns previous value. **/
402
403 /*** === Universal functions working on all fastbuf's [[ffbasic]] ***/
404
405 /**
406  * Close and free fastbuf.
407  * Can not be used for fastbufs not returned from function (initialized in a parameter, for example the one from `fbbuf_init_read`).
408  */
409 void bclose(struct fastbuf *f);
410 void bflush(struct fastbuf *f);                                 /** Write data (if it makes any sense, do not use for in-memory buffers). **/
411 void bseek(struct fastbuf *f, ucw_off_t pos, int whence);       /** Seek in the buffer. See `man fseek` for description of @whence. Only for seekable fastbufs. **/
412 void bsetpos(struct fastbuf *f, ucw_off_t pos);                 /** Set position to @pos bytes from beginning. Only for seekable fastbufs. **/
413 void brewind(struct fastbuf *f);                                /** Go to the beginning of the fastbuf. Only for seekable ones. **/
414 ucw_off_t bfilesize(struct fastbuf *f);                         /** How large is the file? -1 if not seekable. **/
415
416 static inline ucw_off_t btell(struct fastbuf *f)                /** Where am I (from the beginning)? **/
417 {
418   return f->pos + (f->bptr - f->bstop);
419 }
420
421 int bgetc_slow(struct fastbuf *f);
422 static inline int bgetc(struct fastbuf *f)                      /** Return next character from the buffer. **/
423 {
424   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
425 }
426
427 int bpeekc_slow(struct fastbuf *f);
428 static inline int bpeekc(struct fastbuf *f)                     /** Return next character from the buffer, but keep the current position. **/
429 {
430   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
431 }
432
433 static inline void bungetc(struct fastbuf *f)                   /** Return last read character back. Only one back is guaranteed to work. **/
434 {
435   f->bptr--;
436 }
437
438 void bputc_slow(struct fastbuf *f, uns c);
439 static inline void bputc(struct fastbuf *f, uns c)              /** Write a single character. **/
440 {
441   if (f->bptr < f->bufend)
442     *f->bptr++ = c;
443   else
444     bputc_slow(f, c);
445 }
446
447 static inline uns bavailr(struct fastbuf *f)
448 {
449   return f->bstop - f->bptr;
450 }
451
452 static inline uns bavailw(struct fastbuf *f)
453 {
454   return f->bufend - f->bptr;
455 }
456
457 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
458 /**
459  * Read at most @l bytes of data into @b.
460  * Returns number of bytes read.
461  * 0 means end of file.
462  */
463 static inline uns bread(struct fastbuf *f, void *b, uns l)
464 {
465   if (bavailr(f) >= l)
466     {
467       memcpy(b, f->bptr, l);
468       f->bptr += l;
469       return l;
470     }
471   else
472     return bread_slow(f, b, l, 0);
473 }
474
475 /**
476  * Reads exactly @l bytes of data into @b.
477  * If at the end of file, it returns 0.
478  * If there are data, but less than @l, it dies.
479  */
480 static inline uns breadb(struct fastbuf *f, void *b, uns l)
481 {
482   if (bavailr(f) >= l)
483     {
484       memcpy(b, f->bptr, l);
485       f->bptr += l;
486       return l;
487     }
488   else
489     return bread_slow(f, b, l, 1);
490 }
491
492 void bwrite_slow(struct fastbuf *f, const void *b, uns l);
493 static inline void bwrite(struct fastbuf *f, const void *b, uns l) /** Writes buffer @b of length @l into fastbuf. **/
494 {
495   if (bavailw(f) >= l)
496     {
497       memcpy(f->bptr, b, l);
498       f->bptr += l;
499     }
500   else
501     bwrite_slow(f, b, l);
502 }
503
504 /**
505  * Reads a line into @b and strips trailing `\n`.
506  * Returns pointer to the terminating 0 or NULL on `EOF`.
507  * Dies if the line is longer than @l.
508  **/
509 char *bgets(struct fastbuf *f, char *b, uns l);
510 char *bgets0(struct fastbuf *f, char *b, uns l);        /** The same as bgets(), but for 0-terminated strings. **/
511 /**
512  * Returns either length of read string (excluding the terminator) or -1 if it is too long.
513  * In such cases exactly @l bytes are read.
514  */
515 int bgets_nodie(struct fastbuf *f, char *b, uns l);
516
517 struct mempool;
518 struct bb_t;
519 /**
520  * Read a string, strip the trailing `\n` and store it into growing buffer @b.
521  * Dies if the line is longer than @limit.
522  **/
523 uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit);
524 /**
525  * Read a string, strip the trailing `\n` and store it into buffer allocated from a memory pool.
526  **/
527 char *bgets_mp(struct fastbuf *f, struct mempool *mp);
528
529 struct bgets_stk_struct {
530   struct fastbuf *f;
531   byte *old_buf, *cur_buf, *src;
532   uns old_len, cur_len, src_len;
533 };
534 void bgets_stk_init(struct bgets_stk_struct *s);
535 void bgets_stk_step(struct bgets_stk_struct *s);
536
537 /**
538  * Read a string, strip the trailing `\n` and store it on the stack (allocated using alloca()).
539  **/
540 #define bgets_stk(fb) \
541   ({ 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; })
542
543 /**
544  * Write a string, without 0 or `\n` at the end.
545  **/
546 static inline void bputs(struct fastbuf *f, const char *b)
547 {
548   bwrite(f, b, strlen(b));
549 }
550
551 /**
552  * Write string, including terminating 0.
553  **/
554 static inline void bputs0(struct fastbuf *f, const char *b)
555 {
556   bwrite(f, b, strlen(b)+1);
557 }
558
559 /**
560  * Write string and append a newline to the end.
561  **/
562 static inline void bputsn(struct fastbuf *f, const char *b)
563 {
564   bputs(f, b);
565   bputc(f, '\n');
566 }
567
568 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
569 /**
570  * Copy @l bytes of data from fastbuf @f to fastbuf @t.
571  **/
572 static inline void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
573 {
574   if (bavailr(f) >= l && bavailw(t) >= l)
575     {
576       memcpy(t->bptr, f->bptr, l);
577       t->bptr += l;
578       f->bptr += l;
579     }
580   else
581     bbcopy_slow(f, t, l);
582 }
583
584 int bskip_slow(struct fastbuf *f, uns len);
585 static inline int bskip(struct fastbuf *f, uns len) /** Skip @len bytes without reading them. **/
586 {
587   if (bavailr(f) >= len)
588     {
589       f->bptr += len;
590       return 1;
591     }
592   else
593     return bskip_slow(f, len);
594 }
595
596 /*** === Direct I/O on buffers ***/
597 // TODO Documentation -- what do they do?
598
599 static inline uns
600 bdirect_read_prepare(struct fastbuf *f, byte **buf)
601 {
602   if (f->bptr == f->bstop && !f->refill(f))
603     {
604       *buf = NULL;  // This is not needed, but it helps to get rid of spurious warnings
605       return 0;
606     }
607   *buf = f->bptr;
608   return bavailr(f);
609 }
610
611 static inline void
612 bdirect_read_commit(struct fastbuf *f, byte *pos)
613 {
614   f->bptr = pos;
615 }
616
617 static inline void
618 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
619 {
620   f->bptr = pos;
621   f->buffer = pos;      /* Avoid seeking backwards in the buffer */
622 }
623
624 static inline uns
625 bdirect_write_prepare(struct fastbuf *f, byte **buf)
626 {
627   if (f->bptr == f->bufend)
628     f->spout(f);
629   *buf = f->bptr;
630   return bavailw(f);
631 }
632
633 static inline void
634 bdirect_write_commit(struct fastbuf *f, byte *pos)
635 {
636   f->bptr = pos;
637 }
638
639 /*** === Formatted output ***/
640
641 /**
642  * printf into a fastbuf.
643  **/
644 int bprintf(struct fastbuf *b, const char *msg, ...)
645   FORMAT_CHECK(printf,2,3);
646 int vbprintf(struct fastbuf *b, const char *msg, va_list args); /** vprintf into a fastbuf. **/
647
648 #endif