Annotation of libaitsess/inc/aitsess.h, revision 1.3.2.2

1.2       misho       1: /*************************************************************************
                      2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.3.2.2 ! misho       6: * $Id: aitsess.h,v 1.3.2.1 2011/09/07 15:52:35 misho Exp $
1.2       misho       7: *
1.3       misho       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: 
                     15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
                     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: */
1.1       misho      46: #ifndef __AITSESS_H
                     47: #define __AITSESS_H
                     48: 
                     49: 
1.3       misho      50: #include <assert.h>
1.1       misho      51: #include <semaphore.h>
                     52: #include <sys/types.h>
1.3.2.1   misho      53: #include <aitio.h>
1.1       misho      54: 
1.3       misho      55: #define SHARED_UNKNOWN 0
1.1       misho      56: #define SHARED_IPC     1
                     57: #define SHARED_MAP     2
                     58: 
                     59: #define IS_VAL         0x0
                     60: #define IS_ADD         0x40000000
                     61: #define IS_DEF         0x80000000
                     62: 
                     63: #define ISNEW(x)       (((x) & IS_ADD) == IS_ADD)
                     64: #define ISDEF(x)       (((x) & IS_DEF) == IS_DEF)
                     65: 
1.3.2.2 ! misho      66: #define MAX_ATTRIBUTE  64
1.1       misho      67: #define MAX_SEMNAME    14
                     68: 
                     69: 
                     70: typedef struct _tagSess {
                     71:        key_t   key;
                     72:        u_char  type;
1.3.2.1   misho      73:        u_char  zcopy;
1.1       misho      74:        off_t   eom;
                     75:        void    *addr;
                     76:        off_t   offset;
                     77:        union {
                     78:                int     shmid;
                     79:                int     fd;
                     80:        } mem;
                     81:        union {
                     82:                int     semid;
                     83:                sem_t   *sid;
                     84:        } id;
                     85: } tagSess;
                     86: 
                     87: 
                     88: // -------------------------------------------------------
                     89: // sess_GetErrno() Get error code of last operation
                     90: inline int sess_GetErrno();
                     91: // sess_GetError() Get error text of last operation
                     92: inline const char *sess_GetError();
1.3.2.1   misho      93: // sess_SetErr() Set error to variables for internal use!!!
                     94: inline void sess_SetErr(int eno, char *estr, ...);
1.1       misho      95: // -------------------------------------------------------
                     96: 
                     97: 
                     98: /*
                     99:  * initSession() Initializing session structure, if session file not exists creating with specified tech
1.3       misho     100:  * @pnID = Technology using in session. 
                    101:        SHARED_IPC IPC tech; SHARED_MAP BSD MemoryMap tech or if =NULL SHARED_IPC
1.1       misho     102:  * @csFName = Session filename for build key and identified
1.3       misho     103:  * @Sess = Session item, if =NULL allocate memory for session after use must be free!
1.1       misho     104:  * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
                    105: */
