Annotation of libelwix/inc/elwix/avar.h, revision 1.12

1.1       misho       1: /*************************************************************************
                      2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
                      3: *  by Michael Pounov <misho@elwix.org>
                      4: *
                      5: * $Author: misho $
1.12    ! misho       6: * $Id: avar.h,v 1.11.12.1 2022/01/04 22:07:50 misho Exp $
1.1       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.12    ! misho      15: Copyright 2004 - 2022
1.1       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 __AVAR_H
                     47: #define __AVAR_H
                     48: 
                     49: 
                     50: /* AIT RPC variables and managment */
                     51: 
                     52: typedef enum {
                     53:        empty, ptr, data,               /* empty -> variable is not set; ptr -> void*; data -> data after struct  */
                     54:        buffer, string, blob,           /* buffer -> uint8_t*; string -> int8_t*; blob -> uint32_t blobID(+socket); */
                     55:        f32, f64,                       /* float -> f32; double -> f64 */
                     56:        u8, u16, u32, u64,              /* unsigned integers ... */
                     57:        i8, i16, i32, i64,              /* integers ... */
                     58: } ait_type_t;
                     59: 
1.10      misho      60: typedef struct __packed {
1.1       misho      61:        uint8_t         val_type;
                     62:        union {
                     63:                struct {
                     64:                        uint8_t val_in:1;
                     65:                        uint8_t val_be:1;
                     66:                        uint8_t val_le:1;
1.5       misho      67:                        uint8_t val_const:1;
                     68:                        uint8_t val_f0:1;
                     69:                        uint8_t val_f1:1;
                     70:                        uint8_t val_f2:1;
                     71:                        uint8_t val_f3:1;
1.1       misho      72:                };
                     73:                uint8_t         val_opt;
                     74:        };
                     75:        uint16_t        val_key;
                     76:        uint32_t        val_len;
                     77:        union {
                     78:                uint64_t        net;
                     79: 
                     80:                void            *ptr;
                     81:                uint8_t         *buffer;
                     82:                int8_t          *string;
                     83:                uint32_t        blob;
                     84:                float           f32;
                     85:                double          f64;
                     86:                uint8_t         u8;
                     87:                uint16_t        u16;
                     88:                uint32_t        u32;
                     89:                uint64_t        u64;
                     90:                int8_t          i8;
                     91:                int16_t         i16;
                     92:                int32_t         i32;
                     93:                int64_t         i64;
                     94:        } val;
                     95:        uint8_t         val_data[0];
1.10      misho      96: } /*__packed*/ ait_val_t;      /* sizeof 16 bytes */
1.1       misho      97: 
                     98: #define AIT_TYPE(_vl)                  ((ait_type_t) (_vl)->val_type)
                     99: #define AIT_LEN(_vl)                   (_vl)->val_len
                    100: #define AIT_KEY(_vl)                   (_vl)->val_key
                    101: #define AIT_RAW(_vl)                   (_vl)->val.net
                    102: #define AIT_ADDR(_vl)                  (_vl)->val.buffer
                    103: #define AIT_IN(_vl)                    (_vl)->val_in
                    104: #define AIT_BE(_vl)                    (_vl)->val_be
                    105: #define AIT_LE(_vl)                    (_vl)->val_le
1.5       misho     106: #define AIT_CONST(_vl)                 (_vl)->val_const
                    107: #define AIT_FLAG0(_vl)                 (_vl)->val_f0
                    108: #define AIT_FLAG1(_vl)                 (_vl)->val_f1
                    109: #define AIT_FLAG2(_vl)                 (_vl)->val_f2
                    110: #define AIT_FLAG3(_vl)                 (_vl)->val_f3
