2 * UCW Library -- Fast Buffered I/O
4 * (c) 1997--2008 Martin Mares <mj@ucw.cz>
5 * (c) 2004 Robert Spalek <robert@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
11 #ifndef _UCW_FASTBUF_H
12 #define _UCW_FASTBUF_H
18 * === Internal structure [[internal]]
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.
28 * When we are reading from the fastbuf, the buffer is laid out as follows:
30 * +----------------+---------------------------+
31 * | read data | free space |
32 * +----------------+---------------------------+
34 * buffer bptr bstop bufend
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().
40 * When writing, the situation looks like:
42 * +--------+--------------+--------------------+
43 * | unused | written data | free space |
44 * +--------+--------------+--------------------+
46 * buffer bstop bptr bufend
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.
52 * Several dirty tricks can be played:
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
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.
71 * This structure contains the state of the fastbuf. See the discussion above
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 */
89 * === Fastbuf on files [[fbparam]]
91 * If you want to use fastbufs to access files, you can choose one of several
92 * back-ends and set their parameters.
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 */
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.
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 */
116 struct asio_queue *asio;
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` **/
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.
128 * Dies if the file does not exist.
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. **/
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.
138 struct fastbuf *bopen_tmp_file(struct fb_params *params);
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().
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. **/
149 return bopen_fd_name(fd, params, NULL);
153 * Flushes all buffers and makes sure that they are written to the disk.
155 void bfilesync(struct fastbuf *b);
158 * === Fastbufs on regular files [[fbfile]]
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.
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(). **/
171 * === Temporary files [[fbtemp]]
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.
178 #define TEMP_FILE_NAME_LEN 256
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.
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.
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
193 * This function is not specific to fastbufs, it can be used separately.
195 void temp_file_name(char *name_buf, int *open_flags);
198 * Opens a temporary file and returns its file descriptor.
199 * You specify the file @mode and @open_flags passed to open().
201 * If the @name_buf (of at last `TEMP_FILE_NAME_LEN` chars) is not NULL,
202 * the filename is also stored in it.
204 * This function is not specific to fastbufs, it can be used separately.
206 int open_tmp(char *name_buf, int open_flags, int mode);
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
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
219 void bfix_tmp_file(struct fastbuf *fb, const char *name);
221 /* Internal functions of some file back-ends */
223 struct fastbuf *bfdopen_internal(int fd, const char *name, uns buflen);
224 struct fastbuf *bfmmopen_internal(int fd, const char *name, uns mode);
226 extern uns fbdir_cheat;
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);
230 void bclose_file_helper(struct fastbuf *f, int fd, int is_temp_file);
233 * === Fastbufs on file fragments [[fblim]]
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.
239 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit); /** Create a fastbuf which reads at most @limit bytes from @fd. **/
242 * === Fastbufs on in-memory streams [[fbmem]]
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).
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.
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. **/
257 * === Fastbufs on static buffers [[fbbuf]]
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.
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.
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.
272 * It is not possible to close this fastbuf.
274 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite);
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.
281 * Data are written directly into the buffer, so it is not necessary to call @bflush()
284 * It is not possible to close this fastbuf.
286 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
288 static inline uns fbbuf_count_written(struct fastbuf *f) /** Calculates, how many bytes were already written into the buffer. **/
290 return f->bptr - f->bstop;
294 * === Fastbuf on recyclable growing buffers [[fbgrow]]
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.
300 * At every moment, you can use `fastbuf->buffer` to gain access to the stream.
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). **/
308 * === Fastbuf on memory pools [[fbpool]]
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.
315 struct fbpool { /** Structure for fastbufs & mempools. **/
321 * Initialize a new `fbpool`. The structure is allocated by the caller.
323 void fbpool_init(struct fbpool *fb); /** Initialize a new mempool fastbuf. **/
325 * Start a new continuous block and prepare for writing (see <<mempool:mp_start()>>).
326 * Provide the memory pool you want to use for this block as @mp.
328 void fbpool_start(struct fbpool *fb, struct mempool *mp, uns init_size);
330 * Close the block and return the address of its start (see <<mempool:mp_end()>>).
331 * The length can be determined by calling <<mempool:mp_size(mp, ptr)>>.
333 void *fbpool_end(struct fbpool *fb);
336 * === Atomic files for multi-threaded programs [[fbatomic]]
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.
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.
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
354 * Please note that initialization of the clones is not thread-safe,
355 * so you have to serialize it yourself.
360 struct fb_atomic_file *af;
361 byte *expected_max_bptr;
364 #define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
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.
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()
377 * You can specify @record_len, if it is known (for optimisations).
379 * The file is closed when all fastbufs using it are closed.
381 struct fastbuf *fbatomic_open(const char *name, struct fastbuf *master, uns bufsize, int record_len);
382 void fbatomic_internal_write(struct fastbuf *b);
385 * Declare that you have finished writing a record. This is required only
386 * if a fixed record size was not specified.
388 static inline void fbatomic_commit(struct fastbuf *b)
390 if (b->bptr >= ((struct fb_atomic *)b)->expected_max_bptr)
391 fbatomic_internal_write(b);
394 /*** === Configuring stream parameters [[bconfig]] ***/
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 */
401 int bconfig(struct fastbuf *f, uns type, int data); /** Configure a fastbuf. Returns previous value. **/
403 /*** === Universal functions working on all fastbuf's [[ffbasic]] ***/
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`).
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. **/
416 static inline ucw_off_t btell(struct fastbuf *f) /** Where am I (from the beginning)? **/
418 return f->pos + (f->bptr - f->bstop);
421 int bgetc_slow(struct fastbuf *f);
422 static inline int bgetc(struct fastbuf *f) /** Return next character from the buffer. **/
424 return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
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. **/
430 return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
433 static inline void bungetc(struct fastbuf *f) /** Return last read character back. Only one back is guaranteed to work. **/
438 void bputc_slow(struct fastbuf *f, uns c);
439 static inline void bputc(struct fastbuf *f, uns c) /** Write a single character. **/
441 if (f->bptr < f->bufend)
447 static inline uns bavailr(struct fastbuf *f)
449 return f->bstop - f->bptr;
452 static inline uns bavailw(struct fastbuf *f)
454 return f->bufend - f->bptr;
457 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
459 * Read at most @l bytes of data into @b.
460 * Returns number of bytes read.
461 * 0 means end of file.
463 static inline uns bread(struct fastbuf *f, void *b, uns l)
467 memcpy(b, f->bptr, l);
472 return bread_slow(f, b, l, 0);
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.
480 static inline uns breadb(struct fastbuf *f, void *b, uns l)
484 memcpy(b, f->bptr, l);
489 return bread_slow(f, b, l, 1);
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. **/
497 memcpy(f->bptr, b, l);
501 bwrite_slow(f, b, l);
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.
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. **/
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.
515 int bgets_nodie(struct fastbuf *f, char *b, uns l);
520 * Read a string, strip the trailing `\n` and store it into growing buffer @b.
521 * Dies if the line is longer than @limit.
523 uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit);
525 * Read a string, strip the trailing `\n` and store it into buffer allocated from a memory pool.
527 char *bgets_mp(struct fastbuf *f, struct mempool *mp);
529 struct bgets_stk_struct {
531 byte *old_buf, *cur_buf, *src;
532 uns old_len, cur_len, src_len;
534 void bgets_stk_init(struct bgets_stk_struct *s);
535 void bgets_stk_step(struct bgets_stk_struct *s);
538 * Read a string, strip the trailing `\n` and store it on the stack (allocated using alloca()).
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; })
544 * Write a string, without 0 or `\n` at the end.
546 static inline void bputs(struct fastbuf *f, const char *b)
548 bwrite(f, b, strlen(b));
552 * Write string, including terminating 0.
554 static inline void bputs0(struct fastbuf *f, const char *b)
556 bwrite(f, b, strlen(b)+1);
560 * Write string and append a newline to the end.
562 static inline void bputsn(struct fastbuf *f, const char *b)
568 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
570 * Copy @l bytes of data from fastbuf @f to fastbuf @t.
572 static inline void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
574 if (bavailr(f) >= l && bavailw(t) >= l)
576 memcpy(t->bptr, f->bptr, l);
581 bbcopy_slow(f, t, l);
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. **/
587 if (bavailr(f) >= len)
593 return bskip_slow(f, len);
596 /*** === Direct I/O on buffers ***/
597 // TODO Documentation -- what do they do?
600 bdirect_read_prepare(struct fastbuf *f, byte **buf)
602 if (f->bptr == f->bstop && !f->refill(f))
604 *buf = NULL; // This is not needed, but it helps to get rid of spurious warnings
612 bdirect_read_commit(struct fastbuf *f, byte *pos)
618 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
621 f->buffer = pos; /* Avoid seeking backwards in the buffer */
625 bdirect_write_prepare(struct fastbuf *f, byte **buf)
627 if (f->bptr == f->bufend)
634 bdirect_write_commit(struct fastbuf *f, byte *pos)
639 /*** === Formatted output ***/
642 * printf into a fastbuf.
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. **/