net: buf: Add net_buf_reserve() API

This API allows initializing the buffer with a given headroom. This
prepares the way for eventually removing the 'reserve_head' parameter
from the net_buf_get APIs, but can already now be useful in some
scenarios where the headroom is desired to be different than some
higher level API sets it to be.

Change-Id: Iffbe5761fdf3d2ad8cb4b8437b1074cf42ea9c6c
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2016-07-08 10:47:35 +03:00
parent e03151ee3e
commit 727a2459b1
2 changed files with 20 additions and 1 deletions

View file

@ -174,6 +174,17 @@ struct net_buf *net_buf_get(struct nano_fifo *fifo, size_t reserve_head);
struct net_buf *net_buf_get_timeout(struct nano_fifo *fifo,
size_t reserve_head, int32_t timeout);
/**
* @brief Initialize buffer with the given headroom.
*
* Initializes a buffer with a given headroom. The buffer is not expected to
* contain any data when this API is called.
*
* @param buf Buffer to initialize.
* @param reserve How much headroom to reserve.
*/
void net_buf_reserve(struct net_buf *buf, size_t reserve);
/**
* @brief Put a buffer into a FIFO
*

View file

@ -65,8 +65,8 @@ struct net_buf *net_buf_get_timeout(struct nano_fifo *fifo,
*/
if (buf->free == fifo) {
buf->ref = 1;
buf->data = buf->__buf + reserve_head;
buf->len = 0;
net_buf_reserve(buf, reserve_head);
buf->flags = 0;
buf->frags = NULL;
@ -104,6 +104,14 @@ struct net_buf *net_buf_get(struct nano_fifo *fifo, size_t reserve_head)
return net_buf_get_timeout(fifo, reserve_head, TICKS_UNLIMITED);
}
void net_buf_reserve(struct net_buf *buf, size_t reserve)
{
NET_BUF_ASSERT(buf->len == 0);
NET_BUF_DBG("buf %p reserve %u", buf, reserve);
buf->data = buf->__buf + reserve;
}
void net_buf_put(struct nano_fifo *fifo, struct net_buf *buf)
{
struct net_buf *tail;