1.1       misho     111: #define AIT_BLOB_CHUNKS(_vl, _n)       (AIT_LEN((_vl)) / _n + (AIT_LEN((_vl)) % _n) ? 1 : 0)
                    112: #define AIT_ISEMPTY(_vl)               (AIT_TYPE((_vl)) == empty)
                    113: 
                    114: #define AIT_GET_LIKE(_vl, _type)       ((_type) AIT_ADDR((_vl)))
                    115: #define AIT_SET_LIKE(_vl, _t, _l, _v)  (assert((_vl)), ait_setlikeVar((_vl), _t, _l, _v))
                    116: 
                    117: #define AIT_GET_PTR(_vl)               (assert(AIT_TYPE((_vl)) == ptr), (_vl)->val.ptr)
                    118: #define AIT_GET_DATA(_vl)              (assert(AIT_TYPE((_vl)) == data), (_vl)->val_data)
                    119: #define AIT_GET_BUF(_vl)               (assert(AIT_TYPE((_vl)) == buffer), (_vl)->val.buffer)
                    120: #define AIT_GET_STR(_vl)               (assert(AIT_TYPE((_vl)) == string), (char*) (_vl)->val.string)
                    121: #define AIT_GET_STRZ(_vl)              (assert(AIT_TYPE((_vl)) == string), (_vl)->val.string ? \
                    122:                                                                        (char*) (_vl)->val.string : "")
                    123: #define AIT_GET_BLOB(_vl)              (assert(AIT_TYPE((_vl)) == blob), (_vl)->val.blob)
                    124: #define AIT_GET_U8(_vl)                        (assert(AIT_TYPE((_vl)) == u8), (_vl)->val.u8)
                    125: #define AIT_GET_U16(_vl)               (assert(AIT_TYPE((_vl)) == u16), (_vl)->val.u16)
                    126: #define AIT_GET_U32(_vl)               (assert(AIT_TYPE((_vl)) == u32), (_vl)->val.u32)
                    127: #define AIT_GET_U64(_vl)               (assert(AIT_TYPE((_vl)) == u64), (_vl)->val.u64)
                    128: #define AIT_GET_I8(_vl)                        (assert(AIT_TYPE((_vl)) == i8), (_vl)->val.i8)
                    129: #define AIT_GET_I16(_vl)               (assert(AIT_TYPE((_vl)) == i16), (_vl)->val.i16)
                    130: #define AIT_GET_I32(_vl)               (assert(AIT_TYPE((_vl)) == i32), (_vl)->val.i32)
                    131: #define AIT_GET_I64(_vl)               (assert(AIT_TYPE((_vl)) == i64), (_vl)->val.i64)
                    132: #define AIT_GET_F32(_vl)               (assert(AIT_TYPE((_vl)) == f32), (_vl)->val.f32)
                    133: #define AIT_GET_F64(_vl)               (assert(AIT_TYPE((_vl)) == f64), (_vl)->val.f64)
                    134: 
1.5       misho     135: #define AIT_SET_DATA(_vl, _p, _len)    do { assert(!(_vl) || ((_vl) && !AIT_CONST((_vl)))); \
                    136:                                                ait_val_t *__val = e_realloc((_vl), \
                    137:                                                                (sizeof(ait_val_t) + _len)); \
1.1       misho     138:                                                if (__val) { \
                    139:                                                        void *__p = (_p); \
                    140:                                                        if (__p) \
                    141:                                                                memcpy(__val->val_data, __p, _len); \
                    142:                                                        __val->val_in ^= __val->val_in; \
                    143:                                                        __val->val_type = data; AIT_LEN(__val) = _len; \
                    144:                                                        (_vl) = __val; \
                    145:                                                } \
                    146:                                        } while (0);
1.5       misho     147: #define AIT_SET_PTR(_vl, _p, _len)     do { ait_val_t *__val = (_vl); \
                    148:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     149:                                                __val->val_type = ptr; __val->val.ptr = _p; \
                    150:                                                AIT_LEN(__val) = _len; } while (0)
1.5       misho     151: #define AIT_RE_BUF(_vl, _len)          do { ait_val_t *__val = (_vl); \
                    152:                                                assert(__val && !__val->val_in && !AIT_CONST(__val)); \
1.1       misho     153:                                                void *__ptr = e_realloc(AIT_GET_BUF(__val), _len); \
                    154:                                                if (__ptr) { \
                    155:                                                        __val->val.buffer = __ptr; AIT_LEN(__val) = _len; \
                    156:                                                } } while (0)
1.5       misho     157: #define AIT_SET_BUFSIZ(_vl, _c, _len)  do { ait_val_t *__val = (_vl); \
                    158:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     159:                                                __val->val.buffer = e_malloc(_len); \
                    160:                                                if (__val->val.buffer) { \
                    161:                                                        __val->val_in ^= __val->val_in; \
                    162:                                                        __val->val_type = buffer; AIT_LEN(__val) = _len; \
                    163:                                                        memset(__val->val.buffer, _c, _len); \
                    164:                                                } } while (0)
