Lines Matching full:buf
1 /* buf.c - Buffer management */
62 int net_buf_id(const 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 + *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
211 static uint8_t *data_alloc(struct net_buf *buf, size_t *size, k_timeout_t timeout) in data_alloc() argument
213 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id); in data_alloc()
215 return pool->alloc->cb->alloc(buf, size, timeout); in data_alloc()
218 static uint8_t *data_ref(struct net_buf *buf, uint8_t *data) in data_ref() argument
220 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id); in data_ref()
222 return pool->alloc->cb->ref(buf, data); in data_ref()
235 struct net_buf *buf; in net_buf_alloc_len_debug() local
258 buf = k_lifo_get(&pool->free, K_NO_WAIT); in net_buf_alloc_len_debug()
259 if (buf) { in net_buf_alloc_len_debug()
268 buf = pool_get_uninit(pool, uninit_count); in net_buf_alloc_len_debug()
283 buf = k_lifo_get(&pool->free, K_NO_WAIT); in net_buf_alloc_len_debug()
284 while (!buf) { in net_buf_alloc_len_debug()
292 buf = k_lifo_get(&pool->free, WARN_ALLOC_INTERVAL); in net_buf_alloc_len_debug()
304 buf = k_lifo_get(&pool->free, timeout); in net_buf_alloc_len_debug()
307 buf = k_lifo_get(&pool->free, timeout); in net_buf_alloc_len_debug()
309 if (!buf) { in net_buf_alloc_len_debug()
315 NET_BUF_DBG("allocated buf %p", buf); in net_buf_alloc_len_debug()
322 buf->__buf = data_alloc(buf, &size, timeout); in net_buf_alloc_len_debug()
323 if (!buf->__buf) { in net_buf_alloc_len_debug()
326 net_buf_destroy(buf); in net_buf_alloc_len_debug()
334 buf->__buf = NULL; in net_buf_alloc_len_debug()
337 buf->ref = 1U; in net_buf_alloc_len_debug()
338 buf->flags = 0U; in net_buf_alloc_len_debug()
339 buf->frags = NULL; in net_buf_alloc_len_debug()
340 buf->size = size; in net_buf_alloc_len_debug()
341 memset(buf->user_data, 0, buf->user_data_size); in net_buf_alloc_len_debug()
342 net_buf_reset(buf); in net_buf_alloc_len_debug()
348 return buf; in net_buf_alloc_len_debug()
378 struct net_buf *buf; in net_buf_alloc_with_data_debug() local
381 buf = net_buf_alloc_len_debug(pool, 0, timeout, func, line); in net_buf_alloc_with_data_debug()
383 buf = net_buf_alloc_len(pool, 0, timeout); in net_buf_alloc_with_data_debug()
385 if (!buf) { in net_buf_alloc_with_data_debug()
389 net_buf_simple_init_with_data(&buf->b, data, size); in net_buf_alloc_with_data_debug()
390 buf->flags = NET_BUF_EXTERNAL_DATA; in net_buf_alloc_with_data_debug()
392 return buf; in net_buf_alloc_with_data_debug()
402 struct net_buf *buf; in net_buf_get_debug() local
406 buf = k_fifo_get(fifo, timeout); in net_buf_get_debug()
407 if (!buf) { in net_buf_get_debug()
411 NET_BUF_DBG("%s():%d: buf %p fifo %p", func, line, buf, fifo); in net_buf_get_debug()
413 return buf; in net_buf_get_debug()
418 void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf) in net_buf_slist_put() argument
423 __ASSERT_NO_MSG(buf); in net_buf_slist_put()
426 sys_slist_append(list, &buf->node); in net_buf_slist_put()
432 struct net_buf *buf; in net_buf_slist_get() local
439 buf = (void *)sys_slist_get(list); in net_buf_slist_get()
443 return buf; in net_buf_slist_get()
446 void net_buf_put(struct k_fifo *fifo, struct net_buf *buf) in net_buf_put() argument
449 __ASSERT_NO_MSG(buf); in net_buf_put()
451 k_fifo_put(fifo, buf); in net_buf_put()
455 void net_buf_unref_debug(struct net_buf *buf, const char *func, int line) in net_buf_unref_debug() argument
457 void net_buf_unref(struct net_buf *buf) in net_buf_unref_debug()
460 __ASSERT_NO_MSG(buf); in net_buf_unref_debug()
462 while (buf) { in net_buf_unref_debug()
463 struct net_buf *frags = buf->frags; in net_buf_unref_debug()
467 if (!buf->ref) { in net_buf_unref_debug()
468 NET_BUF_ERR("%s():%d: buf %p double free", func, line, in net_buf_unref_debug()
469 buf); in net_buf_unref_debug()
473 NET_BUF_DBG("buf %p ref %u pool_id %u frags %p", buf, buf->ref, in net_buf_unref_debug()
474 buf->pool_id, buf->frags); in net_buf_unref_debug()
476 if (--buf->ref > 0) { in net_buf_unref_debug()
480 buf->data = NULL; in net_buf_unref_debug()
481 buf->frags = NULL; in net_buf_unref_debug()
483 pool = net_buf_pool_get(buf->pool_id); in net_buf_unref_debug()
491 pool->destroy(buf); in net_buf_unref_debug()
493 net_buf_destroy(buf); in net_buf_unref_debug()
496 buf = frags; in net_buf_unref_debug()
500 struct net_buf *net_buf_ref(struct net_buf *buf) in net_buf_ref() argument
502 __ASSERT_NO_MSG(buf); in net_buf_ref()
504 NET_BUF_DBG("buf %p (old) ref %u pool_id %u", in net_buf_ref()
505 buf, buf->ref, buf->pool_id); in net_buf_ref()
506 buf->ref++; in net_buf_ref()
507 return buf; in net_buf_ref()
510 struct net_buf *net_buf_clone(struct net_buf *buf, k_timeout_t timeout) in net_buf_clone() argument
516 __ASSERT_NO_MSG(buf); in net_buf_clone()
518 pool = net_buf_pool_get(buf->pool_id); in net_buf_clone()
528 if (pool->alloc->cb->ref && !(buf->flags & NET_BUF_EXTERNAL_DATA)) { in net_buf_clone()
529 clone->__buf = buf->__buf ? data_ref(buf, buf->__buf) : NULL; in net_buf_clone()
530 clone->data = buf->data; in net_buf_clone()
531 clone->len = buf->len; in net_buf_clone()
532 clone->size = buf->size; in net_buf_clone()
534 size_t size = buf->size; in net_buf_clone()
539 if (!clone->__buf || size < buf->size) { in net_buf_clone()
545 clone->data = clone->__buf + net_buf_headroom(buf); in net_buf_clone()
546 net_buf_add_mem(clone, buf->data, buf->len); in net_buf_clone()
550 __ASSERT(buf->user_data_size == clone->user_data_size, "Unexpected user data size"); in net_buf_clone()
552 memcpy(clone->user_data, buf->user_data, clone->user_data_size); in net_buf_clone()
575 struct net_buf *net_buf_frag_last(struct net_buf *buf) in net_buf_frag_last() argument
577 __ASSERT_NO_MSG(buf); in net_buf_frag_last()
579 while (buf->frags) { in net_buf_frag_last()
580 buf = buf->frags; in net_buf_frag_last()
583 return buf; in net_buf_frag_last()
682 size_t net_buf_append_bytes(struct net_buf *buf, size_t len, in net_buf_append_bytes() argument
686 struct net_buf *frag = net_buf_frag_last(buf); in net_buf_append_bytes()
711 pool = net_buf_pool_get(buf->pool_id); in net_buf_append_bytes()
722 net_buf_frag_add(buf, frag); in net_buf_append_bytes()
729 size_t net_buf_data_match(const struct net_buf *buf, size_t offset, const void *data, size_t len) in net_buf_data_match() argument
736 if (!buf || !data) { in net_buf_data_match()
741 while (buf && offset >= buf->len) { in net_buf_data_match()
742 offset -= buf->len; in net_buf_data_match()
743 buf = buf->frags; in net_buf_data_match()
746 while (buf && len > 0) { in net_buf_data_match()
747 bptr = buf->data + offset; in net_buf_data_match()
748 to_compare = MIN(len, buf->len - offset); in net_buf_data_match()
758 buf = buf->frags; in net_buf_data_match()