]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/fastbuf.h
Example: Fix the previous commit.
[libucw.git] / ucw / fastbuf.h
index c4f7cd90cada9efa1bf15e14ad9ac0b311b8b8c0..d0cf30cad3415f655ba12516ac66a4feab50d7fd 100644 (file)
  *
  * Generally speaking, a fastbuf consists of a buffer and a set of callbacks.
  * All front-end functions operate on the buffer and if the buffer becomes
- * empty or fills up, they ask the corresponding callback to solve the
+ * empty or fills up, they ask the corresponding callback to handle the
  * situation. Back-ends then differ just in the definition of the callbacks.
- * The state of the fastbuf is represented by `struct fastbuf`, which
- * is a simple structure describing the state of the buffer and pointers
- * to the callback functions.
- *
- * When we are reading from the fastbuf, the buffer is laid out as follows:
- *
- *  +----------------+---------------------------+
- *  | read data      | free space                |
- *  +----------------+---------------------------+
- *  ^        ^        ^                           ^
- *  buffer   bptr     bstop                       bufend
- *
- * Here `bptr` points to the next character to be read. After the last character is
- * read, `bptr == bstop` and the `refill` callback gets called upon the next read
- * attempt to bring further data. This gives us an easy way how to implement bungetc().
- *
- * When writing, the situation looks like:
- *
- *  +--------+--------------+--------------------+
- *  | unused | written data | free space         |
- *  +--------+--------------+--------------------+
- *  ^         ^              ^                    ^
- *  buffer    bstop          bptr                 bufend
- *
- * In this case, the `bptr` points at the position where the next character
- * will be written to. When we want to write, but `bptr == bufend`, we call
- * the `spout` hook to flush the data and get an empty buffer.
- *
- * Several dirty tricks can be played:
- *
- *    - The `spout`/`refill` hooks can change not only `bptr` and `bstop`, but also
- *     the location and size of the buffer; the fb-mem back-end takes advantage of it.
- *    - In some cases, the user of the `bdirect` interface can be allowed to modify
- *     the data in the buffer to avoid unnecessary copying. If the back-end
- *     allows such modifications, it can set `fastbuf->can_overwrite_buffer` accordingly:
- *             *  0 if no modification is allowed,
- *             *  1 if the user can modify the buffer on the condition that
- *                  the modifications will be undone before calling the next
- *                  fastbuf operation
- *             *  2 if the user is allowed to overwrite the data in the buffer
- *                  if bdirect_read_commit_modified() is called afterwards.
- *                  In this case, the back-end must be prepared for trimming
- *                  of the buffer which is done by the commit function.
+ *
+ * The state of the fastbuf is represented by a `struct fastbuf`, which
+ * is a simple structure describing the state of the buffer (the pointers
+ * `buffer`, `bufend`), two front-end cursors (`bptr`, `bstop`), position in the file (`pos`)
+ * and pointers to the callback functions.
+ *
+ * The buffer can be in one of the following states:
+ *
+ * 1. Flushed:
+ *
+ *    +----------------+---------------------------+
+ *    | unused         | free space                |
+ *    +----------------+---------------------------+
+ *    ^                ^                           ^
+ *    buffer        <= bptr == bstop (pos)      <= bufend
+ *
+ *   * If `bptr == bstop`, then there is no cached data and
+ *     the fastbuf is ready for any read or write operation.
+ *     Position of the back-end's cursor equals the front-end's one.
+ *   * The interval `[bstop, bufend]` can be used by front-ends
+ *     for writing. If it is empty, the `spout` callback gets called
+ *     upon the first write attempt to allocate a new buffer.
+ *   * When a front-end needs to read something, it calls the `spout` callback.
+ *   * Any of the pointers can be NULL.
+ *
+ * 2. Reading:
+ *
+ *    +----------------+---------------------------+
+ *    | read data      | unused                    |
+ *    +----------------+---------------------------+
+ *    ^         ^      ^                           ^
+ *    buffer <= bptr <= bstop (pos)             <= bufend
+ *
+ *   * If we try to read something, we get to the reading mode.
+ *   * No writing is allowed until a flush operation. But note that @bflush()
+ *     will simply set `bptr` to `bstop` and it breaks the position of the front-end's cursor.
+ *   * The interval `[buffer, bstop]` contains a block of data read by the back-end.
+ *     `bptr` is the front-end's cursor which points to the next character to be read.
+ *     After the last character is read, `bptr == bstop` and the `refill` callback
+ *     gets called upon the next read attempt to bring further data.
+ *     This gives us an easy way how to implement @bungetc().
+ *
+ * 3. Writing:
+ *
+ *    +---------+--------------+-------------------+
+ *    | unused  | written data | free space        |
+ *    +---------+--------------+-------------------+
+ *    ^         ^              ^                   ^
+ *    buffer <= bstop (pos)  < bptr             <= bufend
+ *
+ *   * This schema corresponds to the situation after a write attempt.
+ *   * No reading is allowed until a flush operation.
+ *   * The `bptr` points at the position where the next character
+ *     will be written to. When we want to write, but `bptr == bufend`, we call
+ *     the `spout` hook to flush the data and get an empty buffer.
+ *
+ *
+ * Rules for back-ends:
+ *
+ *   - Front-ends are only allowed to change the value of `bptr`, some flags
+ *     and if a fatal error occurs, then also `bstop`.
+ *   - `buffer <= bstop <= bufend`.
+ *   - `pos` should be the position in the file corresponding of the location of `bstop` in the buffer.
+ *   - Failed callbacks (except `close`) should use @bthrow().
+ *   - Any callback pointers may be NULL in case the callback is not implemented.
+ *   - Callbacks can change not only `bptr` and `bstop`, but also the location and size of the buffer;
+ *     the fb-mem back-end takes advantage of it.
+ *
+ *   - initialization:
+ *     * out: `buffer <= bptr == bstop <= bufend` (flushed)
+ *
+ *   - `refill`:
+ *     * in: `buffer <= bptr == bstop <= bufend` (reading or flushed)
+ *     * out: `buffer <= bptr < bstop <= bufend` (reading)
+ *
+ *   - `spout`:
+ *     * in: `buffer <= bstop <= bptr <= bufend` (writing or flushed)
+ *     * out: `buffer <= bstop <= bufend` (flushed)
+ *     * `bptr` is set automatically to `bstop`.
+ *     * If the input `bptr` equals ` bstop`, then the resulting `bstop` must be lower than `bufend`.
+ *
+ *   - `seek`:
+ *     * in: `buffer <= bstop == bptr <= bufend` (flushed)
+ *     * out: `buffer <= bstop <= bufend` (flushed)
+ *     * `bptr` is set automatically to `bstop`.
+ *
+ *   - `close`:
+ *     * in: `buffer <= bptr == bstop <= bufend` (flushed)
+ *     * `close` must always free all internal structures, even when it throws an exception.
  *
  ***/
 
