Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 43 | void * |
| 44 | vec_resize_allocate_memory (void *v, |
| 45 | word length_increment, |
| 46 | uword data_bytes, |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 47 | uword header_bytes, uword data_align, |
| 48 | uword numa_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 50 | vec_header_t *vh = _vec_find (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | uword old_alloc_bytes, new_alloc_bytes; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 52 | void *old, *new; |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 53 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | |
| 55 | header_bytes = vec_header_bytes (header_bytes); |
| 56 | |
| 57 | data_bytes += header_bytes; |
| 58 | |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 59 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 65 | if (!v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | { |
Dave Barach | 241e522 | 2016-10-13 10:53:26 -0400 | [diff] [blame] | 67 | new = clib_mem_alloc_aligned_at_offset (data_bytes, data_align, header_bytes, 1 /* yes, call os_out_of_memory */ |
| 68 | ); |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 69 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | v = new + header_bytes; |
| 74 | _vec_len (v) = length_increment; |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 75 | _vec_numa (v) = numa_id; |
| 76 | if (PREDICT_FALSE (numa_id != VEC_NUMA_UNSPECIFIED)) |
| 77 | clib_mem_set_per_cpu_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | 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 Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 91 | { |
| 92 | CLIB_MEM_UNPOISON (v, data_bytes); |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 93 | if (PREDICT_FALSE (numa_id != VEC_NUMA_UNSPECIFIED)) |
| 94 | clib_mem_set_per_cpu_heap (oldheap); |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 95 | return v; |
| 96 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | |
Dave Barach | 98bd757 | 2020-02-10 10:16:40 -0500 | [diff] [blame] | 98 | #if CLIB_VECTOR_GROW_BY_ONE > 0 |
| 99 | new_alloc_bytes = data_bytes; |
| 100 | #else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | new_alloc_bytes = (old_alloc_bytes * 3) / 2; |
| 102 | if (new_alloc_bytes < data_bytes) |
| 103 | new_alloc_bytes = data_bytes; |
Dave Barach | 98bd757 | 2020-02-10 10:16:40 -0500 | [diff] [blame] | 104 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 106 | new = |
| 107 | clib_mem_alloc_aligned_at_offset (new_alloc_bytes, data_align, |
Dave Barach | 241e522 | 2016-10-13 10:53:26 -0400 | [diff] [blame] | 108 | header_bytes, |
| 109 | 1 /* yes, call os_out_of_memory */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
| 111 | /* FIXME fail gracefully. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 112 | if (!new) |
| 113 | clib_panic |
| 114 | ("vec_resize fails, length increment %d, data bytes %d, alignment %d", |
| 115 | length_increment, data_bytes, data_align); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 117 | CLIB_MEM_UNPOISON (old, old_alloc_bytes); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 118 | clib_memcpy_fast (new, old, old_alloc_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | clib_mem_free (old); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
| 121 | /* Allocator may give a bit of extra room. */ |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 122 | new_alloc_bytes = clib_mem_size (new); |
| 123 | v = new; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | |
| 125 | /* Zero new memory. */ |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 126 | CLIB_MEM_UNPOISON (new + data_bytes, new_alloc_bytes - data_bytes); |
Florin Coras | 9e041b1 | 2019-03-20 23:16:15 -0700 | [diff] [blame] | 127 | memset (v + old_alloc_bytes, 0, new_alloc_bytes - old_alloc_bytes); |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 128 | CLIB_MEM_POISON (new + data_bytes, new_alloc_bytes - data_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Dave Barach | a690fdb | 2020-01-21 12:34:55 -0500 | [diff] [blame] | 130 | _vec_numa ((v + header_bytes)) = numa_id; |
| 131 | if (PREDICT_FALSE (numa_id != VEC_NUMA_UNSPECIFIED)) |
| 132 | clib_mem_set_per_cpu_heap (oldheap); |
| 133 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | return v + header_bytes; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 135 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 137 | uword |
| 138 | clib_mem_is_vec_h (void *v, uword header_bytes) |
| 139 | { |
| 140 | return clib_mem_is_heap_object (vec_header (v, header_bytes)); |
| 141 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | |
| 143 | /** \cond */ |
| 144 | |
| 145 | #ifdef TEST |
| 146 | |
| 147 | #include <stdio.h> |
| 148 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 149 | void |
| 150 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | { |
| 152 | word n = atoi (argv[1]); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 153 | word i, *x = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 155 | typedef struct |
| 156 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | word x, y, z; |
| 158 | } FOO; |
| 159 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 160 | FOO *foos = vec_init (FOO, 10), *f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | |
| 162 | vec_validate (foos, 100); |
| 163 | foos[100].x = 99; |
| 164 | |
| 165 | _vec_len (foos) = 0; |
| 166 | for (i = 0; i < n; i++) |
| 167 | { |
| 168 | vec_add1 (x, i); |
| 169 | vec_add2 (foos, f, 1); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 170 | f->x = 2 * i; |
| 171 | f->y = 3 * i; |
| 172 | f->z = 4 * i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | { |
| 176 | word n = 2; |
| 177 | word m = 42; |
| 178 | vec_delete (foos, n, m); |
| 179 | } |
| 180 | |
| 181 | { |
| 182 | word n = 2; |
| 183 | word m = 42; |
| 184 | vec_insert (foos, n, m); |
| 185 | } |
| 186 | |
| 187 | vec_free (x); |
| 188 | vec_free (foos); |
| 189 | exit (0); |
| 190 | } |
| 191 | #endif |
| 192 | /** \endcond */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * fd.io coding-style-patch-verification: ON |
| 196 | * |
| 197 | * Local Variables: |
| 198 | * eval: (c-set-style "gnu") |
| 199 | * End: |
| 200 | */ |