Skip to content

Commit f8f43c4

Browse files
JordanYatesnashif
authored andcommitted
lib: net_buf: usage helper functions
Add helper functions for querying the internal fields of `struct net_buf_pool` when `CONFIG_NET_BUF_POOL_USAGE` is enabled. Signed-off-by: Jordan Yates <[email protected]>
1 parent 394b49d commit f8f43c4

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

include/zephyr/net_buf.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,40 @@ struct net_buf_pool *net_buf_pool_get(int id);
13721372
*/
13731373
int net_buf_id(const struct net_buf *buf);
13741374

1375+
#if defined(CONFIG_NET_BUF_POOL_USAGE) || defined(__DOXYGEN__)
1376+
/**
1377+
* @brief Get the number of buffers currently available to claim from a pool.
1378+
*
1379+
* Note that the number of available buffers might already have changed by the time this
1380+
* function returns if other threads are also allocating or freeing buffers from the
1381+
* pool.
1382+
*
1383+
* @kconfig_dep{CONFIG_NET_BUF_POOL_USAGE}
1384+
*
1385+
* @param pool Which pool to check
1386+
*
1387+
* @return Number of buffers currently available in the pool
1388+
*/
1389+
static inline size_t net_buf_get_available(struct net_buf_pool *pool)
1390+
{
1391+
return (size_t)atomic_get(&pool->avail_count);
1392+
}
1393+
1394+
/**
1395+
* @brief Get the maximum number of buffers simultaneously claimed from a pool.
1396+
*
1397+
* @kconfig_dep{CONFIG_NET_BUF_POOL_USAGE}
1398+
*
1399+
* @param pool Which pool to check
1400+
*
1401+
* @return Maximum number of buffers simultaneously claimed from the pool
1402+
*/
1403+
static inline size_t net_buf_get_max_used(struct net_buf_pool *pool)
1404+
{
1405+
return (size_t)pool->max_used;
1406+
}
1407+
#endif /* defined(CONFIG_NET_BUF_POOL_USAGE) || defined(__DOXYGEN__) */
1408+
13751409
/**
13761410
* @brief Allocate a new fixed buffer from a pool.
13771411
*

tests/lib/net_buf/buf/prj.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CONFIG_NET_BUF=y
2-
CONFIG_HEAP_MEM_POOL_SIZE=4096
2+
CONFIG_NET_BUF_POOL_USAGE=y
3+
CONFIG_HEAP_MEM_POOL_SIZE=2048
34
#CONFIG_NET_BUF_LOG=y
45
#CONFIG_NET_BUF_LOG_LEVEL_DBG=y
56
CONFIG_ZTEST=y

tests/lib/net_buf/buf/src/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,20 @@ ZTEST(net_buf_tests, test_net_buf_1)
146146
int i;
147147

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

154157
for (i = 0; i < ARRAY_SIZE(bufs); i++) {
158+
zassert_equal(i, net_buf_get_available(&bufs_pool));
159+
zassert_equal(ARRAY_SIZE(bufs), net_buf_get_max_used(&bufs_pool));
155160
net_buf_unref(bufs[i]);
156161
}
162+
zassert_equal(bufs_pool.buf_count, net_buf_get_available(&bufs_pool));
157163

158164
zassert_equal(destroy_called, ARRAY_SIZE(bufs),
159165
"Incorrect destroy callback count");

0 commit comments

Comments
 (0)