1.5       misho     165: #define AIT_SET_BUF(_vl, _v, _len)     do { ait_val_t *__val = (_vl); void *__p = (_v); \
                    166:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     167:                                                __val->val.buffer = e_malloc(_len); \
                    168:                                                if (__val->val.buffer) { \
                    169:                                                        __val->val_in ^= __val->val_in; \
                    170:                                                        __val->val_type = buffer; AIT_LEN(__val) = _len; \
                    171:                                                        if (__p) \
                    172:                                                                memcpy(__val->val.buffer, __p, _len); \
                    173:                                                        else \
                    174:                                                                memset(__val->val.buffer, 0, _len); \
                    175:                                                } } while (0)
1.5       misho     176: #define AIT_SET_STR(_vl, _v)           do { ait_val_t *__val = (_vl); const char *__s = (_v); \
                    177:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     178:                                                __val->val_type = string; \
                    179:                                                __val->val_in ^= __val->val_in; \
                    180:                                                if (__s) { \
                    181:                                                        __val->val.string = (int8_t*) e_strdup(__s); \
                    182:                                                        AIT_LEN(__val) = strlen((const char*) \
                    183:                                                                        __val->val.string) + 1; \
                    184:                                                } else { \
                    185:                                                        __val->val.string = NULL; \
                    186:                                                        AIT_LEN(__val) = 0; \
                    187:                                                } \
                    188:                                        } while (0)
1.5       misho     189: #define AIT_SET_STRSIZ(_vl, _len)      do { ait_val_t *__val = (_vl); \
                    190:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     191:                                                __val->val.string = (int8_t*) e_malloc(_len + 1); \
                    192:                                                if (__val->val.string) { \
                    193:                                                        __val->val_in ^= __val->val_in; \
                    194:                                                        __val->val_type = string; AIT_LEN(__val) = _len + 1; \
                    195:                                                        memset(__val->val.string, 0, AIT_LEN(__val)); \
                    196:                                                } \
                    197:                                        } while (0)
                    198: #define AIT_SET_STRCAT(_vl, _v)                do { ait_val_t *__val = (_vl); const char *__s = (_v); int __l; \
1.5       misho     199:                                                assert(__val && !__val->val_in && !AIT_CONST(__val)); \
1.1       misho     200:                                                assert(AIT_TYPE(__val) == string); \
                    201:                                                if (!__s || !*__s) \
                    202:                                                        break; \
                    203:                                                else \
                    204:                                                        __l = strlen(__s); \
                    205:                                                if (!__val->val.string) \
                    206:                                                        __l++; \
                    207:                                                void *__p = e_realloc(__val->val.string, AIT_LEN(__val) + __l); \
                    208:                                                if (__p) { \
                    209:                                                        AIT_LEN(__val) += __l; \
                    210:                                                        if (!__val->val.string) \
                    211:                                                                memset(__p, 0, AIT_LEN(__val)); \
                    212:                                                        __val->val.string = __p; \
                    213:                                                        strlcat((char*) __val->val.string, __s, \
                    214:                                                                        AIT_LEN(__val)); \
                    215:                                                } \
                    216:                                        } while (0)
                    217: #define AIT_SET_STRCPY(_vl, _v)                do { ait_val_t *__val = (_vl); const char *__s = (_v); int __l; \
1.5       misho     218:                                                assert(__val && !__val->val_in && !AIT_CONST(__val)); \
1.1       misho     219:                                                assert(AIT_TYPE(__val) == string); \
                    220:                                                if (!__s || !*__s) \
                    221:                                                        break; \
                    222:                                                else \
                    223:                                                        __l = strlen(__s) + 1; \
                    224:                                                void *__p = e_realloc(__val->val.string, __l); \
                    225:                                                if (__p) { \
                    226:                                                        AIT_LEN(__val) = __l; \
                    227:                                                        __val->val.string = __p; \
                    228:                                                        strlcpy((char*) __val->val.string, __s, \
                    229:                                                                        AIT_LEN(__val)); \
                    230:                                                } } while (0)
                    231: #define AIT_SET_STRLCPY(_vl, _v, _len) do { ait_val_t *__val = (_vl); const char *__s = (_v); \
