Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions include/zephyr/net_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,40 @@ struct net_buf_pool *net_buf_pool_get(int id);
*/
int net_buf_id(const struct net_buf *buf);

#if defined(CONFIG_NET_BUF_POOL_USAGE) || defined(__DOXYGEN__)
/**
* @brief Get the number of buffers currently available to claim from a pool.
*
* Note that the number of available buffers might already have changed by the time this
* function returns if other threads are also allocating or freeing buffers from the
* pool.
*
* @kconfig_dep{CONFIG_NET_BUF_POOL_USAGE}
*
* @param pool Which pool to check
*
* @return Number of buffers currently available in the pool
*/
static inline size_t net_buf_get_available(struct net_buf_pool *pool)
{
return (size_t)atomic_get(&pool->avail_count);
}

/**
* @brief Get the maximum number of buffers simultaneously claimed from a pool.
*
* @kconfig_dep{CONFIG_NET_BUF_POOL_USAGE}
*
* @param pool Which pool to check
*
* @return Maximum number of buffers simultaneously claimed from the pool
*/
static inline size_t net_buf_get_max_used(struct net_buf_pool *pool)
{
return (size_t)pool->max_used;
}
#endif /* defined(CONFIG_NET_BUF_POOL_USAGE) || defined(__DOXYGEN__) */

/**
* @brief Allocate a new fixed buffer from a pool.
*
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/net_buf/buf/prj.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CONFIG_NET_BUF=y
CONFIG_HEAP_MEM_POOL_SIZE=4096
CONFIG_NET_BUF_POOL_USAGE=y
CONFIG_HEAP_MEM_POOL_SIZE=2048
#CONFIG_NET_BUF_LOG=y
#CONFIG_NET_BUF_LOG_LEVEL_DBG=y
CONFIG_ZTEST=y
6 changes: 6 additions & 0 deletions tests/lib/net_buf/buf/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,20 @@ ZTEST(net_buf_tests, test_net_buf_1)
int i;

for (i = 0; i < bufs_pool.buf_count; i++) {
zassert_equal(bufs_pool.buf_count - i, net_buf_get_available(&bufs_pool));
/* Assertion requires that this test runs first */
zassert_equal(i, net_buf_get_max_used(&bufs_pool));
buf = net_buf_alloc_len(&bufs_pool, 74, K_NO_WAIT);
zassert_not_null(buf, "Failed to get buffer");
bufs[i] = buf;
}

for (i = 0; i < ARRAY_SIZE(bufs); i++) {
zassert_equal(i, net_buf_get_available(&bufs_pool));
zassert_equal(ARRAY_SIZE(bufs), net_buf_get_max_used(&bufs_pool));
net_buf_unref(bufs[i]);
}
zassert_equal(bufs_pool.buf_count, net_buf_get_available(&bufs_pool));

zassert_equal(destroy_called, ARRAY_SIZE(bufs),
"Incorrect destroy callback count");
Expand Down