blob: 2ee78952d19af96653621c85ee1f4285461bd3d5 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
17
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37
38#include <vppinfra/vec.h>
39#include <vppinfra/mem.h>
40
41/* Vector resize operator. Called as needed by various macros such as
42 vec_add1() when we need to allocate memory. */
Dave Barachc3799992016-08-15 11:12:27 -040043void *
44vec_resize_allocate_memory (void *v,
45 word length_increment,
46 uword data_bytes,
Dave Baracha690fdb2020-01-21 12:34:55 -050047 uword header_bytes, uword data_align,
48 uword numa_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -070049{
Dave Barachc3799992016-08-15 11:12:27 -040050 vec_header_t *vh = _vec_find (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 uword old_alloc_bytes, new_alloc_bytes;
Dave Barachc3799992016-08-15 11:12:27 -040052 void *old, *new;
Dave Baracha690fdb2020-01-21 12:34:55 -050053 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
55 header_bytes = vec_header_bytes (header_bytes);
56
57 data_bytes += header_bytes;
58
Dave Baracha690fdb2020-01-21 12:34:55 -050059 if (PREDICT_FALSE (numa_id != VEC_NUMA_UNSPECIFIED))
60 {
61 oldheap = clib_mem_get_per_cpu_heap ();
62 clib_mem_set_per_cpu_heap (clib_mem_get_per_numa_heap (numa_id));
63 }
64
Dave Barachc3799992016-08-15 11:12:27 -040065 if (!v)
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 {
Dave Barach241e5222016-10-13 10:53:26 -040067 new = clib_mem_alloc_aligned_at_offset (data_bytes, data_align, header_bytes, 1 /* yes, call os_out_of_memory */
68 );
Benoît Ganne9fb6d402019-04-15 15:28:21 +020069 new_alloc_bytes = clib_mem_size (new);
70 CLIB_MEM_UNPOISON (new + data_bytes, new_alloc_bytes - data_bytes);
71 clib_memset (new, 0, new_alloc_bytes);
72 CLIB_MEM_POISON (new + data_bytes, new_alloc_bytes - data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 v = new + header_bytes;
74 _vec_len (v) = length_increment;
Dave Baracha690fdb2020-01-21 12:34:55 -050075 _vec_numa (v) = numa_id;
76 if (PREDICT_FALSE (numa_id != VEC_NUMA_UNSPECIFIED))
77 clib_mem_set_per_cpu_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 return v;
79 }
80
81 vh->len += length_increment;
82 old = v - header_bytes;
83
84 /* Vector header must start heap object. */
85 ASSERT (clib_mem_is_heap_object (old));
86
87 old_alloc_bytes = clib_mem_size (old);
88
89 /* Need to resize? */
90 if (data_bytes <= old_alloc_bytes)
Benoît Ganne9fb6d402019-04-15 15:28:21 +020091 {
92 CLIB_MEM_UNPOISON (v, data_bytes);
Dave Baracha690fdb2020-01-21 12:34:55 -050093 if (PREDICT_FALSE (numa_id != VEC_NUMA_UNSPECIFIED))
94 clib_mem_set_per_cpu_heap (oldheap);
Benoît Ganne9fb6d402019-04-15 15:28:21 +020095 return v;
96 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
98 new_alloc_bytes = (old_alloc_bytes * 3) / 2;
99 if (new_alloc_bytes < data_bytes)
100 new_alloc_bytes = data_bytes;
101
Dave Barachc3799992016-08-15 11:12:27 -0400102 new =
103 clib_mem_alloc_aligned_at_offset (new_alloc_bytes, data_align,
Dave Barach241e5222016-10-13 10:53:26 -0400104 header_bytes,
105 1 /* yes, call os_out_of_memory */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
107 /* FIXME fail gracefully. */
Dave Barachc3799992016-08-15 11:12:27 -0400108 if (!new)
109 clib_panic
110 ("vec_resize fails, length increment %d, data bytes %d, alignment %d",
111 length_increment, data_bytes, data_align);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200113 CLIB_MEM_UNPOISON (old, old_alloc_bytes);
Dave Barach178cf492018-11-13 16:34:13 -0500114 clib_memcpy_fast (new, old, old_alloc_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 clib_mem_free (old);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
117 /* Allocator may give a bit of extra room. */
Dave Barach6a5adc32018-07-04 10:56:23 -0400118 new_alloc_bytes = clib_mem_size (new);
119 v = new;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120
121 /* Zero new memory. */
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200122 CLIB_MEM_UNPOISON (new + data_bytes, new_alloc_bytes - data_bytes);
Florin Coras9e041b12019-03-20 23:16:15 -0700123 memset (v + old_alloc_bytes, 0, new_alloc_bytes - old_alloc_bytes);
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200124 CLIB_MEM_POISON (new + data_bytes, new_alloc_bytes - data_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
Dave Baracha690fdb2020-01-21 12:34:55 -0500126 _vec_numa ((v + header_bytes)) = numa_id;
127 if (PREDICT_FALSE (numa_id != VEC_NUMA_UNSPECIFIED))
128 clib_mem_set_per_cpu_heap (oldheap);
129
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 return v + header_bytes;
Dave Barachc3799992016-08-15 11:12:27 -0400131}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132
Dave Barachc3799992016-08-15 11:12:27 -0400133uword
134clib_mem_is_vec_h (void *v, uword header_bytes)
135{
136 return clib_mem_is_heap_object (vec_header (v, header_bytes));
137}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
139/** \cond */
140
141#ifdef TEST
142
143#include <stdio.h>
144
Dave Barachc3799992016-08-15 11:12:27 -0400145void
146main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147{
148 word n = atoi (argv[1]);
Dave Barachc3799992016-08-15 11:12:27 -0400149 word i, *x = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Dave Barachc3799992016-08-15 11:12:27 -0400151 typedef struct
152 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 word x, y, z;
154 } FOO;
155
Dave Barachc3799992016-08-15 11:12:27 -0400156 FOO *foos = vec_init (FOO, 10), *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
158 vec_validate (foos, 100);
159 foos[100].x = 99;
160
161 _vec_len (foos) = 0;
162 for (i = 0; i < n; i++)
163 {
164 vec_add1 (x, i);
165 vec_add2 (foos, f, 1);
Dave Barachc3799992016-08-15 11:12:27 -0400166 f->x = 2 * i;
167 f->y = 3 * i;
168 f->z = 4 * i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 }
170
171 {
172 word n = 2;
173 word m = 42;
174 vec_delete (foos, n, m);
175 }
176
177 {
178 word n = 2;
179 word m = 42;
180 vec_insert (foos, n, m);
181 }
182
183 vec_free (x);
184 vec_free (foos);
185 exit (0);
186}
187#endif
188/** \endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400189
190/*
191 * fd.io coding-style-patch-verification: ON
192 *
193 * Local Variables:
194 * eval: (c-set-style "gnu")
195 * End:
196 */