Annotation of libelwix/inc/elwix/aring.h, revision 1.6.2.1
1.2 misho 1: /*************************************************************************
2: * (C) 2025 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.6.2.1 ! misho 6: * $Id: aring.h,v 1.6 2026/02/11 13:36:09 misho Exp $
1.2 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.5 misho 15: Copyright 2004 - 2026
1.2 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #ifndef __ARING_H
47: #define __ARING_H
48:
1.3 misho 49: #define E_ATOMIC_ALIGN alignas(sizeof(int) * 8)
1.2 misho 50:
51: typedef struct {
1.3 misho 52: E_ATOMIC_ALIGN int rb_head;
53: E_ATOMIC_ALIGN int rb_tail;
54: int rb_bufnum;
55: struct iovec *rb_buffer;
1.2 misho 56: } ringbuf_t;
57:
1.5 misho 58: typedef struct {
59: E_ATOMIC_ALIGN unsigned int lrb_head;
60: E_ATOMIC_ALIGN unsigned int lrb_tail;
61: E_ATOMIC_ALIGN unsigned int lrb_full;
62: int lrb_size;
63: unsigned char *lrb_data;
64: } lrbuf_t;
1.6.2.1 ! misho 65: #define lrb_head(x) atomic_load_explicit((atomic_int*) &(x)->lrb_head, memory_order_relaxed)
! 66: #define lrb_tail(x) atomic_load_explicit((atomic_int*) &(x)->lrb_tail, memory_order_relaxed)
! 67: #define lrb_size(x) ((x)->lrb_size)
! 68: #define lrb_getb(x) ((x)->lrb_data)
! 69: #define lrb_hptr(x) ((x)->lrb_data + lrb_head((x)))
! 70: #define lrb_tptr(x) ((x)->lrb_data + lrb_tail((x)))
! 71: #define lrb_isrewind(x) (lrb_head((x)) < lrb_tail((x)))
1.5 misho 72: #define lrb_queued(x, r) do { \
73: u_int _h, _t; \
74: _t = atomic_load_explicit((atomic_int*) &(x)->lrb_tail, memory_order_acquire); \
75: _h = atomic_load_explicit((atomic_int*) &(x)->lrb_head, memory_order_relaxed); \
76: if (_h == _t) \
77: (r) = atomic_load_explicit((atomic_int*) &(x)->lrb_full, memory_order_acquire) ? \
78: (x)->lrb_size : 0; \
79: else \
80: (r) = (_h + (x)->lrb_size - _t) % (x)->lrb_size; \
81: } while (0)
82: #define lrb_unused(x, r) do { \
83: u_int _r; \
84: lrb_queued((x), _r); \
85: (r) = (x)->lrb_size - _r; \
86: } while (0)
87:
1.2 misho 88:
89: /*
90: * rbuf_init() - Init ring buffer
91: *
92: * @rbuf = Ring buffer
93: * @num = Number of elements in buffer
94: * return: -1 error or 0 ok
95: */
96: int rbuf_init(ringbuf_t *rbuf, int num);
97: /*
98: * rbuf_free() - Free ring buffer
99: *
100: * @rbuf = Ring buffer
101: * return: none
102: */
103: void rbuf_free(ringbuf_t *rbuf);
104: /*
105: * rbuf_purge() - Purge all buffer
106: *
107: * @rbuf = Ring buffer
108: * return: none
109: */
110: void rbuf_purge(ringbuf_t *rbuf);
111: /*
112: * rbuf_isempty() - Check buffer is empty
113: *
114: * @rbuf = Ring buffer
115: * return: -1 error, 0 it isn't empty
116: */
117: int rbuf_isempty(ringbuf_t *rbuf);
118: /*
119: * rbuf_isfull() - Check buffer is full
120: *
121: * @rbuf = Ring buffer
122: * return: -1 error or 0 it isn't full
123: */
124: int rbuf_isfull(ringbuf_t *rbuf);
125:
126: /*
127: * rbuf_enqueue() - Enqueue data to buffer
128: *
129: * @rbuf = Ring buffer
130: * @data = Data
131: * @len = Length
132: * return: -1 error, 1 can't add data, buffer is full or 0 ok
133: */
134: int rbuf_enqueue(ringbuf_t *rbuf, void *data, size_t len);
135: /*
136: * rbuf_dequeue() - Dequeue data from buffer
137: *
138: * @rbuf = Ring buffer
139: * @out = Data, if =NULL, just dequeue data
140: * return: -1 error, 1 buffer is empty or 0 ok
141: */
1.4 misho 142: int rbuf_dequeue(ringbuf_t *rbuf, struct iovec **out);
1.2 misho 143:
144:
1.5 misho 145: /*
146: * lrb_init() - Init linear ring buffer
147: *
148: * @lrb = Linear ring buffer
149: * @size = Size of ring buffer
150: * return: -1 error or 0 ok
151: */
152: int lrb_init(lrbuf_t *lrb, u_int size);
153: /*
154: * lrb_free() - Free linear ring buffer
155: *
156: * @lrb = Linear ring buffer
157: * return: none
158: */
159: void lrb_free(lrbuf_t *lrb);
160: /*
161: * lrb_purge() - Purge all buffer
162: *
163: * @lrb = Linear ring buffer
164: * return: none
165: */
166: void lrb_purge(lrbuf_t *lrb);
167: /*
168: * lrb_isempty() - Check buffer is empty
169: *
170: * @lrb = Linear ring buffer
171: * return: -1 error, 0 it isn't empty
172: */
173: int lrb_isempty(lrbuf_t *lrb);
174: /*
175: * lrb_isfull() - Check buffer is full
176: *
177: * @lrb = Linear ring buffer
178: * return: -1 error or 0 it isn't full
179: */
180: int lrb_isfull(lrbuf_t *lrb);
181: /*
182: * lrb_enqueue() - Enqueue data to buffer
183: *
184: * @lrb = Linear ring buffer
185: * @data = Data
186: * @len = Length
187: * @lost = Permit to lost data
188: * return: -1 error, 1 buffer is full or 0 ok
189: */
190: int lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int lost);
191: /*
192: * lrb_dequeue() - Dequeue data from buffer
193: *
194: * @lrb = Linear ring buffer
195: * @data = Data, if =NULL, just dequeue data
196: * @len = Length of data
197: * return: -1 error, 0 buffer is empty or >0 stored data bytes
198: */
199: int lrb_dequeue(lrbuf_t *lrb, void *data, size_t len);
1.6 misho 200: /*
201: * lrb_getw() - Get address for write
202: *
203: * @lrb = Linear ring buffer
204: * @len = Return available buffer length for write
205: * return: NULL error or !=NULL pointer for write
206: * remark: After use of lrb_getw() and write to pointer.
207: * You should update ring buffer with lrb_enqueue(,NULL,wrote_len,)
208: */
209: void *lrb_getw(lrbuf_t *lrb, size_t *len);
210: /*
211: * lrb_getr() - Get address for read
212: *
213: * @lrb = Linear ring buffer
214: * @len = Return available data length for read
215: * return: NULL error or !=NULL pointer for read
216: * remark: After use of lrb_getr() and read from pointer.
217: * You could update ring buffer with lrb_dequeue(,NULL,read_len)
218: */
219: void *lrb_getr(lrbuf_t *lrb, size_t *len);
1.5 misho 220:
221:
1.2 misho 222: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>