Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright(c) 2022 Cisco Systems, Inc. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 4 | |
| 5 | #include <vppinfra/vec.h> |
| 6 | #include <vppinfra/mem.h> |
| 7 | |
Damjan Marion | edca8c6 | 2021-04-28 17:30:51 +0200 | [diff] [blame] | 8 | #ifndef CLIB_VECTOR_GROW_BY_ONE |
| 9 | #define CLIB_VECTOR_GROW_BY_ONE 0 |
| 10 | #endif |
| 11 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 12 | __clib_export uword |
| 13 | vec_mem_size (void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 14 | { |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 15 | return v ? clib_mem_size (v - vec_get_header_size (v)) : 0; |
| 16 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 17 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 18 | __clib_export void * |
| 19 | _vec_realloc (void *v, uword n_elts, uword elt_sz, uword hdr_sz, uword align, |
| 20 | void *heap) |
| 21 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 22 | uword n_data_bytes, alloc_size, new_data_size; |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 23 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | |
Damjan Marion | 1da361f | 2022-03-16 17:57:29 +0100 | [diff] [blame] | 25 | /* alignment must be power of 2 */ |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 26 | align = clib_max (align, VEC_MIN_ALIGN); |
| 27 | ASSERT (count_set_bits (align) == 1); |
Damjan Marion | 1da361f | 2022-03-16 17:57:29 +0100 | [diff] [blame] | 28 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 29 | /* mumber of bytes needed to store vector data */ |
| 30 | n_data_bytes = n_elts * elt_sz; |
| 31 | |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 32 | if (v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | { |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 34 | uword data_offset = vec_get_header_size (v); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 35 | uword old_data_size = data_offset + _vec_len (v) * elt_sz; |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 36 | new_data_size = data_offset + n_data_bytes; |
| 37 | heap = _vec_find (v)->default_heap ? 0 : _vec_heap (v); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 38 | p = vec_header (v); |
| 39 | alloc_size = clib_mem_size (p); |
| 40 | |
| 41 | /* check that we are still dealing with the same vector type */ |
| 42 | ASSERT (_vec_find (v)->hdr_size * VEC_MIN_ALIGN == data_offset); |
| 43 | ASSERT (_vec_find (v)->log2_align == min_log2 (align)); |
| 44 | |
| 45 | /* realloc if new size cannot fit into existing allocation */ |
| 46 | if (alloc_size < new_data_size) |
| 47 | { |
| 48 | if (CLIB_VECTOR_GROW_BY_ONE) |
| 49 | alloc_size = n_data_bytes + data_offset; |
| 50 | else |
| 51 | alloc_size = (n_data_bytes * 3) / 2 + data_offset; |
| 52 | |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 53 | p = clib_mem_heap_realloc_aligned (heap, p, alloc_size, align); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 54 | alloc_size = clib_mem_size (p); |
| 55 | v = p + data_offset; |
| 56 | } |
| 57 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 58 | clib_mem_unpoison (p, alloc_size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 59 | clib_memset_u8 (p + old_data_size, 0, alloc_size - old_data_size); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | /* new allocation */ |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 64 | uword data_offset = hdr_sz + sizeof (vec_header_t); |
| 65 | data_offset += heap ? sizeof (void *) : 0; |
| 66 | data_offset = round_pow2 (data_offset, align); |
| 67 | |
| 68 | new_data_size = data_offset + n_data_bytes; |
| 69 | p = clib_mem_heap_alloc_aligned (heap, new_data_size, align); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 70 | alloc_size = clib_mem_size (p); |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 71 | clib_mem_unpoison (p, alloc_size); |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 72 | clib_memset_u8 (p, 0, alloc_size); |
| 73 | v = p + data_offset; |
| 74 | _vec_find (v)->hdr_size = data_offset / VEC_MIN_ALIGN; |
| 75 | _vec_find (v)->log2_align = min_log2 (align); |
Damjan Marion | 2473858 | 2022-03-31 15:12:20 +0200 | [diff] [blame] | 76 | if (heap) |
| 77 | { |
| 78 | _vec_find (v)->default_heap = 0; |
| 79 | _vec_heap (v) = heap; |
| 80 | } |
| 81 | else |
| 82 | _vec_find (v)->default_heap = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Damjan Marion | 79934e8 | 2022-04-05 12:40:31 +0200 | [diff] [blame] | 85 | clib_mem_poison (p + new_data_size, alloc_size - new_data_size); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 86 | _vec_find (v)->len = n_elts; |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 87 | return v; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 88 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | |
Dave Barach | 0761924 | 2020-10-18 06:54:31 -0400 | [diff] [blame] | 90 | __clib_export u32 |
Dave Barach | e09ae01 | 2020-08-19 06:59:53 -0400 | [diff] [blame] | 91 | vec_len_not_inline (void *v) |
| 92 | { |
| 93 | return vec_len (v); |
| 94 | } |
| 95 | |
Dave Barach | 0761924 | 2020-10-18 06:54:31 -0400 | [diff] [blame] | 96 | __clib_export void |
Dave Barach | e09ae01 | 2020-08-19 06:59:53 -0400 | [diff] [blame] | 97 | vec_free_not_inline (void *v) |
| 98 | { |
| 99 | vec_free (v); |
| 100 | } |