FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
ring.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 #ifndef included_ring_h
17 #define included_ring_h
18 
19 #include <vppinfra/error.h>
20 #include <vppinfra/format.h>
21 #include <vppinfra/vec.h>
22 #include <vppinfra/vector.h>
23 
24 typedef struct
25 {
26  u32 next, n_enq;
28 
31 {
32  return vec_aligned_header (v, sizeof (clib_ring_header_t), sizeof (void *));
33 }
34 
35 always_inline void
36 clib_ring_new_inline (void **p, u32 elt_bytes, u32 size, u32 align)
37 {
38  void *v;
40 
41  v = _vec_resize ((void *) 0,
42  /* length increment */ size,
43  /* data bytes */ elt_bytes * size,
44  /* header bytes */ sizeof (h[0]),
45  /* data align */ align);
46 
47  h = clib_ring_header (v);
48  h->next = 0;
49  h->n_enq = 0;
50  p[0] = v;
51 }
52 
53 #define clib_ring_new_aligned(ring, size, align) \
54 { clib_ring_new_inline ((void **)&(ring), sizeof(ring[0]), size, align); }
55 
56 #define clib_ring_new(ring, size) \
57 { clib_ring_new_inline ((void **)&(ring), sizeof(ring[0]), size, 0);}
58 
59 #define clib_ring_free(f) vec_free_h((f), sizeof(clib_ring_header_t))
60 
62 clib_ring_n_enq (void *v)
63 {
65  return h->n_enq;
66 }
67 
68 always_inline void *
69 clib_ring_get_last_inline (void *v, u32 elt_bytes, int enqueue)
70 {
72  u32 slot;
73 
74  if (enqueue)
75  {
76  if (h->n_enq == _vec_len (v))
77  return 0;
78  slot = h->next;
79  h->n_enq++;
80  h->next++;
81  if (h->next == _vec_len (v))
82  h->next = 0;
83  }
84  else
85  {
86  if (h->n_enq == 0)
87  return 0;
88  slot = h->next == 0 ? _vec_len (v) - 1 : h->next - 1;
89  }
90 
91  return (void *) ((u8 *) v + elt_bytes * slot);
92 }
93 
94 #define clib_ring_enq(ring) \
95 clib_ring_get_last_inline (ring, sizeof(ring[0]), 1)
96 
97 #define clib_ring_get_last(ring) \
98 clib_ring_get_last_inline (ring, sizeof(ring[0]), 0)
99 
100 always_inline void *
101 clib_ring_get_first_inline (void *v, u32 elt_bytes, int dequeue)
102 {
104  u32 slot;
105 
106  if (h->n_enq == 0)
107  return 0;
108 
109  if (h->n_enq > h->next)
110  slot = _vec_len (v) + h->next - h->n_enq;
111  else
112  slot = h->next - h->n_enq;
113 
114  if (dequeue)
115  h->n_enq--;
116 
117  return (void *) ((u8 *) v + elt_bytes * slot);
118 }
119 
120 #define clib_ring_deq(ring) \
121 clib_ring_get_first_inline (ring, sizeof(ring[0]), 1)
122 
123 #define clib_ring_get_first(ring) \
124 clib_ring_get_first_inline (ring, sizeof(ring[0]), 0)
125 
126 #endif /* included_ring_h */
127 
128 /*
129  * fd.io coding-style-patch-verification: ON
130  *
131  * Local Variables:
132  * eval: (c-set-style "gnu")
133  * End:
134  */
unsigned char u8
Definition: types.h:56
unsigned int u32
Definition: types.h:88
u64 size
Definition: vhost_user.h:140
#define always_inline
Definition: ipsec.h:28
u8 slot
Definition: pci_types.api:22
static void * vec_aligned_header(void *v, uword header_bytes, uword align)
static u32 clib_ring_n_enq(void *v)
Definition: ring.h:62
static void clib_ring_new_inline(void **p, u32 elt_bytes, u32 size, u32 align)
Definition: ring.h:36
static clib_ring_header_t * clib_ring_header(void *v)
Definition: ring.h:30
static void * clib_ring_get_first_inline(void *v, u32 elt_bytes, int dequeue)
Definition: ring.h:101
static void * clib_ring_get_last_inline(void *v, u32 elt_bytes, int enqueue)
Definition: ring.h:69
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".