Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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) 2012 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/smp_fifo.h> |
| 39 | #include <vppinfra/mem.h> |
| 40 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 41 | clib_smp_fifo_t * |
| 42 | clib_smp_fifo_init (uword max_n_elts, uword n_bytes_per_elt) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 44 | clib_smp_fifo_t *f; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | uword n_bytes_per_elt_cache_aligned; |
| 46 | |
| 47 | f = clib_mem_alloc_aligned (sizeof (f[0]), CLIB_CACHE_LINE_BYTES); |
| 48 | |
| 49 | memset (f, 0, sizeof (f[0])); |
| 50 | |
| 51 | max_n_elts = max_n_elts ? max_n_elts : 32; |
| 52 | f->log2_max_n_elts = max_log2 (max_n_elts); |
| 53 | f->max_n_elts_less_one = (1 << f->log2_max_n_elts) - 1; |
| 54 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 55 | n_bytes_per_elt_cache_aligned = |
| 56 | clib_smp_fifo_round_elt_bytes (n_bytes_per_elt); |
| 57 | clib_exec_on_global_heap ( |
| 58 | { |
| 59 | f->data = |
| 60 | clib_mem_alloc_aligned |
| 61 | (n_bytes_per_elt_cache_aligned << |
| 62 | f->log2_max_n_elts, CLIB_CACHE_LINE_BYTES);} |
| 63 | ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | |
| 65 | /* Zero all data and mark all elements free. */ |
| 66 | { |
| 67 | uword i; |
| 68 | for (i = 0; i <= f->max_n_elts_less_one; i++) |
| 69 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 70 | void *d = clib_smp_fifo_elt_at_index (f, n_bytes_per_elt, i); |
| 71 | clib_smp_fifo_data_footer_t *t; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | |
| 73 | memset (d, 0, n_bytes_per_elt_cache_aligned); |
| 74 | |
| 75 | t = clib_smp_fifo_get_data_footer (d, n_bytes_per_elt); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 76 | clib_smp_fifo_data_footer_set_state (t, |
| 77 | CLIB_SMP_FIFO_DATA_STATE_free); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | return f; |
| 82 | } |
| 83 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * fd.io coding-style-patch-verification: ON |
| 87 | * |
| 88 | * Local Variables: |
| 89 | * eval: (c-set-style "gnu") |
| 90 | * End: |
| 91 | */ |