1.5       misho     232:                                                assert(__val && !__val->val_in && !AIT_CONST(__val)); \
1.1       misho     233:                                                assert(AIT_TYPE(__val) == string); \
                    234:                                                if (!__s || !*__s) \
                    235:                                                        break; \
                    236:                                                void *__p = e_realloc(__val->val.string, _len); \
                    237:                                                if (__p) { \
                    238:                                                        AIT_LEN(__val) = _len; \
                    239:                                                        __val->val.string = __p; \
                    240:                                                        strlcpy((char*) __val->val.string, __s, \
                    241:                                                                        AIT_LEN(__val)); \
                    242:                                                } } while (0)
1.5       misho     243: #define AIT_SET_BLOB(_vl, _n, _len)    do { ait_val_t *__val = (_vl); \
                    244:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     245:                                                __val->val_type = blob; __val->val.blob = _n; \
                    246:                                                AIT_LEN(__val) = _len; } while (0)
1.5       misho     247: #define AIT_SET_BLOB2(_vl, _bv)                do { ait_val_t *__val = (_vl); assert((_bv)); \
                    248:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     249:                                                __val->val_type = blob; AIT_LEN(__val) = \
                    250:                                                        (_bv)->blob_len; \
                    251:                                                __val->val.blob = (_bv)->blob_var; } while (0)
                    252: #define AIT_NEW_BLOB(_vl, _len)                AIT_SET_BLOB((_vl), 0, _len)
                    253: 
1.5       misho     254: #define AIT_SET_U8(_vl, _n)            do { ait_val_t *__val = (_vl); \
                    255:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     256:                                                __val->val_type = u8; __val->val.u8 = _n; \
                    257:                                                AIT_LEN(__val) = sizeof(uint8_t); } while (0)
1.5       misho     258: #define AIT_SET_U16(_vl, _n)           do { ait_val_t *__val = (_vl); \
                    259:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     260:                                                __val->val_type = u16; __val->val.u16 = _n; \
                    261:                                                AIT_LEN(__val) = sizeof(uint16_t); } while (0)
1.5       misho     262: #define AIT_SET_U32(_vl, _n)           do { ait_val_t *__val = (_vl); \
                    263:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     264:                                                __val->val_type = u32; __val->val.u32 = _n; \
                    265:                                                AIT_LEN(__val) = sizeof(uint32_t); } while (0)
1.5       misho     266: #define AIT_SET_U64(_vl, _n)           do { ait_val_t *__val = (_vl); \
                    267:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     268:                                                __val->val_type = u64; __val->val.u64 = _n; \
                    269:                                                AIT_LEN(__val) = sizeof(uint64_t); } while (0)
1.5       misho     270: #define AIT_SET_I8(_vl, _n)            do { ait_val_t *__val = (_vl); \
                    271:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     272:                                                __val->val_type = i8; __val->val.i8 = _n; \
                    273:                                                AIT_LEN(__val) = sizeof(int8_t); } while (0)
1.5       misho     274: #define AIT_SET_I16(_vl, _n)           do { ait_val_t *__val = (_vl); \
                    275:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     276:                                                __val->val_type = i16; __val->val.i16 = _n; \
                    277:                                                AIT_LEN(__val) = sizeof(int16_t); } while (0)
1.5       misho     278: #define AIT_SET_I32(_vl, _n)           do { ait_val_t *__val = (_vl); \
                    279:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     280:                                                __val->val_type = i32; __val->val.i32 = _n; \
                    281:                                                AIT_LEN(__val) = sizeof(int32_t); } while (0)
1.5       misho     282: #define AIT_SET_I64(_vl, _n)           do { ait_val_t *__val = (_vl); \
                    283:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     284:                                                __val->val_type = i64; __val->val.i64 = _n; \
                    285:                                                AIT_LEN(__val) = sizeof(int64_t); } while (0)
1.5       misho     286: #define AIT_SET_F32(_vl, _n)           do { ait_val_t *__val = (_vl); \
                    287:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     288:                                                __val->val_type = f32; __val->val.f32 = _n; \
                    289:                                                AIT_LEN(__val) = sizeof(float); } while (0)
1.5       misho     290: #define AIT_SET_F64(_vl, _n)           do { ait_val_t *__val = (_vl); \
                    291:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     292:                                                __val->val_type = f64; __val->val.f64 = _n; \
                    293:                                                AIT_LEN(__val) = sizeof(double); } while (0)
                    294: 
