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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 Copyright (c) 2001, 2002, 2003, 2004 Eliot Dresselhaus
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:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
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.
38 #include <vppinfra/pool.h>
41 _pool_init_fixed (void **pool_ptr, u32 elt_size, u32 max_elts)
58 vector_size = pool_aligned_header_bytes + (u64) elt_size *max_elts;
59 free_index_size = vec_header_bytes (0) + sizeof (u32) * max_elts;
61 /* Round up to a cache line boundary */
62 vector_size = (vector_size + CLIB_CACHE_LINE_BYTES - 1)
63 & ~(CLIB_CACHE_LINE_BYTES - 1);
65 free_index_size = (free_index_size + CLIB_CACHE_LINE_BYTES - 1)
66 & ~(CLIB_CACHE_LINE_BYTES - 1);
68 total_size = vector_size + free_index_size;
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);
74 /* mmap demand zero memory */
76 mmap_base = mmap (0, total_size, PROT_READ | PROT_WRITE,
77 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
79 if (mmap_base == MAP_FAILED)
81 clib_unix_warning ("mmap");
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 */
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;
99 /* Build the free-index vector */
100 vh = (vec_header_t *) (v + vector_size);
102 fi = (u32 *) (vh + 1);
104 fh->free_indices = fi;
106 /* Set the entire free bitmap */
107 clib_bitmap_alloc (fh->free_bitmap, max_elts);
108 clib_memset (fh->free_bitmap, 0xff,
109 vec_len (fh->free_bitmap) * sizeof (uword));
111 /* Clear any extraneous set bits */
112 set_bits = vec_len (fh->free_bitmap) * BITS (uword);
114 for (i = max_elts; i < set_bits; i++)
115 fh->free_bitmap = clib_bitmap_set (fh->free_bitmap, i, 0);
117 /* Create the initial free vector */
118 for (i = 0; i < max_elts; i++)
119 fi[i] = (max_elts - 1) - i;
125 * fd.io coding-style-patch-verification: ON
128 * eval: (c-set-style "gnu")