Lines Matching full:buf

1 /* buf.c - Buffer management */
21 #include <zephyr/net/buf.h>
62 int net_buf_id(struct net_buf *buf) in net_buf_id() argument
64 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id); in net_buf_id()
67 ptrdiff_t offset = (uint8_t *)buf - (uint8_t *)pool->__bufs; in net_buf_id()
78 struct net_buf *buf; in pool_get_uninit() local
80 buf = (struct net_buf *)(((uint8_t *)pool->__bufs) + byte_offset); in pool_get_uninit()
82 buf->pool_id = pool_id(pool); in pool_get_uninit()
83 buf->user_data_size = pool->user_data_size; in pool_get_uninit()
85 return buf; in pool_get_uninit()
88 void net_buf_reset(struct net_buf *buf) in net_buf_reset() argument
90 __ASSERT_NO_MSG(buf->flags == 0U); in net_buf_reset()
91 __ASSERT_NO_MSG(buf->frags == NULL); in net_buf_reset()
93 net_buf_simple_reset(&buf->b); in net_buf_reset()
96 static uint8_t *generic_data_ref(struct net_buf *buf, uint8_t *data) in generic_data_ref() argument
106 static uint8_t *mem_pool_data_alloc(struct net_buf *buf, size_t *size, in mem_pool_data_alloc() argument
109 struct net_buf_pool *buf_pool = net_buf_pool_get(buf->pool_id); in mem_pool_data_alloc()
127 static void mem_pool_data_unref(struct net_buf *buf, uint8_t *data) in mem_pool_data_unref() argument
129 struct net_buf_pool *buf_pool = net_buf_pool_get(buf->pool_id); in mem_pool_data_unref()
148 static uint8_t *fixed_data_alloc(struct net_buf *buf, size_t *size, in fixed_data_alloc() argument
151 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id); in fixed_data_alloc()
156 return fixed->data_pool + fixed->data_size * net_buf_id(buf); in fixed_data_alloc()
159 static void fixed_data_unref(struct net_buf *buf, uint8_t *data) in fixed_data_unref() argument
171 static uint8_t *heap_data_alloc(struct net_buf *buf, size_t *size, in heap_data_alloc() argument
186 static void heap_data_unref(struct net_buf *buf, uint8_t *data) in heap_data_unref() argument
210 static uint8_t *data_alloc(struct net_buf *buf, size_t *size, k_timeout_t timeout) in data_alloc() argument
212 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id); in data_alloc()
214 return pool->alloc->cb->alloc(buf, size, timeout); in data_alloc()
217 static uint8_t *data_ref(struct net_buf *buf, uint8_t *data) in data_ref() argument
219 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id); in data_ref()
221 return pool->alloc->cb->ref(buf, data); in data_ref()
224 static void data_unref(struct net_buf *buf, uint8_t *data) in data_unref() argument
226 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id); in data_unref()
228 if (buf->flags & NET_BUF_EXTERNAL_DATA) { in data_unref()
232 pool->alloc->cb->unref(buf, data); in data_unref()
245 struct net_buf *buf; in net_buf_alloc_len_debug() local
268 buf = k_lifo_get(&pool->free, K_NO_WAIT); in net_buf_alloc_len_debug()
269 if (buf) { in net_buf_alloc_len_debug()
278 buf = pool_get_uninit(pool, uninit_count); in net_buf_alloc_len_debug()
287 buf = k_lifo_get(&pool->free, K_NO_WAIT); in net_buf_alloc_len_debug()
288 while (!buf) { in net_buf_alloc_len_debug()
296 buf = k_lifo_get(&pool->free, WARN_ALLOC_INTERVAL); in net_buf_alloc_len_debug()
308 buf = k_lifo_get(&pool->free, timeout); in net_buf_alloc_len_debug()
311 buf = k_lifo_get(&pool->free, timeout); in net_buf_alloc_len_debug()
313 if (!buf) { in net_buf_alloc_len_debug()
319 NET_BUF_DBG("allocated buf %p", buf); in net_buf_alloc_len_debug()
326 buf->__buf = data_alloc(buf, &size, timeout); in net_buf_alloc_len_debug()
327 if (!buf->__buf) { in net_buf_alloc_len_debug()
330 net_buf_destroy(buf); in net_buf_alloc_len_debug()
338 buf->__buf = NULL; in net_buf_alloc_len_debug()
341 buf->ref = 1U; in net_buf_alloc_len_debug()
342 buf->flags = 0U; in net_buf_alloc_len_debug()
343 buf->frags = NULL; in net_buf_alloc_len_debug()
344 buf->size = size; in net_buf_alloc_len_debug()
345 net_buf_reset(buf); in net_buf_alloc_len_debug()
351 return buf; in net_buf_alloc_len_debug()
385 struct net_buf *buf; in net_buf_alloc_with_data_debug() local
388 buf = net_buf_alloc_len_debug(pool, 0, timeout, func, line); in net_buf_alloc_with_data_debug()
390 buf = net_buf_alloc_len(pool, 0, timeout); in net_buf_alloc_with_data_debug()
392 if (!buf) { in net_buf_alloc_with_data_debug()
396 net_buf_simple_init_with_data(&buf->b, data, size); in net_buf_alloc_with_data_debug()
397 buf->flags = NET_BUF_EXTERNAL_DATA; in net_buf_alloc_with_data_debug()
399 return buf; in net_buf_alloc_with_data_debug()
409 struct net_buf *buf; in net_buf_get_debug() local
413 buf = k_fifo_get(fifo, timeout); in net_buf_get_debug()
414 if (!buf) { in net_buf_get_debug()
418 NET_BUF_DBG("%s():%d: buf %p fifo %p", func, line, buf, fifo); in net_buf_get_debug()
420 return buf; in net_buf_get_debug()
425 void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf) in net_buf_slist_put() argument
430 __ASSERT_NO_MSG(buf); in net_buf_slist_put()
433 sys_slist_append(list, &buf->node); in net_buf_slist_put()
439 struct net_buf *buf; in net_buf_slist_get() local
446 buf = (void *)sys_slist_get(list); in net_buf_slist_get()
450 return buf; in net_buf_slist_get()
453 void net_buf_put(struct k_fifo *fifo, struct net_buf *buf) in net_buf_put() argument
456 __ASSERT_NO_MSG(buf); in net_buf_put()
458 k_fifo_put(fifo, buf); in net_buf_put()
462 void net_buf_unref_debug(struct net_buf *buf, const char *func, int line) in net_buf_unref_debug() argument
464 void net_buf_unref(struct net_buf *buf) in net_buf_unref_debug()
467 __ASSERT_NO_MSG(buf); in net_buf_unref_debug()
469 while (buf) { in net_buf_unref_debug()
470 struct net_buf *frags = buf->frags; in net_buf_unref_debug()
474 if (!buf->ref) { in net_buf_unref_debug()
475 NET_BUF_ERR("%s():%d: buf %p double free", func, line, in net_buf_unref_debug()
476 buf); in net_buf_unref_debug()
480 NET_BUF_DBG("buf %p ref %u pool_id %u frags %p", buf, buf->ref, in net_buf_unref_debug()
481 buf->pool_id, buf->frags); in net_buf_unref_debug()
483 if (--buf->ref > 0) { in net_buf_unref_debug()
487 if (buf->__buf) { in net_buf_unref_debug()
488 data_unref(buf, buf->__buf); in net_buf_unref_debug()
489 buf->__buf = NULL; in net_buf_unref_debug()
492 buf->data = NULL; in net_buf_unref_debug()
493 buf->frags = NULL; in net_buf_unref_debug()
495 pool = net_buf_pool_get(buf->pool_id); in net_buf_unref_debug()
503 pool->destroy(buf); in net_buf_unref_debug()
505 net_buf_destroy(buf); in net_buf_unref_debug()
508 buf = frags; in net_buf_unref_debug()
512 struct net_buf *net_buf_ref(struct net_buf *buf) in net_buf_ref() argument
514 __ASSERT_NO_MSG(buf); in net_buf_ref()
516 NET_BUF_DBG("buf %p (old) ref %u pool_id %u", in net_buf_ref()
517 buf, buf->ref, buf->pool_id); in net_buf_ref()
518 buf->ref++; in net_buf_ref()
519 return buf; in net_buf_ref()
522 struct net_buf *net_buf_clone(struct net_buf *buf, k_timeout_t timeout) in net_buf_clone() argument
528 __ASSERT_NO_MSG(buf); in net_buf_clone()
530 pool = net_buf_pool_get(buf->pool_id); in net_buf_clone()
540 if (pool->alloc->cb->ref && !(buf->flags & NET_BUF_EXTERNAL_DATA)) { in net_buf_clone()
541 clone->__buf = data_ref(buf, buf->__buf); in net_buf_clone()
542 clone->data = buf->data; in net_buf_clone()
543 clone->len = buf->len; in net_buf_clone()
544 clone->size = buf->size; in net_buf_clone()
546 size_t size = buf->size; in net_buf_clone()
551 if (!clone->__buf || size < buf->size) { in net_buf_clone()
557 clone->data = clone->__buf + net_buf_headroom(buf); in net_buf_clone()
558 net_buf_add_mem(clone, buf->data, buf->len); in net_buf_clone()
564 struct net_buf *net_buf_frag_last(struct net_buf *buf) in net_buf_frag_last() argument
566 __ASSERT_NO_MSG(buf); in net_buf_frag_last()
568 while (buf->frags) { in net_buf_frag_last()
569 buf = buf->frags; in net_buf_frag_last()
572 return buf; in net_buf_frag_last()
671 size_t net_buf_append_bytes(struct net_buf *buf, size_t len, in net_buf_append_bytes() argument
675 struct net_buf *frag = net_buf_frag_last(buf); in net_buf_append_bytes()
699 pool = net_buf_pool_get(buf->pool_id); in net_buf_append_bytes()
707 net_buf_frag_add(buf, frag); in net_buf_append_bytes()