1.5       misho     295: #define AIT_COPY_VAL(_vl, _v)          do { assert((_vl) && (_v) && !AIT_CONST((_vl))); \
1.1       misho     296:                                                memcpy((_vl), (_v), sizeof(ait_val_t)); \
                    297:                                                switch (AIT_TYPE((_vl))) { \
                    298:                                                        case buffer: \
                    299:                                                                AIT_SET_BUF((_vl), \
                    300:                                                                                AIT_GET_BUF((_v)), \
                    301:                                                                                AIT_LEN((_v))); \
                    302:                                                                break; \
                    303:                                                        case string: \
                    304:                                                                AIT_SET_STR((_vl), \
                    305:                                                                                AIT_GET_STR((_v))); \
                    306:                                                                break; \
                    307:                                                        default: \
                    308:                                                                break; \
                    309:                                                } \
                    310:                                        } while (0)
1.11      misho     311: /* Additional helper macro, which can help about copy data variables */
1.1       misho     312: #define AIT_COPY_DATA(_vl, _v)          do { AIT_COPY_VAL((_vl), (_v)); \
                    313:                                                if (AIT_TYPE((_vl)) == data) \
                    314:                                                        AIT_SET_DATA((_vl), AIT_GET_DATA((_v)), \
                    315:                                                                        AIT_LEN((_v))); \
                    316:                                        } while (0)
                    317: 
1.4       misho     318: #define AIT_VAL_INITIALIZER(_vl)       { .val_type = empty, { .val_opt = 0 }, \
1.3       misho     319:                                                .val_key = 0, .val_len = 0, \
                    320:                                                .val.net = 0LL \
                    321:                                        }
1.8       misho     322: #define AIT_VAL_INIT                   AIT_VAL_INITIALIZER()
1.1       misho     323: #define AIT_INIT_VAL(_vl)              (memset((_vl), 0, sizeof(ait_val_t)))
                    324: #define AIT_INIT_VAL2(_vl, _t)         do { \
                    325:                                                AIT_INIT_VAL((_vl)); \
                    326:                                                (_vl)->val_type = _t; \
                    327:                                        } while (0)
                    328:                                        /* if attribute zeroCopy is set not execute e_free() */
                    329: #define AIT_FREE_VAL(_vl)              do { ait_val_t *__val = (_vl); assert(__val); \
                    330:                                                switch (AIT_TYPE(__val)) { \
                    331:                                                        case buffer: \
                    332:                                                                if (!__val->val_in && \
                    333:                                                                                __val->val.buffer) \
                    334:                                                                        e_free(__val->val.buffer); \
                    335:                                                                __val->val.buffer = NULL; \
                    336:                                                                break; \
                    337:                                                        case string: \
                    338:                                                                if (!__val->val_in && \
                    339:                                                                                __val->val.string) \
                    340:                                                                        e_free(__val->val.string); \
                    341:                                                                __val->val.string = NULL; \
                    342:                                                                break; \
                    343:                                                        default: \
                    344:                                                                break; \
                    345:                                                } \
                    346:                                                __val->val_type = empty; \
                    347:                                                __val->val_opt ^= __val->val_opt; \
                    348:                                                AIT_LEN(__val) = 0; \
                    349:                                                AIT_KEY(__val) = 0; \
                    350:                                        } while (0)
1.5       misho     351: #define AIT_ZERO_VAL(_vl)              do { ait_val_t *__val = (_vl); \
                    352:                                                assert(__val && !AIT_CONST(__val)); \
1.1       misho     353:                                                switch (AIT_TYPE(__val)) { \
                    354:                                                        case buffer: \
                    355:                                                        case string: \
1.12    ! misho     356:                                                                assert(!__val->val_in); \
1.1       misho     357:                                                                if (__val->val.buffer) \
                    358:                                                                        memset(__val->val.buffer, 0, \
                    359:                                                                                AIT_LEN(__val)); \
                    360:                                                                break; \
                    361:                                                        case data: \
                    362:                                                                memset(__val->val_data, 0, AIT_LEN(__val)); \
                    363:                                                                break; \
                    364:                                                        default: \
                    365:                                                                __val->val.net = 0LL; \
                    366:                                                                break; \
                    367:                                                } \
                    368:                                                AIT_KEY(__val) = 0; \
                    369:                                        } while (0)
                    370: 
                    371: 
                    372: /*
                    373:  * ait_vars2buffer() - Marshaling data from array with variables to buffer
                    374:  *
                    375:  * @buf = Buffer
                    376:  * @buflen = Size of buffer
                    377:  * @vars = Variable array
                    378:  * return: -1 error, 0 nothing done or >0 size of marshaled data
                    379:  */
