blob: 78361b5457e17feeab1de36b3374ee9f5fbfba37 [file] [log] [blame]
Dave Barachb7f1faa2017-08-29 11:43:37 -04001/*
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
Damjan Mariondae1c7e2020-10-17 13:32:25 +020040__clib_export void
Dave Barachb7f1faa2017-08-29 11:43:37 -040041_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
Dave Barach6e495ce2020-02-24 09:19:12 -050058 vector_size = pool_aligned_header_bytes + (u64) elt_size *max_elts;
Dave Barachb7f1faa2017-08-29 11:43:37 -040059 free_index_size = vec_header_bytes (0) + sizeof (u32) * max_elts;
60
61 /* Round up to a cache line boundary */
62 vector_size = (vector_size + CLIB_CACHE_LINE_BYTES - 1)
63 & ~(CLIB_CACHE_LINE_BYTES - 1);
64
65 free_index_size = (free_index_size + CLIB_CACHE_LINE_BYTES - 1)
66 & ~(CLIB_CACHE_LINE_BYTES - 1);
67
68 total_size = vector_size + free_index_size;
69
70 /* Round up to an even number of pages */
71 page_size = clib_mem_get_page_size ();
72 total_size = (total_size + page_size - 1) & ~(page_size - 1);
73
74 /* mmap demand zero memory */
75
76 mmap_base = mmap (0, total_size, PROT_READ | PROT_WRITE,
77 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
78
79 if (mmap_base == MAP_FAILED)
80 {
81 clib_unix_warning ("mmap");
82 *pool_ptr = 0;
83 }
84
85 /* First comes the pool header */
86 fh = (pool_header_t *) mmap_base;
87 /* Find the user vector pointer */
88 v = (u8 *) (mmap_base + pool_aligned_header_bytes);
89 /* Finally, the vector header */
90 vh = _vec_find (v);
91
92 fh->free_bitmap = 0; /* No free elts (yet) */
93 fh->max_elts = max_elts;
94 fh->mmap_base = mmap_base;
95 fh->mmap_size = total_size;
96
97 vh->len = max_elts;
98
99 /* Build the free-index vector */
100 vh = (vec_header_t *) (v + vector_size);
101 vh->len = max_elts;
102 fi = (u32 *) (vh + 1);
103
104 fh->free_indices = fi;
105
106 /* Set the entire free bitmap */
107 clib_bitmap_alloc (fh->free_bitmap, max_elts);
Dave Barachb7b92992018-10-17 10:38:51 -0400108 clib_memset (fh->free_bitmap, 0xff,
109 vec_len (fh->free_bitmap) * sizeof (uword));
Dave Barachb7f1faa2017-08-29 11:43:37 -0400110
111 /* Clear any extraneous set bits */
112 set_bits = vec_len (fh->free_bitmap) * BITS (uword);
113
114 for (i = max_elts; i < set_bits; i++)
115 fh->free_bitmap = clib_bitmap_set (fh->free_bitmap, i, 0);
116
117 /* Create the initial free vector */
118 for (i = 0; i < max_elts; i++)
119 fi[i] = (max_elts - 1) - i;
120
121 *pool_ptr = v;
122}
123
124/*
125 * fd.io coding-style-patch-verification: ON
126 *
127 * Local Variables:
128 * eval: (c-set-style "gnu")
129 * End:
130 */