FD.io VPP  v16.06
Vector Packet Processing
smp_fifo.c
Go to the documentation of this file.
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 
41 clib_smp_fifo_t * clib_smp_fifo_init (uword max_n_elts, uword n_bytes_per_elt)
42 {
43  clib_smp_fifo_t * f;
44  uword n_bytes_per_elt_cache_aligned;
45 
46  f = clib_mem_alloc_aligned (sizeof (f[0]), CLIB_CACHE_LINE_BYTES);
47 
48  memset (f, 0, sizeof (f[0]));
49 
50  max_n_elts = max_n_elts ? max_n_elts : 32;
51  f->log2_max_n_elts = max_log2 (max_n_elts);
52  f->max_n_elts_less_one = (1 << f->log2_max_n_elts) - 1;
53 
54  n_bytes_per_elt_cache_aligned = clib_smp_fifo_round_elt_bytes (n_bytes_per_elt);
56  f->data = clib_mem_alloc_aligned (n_bytes_per_elt_cache_aligned << f->log2_max_n_elts,
58  });
59 
60  /* Zero all data and mark all elements free. */
61  {
62  uword i;
63  for (i = 0; i <= f->max_n_elts_less_one; i++)
64  {
65  void * d = clib_smp_fifo_elt_at_index (f, n_bytes_per_elt, i);
67 
68  memset (d, 0, n_bytes_per_elt_cache_aligned);
69 
70  t = clib_smp_fifo_get_data_footer (d, n_bytes_per_elt);
71  clib_smp_fifo_data_footer_set_state (t, CLIB_SMP_FIFO_DATA_STATE_free);
72  }
73  }
74 
75  return f;
76 }
77 
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
always_inline void * clib_smp_fifo_elt_at_index(clib_smp_fifo_t *f, uword n_bytes_per_elt, uword i)
Definition: smp_fifo.h:121
always_inline uword max_log2(uword x)
Definition: clib.h:216
always_inline void clib_smp_fifo_data_footer_set_state(clib_smp_fifo_data_footer_t *f, clib_smp_fifo_data_state_t s)
Definition: smp_fifo.h:77
always_inline void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:113
#define clib_exec_on_global_heap(body)
Definition: smp.h:325
void * data
Definition: smp_fifo.h:93
clib_smp_fifo_t * clib_smp_fifo_init(uword max_n_elts, uword n_bytes_per_elt)
Definition: smp_fifo.c:41
u32 log2_max_n_elts
Definition: smp_fifo.h:90
always_inline uword clib_smp_fifo_round_elt_bytes(uword n_bytes_per_elt)
Definition: smp_fifo.h:101
u64 uword
Definition: types.h:112
always_inline clib_smp_fifo_data_footer_t * clib_smp_fifo_get_data_footer(void *d, uword n_bytes_per_elt)
Definition: smp_fifo.h:113
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 max_n_elts_less_one
Definition: smp_fifo.h:87