1.2       misho     380: int ait_vars2buffer(unsigned char * __restrict buf, int buflen, 
1.1       misho     381:                array_t * __restrict vars);
                    382: /*
                    383:  * ait_buffer2vars() - De-marshaling data from buffer to array with variables
                    384:  *
                    385:  * @buf = Buffer
                    386:  * @buflen = Size of buffer
                    387:  * @vnum = Number of variables into buffer
                    388:  * @zcpy = Zero-copy for variables, if !=0 don't use array_Free() for free variables and 
                    389:                *DON'T MODIFY OR DESTROY BUFFER*. =0 call array_Free() before array_Destroy()
                    390:  * return: =NULL error, !=NULL allocated variable array, after use must free with array_Destroy()
                    391:  */
1.2       misho     392: array_t *ait_buffer2vars(unsigned char * __restrict buf, int buflen, int vnum, int zcpy);
1.1       misho     393: /*
                    394:  * ait_vars2map() - Marshaling data from array with variables to memory map
                    395:  *
                    396:  * @buf = Buffer
                    397:  * @buflen = Size of buffer
                    398:  * @vars = Variable array
                    399:  * return: -1 error, 0 nothing done or >0 size of marshaled data
                    400:  */
1.2       misho     401: int ait_vars2map(unsigned char * __restrict buf, int buflen, array_t * __restrict vars);
1.1       misho     402: /*
                    403:  * ait_map2vars() - De-marshaling data from memory map to array with variables
                    404:  *
                    405:  * @buf = Buffer
                    406:  * @buflen = Size of buffer
                    407:  * @vnum = Number of variables into buffer
                    408:  * @zcpy = Zero-copy for variables, if !=0 don't use array_Free() for free variables and 
                    409:                *DON'T MODIFY OR DESTROY BUFFER*. =0 call array_Free() before array_Destroy()
                    410:  * return: =NULL error, !=NULL allocated variable array, after use must free with array_Destroy()
                    411:  */
1.2       misho     412: array_t *ait_map2vars(unsigned char * __restrict buf, int buflen, int vnum, int zcpy);
1.1       misho     413: 
                    414: 
                    415: /*
                    416:  * ait_allocVar() - Allocate memory for variable
                    417:  *
                    418:  * return: NULL error or new variable, after use free variable with ait_freeVar()
                    419:  */
1.2       misho     420: ait_val_t *ait_allocVar(void);
1.1       misho     421: /*
                    422:  * ait_freeVar() - Free allocated memory for variable
                    423:  *
                    424:  * @val = Variable
                    425:  * return: none
                    426:  */
1.2       misho     427: void ait_freeVar(ait_val_t ** __restrict val);
1.1       misho     428: /*
                    429:  * ait_makeVar() - Allocate memory and fill variable
                    430:  *
                    431:  * @type = type of variable
                    432:  * @... = arg1 is value of variable
                    433:  * @... = arg2 is length of variabla. Not required for numbers and strings!
                    434:  * return: NULL error or new variable, after use free variable with ait_freeVar()
                    435:  */
                    436: ait_val_t *ait_makeVar(ait_type_t type, ...);
                    437: /*
                    438:  * ait_getlikeVar() - Get variable like ...
                    439:  *
                    440:  * @v = variable
                    441:  * return: return raw data
                    442:  */
1.2       misho     443: uint64_t ait_getlikeVar(ait_val_t * __restrict v);
1.1       misho     444: /*
                    445:  * ait_setlikeVar() - Set variable like ...
                    446:  *
                    447:  * @v = variable
                    448:  * @t = type of data
                    449:  * @l = length of data
                    450:  * @... = data
                    451:  * return: -1 error or 0 ok
                    452:  */
1.2       misho     453: int ait_setlikeVar(ait_val_t * __restrict v, ait_type_t t, unsigned int l, ...);
1.1       misho     454: /*
                    455:  * ait_sprintfVar() - Builtin string variable from formatted input
                    456:  *
                    457:  * @v = variable
                    458:  * @fmt = format string
                    459:  * @... = argument(s)
                    460:  * return: -1 error or >0 copied bytes to variable
                    461:  */
                    462: int ait_sprintfVar(ait_val_t * __restrict v, const char *fmt, ...);
                    463: /*
                    464:  * ait_cmpVar() - Compare two variables
                    465:  *
                    466:  * @a = 1st variable
                    467:  * @b = 2nd variable
                    468:  * return: 0 is equal or !=0 is different
                    469:  */
