Lines Matching +full:data +full:- +full:timeout

1 /* buf.c - Buffer management */
4 * Copyright (c) 2015-2019 Intel Corporation
6 * SPDX-License-Identifier: Apache-2.0
45 /* Linker-defined symbol bound to the static pool structs */
59 return pool - TYPE_SECTION_START(net_buf_pool); in pool_id()
64 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id); in net_buf_id()
65 size_t struct_size = ROUND_UP(sizeof(struct net_buf) + pool->user_data_size, in net_buf_id()
67 ptrdiff_t offset = (uint8_t *)buf - (uint8_t *)pool->__bufs; in net_buf_id()
75 size_t struct_size = ROUND_UP(sizeof(struct net_buf) + pool->user_data_size, in pool_get_uninit()
77 size_t byte_offset = (pool->buf_count - uninit_count) * struct_size; in pool_get_uninit()
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()
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
100 ref_count = data - sizeof(void *); in generic_data_ref()
103 return data; in generic_data_ref()
107 k_timeout_t timeout) 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()
110 struct k_heap *pool = buf_pool->alloc->alloc_data; in mem_pool_data_alloc()
113 /* Reserve extra space for a ref-count (uint8_t) */ in mem_pool_data_alloc()
114 void *b = k_heap_alloc(pool, sizeof(void *) + *size, timeout); 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()
130 struct k_heap *pool = buf_pool->alloc->alloc_data; in mem_pool_data_unref()
133 ref_count = data - sizeof(void *); in mem_pool_data_unref()
134 if (--(*ref_count)) { in mem_pool_data_unref()
149 k_timeout_t timeout) in fixed_data_alloc() argument
151 struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id); in fixed_data_alloc()
152 const struct net_buf_pool_fixed *fixed = pool->alloc->alloc_data; in fixed_data_alloc()
154 *size = pool->alloc->max_alloc_size; 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
161 /* Nothing needed for fixed-size data pools */ in fixed_data_unref()
172 k_timeout_t timeout) in heap_data_alloc() argument
186 static void heap_data_unref(struct net_buf *buf, uint8_t *data) in heap_data_unref() argument
190 ref_count = data - sizeof(void *); in heap_data_unref()
191 if (--(*ref_count)) { in heap_data_unref()
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()
227 k_timeout_t timeout, const char *func, in net_buf_alloc_len_debug() argument
231 k_timeout_t timeout) in net_buf_alloc_len_debug()
234 k_timepoint_t end = sys_timepoint_calc(timeout); in net_buf_alloc_len_debug()
243 * when accessing pool->uninit_count. in net_buf_alloc_len_debug()
245 key = k_spin_lock(&pool->lock); in net_buf_alloc_len_debug()
250 if (pool->uninit_count) { in net_buf_alloc_len_debug()
257 if (pool->uninit_count < pool->buf_count) { in net_buf_alloc_len_debug()
258 buf = k_lifo_get(&pool->free, K_NO_WAIT); in net_buf_alloc_len_debug()
260 k_spin_unlock(&pool->lock, key); in net_buf_alloc_len_debug()
265 uninit_count = pool->uninit_count--; in net_buf_alloc_len_debug()
266 k_spin_unlock(&pool->lock, key); in net_buf_alloc_len_debug()
272 k_spin_unlock(&pool->lock, key); in net_buf_alloc_len_debug()
274 if (!K_TIMEOUT_EQ(timeout, K_NO_WAIT) && in net_buf_alloc_len_debug()
276 LOG_WRN("Timeout discarded. No blocking in syswq"); in net_buf_alloc_len_debug()
277 timeout = K_NO_WAIT; in net_buf_alloc_len_debug()
281 if (K_TIMEOUT_EQ(timeout, K_FOREVER)) { in net_buf_alloc_len_debug()
283 buf = k_lifo_get(&pool->free, K_NO_WAIT); in net_buf_alloc_len_debug()
287 func, line, pool->name); in net_buf_alloc_len_debug()
292 buf = k_lifo_get(&pool->free, WARN_ALLOC_INTERVAL); in net_buf_alloc_len_debug()
295 func, line, pool->name, in net_buf_alloc_len_debug()
296 (k_uptime_get_32() - ref) / MSEC_PER_SEC); in net_buf_alloc_len_debug()
300 (k_uptime_get_32() - ref) / MSEC_PER_SEC); 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()
321 timeout = sys_timepoint_timeout(end); 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()
324 NET_BUF_ERR("%s():%d: Failed to allocate data", 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()
345 atomic_dec(&pool->avail_count); in net_buf_alloc_len_debug()
346 __ASSERT_NO_MSG(atomic_get(&pool->avail_count) >= 0); in net_buf_alloc_len_debug()
353 k_timeout_t timeout, const char *func, in net_buf_alloc_fixed_debug() argument
356 return net_buf_alloc_len_debug(pool, pool->alloc->max_alloc_size, timeout, func, in net_buf_alloc_fixed_debug()
361 k_timeout_t timeout) in net_buf_alloc_fixed() argument
363 return net_buf_alloc_len(pool, pool->alloc->max_alloc_size, timeout); in net_buf_alloc_fixed()
369 void *data, size_t size, in net_buf_alloc_with_data_debug() argument
370 k_timeout_t timeout, in net_buf_alloc_with_data_debug() argument
374 void *data, size_t size, in net_buf_alloc_with_data_debug()
375 k_timeout_t timeout) in net_buf_alloc_with_data_debug()
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()
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()
396 struct net_buf *net_buf_get_debug(struct k_fifo *fifo, k_timeout_t timeout, in net_buf_get_debug() argument
399 struct net_buf *net_buf_get(struct k_fifo *fifo, k_timeout_t timeout) in net_buf_get_debug()
406 buf = k_fifo_get(fifo, timeout); in net_buf_get_debug()
426 sys_slist_append(list, &buf->node); in net_buf_slist_put()
463 struct net_buf *frags = buf->frags; in net_buf_unref_debug()
467 if (!buf->ref) { 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()
486 atomic_inc(&pool->avail_count); in net_buf_unref_debug()
487 __ASSERT_NO_MSG(atomic_get(&pool->avail_count) <= pool->buf_count); in net_buf_unref_debug()
490 if (pool->destroy) { in net_buf_unref_debug()
491 pool->destroy(buf); in net_buf_unref_debug()
505 buf, buf->ref, buf->pool_id); in net_buf_ref()
506 buf->ref++; in net_buf_ref()
510 struct net_buf *net_buf_clone(struct net_buf *buf, k_timeout_t timeout) in net_buf_clone() argument
512 k_timepoint_t end = sys_timepoint_calc(timeout); in net_buf_clone()
518 pool = net_buf_pool_get(buf->pool_id); in net_buf_clone()
520 clone = net_buf_alloc_len(pool, 0, timeout); in net_buf_clone()
525 /* If the pool supports data referencing use that. Otherwise in net_buf_clone()
526 * we need to allocate new data and make a copy. 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()
536 timeout = sys_timepoint_timeout(end); in net_buf_clone()
538 clone->__buf = data_alloc(clone, &size, timeout); in net_buf_clone()
539 if (!clone->__buf || size < buf->size) { in net_buf_clone()
544 clone->size = 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()
566 if (dst->user_data_size < src->user_data_size) { in net_buf_user_data_copy()
567 return -EINVAL; in net_buf_user_data_copy()
570 memcpy(dst->user_data, src->user_data, src->user_data_size); in net_buf_user_data_copy()
579 while (buf->frags) { in net_buf_frag_last()
580 buf = buf->frags; in net_buf_frag_last()
591 if (parent->frags) { in net_buf_frag_insert()
592 net_buf_frag_last(frag)->frags = parent->frags; in net_buf_frag_insert()
595 parent->frags = frag; in net_buf_frag_insert()
624 __ASSERT_NO_MSG(parent->frags); in net_buf_frag_del_debug()
625 __ASSERT_NO_MSG(parent->frags == frag); in net_buf_frag_del_debug()
626 parent->frags = frag->frags; in net_buf_frag_del_debug()
629 next_frag = frag->frags; in net_buf_frag_del_debug()
631 frag->frags = NULL; in net_buf_frag_del_debug()
654 while (frag && offset >= frag->len) { in net_buf_linearize()
655 offset -= frag->len; in net_buf_linearize()
656 frag = frag->frags; in net_buf_linearize()
662 to_copy = MIN(len, frag->len - offset); in net_buf_linearize()
663 memcpy((uint8_t *)dst + copied, frag->data + offset, to_copy); in net_buf_linearize()
668 len -= to_copy; in net_buf_linearize()
669 frag = frag->frags; in net_buf_linearize()
679 * the data in current fragment then create new fragment and add it to
683 const void *value, k_timeout_t timeout, in net_buf_append_bytes() argument
695 len -= count; in net_buf_append_bytes()
704 frag = allocate_cb(timeout, user_data); in net_buf_append_bytes()
711 pool = net_buf_pool_get(buf->pool_id); in net_buf_append_bytes()
712 max_size = pool->alloc->max_alloc_size; in net_buf_append_bytes()
715 timeout); 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
731 const uint8_t *dptr = data; in net_buf_data_match()
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()
747 bptr = buf->data + offset; in net_buf_data_match()
748 to_compare = MIN(len, buf->len - offset); in net_buf_data_match()
757 len -= to_compare; in net_buf_data_match()
758 buf = buf->frags; in net_buf_data_match()