Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 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 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 40 | __clib_export void |
Damjan Marion | 299571a | 2022-03-19 00:07:52 +0100 | [diff] [blame] | 41 | _pool_init_fixed (void **pool_ptr, uword elt_size, uword max_elts, uword align) |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 42 | { |
Damjan Marion | 86bbdf9 | 2022-03-18 12:28:35 +0100 | [diff] [blame] | 43 | uword *b; |
| 44 | pool_header_t *ph; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 45 | u8 *v; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 46 | u32 i; |
Damjan Marion | e4fa1d2 | 2022-04-11 18:41:49 +0200 | [diff] [blame] | 47 | vec_attr_t va = { .elt_sz = elt_size, |
| 48 | .align = align, |
| 49 | .hdr_sz = sizeof (pool_header_t) }; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 50 | |
| 51 | ASSERT (elt_size); |
| 52 | ASSERT (max_elts); |
| 53 | |
Damjan Marion | e4fa1d2 | 2022-04-11 18:41:49 +0200 | [diff] [blame] | 54 | v = _vec_alloc_internal (max_elts, &va); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 55 | |
Damjan Marion | 86bbdf9 | 2022-03-18 12:28:35 +0100 | [diff] [blame] | 56 | ph = pool_header (v); |
| 57 | ph->max_elts = max_elts; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 58 | |
| 59 | /* Build the free-index vector */ |
Damjan Marion | 86bbdf9 | 2022-03-18 12:28:35 +0100 | [diff] [blame] | 60 | vec_validate_aligned (ph->free_indices, max_elts - 1, CLIB_CACHE_LINE_BYTES); |
| 61 | for (i = 0; i < max_elts; i++) |
| 62 | ph->free_indices[i] = (max_elts - 1) - i; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 63 | |
| 64 | /* Set the entire free bitmap */ |
Damjan Marion | 86bbdf9 | 2022-03-18 12:28:35 +0100 | [diff] [blame] | 65 | clib_bitmap_alloc (ph->free_bitmap, max_elts); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 66 | |
Damjan Marion | 86bbdf9 | 2022-03-18 12:28:35 +0100 | [diff] [blame] | 67 | for (b = ph->free_bitmap, i = max_elts; i >= uword_bits; |
| 68 | i -= uword_bits, b++) |
| 69 | b[0] = ~0ULL; |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 70 | |
Damjan Marion | 86bbdf9 | 2022-03-18 12:28:35 +0100 | [diff] [blame] | 71 | if (i) |
| 72 | b[0] = pow2_mask (i); |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 73 | |
| 74 | *pool_ptr = v; |
| 75 | } |
| 76 | |