Annotation of libelwix/inc/elwix/aring.h, revision 1.6.2.2

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.2 ! misho       6: * $Id: aring.h,v 1.6.2.1 2026/02/13 17:09:32 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_hptr(x)            ((x)->lrb_data + lrb_head((x)))
                     68: #define lrb_tptr(x)            ((x)->lrb_data + lrb_tail((x)))
1.6.2.2 ! misho      69: #define lrb_ptr(x, n)          ((x)->lrb_data + ((lrb_tail((x)) + (n)) % (x)->lrb_size))
        !            70: #define lrb_size(x)            ((x)->lrb_size)
        !            71: #define lrb_getb(x)            ((x)->lrb_data)
        !            72: #define lrb_getc(x, n)         *lrb_ptr((x), (n))
1.6.2.1   misho      73: #define lrb_isrewind(x)                (lrb_head((x)) < lrb_tail((x)))
1.5       misho      74: #define lrb_queued(x, r)       do { \
                     75:                                        u_int _h, _t; \
                     76:                                        _t = atomic_load_explicit((atomic_int*) &(x)->lrb_tail, memory_order_acquire); \
                     77:                                        _h = atomic_load_explicit((atomic_int*) &(x)->lrb_head, memory_order_relaxed); \
                     78:                                        if (_h == _t) \
                     79:                                                (r) = atomic_load_explicit((atomic_int*) &(x)->lrb_full, memory_order_acquire) ? \
                     80:                                                        (x)->lrb_size : 0; \
                     81:                                        else \
                     82:                                                (r) = (_h + (x)->lrb_size - _t) % (x)->lrb_size; \
                     83:                                } while (0)
                     84: #define lrb_unused(x, r)       do { \
                     85:                                        u_int _r; \
                     86:                                        lrb_queued((x), _r); \
                     87:                                        (r) = (x)->lrb_size - _r; \
                     88:                                } while (0)
                     89: 
1.2       misho      90: 
                     91: /*
                     92:  * rbuf_init() - Init ring buffer
                     93:  *
                     94:  * @rbuf = Ring buffer
                     95:  * @num = Number of elements in buffer
                     96:  * return: -1 error or 0 ok
                     97:  */
                     98: int rbuf_init(ringbuf_t *rbuf, int num);
                     99: /*
                    100:  * rbuf_free() - Free ring buffer
                    101:  *
                    102:  * @rbuf = Ring buffer
                    103:  * return: none
                    104:  */
                    105: void rbuf_free(ringbuf_t *rbuf);
                    106: /*
                    107:  * rbuf_purge() - Purge all buffer
                    108:  *
                    109:  * @rbuf = Ring buffer
                    110:  * return: none
                    111:  */
                    112: void rbuf_purge(ringbuf_t *rbuf);
                    113: /*
                    114:  * rbuf_isempty() - Check buffer is empty
                    115:  *
                    116:  * @rbuf = Ring buffer
                    117:  * return: -1 error, 0 it isn't empty
                    118:  */
                    119: int rbuf_isempty(ringbuf_t *rbuf);
                    120: /*
                    121:  * rbuf_isfull() - Check buffer is full
                    122:  *
                    123:  * @rbuf = Ring buffer
                    124:  * return: -1 error or 0 it isn't full
                    125:  */
                    126: int rbuf_isfull(ringbuf_t *rbuf);
                    127: 
                    128: /*
                    129:  * rbuf_enqueue() - Enqueue data to buffer
                    130:  *
                    131:  * @rbuf = Ring buffer
                    132:  * @data = Data
                    133:  * @len = Length
                    134:  * return: -1 error, 1 can't add data, buffer is full or 0 ok
                    135:  */
                    136: int rbuf_enqueue(ringbuf_t *rbuf, void *data, size_t len);
                    137: /*
                    138:  * rbuf_dequeue() - Dequeue data from buffer
                    139:  *
                    140:  * @rbuf = Ring buffer
                    141:  * @out = Data, if =NULL, just dequeue data
                    142:  * return: -1 error, 1 buffer is empty or 0 ok
                    143:  */
1.4       misho     144: int rbuf_dequeue(ringbuf_t *rbuf, struct iovec **out);
1.2       misho     145: 
                    146: 
1.5       misho     147: /*
                    148:  * lrb_init() - Init linear ring buffer
                    149:  *
                    150:  * @lrb = Linear ring buffer
                    151:  * @size = Size of ring buffer
                    152:  * return: -1 error or 0 ok
                    153:  */
                    154: int lrb_init(lrbuf_t *lrb, u_int size);
                    155: /*
                    156:  * lrb_free() - Free linear ring buffer
                    157:  *
                    158:  * @lrb = Linear ring buffer
                    159:  * return: none
                    160:  */
                    161: void lrb_free(lrbuf_t *lrb);
                    162: /*
                    163:  * lrb_purge() - Purge all buffer
                    164:  *
                    165:  * @lrb = Linear ring buffer
                    166:  * return: none
                    167:  */
                    168: void lrb_purge(lrbuf_t *lrb);
                    169: /*
                    170:  * lrb_isempty() - Check buffer is empty
                    171:  *
                    172:  * @lrb = Linear ring buffer
                    173:  * return: -1 error, 0 it isn't empty
                    174:  */
                    175: int lrb_isempty(lrbuf_t *lrb);
                    176: /*
                    177:  * lrb_isfull() - Check buffer is full
                    178:  *
                    179:  * @lrb = Linear ring buffer
                    180:  * return: -1 error or 0 it isn't full
                    181:  */
                    182: int lrb_isfull(lrbuf_t *lrb);
                    183: /*
                    184:  * lrb_enqueue() - Enqueue data to buffer
                    185:  *
                    186:  * @lrb = Linear ring buffer
                    187:  * @data = Data
                    188:  * @len = Length
                    189:  * @lost = Permit to lost data
                    190:  * return: -1 error, 1 buffer is full or 0 ok
                    191:  */
                    192: int lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int lost);
                    193: /*
                    194:  * lrb_dequeue() - Dequeue data from buffer
                    195:  *
                    196:  * @lrb = Linear ring buffer
                    197:  * @data = Data, if =NULL, just dequeue data
                    198:  * @len = Length of data
                    199:  * return: -1 error, 0 buffer is empty or >0 stored data bytes
                    200:  */
                    201: int lrb_dequeue(lrbuf_t *lrb, void *data, size_t len);
1.6       misho     202: /*
                    203:  * lrb_getw() - Get address for write
                    204:  *
                    205:  * @lrb = Linear ring buffer
                    206:  * @len = Return available buffer length for write
                    207:  * return: NULL error or !=NULL pointer for write
                    208:  * remark: After use of lrb_getw() and write to pointer.
                    209:  *             You should update ring buffer with lrb_enqueue(,NULL,wrote_len,)
                    210:  */
                    211: void *lrb_getw(lrbuf_t *lrb, size_t *len);
                    212: /*
                    213:  * lrb_getr() - Get address for read
                    214:  *
                    215:  * @lrb = Linear ring buffer
                    216:  * @len = Return available data length for read
                    217:  * return: NULL error or !=NULL pointer for read
                    218:  * remark: After use of lrb_getr() and read from pointer.
                    219:  *             You could update ring buffer with lrb_dequeue(,NULL,read_len)
                    220:  */
                    221: void *lrb_getr(lrbuf_t *lrb, size_t *len);
1.5       misho     222: 
                    223: 
1.2       misho     224: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>