Annotation of libaitsess/inc/aitsess.h, revision 1.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 $
        !             6: * $Id: aitsess.h,v 1.1.1.1.2.5 2010/09/01 21:07:10 misho Exp $
        !             7: *
        !             8: *************************************************************************/
1.1       misho       9: #ifndef __AITSESS_H
                     10: #define __AITSESS_H
                     11: 
                     12: 
                     13: #include <semaphore.h>
                     14: #include <sys/types.h>
                     15: 
                     16: #define SHARED_IPC     1
                     17: #define SHARED_MAP     2
                     18: 
                     19: #define IS_VAL         0x0
                     20: #define IS_ADD         0x40000000
                     21: #define IS_DEF         0x80000000
                     22: 
                     23: #define ISNEW(x)       (((x) & IS_ADD) == IS_ADD)
                     24: #define ISDEF(x)       (((x) & IS_DEF) == IS_DEF)
                     25: 
                     26: #define MAX_ATTRIBUTE  63
                     27: #define MAX_SEMNAME    14
                     28: 
                     29: 
                     30: typedef struct _tagSess {
                     31:        key_t   key;
                     32:        u_char  type;
                     33:        off_t   eom;
                     34:        void    *addr;
                     35:        off_t   offset;
                     36:        union {
                     37:                int     shmid;
                     38:                int     fd;
                     39:        } mem;
                     40:        union {
                     41:                int     semid;
                     42:                sem_t   *sid;
                     43:        } id;
                     44: } tagSess;
                     45: 
                     46: 
                     47: // -------------------------------------------------------
                     48: // sess_GetErrno() Get error code of last operation
                     49: inline int sess_GetErrno();
                     50: // sess_GetError() Get error text of last operation
                     51: inline const char *sess_GetError();
                     52: // -------------------------------------------------------
                     53: 
                     54: 
                     55: /*
                     56:  * initSession() Initializing session structure, if session file not exists creating with specified tech
                     57:  * @cnID = Technology using in session. SHARED_IPC IPC tech; SHARED_MAP BSD MemoryMap tech
                     58:  * @csFName = Session filename for build key and identified
                     59:  * @Sess = Session item
                     60:  * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
                     61: */
                     62: inline int initSession(const int cnID, const char *csFName, tagSess ** __restrict Sess);
                     63: /*
                     64:  * freeSession() Free allocated memory for session item and delete session file if present name
                     65:  * @csFName = Session filename for delete, if NULL nothing delete
                     66:  * @Sess = Session item
                     67: */
                     68: inline void freeSession(const char *csFName, tagSess ** __restrict Sess);
                     69: 
                     70: /*
                     71:  * map_createSession() MMAP Created session and allocated resources
                     72:  * @csFName = Session name for identified
                     73:  * @cnSeed = Seed for securing key
                     74:  * @cnSize = Allocated shared memory size in bytes
                     75:  * @Sess = Session item
                     76:  * return: 0 Ok successful, -1 error: not allocated resources
                     77: */
                     78: int map_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess);
                     79: /*
                     80:  * map_destroySession() MMAP free shared resources
                     81:  * @csFName = Session name for delete
                     82:  * @Sess = Session item
                     83: */
                     84: void map_destroySession(const char *csFName, tagSess ** __restrict Sess);
                     85: 
                     86: /*
                     87:  * ipc_createSession() IPC Created session and allocated resources
                     88:  * @csFName = Session name for identified
                     89:  * @cnSeed = Seed for securing key
                     90:  * @cnSize = Allocated shared memory size in bytes
                     91:  * @Sess = Session item
                     92:  * return: 0 Ok successful, -1 error: not allocated resources
                     93: */
                     94: int ipc_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess);
                     95: /*
                     96:  * ipc_destroySession() IPC free shared resources
                     97:  * @csFName = Session name for delete
                     98:  * @Sess = Session item
                     99: */
                    100: void ipc_destroySession(const char *csFName, tagSess ** __restrict Sess);
                    101: 
                    102: /*
                    103:  * map_attachSession() MMAP Attach to shared memory & return begin address
                    104:  * @s = Session item
                    105:  * @procMem = Custom start address (optionl) *default must be 0*
                    106:  * return: NULL failed attach, !=NULL begin address of memory
                    107: */
                    108: inline void *map_attachSession(tagSess * __restrict s, void *procMem);
                    109: /*
                    110:  * map_detachSession() MMAP Detach from shared memory
                    111:  * @s = Session item
                    112: */
                    113: inline void map_detachSession(tagSess * __restrict s);
                    114: 
                    115: /*
                    116:  * ipc_attachSession() IPC Attach to shared memory & return begin address
                    117:  * @s = Session item
                    118:  * @procMem = Custom start address (optionl) *default must be 0*
                    119:  * return: NULL failed attach, !=NULL begin address of memory
                    120: */
                    121: inline void *ipc_attachSession(tagSess * __restrict s, void *procMem);
                    122: /*
                    123:  * ipc_detachSession() IPC Detach from shared memory
                    124:  * @s = Session item
                    125: */
                    126: inline void ipc_detachSession(tagSess * __restrict s);
                    127: 
                    128: /*
1.2     ! misho     129:  * isAttached() Check for mapped/(attached) shared memory
        !           130:  * @s = Session item
        !           131:  * return: -1 null session item, 0 not attached, 1 attached memory
        !           132: */
        !           133: inline int isAttached(tagSess * __restrict s);
        !           134: 
        !           135: 
        !           136: /*
1.1       misho     137:  * map_notSemaphore() MMAP negative block if semaphore isn`t signaled
                    138:  * @s = Session item
                    139: */
                    140: inline void map_notSemaphore(tagSess * __restrict s);
                    141: /*
                    142:  * map_isSemaphored() MMAP Check semaphore
                    143:  * @s = Session item
                    144:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
                    145: */
                    146: inline int map_isSemaphored(tagSess * __restrict s);
                    147: /*
                    148:  * map_addSemaphore() MMAP unblock semaphore, increment semaphore
                    149:  * @s = Session item
                    150:  * return: 0 Ok, -1 error: can`t increment 
                    151: */
                    152: inline int map_addSemaphore(tagSess * __restrict s);
                    153: /*
                    154:  * map_decSemaphore() MMAP block semaphore, decrement semaphore
                    155:  * @s = Session item
                    156:  * return: 0 Ok, -1 error: can`t decrement 
                    157: */
                    158: inline int map_decSemaphore(tagSess * __restrict s);
                    159: 
                    160: /*
                    161:  * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled
                    162:  * @s = Session item
                    163: */
                    164: inline void ipc_notSemaphore(tagSess * __restrict s);
                    165: /*
                    166:  * ipc_isSemaphored() IPC Check semaphore
                    167:  * @s = Session item
                    168:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
                    169: */
                    170: inline int ipc_isSemaphored(tagSess * __restrict s);
                    171: /*
                    172:  * ipc_addSemaphore() IPC unblock semaphore, increment semaphore
                    173:  * @s = Session item
                    174:  * return: 0 Ok, -1 error: can`t increment 
                    175: */
                    176: inline int ipc_addSemaphore(tagSess * __restrict s);
                    177: /*
                    178:  * ipc_decSemaphore() IPC block semaphore, decrement semaphore
                    179:  * @s = Session item
                    180:  * return: 0 Ok, -1 error: can`t decrement 
                    181: */
                    182: inline int ipc_decSemaphore(tagSess * __restrict s);
                    183: 