1.3       misho     106: inline int initSession(int *pnID, const char *csFName, tagSess ** __restrict Sess);
1.1       misho     107: /*
                    108:  * freeSession() Free allocated memory for session item and delete session file if present name
                    109:  * @csFName = Session filename for delete, if NULL nothing delete
                    110:  * @Sess = Session item
                    111: */
                    112: inline void freeSession(const char *csFName, tagSess ** __restrict Sess);
                    113: 
                    114: /*
                    115:  * map_createSession() MMAP Created session and allocated resources
                    116:  * @csFName = Session name for identified
                    117:  * @cnSeed = Seed for securing key
                    118:  * @cnSize = Allocated shared memory size in bytes
                    119:  * @Sess = Session item
                    120:  * return: 0 Ok successful, -1 error: not allocated resources
                    121: */
                    122: int map_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess);
                    123: /*
                    124:  * map_destroySession() MMAP free shared resources
                    125:  * @csFName = Session name for delete
                    126:  * @Sess = Session item
                    127: */
                    128: void map_destroySession(const char *csFName, tagSess ** __restrict Sess);
                    129: 
                    130: /*
                    131:  * ipc_createSession() IPC Created session and allocated resources
                    132:  * @csFName = Session name for identified
                    133:  * @cnSeed = Seed for securing key
                    134:  * @cnSize = Allocated shared memory size in bytes
                    135:  * @Sess = Session item
                    136:  * return: 0 Ok successful, -1 error: not allocated resources
                    137: */
                    138: int ipc_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess);
                    139: /*
                    140:  * ipc_destroySession() IPC free shared resources
                    141:  * @csFName = Session name for delete
                    142:  * @Sess = Session item
                    143: */
                    144: void ipc_destroySession(const char *csFName, tagSess ** __restrict Sess);
                    145: 
                    146: /*
                    147:  * map_attachSession() MMAP Attach to shared memory & return begin address
                    148:  * @s = Session item
                    149:  * @procMem = Custom start address (optionl) *default must be 0*
                    150:  * return: NULL failed attach, !=NULL begin address of memory
                    151: */
                    152: inline void *map_attachSession(tagSess * __restrict s, void *procMem);
                    153: /*
                    154:  * map_detachSession() MMAP Detach from shared memory
                    155:  * @s = Session item
                    156: */
                    157: inline void map_detachSession(tagSess * __restrict s);
                    158: 
                    159: /*
                    160:  * ipc_attachSession() IPC Attach to shared memory & return begin address
                    161:  * @s = Session item
                    162:  * @procMem = Custom start address (optionl) *default must be 0*
                    163:  * return: NULL failed attach, !=NULL begin address of memory
                    164: */
                    165: inline void *ipc_attachSession(tagSess * __restrict s, void *procMem);
                    166: /*
                    167:  * ipc_detachSession() IPC Detach from shared memory
                    168:  * @s = Session item
                    169: */
                    170: inline void ipc_detachSession(tagSess * __restrict s);
                    171: 
                    172: /*
1.2       misho     173:  * isAttached() Check for mapped/(attached) shared memory
                    174:  * @s = Session item
                    175:  * return: -1 null session item, 0 not attached, 1 attached memory
                    176: */
                    177: inline int isAttached(tagSess * __restrict s);
                    178: 
                    179: 
                    180: /*
1.1       misho     181:  * map_notSemaphore() MMAP negative block if semaphore isn`t signaled
                    182:  * @s = Session item
                    183: */
                    184: inline void map_notSemaphore(tagSess * __restrict s);
                    185: /*
                    186:  * map_isSemaphored() MMAP Check semaphore
                    187:  * @s = Session item
                    188:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
                    189: */
                    190: inline int map_isSemaphored(tagSess * __restrict s);
                    191: /*
                    192:  * map_addSemaphore() MMAP unblock semaphore, increment semaphore
                    193:  * @s = Session item
                    194:  * return: 0 Ok, -1 error: can`t increment 
                    195: */
                    196: inline int map_addSemaphore(tagSess * __restrict s);
                    197: /*
                    198:  * map_decSemaphore() MMAP block semaphore, decrement semaphore
                    199:  * @s = Session item
                    200:  * return: 0 Ok, -1 error: can`t decrement 
                    201: */
                    202: inline int map_decSemaphore(tagSess * __restrict s);
                    203: 
                    204: /*
                    205:  * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled
                    206:  * @s = Session item
                    207: */
                    208: inline void ipc_notSemaphore(tagSess * __restrict s);
                    209: /*
                    210:  * ipc_isSemaphored() IPC Check semaphore
                    211:  * @s = Session item
                    212:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
                    213: */
                    214: inline int ipc_isSemaphored(tagSess * __restrict s);
                    215: /*
                    216:  * ipc_addSemaphore() IPC unblock semaphore, increment semaphore
                    217:  * @s = Session item
                    218:  * return: 0 Ok, -1 error: can`t increment 
                    219: */
                    220: inline int ipc_addSemaphore(tagSess * __restrict s);
                    221: /*
                    222:  * ipc_decSemaphore() IPC block semaphore, decrement semaphore
                    223:  * @s = Session item
                    224:  * return: 0 Ok, -1 error: can`t decrement 
                    225: */
                    226: inline int ipc_decSemaphore(tagSess * __restrict s);
                    227: 
1.2       misho     228: 
1.1       misho     229: // ---------------------------------------------------------
                    230: //
                    231: //  Lazy macros for lazy programmers :-) by Michael Pounov; Optimizing work with sessions!
                    232: //
                    233: 
1.3       misho     234: #define DESTROY_SESSION(fname, s)      do { assert((s)); assert((*s)); \
1.2       misho     235:                                                switch (((*s))->type) { \
1.1       misho     236:                                                        case SHARED_IPC: \
                    237:                                                                         ipc_destroySession((fname), (s)); \
                    238:                                                                         break; \
                    239:                                                        case SHARED_MAP: \
                    240:                                                                         map_destroySession((fname), (s)); \
                    241:                                                                         break; \
                    242:                                                } \
                    243:                                        } while(0)
                    244: 
1.3       misho     245: #define ATTACH_MEMORY(s)               do { assert((s)); \
1.1       misho     246:                                                switch ((s)->type) { \
                    247:                                                        case SHARED_IPC: \
1.2       misho     248:                                                                         ipc_attachSession((s), 0); \
1.1       misho     249:                                                                         break; \
                    250:                                                        case SHARED_MAP: \
1.2       misho     251:                                                                         map_attachSession((s), 0); \
1.1       misho     252:                                                                         break; \
                    253:                                                } \
                    254:                                        } while(0)
                    255: 
1.3       misho     256: #define DETACH_MEMORY(s)               do { assert((s)); \
1.1       misho     257:                                                switch ((s)->type) { \
                    258:                                                        case SHARED_IPC: \
                    259:                                                                         ipc_detachSession((s)); \
                    260:                                                                         break; \
                    261:                                                        case SHARED_MAP: \
                    262:                                                                         map_detachSession((s)); \
                    263:                                                                         break; \
                    264:                                                } \
                    265:                                        } while(0)
                    266: 
