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

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.4 ! misho       6: * $Id: aitsess.h,v 1.3.2.3 2012/02/10 16:45:36 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.2.3   misho      55: #define SHARED_UNKNOWN -1
                     56: #define SHARED_IPC     0
                     57: #define SHARED_MAP     1
1.1       misho      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: 
1.3.2.3   misho      70: struct tagSess;
                     71: typedef struct tagSess ait_sess_t;
                     72: 
                     73: struct tagSess {
1.1       misho      74:        key_t   key;
1.3.2.3   misho      75:        char    type;
                     76:        char    zcpy;
1.1       misho      77:        off_t   eom;
                     78:        void    *addr;
                     79:        off_t   offset;
                     80:        union {
                     81:                int     shmid;
                     82:                int     fd;
                     83:        } mem;
                     84:        union {
                     85:                int     semid;
                     86:                sem_t   *sid;
                     87:        } id;
1.3.2.3   misho      88: 
                     89:        struct {
                     90:                int (*create)(const char *, int, long, ait_sess_t * __restrict, ...);
                     91:                void (*destroy)(const char*, ait_sess_t * __restrict);
                     92:                void *(*attach)(ait_sess_t * __restrict, void *);
                     93:                void (*detach)(ait_sess_t * __restrict);
                     94:                void (*notSem)(ait_sess_t * __restrict);
                     95:                int (*isSemOK)(ait_sess_t * __restrict);
                     96:                int (*incSem)(ait_sess_t * __restrict);
                     97:                int (*decSem)(ait_sess_t * __restrict);
                     98:        } sess;
                     99: };
1.1       misho     100: 
                    101: 
                    102: // -------------------------------------------------------
                    103: // sess_GetErrno() Get error code of last operation
                    104: inline int sess_GetErrno();
                    105: // sess_GetError() Get error text of last operation
                    106: inline const char *sess_GetError();
1.3.2.1   misho     107: // sess_SetErr() Set error to variables for internal use!!!
                    108: inline void sess_SetErr(int eno, char *estr, ...);
1.1       misho     109: // -------------------------------------------------------
                    110: 
                    111: 
                    112: /*
1.3.2.3   misho     113:  * sess_initSession() Initializing session structure, if session file not exists creating with specified tech
                    114:  *
                    115:  * @id = Technology using in session. SHARED_IPC IPC tech orSHARED_MAP BSD MemoryMap tech
1.1       misho     116:  * @csFName = Session filename for build key and identified
1.3       misho     117:  * @Sess = Session item, if =NULL allocate memory for session after use must be free!
1.1       misho     118:  * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
1.3.2.3   misho     119:  */
                    120: int sess_initSession(int id, const char *csFName, ait_sess_t ** __restrict Sess);
1.1       misho     121: /*
1.3.2.3   misho     122:  * sess_freeSession() Free allocated memory for session item and delete session file if present name
                    123:  *
1.1       misho     124:  * @csFName = Session filename for delete, if NULL nothing delete
                    125:  * @Sess = Session item
1.3.2.3   misho     126:  * return: none
                    127:  */
                    128: void sess_freeSession(const char *csFName, ait_sess_t ** __restrict Sess);
                    129: 
1.1       misho     130: 
                    131: /*
                    132:  * map_createSession() MMAP Created session and allocated resources
1.3.2.3   misho     133:  *
1.1       misho     134:  * @csFName = Session name for identified
1.3.2.3   misho     135:  * @nSeed = Seed for securing key, if =-1 must add ready for use key
                    136:  * @nSize = Allocated shared memory size in bytes
1.1       misho     137:  * @Sess = Session item
1.3.2.3   misho     138:  * @... = If nSeed == -1 add ready for use key value
1.1       misho     139:  * return: 0 Ok successful, -1 error: not allocated resources
1.3.2.3   misho     140:  */
                    141: int map_createSession(const char *csFName, int nSeed, long nSize, ait_sess_t * __restrict Sess, ...);