1.2     ! misho     184: 
1.1       misho     185: // ---------------------------------------------------------
                    186: //
                    187: //  Lazy macros for lazy programmers :-) by Michael Pounov; Optimizing work with sessions!
                    188: //
                    189: 
                    190: #define DESTROY_SESSION(fname, s)      do { \
1.2     ! misho     191:                                                switch (((*s))->type) { \
1.1       misho     192:                                                        case SHARED_IPC: \
                    193:                                                                         ipc_destroySession((fname), (s)); \
                    194:                                                                         break; \
                    195:                                                        case SHARED_MAP: \
                    196:                                                                         map_destroySession((fname), (s)); \
                    197:                                                                         break; \
                    198:                                                } \
                    199:                                        } while(0)
                    200: 
1.2     ! misho     201: #define ATTACH_MEMORY(s)               do { \
1.1       misho     202:                                                switch ((s)->type) { \
                    203:                                                        case SHARED_IPC: \
1.2     ! misho     204:                                                                         ipc_attachSession((s), 0); \
1.1       misho     205:                                                                         break; \
                    206:                                                        case SHARED_MAP: \
1.2     ! misho     207:                                                                         map_attachSession((s), 0); \
1.1       misho     208:                                                                         break; \
                    209:                                                } \
                    210:                                        } while(0)
                    211: 
                    212: #define DETACH_MEMORY(s)               do { \
                    213:                                                switch ((s)->type) { \
                    214:                                                        case SHARED_IPC: \
                    215:                                                                         ipc_detachSession((s)); \
                    216:                                                                         break; \
                    217:                                                        case SHARED_MAP: \
                    218:                                                                         map_detachSession((s)); \
                    219:                                                                         break; \
                    220:                                                } \
                    221:                                        } while(0)
                    222: 
                    223: #define NOT_SEMAPHORE(s)       do { \
                    224:                                        switch ((s)->type) { \
                    225:                                                case SHARED_IPC: \
                    226:                                                                 ipc_notSemaphore((s)); \
                    227:                                                                 break; \
                    228:                                                case SHARED_MAP: \
                    229:                                                                 map_notSemaphore((s)); \
                    230:                                                                 break; \
                    231:                                        } \
                    232:                                } while(0)
                    233: 
                    234: #define IS_SEMAPHORED(s, ret)  do { \
                    235:                                        switch ((s)->type) { \
                    236:                                                case SHARED_IPC: \
                    237:                                                                 (ret) = ipc_isSemaphored((s)); \
                    238:                                                                 break; \
                    239:                                                case SHARED_MAP: \
                    240:                                                                 (ret) = map_isSemaphored((s)); \
                    241:                                                                 break; \
                    242:                                                default: \
                    243:                                                         (ret) = -1; \
                    244:                                        } \
                    245:                                } while(0)
                    246: 
                    247: #define ADD_SEMAPHORE(s, ret)  do { \
                    248:                                        switch ((s)->type) { \
                    249:                                                case SHARED_IPC: \
                    250:                                                                 (ret) = ipc_addSemaphore((s)); \
                    251:                                                                 break; \
                    252:                                                case SHARED_MAP: \
                    253:                                                                 (ret) = map_addSemaphore((s)); \
                    254:                                                                 break; \
                    255:                                                default: \
                    256:                                                         (ret) = -1; \
                    257:                                        } \
                    258:                                } while(0)
                    259: 
                    260: #define DEC_SEMAPHORE(s, ret)  do { \
                    261:                                        switch ((s)->type) { \
                    262:                                                case SHARED_IPC: \
                    263:                                                                 (ret) = ipc_decSemaphore((s)); \
                    264:                                                                 break; \
                    265:                                                case SHARED_MAP: \
                    266:                                                                 (ret) = map_decSemaphore((s)); \
                    267:                                                                 break; \
                    268:                                                default: \
                    269:                                                         (ret) = -1; \
                    270:                                        } \
                    271:                                } while(0)
                    272: 
                    273: // ---------------------------------------------------------
                    274: 
                    275: /*
1.2     ! misho     276:  * sess_FreeValues() Free all values from value array allocated from sess_GetValues()
        !           277:  * @ppsVals = Array strings
        !           278:  * return: none
        !           279: */
        !           280: inline void sess_FreeValues(char *** __restrict ppsVals);
        !           281: /*
        !           282:  * sess_GetValues() Get all values from session shared memory
        !           283:  * @s = Session item
        !           284:  * @ppsVals = Return array strings
        !           285:  * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)
        !           286: */
        !           287: int sess_GetValues(tagSess * __restrict s, char ***ppsVals);
        !           288: /*
1.1       misho     289:  * sess_GetValue() Get value from session shared memory from attribute
                    290:  * @s = Session item
                    291:  * @csAttr = Attribute for search
                    292:  * @psVal = Return string buffer
                    293:  * @pnLen = Length of return string buffer, 
                    294:        // *{pnLen} input is max_size of buffer & output is really taken bytes
                    295:  * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
                    296: */
                    297: int sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen);
                    298: /*
                    299:  * sess_DelValue() Delete item from session shared memory
                    300:  * @s = Session item
                    301:  * @csAttr = Attribute for erasing
                    302:  * return: 0 Ok, -1 error: in parameter
                    303: */
                    304: int sess_DelValue(tagSess * __restrict s, const char *csAttr);
                    305: /*
                    306:  * sess_SetValue() Set item into session shared memory or update if find it
                    307:  * @s = Session item
                    308:  * @csAttr = Attribute
                    309:  * @psVal = Value
                    310:  * return: 0 nothing, -1 error: in parameter, 
                    311:        >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
                    312: */
                    313: int sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal);
                    314: 
                    315: 
                    316: #endif

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