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

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