1.1       misho     142: /*
                    143:  * map_destroySession() MMAP free shared resources
1.3.2.3   misho     144:  *
1.1       misho     145:  * @csFName = Session name for delete
                    146:  * @Sess = Session item
1.3.2.3   misho     147:  * return: none
                    148:  */
                    149: void map_destroySession(const char *csFName, ait_sess_t * __restrict Sess);
1.1       misho     150: 
                    151: /*
                    152:  * ipc_createSession() IPC Created session and allocated resources
1.3.2.3   misho     153:  *
1.1       misho     154:  * @csFName = Session name for identified
1.3.2.3   misho     155:  * @nSeed = Seed for securing key, if =-1 must add ready for use key
                    156:  * @nSize = Allocated shared memory size in bytes
1.1       misho     157:  * @Sess = Session item
1.3.2.3   misho     158:  * @... = If nSeed == -1 add ready for use key value
1.1       misho     159:  * return: 0 Ok successful, -1 error: not allocated resources
1.3.2.3   misho     160:  */
                    161: int ipc_createSession(const char *csFName, int nSeed, long nSize, ait_sess_t * __restrict Sess, ...);
1.1       misho     162: /*
                    163:  * ipc_destroySession() IPC free shared resources
1.3.2.3   misho     164:  *
1.1       misho     165:  * @csFName = Session name for delete
                    166:  * @Sess = Session item
1.3.2.3   misho     167:  * return: none
                    168:  */
                    169: void ipc_destroySession(const char *csFName, ait_sess_t * __restrict Sess);
1.1       misho     170: 
                    171: /*
                    172:  * map_attachSession() MMAP Attach to shared memory & return begin address
1.3.2.3   misho     173:  *
1.1       misho     174:  * @s = Session item
                    175:  * @procMem = Custom start address (optionl) *default must be 0*
                    176:  * return: NULL failed attach, !=NULL begin address of memory
1.3.2.3   misho     177:  */
                    178: void *map_attachSession(ait_sess_t * __restrict s, void *procMem);
1.1       misho     179: /*
                    180:  * map_detachSession() MMAP Detach from shared memory
1.3.2.3   misho     181:  *
1.1       misho     182:  * @s = Session item
1.3.2.3   misho     183:  * return: none
                    184:  */
                    185: void map_detachSession(ait_sess_t * __restrict s);
1.1       misho     186: 
                    187: /*
                    188:  * ipc_attachSession() IPC Attach to shared memory & return begin address
1.3.2.3   misho     189:  *
1.1       misho     190:  * @s = Session item
                    191:  * @procMem = Custom start address (optionl) *default must be 0*
                    192:  * return: NULL failed attach, !=NULL begin address of memory
1.3.2.3   misho     193:  */
                    194: void *ipc_attachSession(ait_sess_t * __restrict s, void *procMem);
1.1       misho     195: /*
                    196:  * ipc_detachSession() IPC Detach from shared memory
1.3.2.3   misho     197:  *
1.1       misho     198:  * @s = Session item
1.3.2.3   misho     199:  * return: none
                    200:  */
                    201: void ipc_detachSession(ait_sess_t * __restrict s);
1.1       misho     202: 
                    203: /*
1.3.2.3   misho     204:  * sess_isAttached() Check for attached shared memory
                    205:  *
1.2       misho     206:  * @s = Session item
                    207:  * return: -1 null session item, 0 not attached, 1 attached memory
1.3.2.3   misho     208:  */
                    209: inline int sess_isAttached(ait_sess_t * __restrict s);
1.2       misho     210: 
                    211: 
                    212: /*
1.1       misho     213:  * map_notSemaphore() MMAP negative block if semaphore isn`t signaled
1.3.2.3   misho     214:  *
1.1       misho     215:  * @s = Session item
1.3.2.3   misho     216:  * return: none
                    217:  */
                    218: void map_notSemaphore(ait_sess_t * __restrict s);