@@ -79,7 +125,7 @@ struct fastbuf {
   ucw_off_t pos;                               /* Position of bstop in the file */
   int (*refill)(struct fastbuf *);             /* Get a buffer with new data, returns 0 on EOF */
   void (*spout)(struct fastbuf *);             /* Write buffer data to the file */
-  int (*seek)(struct fastbuf *, ucw_off_t, int);/* Slow path for bseek(), buffer already flushed; returns success */
+  int (*seek)(struct fastbuf *, ucw_off_t, int);/* Slow path for @bseek(), buffer already flushed; returns success */
   void (*close)(struct fastbuf *);             /* Close the stream */
   int (*config)(struct fastbuf *, uns, int);   /* Configure the stream */
   int can_overwrite_buffer;                    /* Can the buffer be altered? 0=never, 1=temporarily, 2=permanently */
@@ -141,7 +187,7 @@ struct fastbuf *bopen_tmp_file(struct fb_params *params);
  * Creates a fastbuf from a file descriptor @fd and sets its filename
  * to @name (the name is used only in error messages).
  * When the fastbuf is closed, the fd is closed as well. You can override
- * this behavior by calling bconfig().
+ * this behavior by calling @bconfig().
  */
 struct fastbuf *bopen_fd_name(int fd, struct fb_params *params, const char *name);
 static inline struct fastbuf *bopen_fd(int fd, struct fb_params *params) /** Same as above, but with an auto-generated filename. **/
@@ -161,16 +207,16 @@ void bfilesync(struct fastbuf *b);
  * up any parameters, there is a couple of shortcuts.
  ***/
 
-struct fastbuf *bopen(const char *name, uns mode, uns buflen);         /** Equivalent to bopen_file() with `FB_STD` back-end. **/
-struct fastbuf *bopen_try(const char *name, uns mode, uns buflen);     /** Equivalent to bopen_file_try() with `FB_STD` back-end. **/
-struct fastbuf *bopen_tmp(uns buflen);                                 /** Equivalent to bopen_tmp_file() with `FB_STD` back-end. **/
-struct fastbuf *bfdopen(int fd, uns buflen);                           /** Equivalent to bopen_fd() with `FB_STD` back-end. **/
-struct fastbuf *bfdopen_shared(int fd, uns buflen);                    /** Like bfdopen(), but it does not close the @fd on bclose(). **/
+struct fastbuf *bopen(const char *name, uns mode, uns buflen);         /** Equivalent to @bopen_file() with `FB_STD` back-end. **/
+struct fastbuf *bopen_try(const char *name, uns mode, uns buflen);     /** Equivalent to @bopen_file_try() with `FB_STD` back-end. **/
+struct fastbuf *bopen_tmp(uns buflen);                                 /** Equivalent to @bopen_tmp_file() with `FB_STD` back-end. **/
+struct fastbuf *bfdopen(int fd, uns buflen);                           /** Equivalent to @bopen_fd() with `FB_STD` back-end. **/
+struct fastbuf *bfdopen_shared(int fd, uns buflen);                    /** Like @bfdopen(), but it does not close the @fd on @bclose(). **/
 
 /***
  * === Temporary files [[fbtemp]]
  *
- * Usually, bopen_tmp_file() is the best way how to come to a temporary file.
+ * Usually, @bopen_tmp_file() is the best way how to come to a temporary file.
  * However, in some specific cases you can need more, so there is also a set
  * of more general functions.
  ***/
@@ -207,8 +253,8 @@ int open_tmp(char *name_buf, int open_flags, int mode);
 
 /**
  * Sometimes, a file is created as temporary and then moved to a stable
- * location. This function takes a fastbuf created by bopen_tmp_file()
- * or bopen_tmp(), marks it as permanent, closes it and renames it to
+ * location. This function takes a fastbuf created by @bopen_tmp_file()
+ * or @bopen_tmp(), marks it as permanent, closes it and renames it to
  * @name.
  *
  * Please note that it assumes that the temporary file and the @name
@@ -245,8 +291,8 @@ struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit); /** Create a f
  * in memory (as a linked list of memory blocks, so address space
  * fragmentation is avoided).
  *
- * First, you use fbmem_create() to create the stream and the fastbuf
- * used for writing to it. Then you can call fbmem_clone_read() to get
+ * First, you use @fbmem_create() to create the stream and the fastbuf
+ * used for writing to it. Then you can call @fbmem_clone_read() to get
  * an arbitrary number of fastbuf for reading from the stream.
  ***/
 
@@ -278,7 +324,7 @@ void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrit
  * The fastbuf structure is allocated by the caller and pointed to by @f.
  * An attempt to write behind the end of the buffer dies.
  *
- * Data are written directly into the buffer, so it is not necessary to call bflush()
+ * Data are written directly into the buffer, so it is not necessary to call @bflush()
  * at any moment.
  *
  * It is not possible to close this fastbuf.
@@ -322,13 +368,13 @@ struct fbpool { /** Structure for fastbufs & mempools. **/
  **/
 void fbpool_init(struct fbpool *fb);   /** Initialize a new mempool fastbuf. **/
 /**
- * Start a new continuous block and prepare for writing (see mp_start()).
+ * Start a new continuous block and prepare for writing (see <<mempool:mp_start()>>).
  * Provide the memory pool you want to use for this block as @mp.
  **/
 void fbpool_start(struct fbpool *fb, struct mempool *mp, uns init_size);
 /**
- * Close the block and return the address of its start (see mp_end()).
- * The length can be determined by calling mp_size(mp, ptr).
+ * Close the block and return the address of its start (see <<mempool:mp_end()>>).
+ * The length can be determined by calling <<mempool:mp_size(mp, ptr)>>.
  **/
 void *fbpool_end(struct fbpool *fb);
 
@@ -371,7 +417,7 @@ struct fb_atomic {
  * If the file has fixed record length, just set @record_len to it.
  * Otherwise set @record_len to the expected maximum record length
  * with a negative sign (you need not fit in this length, but as long
- * as you do, the fastbuf is more efficient) and call fbatomic_commit()
+ * as you do, the fastbuf is more efficient) and call @fbatomic_commit()
  * after each record.
  *
  * You can specify @record_len, if it is known (for optimisations).
@@ -444,12 +490,12 @@ static inline void bputc(struct fastbuf *f, uns c)                /** Write a single character
     bputc_slow(f, c);
 }
 
-static inline uns bavailr(struct fastbuf *f)
+static inline uns bavailr(struct fastbuf *f)                   /** Return the length of the cached data to be read. Do not use directly. **/
 {
   return f->bstop - f->bptr;
 }
 
-static inline uns bavailw(struct fastbuf *f)
+static inline uns bavailw(struct fastbuf *f)                   /** Return the length of the buffer available for writing. Do not use directly. **/
 {
   return f->bufend - f->bptr;
 }
@@ -507,7 +553,7 @@ static inline void bwrite(struct fastbuf *f, const void *b, uns l) /** Writes bu
  * Dies if the line is longer than @l.
  **/
 char *bgets(struct fastbuf *f, char *b, uns l);
-char *bgets0(struct fastbuf *f, char *b, uns l);       /** The same as bgets(), but for 0-terminated strings. **/
+char *bgets0(struct fastbuf *f, char *b, uns l);       /** The same as @bgets(), but for 0-terminated strings. **/
 /**
  * Returns either length of read string (excluding the terminator) or -1 if it is too long.
  * In such cases exactly @l bytes are read.
@@ -568,6 +614,7 @@ static inline void bputsn(struct fastbuf *f, const char *b)
 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
 /**
  * Copy @l bytes of data from fastbuf @f to fastbuf @t.
+ * `UINT_MAX` (`~0U`) means all data, even if more than `UINT_MAX` bytes remain.
  **/
 static inline void bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
 {
@@ -594,10 +641,28 @@ static inline int bskip(struct fastbuf *f, uns len) /** Skip @len bytes without
 }
 
 /*** === Direct I/O on buffers ***/
-// TODO Documentation -- what do they do?
 
-static inline uns
-bdirect_read_prepare(struct fastbuf *f, byte **buf)
+/**
+ * Begin direct reading from fastbuf's internal buffer to avoid unnecessary copying.
+ * The function returns a buffer @buf together with its length in bytes (zero means EOF)
+ * with cached data to be read.
+ *
+ * Some back-ends allow the user to modify the data in the returned buffer to avoid unnecessary.
+ * If the back-end allows such modifications, it can set `f->can_overwrite_buffer` accordingly:
+ *
+ *   - 0 if no modification is allowed,
+ *   - 1 if the user can modify the buffer on the condition that
+ *       the modifications will be undone before calling the next
+ *       fastbuf operation
+ *   - 2 if the user is allowed to overwrite the data in the buffer
+ *       if @bdirect_read_commit_modified() is called afterwards.
+ *       In this case, the back-end must be prepared for trimming
+ *       of the buffer which is done by the commit function.
+ *
+ * The reading must be ended by @bdirect_read_commit() or @bdirect_read_commit_modified(),
+ * unless the user did not read or modify anything.
+ **/
+static inline uns bdirect_read_prepare(struct fastbuf *f, byte **buf)
 {
   if (f->bptr == f->bstop && !f->refill(f))
     {
@@ -608,21 +673,33 @@ bdirect_read_prepare(struct fastbuf *f, byte **buf)
   return bavailr(f);
 }
 
-static inline void
-bdirect_read_commit(struct fastbuf *f, byte *pos)
+/**
+ * End direct reading started by @bdirect_read_prepare() and move the cursor at @pos.
+ * Data in the returned buffer must be same as after @bdirect_read_prepare() and
+ * @pos must point somewhere inside the buffer.
+ **/
+static inline void bdirect_read_commit(struct fastbuf *f, byte *pos)
 {
   f->bptr = pos;
 }
 
-static inline void
-bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
+/**
+ * Similar to @bdirect_read_commit(), but accepts also modified data before @pos.
+ * Note that such modifications are supported only if `f->can_overwrite_buffer == 2`.
+ **/
+static inline void bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
 {
   f->bptr = pos;
   f->buffer = pos;     /* Avoid seeking backwards in the buffer */
 }
 
-static inline uns
-bdirect_write_prepare(struct fastbuf *f, byte **buf)
+/**
+ * Start direct writing to fastbuf's internal buffer to avoid copy overhead.
+ * The function returns the length of the buffer in @buf (at least one byte)
+ * where we can write to. The operation must be ended by @bdirect_write_commit(),
+ * unless nothing is written.
+ **/
+static inline uns bdirect_write_prepare(struct fastbuf *f, byte **buf)
 {
   if (f->bptr == f->bufend)
     f->spout(f);
@@ -630,8 +707,12 @@ bdirect_write_prepare(struct fastbuf *f, byte **buf)
   return bavailw(f);
 }
 
-static inline void
-bdirect_write_commit(struct fastbuf *f, byte *pos)
+/**
+ * Commit the data written to the buffer returned by @bdirect_write_prepare().
+ * The length is specified by @pos which must point just after the written data.
+ * Also moves the cursor to @pos.
+ **/
+static inline void bdirect_write_commit(struct fastbuf *f, byte *pos)
 {
   f->bptr = pos;
 }