diff --git a/include/zephyr/net/buf.h b/include/zephyr/net/buf.h index 6e7f8b5b2a..8b6ebdcafd 100644 --- a/include/zephyr/net/buf.h +++ b/include/zephyr/net/buf.h @@ -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, \ diff --git a/subsys/net/buf.c b/subsys/net/buf.c index 0ea9c0d387..7a82c83ddb 100644 --- a/subsys/net/buf.c +++ b/subsys/net/buf.c @@ -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) {