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

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