1.1       misho     219: /*
1.3.2.3   misho     220:  * map_isSemaphoreOK() MMAP Check semaphore
                    221:  *
1.1       misho     222:  * @s = Session item
                    223:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
1.3.2.3   misho     224:  */
                    225: int map_isSemaphoreOK(ait_sess_t * __restrict s);
1.1       misho     226: /*
1.3.2.3   misho     227:  * map_incSemaphore() MMAP unblock semaphore, increment semaphore
                    228:  *
1.1       misho     229:  * @s = Session item
                    230:  * return: 0 Ok, -1 error: can`t increment 
1.3.2.3   misho     231:  */
                    232: int map_incSemaphore(ait_sess_t * __restrict s);
1.1       misho     233: /*
                    234:  * map_decSemaphore() MMAP block semaphore, decrement semaphore
1.3.2.3   misho     235:  *
1.1       misho     236:  * @s = Session item
                    237:  * return: 0 Ok, -1 error: can`t decrement 
1.3.2.3   misho     238:  */
                    239: int map_decSemaphore(ait_sess_t * __restrict s);
1.1       misho     240: 
                    241: /*
                    242:  * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled
1.3.2.3   misho     243:  *
1.1       misho     244:  * @s = Session item
1.3.2.3   misho     245:  * return: none
                    246:  */
                    247: void ipc_notSemaphore(ait_sess_t * __restrict s);
1.1       misho     248: /*
1.3.2.3   misho     249:  * ipc_isSemaphoreOK() IPC Check semaphore
                    250:  *
1.1       misho     251:  * @s = Session item
                    252:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
1.3.2.3   misho     253:  */
                    254: int ipc_isSemaphoreOK(ait_sess_t * __restrict s);
1.1       misho     255: /*
1.3.2.3   misho     256:  * ipc_incSemaphore() IPC unblock semaphore, increment semaphore
                    257:  *
1.1       misho     258:  * @s = Session item
                    259:  * return: 0 Ok, -1 error: can`t increment 
1.3.2.3   misho     260:  */
                    261: int ipc_incSemaphore(ait_sess_t * __restrict s);
1.1       misho     262: /*
                    263:  * ipc_decSemaphore() IPC block semaphore, decrement semaphore
1.3.2.3   misho     264:  *
1.1       misho     265:  * @s = Session item
                    266:  * return: 0 Ok, -1 error: can`t decrement 
1.3.2.3   misho     267:  */
                    268: int ipc_decSemaphore(ait_sess_t * __restrict s);
                    269: 
                    270: 
                    271: /* --------------------------------------------------------- */
1.1       misho     272: 
1.3.2.4 ! misho     273: #define ALLOC_MEMORY(fname, sd, siz, s, ...)   (assert((s)), (s)->sess.create((fname), (sd), \
1.3.2.3   misho     274:                                                                (siz), (s), ## __VA_ARGS__))
1.3.2.4 ! misho     275: #define FREE_MEMORY(fname, s)                  do { assert((s)); \
1.3.2.3   misho     276:                                                        (s)->sess.destroy((fname), (s)); \
                    277:                                                } while(0)
                    278: #define ATTACH_MEMORY(s)                       (assert((s)), (s)->sess.attach((s), NULL))
                    279: #define DETACH_MEMORY(s)                       do { assert((s)); (s)->sess.detach((s)); } while(0)
1.1       misho     280: 
1.3.2.3   misho     281: #define IS_SEMOK(s)                            (assert((s)), (s)->sess.isSemOK((s)))
                    282: #define INC_SEM(s)                             (assert((s)), (s)->sess.incSem((s)))
                    283: #define DEC_SEM(s)                             (assert((s)), (s)->sess.decSem((s)))
                    284: #define NOT_SEM(s)                             do { assert((s)); (s)->sess.notSem((s)); } while(0)
                    285: 
                    286: /* --------------------------------------------------------- */
