FD.io VPP  v16.06
Vector Packet Processing
vec.c
Go to the documentation of this file.
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. */
43 void * vec_resize_allocate_memory (void * v,
44  word length_increment,
45  uword data_bytes,
46  uword header_bytes,
47  uword data_align)
48 {
49  vec_header_t * vh = _vec_find (v);
50  uword old_alloc_bytes, new_alloc_bytes;
51  void * old, * new;
52 
53  header_bytes = vec_header_bytes (header_bytes);
54 
55  data_bytes += header_bytes;
56 
57  if (! v)
58  {
59  new = clib_mem_alloc_aligned_at_offset (data_bytes, data_align, header_bytes);
60  data_bytes = clib_mem_size (new);
61  memset (new, 0, data_bytes);
62  v = new + header_bytes;
63  _vec_len (v) = length_increment;
64  return v;
65  }
66 
67  vh->len += length_increment;
68  old = v - header_bytes;
69 
70  /* Vector header must start heap object. */
72 
73  old_alloc_bytes = clib_mem_size (old);
74 
75  /* Need to resize? */
76  if (data_bytes <= old_alloc_bytes)
77  return v;
78 
79  new_alloc_bytes = (old_alloc_bytes * 3) / 2;
80  if (new_alloc_bytes < data_bytes)
81  new_alloc_bytes = data_bytes;
82 
83  new = clib_mem_alloc_aligned_at_offset (new_alloc_bytes, data_align, header_bytes);
84 
85  /* FIXME fail gracefully. */
86  if (! new)
87  clib_panic ("vec_resize fails, length increment %d, data bytes %d, alignment %d",
88  length_increment, data_bytes, data_align);
89 
90  clib_memcpy (new, old, old_alloc_bytes);
91  clib_mem_free (old);
92  v = new;
93 
94  /* Allocator may give a bit of extra room. */
95  new_alloc_bytes = clib_mem_size (v);
96 
97  /* Zero new memory. */
98  memset (v + old_alloc_bytes, 0, new_alloc_bytes - old_alloc_bytes);
99 
100  return v + header_bytes;
101 }
102 
103 uword clib_mem_is_vec_h (void * v, uword header_bytes)
104 { return clib_mem_is_heap_object (vec_header (v, header_bytes)); }
105 
106 /** \cond */
107 
108 #ifdef TEST
109 
110 #include <stdio.h>
111 
112 void main (int argc, char * argv[])
113 {
114  word n = atoi (argv[1]);
115  word i, * x = 0;
116 
117  typedef struct {
118  word x, y, z;
119  } FOO;
120 
121  FOO * foos = vec_init (FOO, 10), * f;
122 
123  vec_validate (foos, 100);
124  foos[100].x = 99;
125 
126  _vec_len (foos) = 0;
127  for (i = 0; i < n; i++)
128  {
129  vec_add1 (x, i);
130  vec_add2 (foos, f, 1);
131  f->x = 2*i; f->y = 3*i; f->z = 4*i;
132  }
133 
134  {
135  word n = 2;
136  word m = 42;
137  vec_delete (foos, n, m);
138  }
139 
140  {
141  word n = 2;
142  word m = 42;
143  vec_insert (foos, n, m);
144  }
145 
146  vec_free (x);
147  vec_free (foos);
148  exit (0);
149 }
150 #endif
151 /** \endcond */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:394
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
void * vec_resize_allocate_memory(void *v, word length_increment, uword data_bytes, uword header_bytes, uword data_align)
Low-level resize allocation function, usually not called directly.
Definition: vec.c:43
always_inline void * clib_mem_alloc_aligned_at_offset(uword size, uword align, uword align_offset)
Definition: mem.h:70
always_inline void clib_mem_free(void *p)
Definition: mem.h:149
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:519
add_epi add_epi sub_epi sub_epi adds_epu subs_epu i16x8 y
Definition: vector_sse2.h:231
uword clib_mem_is_vec_h(void *v, uword header_bytes)
Predicate function, says whether the supplied vector is a clib heap object (general version)...
Definition: vec.c:103
always_inline uword clib_mem_size(void *p)
Definition: mem.h:180
#define vec_insert(V, N, M)
Insert N vector elements starting at element M, initialize new elements to zero (no header...
Definition: vec.h:644
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
#define clib_memcpy(a, b, c)
Definition: string.h:63
u32 len
Number of elements in vector (NOT its allocated length).
Definition: vec_bootstrap.h:59
#define ASSERT(truth)
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:743
vector header structure
Definition: vec_bootstrap.h:55
u64 uword
Definition: types.h:112
i64 word
Definition: types.h:111
always_inline uword clib_mem_is_heap_object(void *p)
Definition: mem.h:133
always_inline void * vec_header(void *v, uword header_bytes)
Find a user vector header.
Definition: vec_bootstrap.h:88
#define clib_panic(format, args...)
Definition: error.h:72
int main(int argc, char *argv[])
Definition: elftool.c:369
always_inline uword vec_header_bytes(uword header_bytes)
Definition: vec_bootstrap.h:78
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".