FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
pool.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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, 2004 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/pool.h>
39 
40 void
41 _pool_init_fixed (void **pool_ptr, u32 elt_size, u32 max_elts)
42 {
43  u8 *mmap_base;
44  u64 vector_size;
45  u64 free_index_size;
46  u64 total_size;
47  u64 page_size;
48  pool_header_t *fh;
49  vec_header_t *vh;
50  u8 *v;
51  u32 *fi;
52  u32 i;
53  u32 set_bits;
54 
55  ASSERT (elt_size);
56  ASSERT (max_elts);
57 
59  + (u64) elt_size *max_elts;
60 
61  free_index_size = vec_header_bytes (0) + sizeof (u32) * max_elts;
62 
63  /* Round up to a cache line boundary */
64  vector_size = (vector_size + CLIB_CACHE_LINE_BYTES - 1)
65  & ~(CLIB_CACHE_LINE_BYTES - 1);
66 
67  free_index_size = (free_index_size + CLIB_CACHE_LINE_BYTES - 1)
68  & ~(CLIB_CACHE_LINE_BYTES - 1);
69 
70  total_size = vector_size + free_index_size;
71 
72  /* Round up to an even number of pages */
73  page_size = clib_mem_get_page_size ();
74  total_size = (total_size + page_size - 1) & ~(page_size - 1);
75 
76  /* mmap demand zero memory */
77 
78  mmap_base = mmap (0, total_size, PROT_READ | PROT_WRITE,
79  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
80 
81  if (mmap_base == MAP_FAILED)
82  {
83  clib_unix_warning ("mmap");
84  *pool_ptr = 0;
85  }
86 
87  /* First comes the pool header */
88  fh = (pool_header_t *) mmap_base;
89  /* Find the user vector pointer */
90  v = (u8 *) (mmap_base + pool_aligned_header_bytes);
91  /* Finally, the vector header */
92  vh = _vec_find (v);
93 
94  fh->free_bitmap = 0; /* No free elts (yet) */
95  fh->max_elts = max_elts;
96  fh->mmap_base = mmap_base;
97  fh->mmap_size = total_size;
98 
99  vh->len = max_elts;
100 
101  /* Build the free-index vector */
102  vh = (vec_header_t *) (v + vector_size);
103  vh->len = max_elts;
104  fi = (u32 *) (vh + 1);
105 
106  fh->free_indices = fi;
107 
108  /* Set the entire free bitmap */
109  clib_bitmap_alloc (fh->free_bitmap, max_elts);
110  memset (fh->free_bitmap, 0xff, vec_len (fh->free_bitmap) * sizeof (uword));
111 
112  /* Clear any extraneous set bits */
113  set_bits = vec_len (fh->free_bitmap) * BITS (uword);
114 
115  for (i = max_elts; i < set_bits; i++)
116  fh->free_bitmap = clib_bitmap_set (fh->free_bitmap, i, 0);
117 
118  /* Create the initial free vector */
119  for (i = 0; i < max_elts; i++)
120  fi[i] = (max_elts - 1) - i;
121 
122  *pool_ptr = v;
123 }
124 
125 /*
126  * fd.io coding-style-patch-verification: ON
127  *
128  * Local Variables:
129  * eval: (c-set-style "gnu")
130  * End:
131  */
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
u64 mmap_size
Definition: pool.h:67
Fixed length block allocator.
u32 * free_indices
Vector of free indices.
Definition: pool.h:58
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
Definition: bitmap.h:167
static uword vec_header_bytes(uword header_bytes)
Definition: vec_bootstrap.h:79
unsigned long u64
Definition: types.h:89
#define v
Definition: acl.c:341
#define clib_bitmap_alloc(v, n_bits)
Allocate a bitmap with the supplied number of bits.
Definition: bitmap.h:109
#define pool_aligned_header_bytes
Align pool header so that pointers are naturally aligned.
Definition: pool.h:72
u32 len
Number of elements in vector (NOT its allocated length).
Definition: vec_bootstrap.h:60
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
vector header structure
Definition: vec_bootstrap.h:55
u64 uword
Definition: types.h:112
uword * free_bitmap
Bitmap of indices of free objects.
Definition: pool.h:55
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
#define clib_unix_warning(format, args...)
Definition: error.h:68
u32 max_elts
Maximum size of the pool, in elements.
Definition: pool.h:63
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
uword clib_mem_get_page_size(void)
Definition: mem_mheap.c:110
#define BITS(x)
Definition: clib.h:58
u8 * mmap_base
mmap segment info: base + length
Definition: pool.h:66