1.3       misho     267: #define NOT_SEMAPHORE(s)       do { assert((s)); \
1.1       misho     268:                                        switch ((s)->type) { \
                    269:                                                case SHARED_IPC: \
                    270:                                                                 ipc_notSemaphore((s)); \
                    271:                                                                 break; \
                    272:                                                case SHARED_MAP: \
                    273:                                                                 map_notSemaphore((s)); \
                    274:                                                                 break; \
                    275:                                        } \
                    276:                                } while(0)
                    277: 
1.3       misho     278: #define IS_SEMAPHORED(s, ret)  do { assert((s)); \
1.1       misho     279:                                        switch ((s)->type) { \
                    280:                                                case SHARED_IPC: \
                    281:                                                                 (ret) = ipc_isSemaphored((s)); \
                    282:                                                                 break; \
                    283:                                                case SHARED_MAP: \
                    284:                                                                 (ret) = map_isSemaphored((s)); \
                    285:                                                                 break; \
                    286:                                                default: \
                    287:                                                         (ret) = -1; \
                    288:                                        } \
                    289:                                } while(0)
                    290: 
1.3       misho     291: #define ADD_SEMAPHORE(s, ret)  do { assert((s)); \
1.1       misho     292:                                        switch ((s)->type) { \
                    293:                                                case SHARED_IPC: \
                    294:                                                                 (ret) = ipc_addSemaphore((s)); \
                    295:                                                                 break; \
                    296:                                                case SHARED_MAP: \
                    297:                                                                 (ret) = map_addSemaphore((s)); \
                    298:                                                                 break; \
                    299:                                                default: \
                    300:                                                         (ret) = -1; \
                    301:                                        } \
                    302:                                } while(0)
                    303: 
1.3       misho     304: #define DEC_SEMAPHORE(s, ret)  do { assert((s)); \
1.1       misho     305:                                        switch ((s)->type) { \
                    306:                                                case SHARED_IPC: \
                    307:                                                                 (ret) = ipc_decSemaphore((s)); \
                    308:                                                                 break; \
                    309:                                                case SHARED_MAP: \
                    310:                                                                 (ret) = map_decSemaphore((s)); \
                    311:                                                                 break; \
                    312:                                                default: \
                    313:                                                         (ret) = -1; \
                    314:                                        } \
                    315:                                } while(0)
                    316: 
                    317: // ---------------------------------------------------------
                    318: 
                    319: /*
1.2       misho     320:  * sess_FreeValues() Free all values from value array allocated from sess_GetValues()
                    321:  * @ppsVals = Array strings
                    322:  * return: none
                    323: */
                    324: inline void sess_FreeValues(char *** __restrict ppsVals);
                    325: /*
                    326:  * sess_GetValues() Get all values from session shared memory
                    327:  * @s = Session item
                    328:  * @ppsVals = Return array strings
                    329:  * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)
                    330: */
                    331: int sess_GetValues(tagSess * __restrict s, char ***ppsVals);
                    332: /*
1.1       misho     333:  * sess_GetValue() Get value from session shared memory from attribute
                    334:  * @s = Session item
                    335:  * @csAttr = Attribute for search
                    336:  * @psVal = Return string buffer
                    337:  * @pnLen = Length of return string buffer, 
                    338:        // *{pnLen} input is max_size of buffer & output is really taken bytes
                    339:  * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
                    340: */
                    341: int sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen);
                    342: /*
                    343:  * sess_DelValue() Delete item from session shared memory
                    344:  * @s = Session item
                    345:  * @csAttr = Attribute for erasing
                    346:  * return: 0 Ok, -1 error: in parameter
                    347: */
                    348: int sess_DelValue(tagSess * __restrict s, const char *csAttr);
                    349: /*
                    350:  * sess_SetValue() Set item into session shared memory or update if find it
                    351:  * @s = Session item
                    352:  * @csAttr = Attribute
                    353:  * @psVal = Value
                    354:  * return: 0 nothing, -1 error: in parameter, 
                    355:        >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
                    356: */
                    357: int sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal);
                    358: 
1.3.2.1   misho     359: /*
                    360:  * sess_prepareSession() Attach to shared memory and de-marshaling data
                    361:  * @s = Session
                    362:  * @useDirect = Use direct shared memory if !=0 or snapshot of data to array
                    363:  * return: NULL error or no data, !=NULL array with variables, 
                    364:  *             after use must free resources with sess_doneSession()
                    365:  */
                    366: array_t *sess_prepareSession(tagSess * __restrict s, unsigned char useDirect);
                    367: /*
                    368:  * sess_doneSession() Free resources allocated with sess_prepareSession()
                    369:  * @s = Session
                    370:  * @arr = Array with variables for free
                    371:  * return: none
                    372:  */
                    373: void sess_doneSession(tagSess * __restrict s, array_t ** __restrict arr);
                    374: /*
                    375:  * sess_commitSession() Commit data to shared memory
                    376:  * @s = Session
                    377:  * @arr = Array with variables for save
                    378:  * return -1 error or !=-1 size of stored variables into shared memory
                    379:  */
                    380: int sess_commitSession(tagSess * __restrict s, array_t * __restrict arr);
                    381: 
1.1       misho     382: 
                    383: #endif

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