net: buf: add max allocation size to allocation info

Previously, there was no way to determine maximum number of bytes
that can be allocated using only net_buf structure. This commit
introduces such field.

Moreover, this commit fixes an issue where allocation of less than
maximum number of bytes from a fixed buffer pool would set buffer's
size to this number instead of the whole buffer size.

Signed-off-by: Konrad Derda <konrad.derda@nordicsemi.no>
This commit is contained in:
Konrad Derda 2024-02-01 14:08:05 +01:00 committed by Henrik Brix Andersen
parent 57cd012476
commit b324e1e4d8
2 changed files with 13 additions and 11 deletions

View file

@ -965,6 +965,7 @@ struct net_buf_data_cb {
struct net_buf_data_alloc {
const struct net_buf_data_cb *cb;
void *alloc_data;
size_t max_alloc_size;
};
/**
@ -1078,7 +1079,6 @@ extern const struct net_buf_data_alloc net_buf_heap_alloc;
_destroy)
struct net_buf_pool_fixed {
size_t data_size;
uint8_t *data_pool;
};
@ -1118,12 +1118,12 @@ extern const struct net_buf_data_cb net_buf_fixed_cb;
_NET_BUF_ARRAY_DEFINE(_name, _count, _ud_size); \
static uint8_t __noinit net_buf_data_##_name[_count][_data_size] __net_buf_align; \
static const struct net_buf_pool_fixed net_buf_fixed_##_name = { \
.data_size = _data_size, \
.data_pool = (uint8_t *)net_buf_data_##_name, \
}; \
static const struct net_buf_data_alloc net_buf_fixed_alloc_##_name = { \
.cb = &net_buf_fixed_cb, \
.alloc_data = (void *)&net_buf_fixed_##_name, \
.max_alloc_size = _data_size, \
}; \
static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
NET_BUF_POOL_INITIALIZER(_name, &net_buf_fixed_alloc_##_name, \
@ -1164,6 +1164,7 @@ extern const struct net_buf_data_cb net_buf_var_cb;
static const struct net_buf_data_alloc net_buf_data_alloc_##_name = { \
.cb = &net_buf_var_cb, \
.alloc_data = &net_buf_mem_pool_##_name, \
.max_alloc_size = 0, \
}; \
static STRUCT_SECTION_ITERABLE(net_buf_pool, _name) = \
NET_BUF_POOL_INITIALIZER(_name, &net_buf_data_alloc_##_name, \

View file

@ -151,9 +151,9 @@ static uint8_t *fixed_data_alloc(struct net_buf *buf, size_t *size,
struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
const struct net_buf_pool_fixed *fixed = pool->alloc->alloc_data;
*size = MIN(fixed->data_size, *size);
*size = pool->alloc->max_alloc_size;
return fixed->data_pool + fixed->data_size * net_buf_id(buf);
return fixed->data_pool + *size * net_buf_id(buf);
}
static void fixed_data_unref(struct net_buf *buf, uint8_t *data)
@ -203,6 +203,7 @@ static const struct net_buf_data_cb net_buf_heap_cb = {
const struct net_buf_data_alloc net_buf_heap_alloc = {
.cb = &net_buf_heap_cb,
.max_alloc_size = 0,
};
#endif /* K_HEAP_MEM_POOL_SIZE > 0 */
@ -345,18 +346,14 @@ struct net_buf *net_buf_alloc_fixed_debug(struct net_buf_pool *pool,
k_timeout_t timeout, const char *func,
int line)
{
const struct net_buf_pool_fixed *fixed = pool->alloc->alloc_data;
return net_buf_alloc_len_debug(pool, fixed->data_size, timeout, func,
return net_buf_alloc_len_debug(pool, pool->alloc->max_alloc_size, timeout, func,
line);
}
#else
struct net_buf *net_buf_alloc_fixed(struct net_buf_pool *pool,
k_timeout_t timeout)
{
const struct net_buf_pool_fixed *fixed = pool->alloc->alloc_data;
return net_buf_alloc_len(pool, fixed->data_size, timeout);
return net_buf_alloc_len(pool, pool->alloc->max_alloc_size, timeout);
}
#endif
@ -659,6 +656,7 @@ size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
struct net_buf *frag = net_buf_frag_last(buf);
size_t added_len = 0;
const uint8_t *value8 = value;
size_t max_size;
do {
uint16_t count = MIN(len, net_buf_tailroom(frag));
@ -681,7 +679,10 @@ size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
* been provided.
*/
pool = net_buf_pool_get(buf->pool_id);
frag = net_buf_alloc_len(pool, len, timeout);
max_size = pool->alloc->max_alloc_size;
frag = net_buf_alloc_len(pool,
max_size ? MIN(len, max_size) : len,
timeout);
}
if (!frag) {