1.1       misho     287: 
                    288: /*
1.2       misho     289:  * sess_FreeValues() Free all values from value array allocated from sess_GetValues()
1.3.2.3   misho     290:  *
1.2       misho     291:  * @ppsVals = Array strings
                    292:  * return: none
1.3.2.3   misho     293:  */
1.2       misho     294: inline void sess_FreeValues(char *** __restrict ppsVals);
                    295: /*
                    296:  * sess_GetValues() Get all values from session shared memory
1.3.2.3   misho     297:  *
1.2       misho     298:  * @s = Session item
                    299:  * @ppsVals = Return array strings
                    300:  * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)
1.3.2.3   misho     301:  */
                    302: int sess_GetValues(ait_sess_t * __restrict s, char ***ppsVals);
1.2       misho     303: /*
1.1       misho     304:  * sess_GetValue() Get value from session shared memory from attribute
1.3.2.3   misho     305:  *
1.1       misho     306:  * @s = Session item
                    307:  * @csAttr = Attribute for search
                    308:  * @psVal = Return string buffer
                    309:  * @pnLen = Length of return string buffer, 
                    310:        // *{pnLen} input is max_size of buffer & output is really taken bytes
                    311:  * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
1.3.2.3   misho     312:  */
                    313: int sess_GetValue(ait_sess_t * __restrict s, const char *csAttr, char *psVal, int *pnLen);
1.1       misho     314: /*
                    315:  * sess_DelValue() Delete item from session shared memory
1.3.2.3   misho     316:  *
1.1       misho     317:  * @s = Session item
                    318:  * @csAttr = Attribute for erasing
                    319:  * return: 0 Ok, -1 error: in parameter
1.3.2.3   misho     320:  */
                    321: int sess_DelValue(ait_sess_t * __restrict s, const char *csAttr);
1.1       misho     322: /*
                    323:  * sess_SetValue() Set item into session shared memory or update if find it
1.3.2.3   misho     324:  *
1.1       misho     325:  * @s = Session item
                    326:  * @csAttr = Attribute
                    327:  * @psVal = Value
                    328:  * return: 0 nothing, -1 error: in parameter, 
                    329:        >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
1.3.2.3   misho     330:  */
                    331: int sess_SetValue(ait_sess_t * __restrict s, const char *csAttr, const char *psVal);
1.1       misho     332: 
1.3.2.1   misho     333: /*
                    334:  * sess_prepareSession() Attach to shared memory and de-marshaling data
1.3.2.3   misho     335:  *
1.3.2.1   misho     336:  * @s = Session
                    337:  * @useDirect = Use direct shared memory if !=0 or snapshot of data to array
                    338:  * return: NULL error or no data, !=NULL array with variables, 
                    339:  *             after use must free resources with sess_doneSession()
                    340:  */
1.3.2.3   misho     341: array_t *sess_prepareSession(ait_sess_t * __restrict s, char useDirect);
1.3.2.1   misho     342: /*
                    343:  * sess_doneSession() Free resources allocated with sess_prepareSession()
1.3.2.3   misho     344:  *
1.3.2.1   misho     345:  * @s = Session
                    346:  * @arr = Array with variables for free
                    347:  * return: none
                    348:  */
1.3.2.3   misho     349: void sess_doneSession(ait_sess_t * __restrict s, array_t ** __restrict arr);
1.3.2.1   misho     350: /*
                    351:  * sess_commitSession() Commit data to shared memory
1.3.2.3   misho     352:  *
1.3.2.1   misho     353:  * @s = Session
                    354:  * @arr = Array with variables for save
                    355:  * return -1 error or !=-1 size of stored variables into shared memory
                    356:  */
1.3.2.3   misho     357: int sess_commitSession(ait_sess_t * __restrict s, array_t * __restrict arr);
1.3.2.1   misho     358: 
1.1       misho     359: 
                    360: #endif

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