blob: ae25e40b5c0721711e174cab44b53202a14ec4d1 [file] [log] [blame]
Damjan Marionaed75f32018-12-28 15:46:10 +01001/*
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
24typedef struct
25{
26 u32 next, n_enq;
27} clib_ring_header_t;
28
29always_inline clib_ring_header_t *
30clib_ring_header (void *v)
31{
Damjan Mariona4a28f02022-03-17 15:46:25 +010032 return vec_header (v);
Damjan Marionaed75f32018-12-28 15:46:10 +010033}
34
35always_inline void
Damjan Marion47447f12023-10-17 16:08:18 +000036clib_ring_reset (void *v)
37{
38 clib_ring_header_t *h = clib_ring_header (v);
39 h->next = 0;
40 h->n_enq = 0;
41}
42
43always_inline void
Damjan Marionaed75f32018-12-28 15:46:10 +010044clib_ring_new_inline (void **p, u32 elt_bytes, u32 size, u32 align)
45{
46 void *v;
Damjan Marione4fa1d22022-04-11 18:41:49 +020047 vec_attr_t va = { .elt_sz = elt_bytes,
48 .hdr_sz = sizeof (clib_ring_header_t),
49 .align = align };
Damjan Marionaed75f32018-12-28 15:46:10 +010050
Damjan Marione4fa1d22022-04-11 18:41:49 +020051 v = _vec_alloc_internal (size, &va);
Damjan Marionaed75f32018-12-28 15:46:10 +010052
Damjan Marion47447f12023-10-17 16:08:18 +000053 clib_ring_reset (v);
Damjan Marionaed75f32018-12-28 15:46:10 +010054 p[0] = v;
55}
56
57#define clib_ring_new_aligned(ring, size, align) \
58{ clib_ring_new_inline ((void **)&(ring), sizeof(ring[0]), size, align); }
59
60#define clib_ring_new(ring, size) \
61{ clib_ring_new_inline ((void **)&(ring), sizeof(ring[0]), size, 0);}
62
Damjan Marion05563c92022-03-17 18:29:32 +010063#define clib_ring_free(f) vec_free ((f))
Damjan Marionaed75f32018-12-28 15:46:10 +010064
65always_inline u32
66clib_ring_n_enq (void *v)
67{
68 clib_ring_header_t *h = clib_ring_header (v);
69 return h->n_enq;
70}
71
72always_inline void *
73clib_ring_get_last_inline (void *v, u32 elt_bytes, int enqueue)
74{
75 clib_ring_header_t *h = clib_ring_header (v);
76 u32 slot;
77
78 if (enqueue)
79 {
80 if (h->n_enq == _vec_len (v))
81 return 0;
82 slot = h->next;
83 h->n_enq++;
84 h->next++;
85 if (h->next == _vec_len (v))
86 h->next = 0;
87 }
88 else
89 {
90 if (h->n_enq == 0)
91 return 0;
92 slot = h->next == 0 ? _vec_len (v) - 1 : h->next - 1;
93 }
94
95 return (void *) ((u8 *) v + elt_bytes * slot);
96}
97
98#define clib_ring_enq(ring) \
99clib_ring_get_last_inline (ring, sizeof(ring[0]), 1)
100
101#define clib_ring_get_last(ring) \
102clib_ring_get_last_inline (ring, sizeof(ring[0]), 0)
103
104always_inline void *
105clib_ring_get_first_inline (void *v, u32 elt_bytes, int dequeue)
106{
107 clib_ring_header_t *h = clib_ring_header (v);
108 u32 slot;
109
110 if (h->n_enq == 0)
111 return 0;
112
113 if (h->n_enq > h->next)
114 slot = _vec_len (v) + h->next - h->n_enq;
115 else
116 slot = h->next - h->n_enq;
117
118 if (dequeue)
119 h->n_enq--;
120
121 return (void *) ((u8 *) v + elt_bytes * slot);
122}
123
124#define clib_ring_deq(ring) \
125clib_ring_get_first_inline (ring, sizeof(ring[0]), 1)
126
127#define clib_ring_get_first(ring) \
128clib_ring_get_first_inline (ring, sizeof(ring[0]), 0)
129
130#endif /* included_ring_h */
131
132/*
133 * fd.io coding-style-patch-verification: ON
134 *
135 * Local Variables:
136 * eval: (c-set-style "gnu")
137 * End:
138 */