1.2       misho     470: int ait_cmpVar(ait_val_t * __restrict a, ait_val_t * __restrict b);
1.1       misho     471: /*
                    472:  * ait_hashVar() - Generate hash key for variable from string or value
                    473:  *
                    474:  * @v = variable
                    475:  * @key = key string for hash, if =NULL hash will built from variable
                    476:  * return: hash key
                    477:  */
                    478: unsigned short ait_hashVar(ait_val_t * __restrict v, const char * __restrict key);
                    479: 
                    480: 
                    481: /*
                    482:  * ait_allocVars() - Allocate ait_val_t array
                    483:  *
                    484:  * @varnum = Number of variables
                    485:  * return: =NULL error or !=NULL allocated array
                    486:  */
1.2       misho     487: array_t *ait_allocVars(int varnum);
1.1       misho     488: /*
                    489:  * ait_clrVars() - Clear ait_val_t elements from array
                    490:  *
                    491:  * @vars = Variable array
                    492:  * return: -1 error or size of array
                    493:  */
1.2       misho     494: int ait_clrVars(array_t * __restrict vars);
1.1       misho     495: /*
                    496:  * ait_freeVars() - Free ait_val_t array
                    497:  *
                    498:  * @vars = Variable array
                    499:  * return: none
                    500:  */
1.2       misho     501: void ait_freeVars(array_t ** __restrict vars);
1.1       misho     502: /*
                    503:  * ait_getVars() - Get ait_val_t element from array and if not exists allocate it
                    504:  *
                    505:  * @vars = Variable array
                    506:  * @n = index of variable into array
                    507:  * return: NULL error or !=NULL ait_val_t element
                    508:  */
1.2       misho     509: ait_val_t *ait_getVars(array_t ** __restrict vars, int n);
1.1       misho     510: /*
1.6       misho     511:  * ait_resideVars() - Calculate footprint of resided variables into array
                    512:  *
                    513:  * @vars = Variable array
                    514:  * return: bytes for whole array
                    515:  */
                    516: size_t ait_resideVars(array_t * __restrict vars);
                    517: /*
1.1       misho     518:  * ait_sortVarsByKey() - Sorting array with variables by key
                    519:  *
                    520:  * @vars = Variable array
                    521:  * @order = Sort order. If =0 ascend or !=0 descend
                    522:  * return: none
                    523:  */
1.2       misho     524: void ait_sortVarsByKey(array_t * __restrict vars, int order);
1.1       misho     525: /*
                    526:  * ait_sortVarsByVal() - Sorting array with variables by value
                    527:  *
                    528:  * @vars = Variable array
                    529:  * @order = Sort order. If =0 ascend or !=0 descend
                    530:  * @cmp = Custom compare function for sorting. If =NULL compare by value
                    531:  * return: none
                    532:  */
1.2       misho     533: void ait_sortVarsByVal(array_t * __restrict vars, int order,  
1.1       misho     534:                int (*cmp)(const void*, const void*));
                    535: /*
                    536:  * ait_findKeyVars() - Find variable by key from array
                    537:  *
                    538:  * @vars = Variables
                    539:  * @key = Search key
                    540:  * return: NULL error or not found, !=NULL valid element
                    541:  */
                    542: ait_val_t *ait_findKeyVars(array_t * __restrict vars, unsigned short key);
                    543: /*
                    544:  * ait_findKeyHash() - Find variable by hash string from array
                    545:  *
                    546:  * @vars = Variables
                    547:  * @key = Search string
                    548:  * return: NULL error or not found, !=NULL valid element
                    549:  */
1.2       misho     550: ait_val_t *ait_findKeyHash(array_t * __restrict vars, const char * __restrict key);
1.1       misho     551: /*
                    552:  * ait_hashKeyVars() - Generate hash keys for variables
                    553:  *
                    554:  * @vars = Variables
                    555:  * return -1 error or 0 ok
                    556:  */
1.2       misho     557: int ait_hashKeyVars(array_t * __restrict vars);
1.1       misho     558: 
                    559: 
                    560: #endif

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