blob: 4dc8f18ce246fed911d848e6e516918167cc60f4 [file] [log] [blame]
Damjan Marion299571a2022-03-19 00:07:52 +01001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2022 Cisco Systems, Inc.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004
5#include <vppinfra/vec.h>
6#include <vppinfra/mem.h>
7
Damjan Marionedca8c62021-04-28 17:30:51 +02008#ifndef CLIB_VECTOR_GROW_BY_ONE
9#define CLIB_VECTOR_GROW_BY_ONE 0
10#endif
11
Damjan Marion299571a2022-03-19 00:07:52 +010012__clib_export uword
13vec_mem_size (void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -070014{
Damjan Marion299571a2022-03-19 00:07:52 +010015 return v ? clib_mem_size (v - vec_get_header_size (v)) : 0;
16}
Ed Warnickecb9cada2015-12-08 15:45:58 -070017
Damjan Marion299571a2022-03-19 00:07:52 +010018__clib_export void *
19_vec_realloc (void *v, uword n_elts, uword elt_sz, uword hdr_sz, uword align,
20 void *heap)
21{
Damjan Marion24738582022-03-31 15:12:20 +020022 uword n_data_bytes, alloc_size, new_data_size;
Damjan Marion299571a2022-03-19 00:07:52 +010023 void *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070024
Damjan Marion1da361f2022-03-16 17:57:29 +010025 /* alignment must be power of 2 */
Damjan Marion299571a2022-03-19 00:07:52 +010026 align = clib_max (align, VEC_MIN_ALIGN);
27 ASSERT (count_set_bits (align) == 1);
Damjan Marion1da361f2022-03-16 17:57:29 +010028
Damjan Marion299571a2022-03-19 00:07:52 +010029 /* mumber of bytes needed to store vector data */
30 n_data_bytes = n_elts * elt_sz;
31
Damjan Marion299571a2022-03-19 00:07:52 +010032 if (v)
Ed Warnickecb9cada2015-12-08 15:45:58 -070033 {
Damjan Marion24738582022-03-31 15:12:20 +020034 uword data_offset = vec_get_header_size (v);
Damjan Marion299571a2022-03-19 00:07:52 +010035 uword old_data_size = data_offset + _vec_len (v) * elt_sz;
Damjan Marion24738582022-03-31 15:12:20 +020036 new_data_size = data_offset + n_data_bytes;
37 heap = _vec_find (v)->default_heap ? 0 : _vec_heap (v);
Damjan Marion299571a2022-03-19 00:07:52 +010038 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 Marion24738582022-03-31 15:12:20 +020053 p = clib_mem_heap_realloc_aligned (heap, p, alloc_size, align);
Damjan Marion299571a2022-03-19 00:07:52 +010054 alloc_size = clib_mem_size (p);
55 v = p + data_offset;
56 }
57
Damjan Marion79934e82022-04-05 12:40:31 +020058 clib_mem_unpoison (p, alloc_size);
Damjan Marion299571a2022-03-19 00:07:52 +010059 clib_memset_u8 (p + old_data_size, 0, alloc_size - old_data_size);
60 }
61 else
62 {
63 /* new allocation */
Damjan Marion24738582022-03-31 15:12:20 +020064 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 Marion299571a2022-03-19 00:07:52 +010070 alloc_size = clib_mem_size (p);
Damjan Marion79934e82022-04-05 12:40:31 +020071 clib_mem_unpoison (p, alloc_size);
Damjan Marion299571a2022-03-19 00:07:52 +010072 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 Marion24738582022-03-31 15:12:20 +020076 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 Warnickecb9cada2015-12-08 15:45:58 -070083 }
84
Damjan Marion79934e82022-04-05 12:40:31 +020085 clib_mem_poison (p + new_data_size, alloc_size - new_data_size);
Damjan Marion8bea5892022-04-04 22:40:45 +020086 _vec_find (v)->len = n_elts;
Damjan Marion299571a2022-03-19 00:07:52 +010087 return v;
Dave Barachc3799992016-08-15 11:12:27 -040088}
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
Dave Barach07619242020-10-18 06:54:31 -040090__clib_export u32
Dave Barache09ae012020-08-19 06:59:53 -040091vec_len_not_inline (void *v)
92{
93 return vec_len (v);
94}
95
Dave Barach07619242020-10-18 06:54:31 -040096__clib_export void
Dave Barache09ae012020-08-19 06:59:53 -040097vec_free_not_inline (void *v)
98{
99 vec_free (v);
100}