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

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.5     ! misho       6: * $Id: aring.h,v 1.4.4.2 2026/02/10 17:24:39 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;
        !            65: #define lrb_queued(x, r)       do { \
        !            66:                                        u_int _h, _t; \
        !            67:                                        _t = atomic_load_explicit((atomic_int*) &(x)->lrb_tail, memory_order_acquire); \
        !            68:                                        _h = atomic_load_explicit((atomic_int*) &(x)->lrb_head, memory_order_relaxed); \
        !            69:                                        if (_h == _t) \
        !            70:                                                (r) = atomic_load_explicit((atomic_int*) &(x)->lrb_full, memory_order_acquire) ? \
        !            71:                                                        (x)->lrb_size : 0; \
        !            72:                                        else \
        !            73:                                                (r) = (_h + (x)->lrb_size - _t) % (x)->lrb_size; \
        !            74:                                } while (0)
        !            75: #define lrb_unused(x, r)       do { \
        !            76:                                        u_int _r; \
        !            77:                                        lrb_queued((x), _r); \
        !            78:                                        (r) = (x)->lrb_size - _r; \
        !            79:                                } while (0)
        !            80: 
1.2       misho      81: 
                     82: /*
                     83:  * rbuf_init() - Init ring buffer
                     84:  *
                     85:  * @rbuf = Ring buffer
                     86:  * @num = Number of elements in buffer
                     87:  * return: -1 error or 0 ok
                     88:  */
                     89: int rbuf_init(ringbuf_t *rbuf, int num);
                     90: /*
                     91:  * rbuf_free() - Free ring buffer
                     92:  *
                     93:  * @rbuf = Ring buffer
                     94:  * return: none
                     95:  */
                     96: void rbuf_free(ringbuf_t *rbuf);
                     97: /*
                     98:  * rbuf_purge() - Purge all buffer
                     99:  *
                    100:  * @rbuf = Ring buffer
                    101:  * return: none
                    102:  */
                    103: void rbuf_purge(ringbuf_t *rbuf);
                    104: /*
                    105:  * rbuf_isempty() - Check buffer is empty
                    106:  *
                    107:  * @rbuf = Ring buffer
                    108:  * return: -1 error, 0 it isn't empty
                    109:  */
                    110: int rbuf_isempty(ringbuf_t *rbuf);
                    111: /*
                    112:  * rbuf_isfull() - Check buffer is full
                    113:  *
                    114:  * @rbuf = Ring buffer
                    115:  * return: -1 error or 0 it isn't full
                    116:  */
                    117: int rbuf_isfull(ringbuf_t *rbuf);
                    118: 
                    119: /*
                    120:  * rbuf_enqueue() - Enqueue data to buffer
                    121:  *
                    122:  * @rbuf = Ring buffer
                    123:  * @data = Data
                    124:  * @len = Length
                    125:  * return: -1 error, 1 can't add data, buffer is full or 0 ok
                    126:  */
                    127: int rbuf_enqueue(ringbuf_t *rbuf, void *data, size_t len);
                    128: /*
                    129:  * rbuf_dequeue() - Dequeue data from buffer
                    130:  *
                    131:  * @rbuf = Ring buffer
                    132:  * @out = Data, if =NULL, just dequeue data
                    133:  * return: -1 error, 1 buffer is empty or 0 ok
                    134:  */
1.4       misho     135: int rbuf_dequeue(ringbuf_t *rbuf, struct iovec **out);
1.2       misho     136: 
                    137: 
1.5     ! misho     138: /*
        !           139:  * lrb_init() - Init linear ring buffer
        !           140:  *
        !           141:  * @lrb = Linear ring buffer
        !           142:  * @size = Size of ring buffer
        !           143:  * return: -1 error or 0 ok
        !           144:  */
        !           145: int lrb_init(lrbuf_t *lrb, u_int size);
        !           146: /*
        !           147:  * lrb_free() - Free linear ring buffer
        !           148:  *
        !           149:  * @lrb = Linear ring buffer
        !           150:  * return: none
        !           151:  */
        !           152: void lrb_free(lrbuf_t *lrb);
        !           153: /*
        !           154:  * lrb_purge() - Purge all buffer
        !           155:  *
        !           156:  * @lrb = Linear ring buffer
        !           157:  * return: none
        !           158:  */
        !           159: void lrb_purge(lrbuf_t *lrb);
        !           160: /*
        !           161:  * lrb_isempty() - Check buffer is empty
        !           162:  *
        !           163:  * @lrb = Linear ring buffer
        !           164:  * return: -1 error, 0 it isn't empty
        !           165:  */
        !           166: int lrb_isempty(lrbuf_t *lrb);
        !           167: /*
        !           168:  * lrb_isfull() - Check buffer is full
        !           169:  *
        !           170:  * @lrb = Linear ring buffer
        !           171:  * return: -1 error or 0 it isn't full
        !           172:  */
        !           173: int lrb_isfull(lrbuf_t *lrb);
        !           174: /*
        !           175:  * lrb_enqueue() - Enqueue data to buffer
        !           176:  *
        !           177:  * @lrb = Linear ring buffer
        !           178:  * @data = Data
        !           179:  * @len = Length
        !           180:  * @lost = Permit to lost data
        !           181:  * return: -1 error, 1 buffer is full or 0 ok
        !           182:  */
        !           183: int lrb_enqueue(lrbuf_t *lrb, void *data, size_t len, int lost);
        !           184: /*
        !           185:  * lrb_dequeue() - Dequeue data from buffer
        !           186:  *
        !           187:  * @lrb = Linear ring buffer
        !           188:  * @data = Data, if =NULL, just dequeue data
        !           189:  * @len = Length of data
        !           190:  * return: -1 error, 0 buffer is empty or >0 stored data bytes
        !           191:  */
        !           192: int lrb_dequeue(lrbuf_t *lrb, void *data, size_t len);
        !           193: 
        !           194: 
1.2       misho     195: #endif

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