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, |
| 47 | uword header_bytes, uword data_align) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 49 | vec_header_t *vh = _vec_find (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | uword old_alloc_bytes, new_alloc_bytes; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 51 | void *old, *new; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | |
| 53 | header_bytes = vec_header_bytes (header_bytes); |
| 54 | |
| 55 | data_bytes += header_bytes; |
| 56 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 57 | if (!v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | { |
Dave Barach | 241e522 | 2016-10-13 10:53:26 -0400 | [diff] [blame] | 59 | new = clib_mem_alloc_aligned_at_offset (data_bytes, data_align, header_bytes, 1 /* yes, call os_out_of_memory */ |
| 60 | ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | data_bytes = clib_mem_size (new); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 62 | clib_memset (new, 0, data_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | v = new + header_bytes; |
| 64 | _vec_len (v) = length_increment; |
| 65 | return v; |
| 66 | } |
| 67 | |
| 68 | vh->len += length_increment; |
| 69 | old = v - header_bytes; |
| 70 | |
| 71 | /* Vector header must start heap object. */ |
| 72 | ASSERT (clib_mem_is_heap_object (old)); |
| 73 | |
| 74 | old_alloc_bytes = clib_mem_size (old); |
| 75 | |
| 76 | /* Need to resize? */ |
| 77 | if (data_bytes <= old_alloc_bytes) |
| 78 | return v; |
| 79 | |
| 80 | new_alloc_bytes = (old_alloc_bytes * 3) / 2; |
| 81 | if (new_alloc_bytes < data_bytes) |
| 82 | new_alloc_bytes = data_bytes; |
| 83 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 84 | new = |
| 85 | clib_mem_alloc_aligned_at_offset (new_alloc_bytes, data_align, |
Dave Barach | 241e522 | 2016-10-13 10:53:26 -0400 | [diff] [blame] | 86 | header_bytes, |
| 87 | 1 /* yes, call os_out_of_memory */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | |
| 89 | /* FIXME fail gracefully. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 90 | if (!new) |
| 91 | clib_panic |
| 92 | ("vec_resize fails, length increment %d, data bytes %d, alignment %d", |
| 93 | length_increment, data_bytes, data_align); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 95 | clib_memcpy (new, old, old_alloc_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | clib_mem_free (old); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | |
| 98 | /* Allocator may give a bit of extra room. */ |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 99 | new_alloc_bytes = clib_mem_size (new); |
| 100 | v = new; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | |
| 102 | /* Zero new memory. */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 103 | clib_memset (v + old_alloc_bytes, 0, new_alloc_bytes - old_alloc_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
| 105 | return v + header_bytes; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 106 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 108 | uword |
| 109 | clib_mem_is_vec_h (void *v, uword header_bytes) |
| 110 | { |
| 111 | return clib_mem_is_heap_object (vec_header (v, header_bytes)); |
| 112 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | |
| 114 | /** \cond */ |
| 115 | |
| 116 | #ifdef TEST |
| 117 | |
| 118 | #include <stdio.h> |
| 119 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 120 | void |
| 121 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | { |
| 123 | word n = atoi (argv[1]); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 124 | word i, *x = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 126 | typedef struct |
| 127 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | word x, y, z; |
| 129 | } FOO; |
| 130 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 131 | FOO *foos = vec_init (FOO, 10), *f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | |
| 133 | vec_validate (foos, 100); |
| 134 | foos[100].x = 99; |
| 135 | |
| 136 | _vec_len (foos) = 0; |
| 137 | for (i = 0; i < n; i++) |
| 138 | { |
| 139 | vec_add1 (x, i); |
| 140 | vec_add2 (foos, f, 1); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 141 | f->x = 2 * i; |
| 142 | f->y = 3 * i; |
| 143 | f->z = 4 * i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | { |
| 147 | word n = 2; |
| 148 | word m = 42; |
| 149 | vec_delete (foos, n, m); |
| 150 | } |
| 151 | |
| 152 | { |
| 153 | word n = 2; |
| 154 | word m = 42; |
| 155 | vec_insert (foos, n, m); |
| 156 | } |
| 157 | |
| 158 | vec_free (x); |
| 159 | vec_free (foos); |
| 160 | exit (0); |
| 161 | } |
| 162 | #endif |
| 163 | /** \endcond */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * fd.io coding-style-patch-verification: ON |
| 167 | * |
| 168 | * Local Variables: |
| 169 | * eval: (c-set-style "gnu") |
| 170 | * End: |
| 171 | */ |