From 45bc46d42e28a0a74e2cfb68548bcaa49a919a09 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Wed, 14 Dec 2016 08:16:34 +0200 Subject: [PATCH] net: buf: Introduce net_buf_destroy() wrapper This is only for use with custom destroy callbacks, so that the application gets isolated away from the details of how exactly the buffers are managed. This opens up the possibility of switching away from k_fifo to potentially better solutions, such as k_lifo. Change-Id: I0d8322fdec3500d8ae060ae471b9448aeaa4572a Signed-off-by: Johan Hedberg --- include/net/buf.h | 18 ++++++++++++++++-- subsys/bluetooth/host/hci_core.c | 2 +- subsys/net/buf.c | 2 +- subsys/net/ip/nbuf.c | 6 +++--- tests/net/buf/src/main.c | 6 +++--- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/include/net/buf.h b/include/net/buf.h index 098f1e9e86f..c601dd41279 100644 --- a/include/net/buf.h +++ b/include/net/buf.h @@ -469,8 +469,8 @@ struct net_buf_pool { * than an extern declaration. * * If provided with a custom destroy callback this callback is - * responsible for eventually returning the buffer back to the free - * buffers FIFO through k_fifo_put(buf->free, buf). + * responsible for eventually calling net_buf_destroy() to complete the + * process of returning the buffer to the pool. * * @param _name Name of the pool variable. * @param _count Number of buffers in the pool. @@ -548,6 +548,20 @@ struct net_buf *net_buf_get_debug(struct k_fifo *fifo, int32_t timeout, struct net_buf *net_buf_get(struct k_fifo *fifo, int32_t timeout); #endif +/** + * @brief Destroy buffer from custom destroy callback + * + * This helper is only intended to be used from custom destroy callbacks. + * If no custom destroy callback is given to NET_BUF_POOL_DEFINE() then + * there is no need to use this API. + * + * @param buf Buffer to destroy. + */ +static inline void net_buf_destroy(struct net_buf *buf) +{ + k_fifo_put(&buf->pool->free, buf); +} + /** * @brief Initialize buffer with the given headroom. * diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index b5710111093..3f146f048f5 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -135,7 +135,7 @@ static void report_completed_packet(struct net_buf *buf) uint16_t handle = acl(buf)->handle; struct bt_hci_handle_count *hc; - k_fifo_put(&buf->pool->free, buf); + net_buf_destroy(buf); /* Do nothing if controller to host flow control is not supported */ if (!(bt_dev.supported_commands[10] & 0x20)) { diff --git a/subsys/net/buf.c b/subsys/net/buf.c index 9df25e4674d..dc6118ed0fb 100644 --- a/subsys/net/buf.c +++ b/subsys/net/buf.c @@ -198,7 +198,7 @@ void net_buf_unref(struct net_buf *buf) if (buf->pool->destroy) { buf->pool->destroy(buf); } else { - k_fifo_put(&buf->pool->free, buf); + net_buf_destroy(buf); } buf = frags; diff --git a/subsys/net/ip/nbuf.c b/subsys/net/ip/nbuf.c index db1887da407..d11cc863041 100644 --- a/subsys/net/ip/nbuf.c +++ b/subsys/net/ip/nbuf.c @@ -225,21 +225,21 @@ static inline void free_rx_bufs_func(struct net_buf *buf) { inc_free_rx_bufs_func(buf); - k_fifo_put(&buf->pool->free, buf); + net_buf_destroy(buf); } static inline void free_tx_bufs_func(struct net_buf *buf) { inc_free_tx_bufs_func(buf); - k_fifo_put(&buf->pool->free, buf); + net_buf_destroy(buf); } static inline void free_data_bufs_func(struct net_buf *buf) { inc_free_data_bufs_func(buf); - k_fifo_put(&buf->pool->free, buf); + net_buf_destroy(buf); } /* The RX and TX pools do not store any data. Only bearer / protocol diff --git a/tests/net/buf/src/main.c b/tests/net/buf/src/main.c index 37f2192187a..23698844b2a 100644 --- a/tests/net/buf/src/main.c +++ b/tests/net/buf/src/main.c @@ -85,7 +85,7 @@ static void buf_destroy(struct net_buf *buf) destroy_called++; assert_equal(pool, &bufs_pool, "Invalid free pointer in buffer"); - k_fifo_put(&pool->free, buf); + net_buf_destroy(buf); } static void frag_destroy(struct net_buf *buf) @@ -95,7 +95,7 @@ static void frag_destroy(struct net_buf *buf) frag_destroy_called++; assert_equal(pool, &frags_pool, "Invalid free frag pointer in buffer"); - k_fifo_put(&pool->free, buf); + net_buf_destroy(buf); } static void frag_destroy_big(struct net_buf *buf) @@ -105,7 +105,7 @@ static void frag_destroy_big(struct net_buf *buf) frag_destroy_called++; assert_equal(pool, &big_frags_pool, "Invalid free big frag pointer in buffer"); - k_fifo_put(&pool->free, buf); + net_buf_destroy(buf); } static const char example_data[] = "0123456789"