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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef included_ring_h
17 #define included_ring_h
19 #include <vppinfra/error.h>
20 #include <vppinfra/format.h>
21 #include <vppinfra/vec.h>
22 #include <vppinfra/vector.h>
29 always_inline clib_ring_header_t *
30 clib_ring_header (void *v)
32 return vec_header (v);
36 clib_ring_reset (void *v)
38 clib_ring_header_t *h = clib_ring_header (v);
44 clib_ring_new_inline (void **p, u32 elt_bytes, u32 size, u32 align)
47 vec_attr_t va = { .elt_sz = elt_bytes,
48 .hdr_sz = sizeof (clib_ring_header_t),
51 v = _vec_alloc_internal (size, &va);
57 #define clib_ring_new_aligned(ring, size, align) \
58 { clib_ring_new_inline ((void **)&(ring), sizeof(ring[0]), size, align); }
60 #define clib_ring_new(ring, size) \
61 { clib_ring_new_inline ((void **)&(ring), sizeof(ring[0]), size, 0);}
63 #define clib_ring_free(f) vec_free ((f))
66 clib_ring_n_enq (void *v)
68 clib_ring_header_t *h = clib_ring_header (v);
73 clib_ring_get_last_inline (void *v, u32 elt_bytes, int enqueue)
75 clib_ring_header_t *h = clib_ring_header (v);
80 if (h->n_enq == _vec_len (v))
85 if (h->next == _vec_len (v))
92 slot = h->next == 0 ? _vec_len (v) - 1 : h->next - 1;
95 return (void *) ((u8 *) v + elt_bytes * slot);
98 #define clib_ring_enq(ring) \
99 clib_ring_get_last_inline (ring, sizeof(ring[0]), 1)
101 #define clib_ring_get_last(ring) \
102 clib_ring_get_last_inline (ring, sizeof(ring[0]), 0)
105 clib_ring_get_first_inline (void *v, u32 elt_bytes, int dequeue)
107 clib_ring_header_t *h = clib_ring_header (v);
113 if (h->n_enq > h->next)
114 slot = _vec_len (v) + h->next - h->n_enq;
116 slot = h->next - h->n_enq;
121 return (void *) ((u8 *) v + elt_bytes * slot);
124 #define clib_ring_deq(ring) \
125 clib_ring_get_first_inline (ring, sizeof(ring[0]), 1)
127 #define clib_ring_get_first(ring) \
128 clib_ring_get_first_inline (ring, sizeof(ring[0]), 0)
130 #endif /* included_ring_h */
133 * fd.io coding-style-patch-verification: ON
136 * eval: (c-set-style "gnu")