MINOR: buffers: add b_xfer() to transfer data between buffers

Instead of open-coding buffer-to-buffer transfers using blocks, let's
have a dedicated function for this. It also adjusts the buffer counts.
This commit is contained in:
Willy Tarreau 2018-07-20 16:24:39 +02:00
parent f7d0447376
commit f148888d19
2 changed files with 43 additions and 0 deletions

View File

@ -459,6 +459,13 @@ b_rep_blk() | buffer *buf | writes the block <blk> at position
| | done. If <len> is null, the <blk> | | done. If <len> is null, the <blk>
| | pointer is allowed to be null, in | | pointer is allowed to be null, in
| | order to erase a block | | order to erase a block
--------------------+------------------+---------------------------------------
b_xfer() | buffer *src | transfers at most <count> bytes from
| buffer *dst | buffer <src> to buffer <dst> and
| size_t cout | returns the number of bytes copied.
| ret: size_t | The bytes are removed from <src> and
| | added to <dst>. The caller guarantees
| | that <count> is <= b_room(dst)
====================+==================+======================================= ====================+==================+=======================================

View File

@ -514,6 +514,42 @@ static inline size_t b_putblk(struct buffer *b, const char *blk, size_t len)
return len; return len;
} }
/* b_xfer() : transfers at most <count> bytes from buffer <src> to buffer <dst>
* and returns the number of bytes copied. The bytes are removed from <src> and
* added to <dst>. The caller is responsible for ensuring that <count> is not
* larger than b_room(dst).
*/
static inline size_t b_xfer(struct buffer *dst, struct buffer *src, size_t count)
{
size_t ret, block1, block2;
ret = 0;
if (!count)
goto leave;
ret = b_data(src);
if (!ret)
goto leave;
if (ret > count)
ret = count;
block1 = b_contig_data(src, 0);
if (block1 > ret)
block1 = ret;
block2 = ret - block1;
if (block1)
__b_putblk(dst, b_head(src), block1);
if (block2)
__b_putblk(dst, b_peek(src, block1), block2);
b_del(src, ret);
leave:
return ret;
}
/* b_rep_blk() : writes the block <blk> at position <pos> which must be in /* b_rep_blk() : writes the block <blk> at position <pos> which must be in
* buffer <b>, and moves the part between <end> and the buffer's tail just * buffer <b>, and moves the part between <end> and the buffer's tail just
* after the end of the copy of <blk>. This effectively replaces the part * after the end of the copy of <blk>. This effectively replaces the part