version 1.3.2.2, 2011/09/07 16:08:41
|
version 1.3.2.5, 2012/02/10 17:14:21
|
Line 52 SUCH DAMAGE.
|
Line 52 SUCH DAMAGE.
|
#include <sys/types.h> |
#include <sys/types.h> |
#include <aitio.h> |
#include <aitio.h> |
|
|
#define SHARED_UNKNOWN 0 | #define SHARED_UNKNOWN -1 |
#define SHARED_IPC 1 | #define SHARED_IPC 0 |
#define SHARED_MAP 2 | #define SHARED_MAP 1 |
|
|
#define IS_VAL 0x0 |
#define IS_VAL 0x0 |
#define IS_ADD 0x40000000 |
#define IS_ADD 0x40000000 |
Line 67 SUCH DAMAGE.
|
Line 67 SUCH DAMAGE.
|
#define MAX_SEMNAME 14 |
#define MAX_SEMNAME 14 |
|
|
|
|
typedef struct _tagSess { | struct tagSess; |
| typedef struct tagSess ait_sess_t; |
| |
| struct tagSess { |
key_t key; |
key_t key; |
u_char type; | char type; |
u_char zcopy; | char zcpy; |
| |
| char name[BUFSIZ]; |
off_t eom; |
off_t eom; |
void *addr; |
void *addr; |
off_t offset; |
off_t offset; |
Line 82 typedef struct _tagSess {
|
Line 87 typedef struct _tagSess {
|
int semid; |
int semid; |
sem_t *sid; |
sem_t *sid; |
} id; |
} id; |
} tagSess; |
|
|
|
|
struct { |
|
int (*create)(int, long, ait_sess_t * __restrict, ...); |
|
void (*destroy)(ait_sess_t * __restrict); |
|
void *(*attach)(ait_sess_t * __restrict, void *); |
|
void (*detach)(ait_sess_t * __restrict); |
|
void (*notSem)(ait_sess_t * __restrict); |
|
int (*isSemOK)(ait_sess_t * __restrict); |
|
int (*incSem)(ait_sess_t * __restrict); |
|
int (*decSem)(ait_sess_t * __restrict); |
|
} sess; |
|
}; |
|
|
|
|
// ------------------------------------------------------- |
// ------------------------------------------------------- |
// sess_GetErrno() Get error code of last operation |
// sess_GetErrno() Get error code of last operation |
inline int sess_GetErrno(); |
inline int sess_GetErrno(); |
Line 96 inline void sess_SetErr(int eno, char *estr, ...);
|
Line 112 inline void sess_SetErr(int eno, char *estr, ...);
|
|
|
|
|
/* |
/* |
* initSession() Initializing session structure, if session file not exists creating with specified tech | * sess_initSession() Initializing session structure, if session file not exists creating with specified tech |
* @pnID = Technology using in session. | * |
SHARED_IPC IPC tech; SHARED_MAP BSD MemoryMap tech or if =NULL SHARED_IPC | * @id = Technology using in session. SHARED_IPC IPC tech orSHARED_MAP BSD MemoryMap tech |
* @csFName = Session filename for build key and identified |
* @csFName = Session filename for build key and identified |
* @Sess = Session item, if =NULL allocate memory for session after use must be free! |
* @Sess = Session item, if =NULL allocate memory for session after use must be free! |
* return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded |
* return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded |
*/ | */ |
inline int initSession(int *pnID, const char *csFName, tagSess ** __restrict Sess); | int sess_initSession(int id, const char *csFName, ait_sess_t ** __restrict Sess); |
/* |
/* |
* freeSession() Free allocated memory for session item and delete session file if present name | * sess_freeSession() Free allocated memory for session item and delete session file if present name |
| * |
* @csFName = Session filename for delete, if NULL nothing delete |
* @csFName = Session filename for delete, if NULL nothing delete |
* @Sess = Session item |
* @Sess = Session item |
*/ | * return: none |
inline void freeSession(const char *csFName, tagSess ** __restrict Sess); | */ |
| void sess_freeSession(const char *csFName, ait_sess_t ** __restrict Sess); |
|
|
|
|
/* |
/* |
* map_createSession() MMAP Created session and allocated resources |
* map_createSession() MMAP Created session and allocated resources |
* @csFName = Session name for identified | * |
* @cnSeed = Seed for securing key | * @nSeed = Seed for securing key, if =-1 must add ready for use key |
* @cnSize = Allocated shared memory size in bytes | * @nSize = Allocated shared memory size in bytes |
* @Sess = Session item |
* @Sess = Session item |
|
* @... = If nSeed == -1 add ready for use key value |
* return: 0 Ok successful, -1 error: not allocated resources |
* return: 0 Ok successful, -1 error: not allocated resources |
*/ | */ |
int map_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess); | int map_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...); |
/* |
/* |
* map_destroySession() MMAP free shared resources |
* map_destroySession() MMAP free shared resources |
* @csFName = Session name for delete | * |
* @Sess = Session item |
* @Sess = Session item |
*/ | * return: none |
void map_destroySession(const char *csFName, tagSess ** __restrict Sess); | */ |
| void map_destroySession(ait_sess_t * __restrict Sess); |
|
|
/* |
/* |
* ipc_createSession() IPC Created session and allocated resources |
* ipc_createSession() IPC Created session and allocated resources |
* @csFName = Session name for identified | * |
* @cnSeed = Seed for securing key | * @nSeed = Seed for securing key, if =-1 must add ready for use key |
* @cnSize = Allocated shared memory size in bytes | * @nSize = Allocated shared memory size in bytes |
* @Sess = Session item |
* @Sess = Session item |
|
* @... = If nSeed == -1 add ready for use key value |
* return: 0 Ok successful, -1 error: not allocated resources |
* return: 0 Ok successful, -1 error: not allocated resources |
*/ | */ |
int ipc_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess); | int ipc_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...); |
/* |
/* |
* ipc_destroySession() IPC free shared resources |
* ipc_destroySession() IPC free shared resources |
* @csFName = Session name for delete | * |
* @Sess = Session item |
* @Sess = Session item |
*/ | * return: none |
void ipc_destroySession(const char *csFName, tagSess ** __restrict Sess); | */ |
| void ipc_destroySession(ait_sess_t * __restrict Sess); |
|
|
/* |
/* |
* map_attachSession() MMAP Attach to shared memory & return begin address |
* map_attachSession() MMAP Attach to shared memory & return begin address |
|
* |
* @s = Session item |
* @s = Session item |
* @procMem = Custom start address (optionl) *default must be 0* |
* @procMem = Custom start address (optionl) *default must be 0* |
* return: NULL failed attach, !=NULL begin address of memory |
* return: NULL failed attach, !=NULL begin address of memory |
*/ | */ |
inline void *map_attachSession(tagSess * __restrict s, void *procMem); | void *map_attachSession(ait_sess_t * __restrict s, void *procMem); |
/* |
/* |
* map_detachSession() MMAP Detach from shared memory |
* map_detachSession() MMAP Detach from shared memory |
|
* |
* @s = Session item |
* @s = Session item |
*/ | * return: none |
inline void map_detachSession(tagSess * __restrict s); | */ |
| void map_detachSession(ait_sess_t * __restrict s); |
|
|
/* |
/* |
* ipc_attachSession() IPC Attach to shared memory & return begin address |
* ipc_attachSession() IPC Attach to shared memory & return begin address |
|
* |
* @s = Session item |
* @s = Session item |
* @procMem = Custom start address (optionl) *default must be 0* |
* @procMem = Custom start address (optionl) *default must be 0* |
* return: NULL failed attach, !=NULL begin address of memory |
* return: NULL failed attach, !=NULL begin address of memory |
*/ | */ |
inline void *ipc_attachSession(tagSess * __restrict s, void *procMem); | void *ipc_attachSession(ait_sess_t * __restrict s, void *procMem); |
/* |
/* |
* ipc_detachSession() IPC Detach from shared memory |
* ipc_detachSession() IPC Detach from shared memory |
|
* |
* @s = Session item |
* @s = Session item |
*/ | * return: none |
inline void ipc_detachSession(tagSess * __restrict s); | */ |
| void ipc_detachSession(ait_sess_t * __restrict s); |
|
|
/* |
/* |
* isAttached() Check for mapped/(attached) shared memory | * sess_isAttached() Check for attached shared memory |
| * |
* @s = Session item |
* @s = Session item |
* return: -1 null session item, 0 not attached, 1 attached memory |
* return: -1 null session item, 0 not attached, 1 attached memory |
*/ | */ |
inline int isAttached(tagSess * __restrict s); | inline int sess_isAttached(ait_sess_t * __restrict s); |
|
|
|
|
/* |
/* |
* map_notSemaphore() MMAP negative block if semaphore isn`t signaled |
* map_notSemaphore() MMAP negative block if semaphore isn`t signaled |
|
* |
* @s = Session item |
* @s = Session item |
*/ | * return: none |
inline void map_notSemaphore(tagSess * __restrict s); | */ |
| void map_notSemaphore(ait_sess_t * __restrict s); |
/* |
/* |
* map_isSemaphored() MMAP Check semaphore | * map_isSemaphoreOK() MMAP Check semaphore |
| * |
* @s = Session item |
* @s = Session item |
* return: -1 error: can`t return semaphore, 0 = false, 1 = true |
* return: -1 error: can`t return semaphore, 0 = false, 1 = true |
*/ | */ |
inline int map_isSemaphored(tagSess * __restrict s); | int map_isSemaphoreOK(ait_sess_t * __restrict s); |
/* |
/* |
* map_addSemaphore() MMAP unblock semaphore, increment semaphore | * map_incSemaphore() MMAP unblock semaphore, increment semaphore |
| * |
* @s = Session item |
* @s = Session item |
* return: 0 Ok, -1 error: can`t increment |
* return: 0 Ok, -1 error: can`t increment |
*/ | */ |
inline int map_addSemaphore(tagSess * __restrict s); | int map_incSemaphore(ait_sess_t * __restrict s); |
/* |
/* |
* map_decSemaphore() MMAP block semaphore, decrement semaphore |
* map_decSemaphore() MMAP block semaphore, decrement semaphore |
|
* |
* @s = Session item |
* @s = Session item |
* return: 0 Ok, -1 error: can`t decrement |
* return: 0 Ok, -1 error: can`t decrement |
*/ | */ |
inline int map_decSemaphore(tagSess * __restrict s); | int map_decSemaphore(ait_sess_t * __restrict s); |
|
|
/* |
/* |
* ipc_notSemaphore() IPC negative block if semaphore isn`t signaled |
* ipc_notSemaphore() IPC negative block if semaphore isn`t signaled |
|
* |
* @s = Session item |
* @s = Session item |
*/ | * return: none |
inline void ipc_notSemaphore(tagSess * __restrict s); | */ |
| void ipc_notSemaphore(ait_sess_t * __restrict s); |
/* |
/* |
* ipc_isSemaphored() IPC Check semaphore | * ipc_isSemaphoreOK() IPC Check semaphore |
| * |
* @s = Session item |
* @s = Session item |
* return: -1 error: can`t return semaphore, 0 = false, 1 = true |
* return: -1 error: can`t return semaphore, 0 = false, 1 = true |
*/ | */ |
inline int ipc_isSemaphored(tagSess * __restrict s); | int ipc_isSemaphoreOK(ait_sess_t * __restrict s); |
/* |
/* |
* ipc_addSemaphore() IPC unblock semaphore, increment semaphore | * ipc_incSemaphore() IPC unblock semaphore, increment semaphore |
| * |
* @s = Session item |
* @s = Session item |
* return: 0 Ok, -1 error: can`t increment |
* return: 0 Ok, -1 error: can`t increment |
*/ | */ |
inline int ipc_addSemaphore(tagSess * __restrict s); | int ipc_incSemaphore(ait_sess_t * __restrict s); |
/* |
/* |
* ipc_decSemaphore() IPC block semaphore, decrement semaphore |
* ipc_decSemaphore() IPC block semaphore, decrement semaphore |
|
* |
* @s = Session item |
* @s = Session item |
* return: 0 Ok, -1 error: can`t decrement |
* return: 0 Ok, -1 error: can`t decrement |
*/ | */ |
inline int ipc_decSemaphore(tagSess * __restrict s); | int ipc_decSemaphore(ait_sess_t * __restrict s); |
|
|
|
|
// --------------------------------------------------------- | /* --------------------------------------------------------- */ |
// | |
// Lazy macros for lazy programmers :-) by Michael Pounov; Optimizing work with sessions! | |
// | |
|
|
#define DESTROY_SESSION(fname, s) do { assert((s)); assert((*s)); \ | #define ALLOC_MEMORY(sd, siz, s, ...) (assert((s)), (s)->sess.create((sd), \ |
switch (((*s))->type) { \ | (siz), (s), ## __VA_ARGS__)) |
case SHARED_IPC: \ | #define ATTACH_MEMORY(s) (assert((s)), (s)->sess.attach((s), NULL)) |
ipc_destroySession((fname), (s)); \ | #define DETACH_MEMORY(s) do { assert((s)); (s)->sess.detach((s)); } while(0) |
break; \ | #define FREE_MEMORY(s) do { assert((s)); (s)->sess.destroy((s)); } while(0) |
case SHARED_MAP: \ | |
map_destroySession((fname), (s)); \ | |
break; \ | |
} \ | |
} while(0) | |
|
|
#define ATTACH_MEMORY(s) do { assert((s)); \ | #define IS_SEMOK(s) (assert((s)), (s)->sess.isSemOK((s))) |
switch ((s)->type) { \ | #define INC_SEM(s) (assert((s)), (s)->sess.incSem((s))) |
case SHARED_IPC: \ | #define DEC_SEM(s) (assert((s)), (s)->sess.decSem((s))) |
ipc_attachSession((s), 0); \ | #define NOT_SEM(s) do { assert((s)); (s)->sess.notSem((s)); } while(0) |
break; \ | |
case SHARED_MAP: \ | |
map_attachSession((s), 0); \ | |
break; \ | |
} \ | |
} while(0) | |
|
|
#define DETACH_MEMORY(s) do { assert((s)); \ | /* --------------------------------------------------------- */ |
switch ((s)->type) { \ | |
case SHARED_IPC: \ | |
ipc_detachSession((s)); \ | |
break; \ | |
case SHARED_MAP: \ | |
map_detachSession((s)); \ | |
break; \ | |
} \ | |
} while(0) | |
|
|
#define NOT_SEMAPHORE(s) do { assert((s)); \ |
|
switch ((s)->type) { \ |
|
case SHARED_IPC: \ |
|
ipc_notSemaphore((s)); \ |
|
break; \ |
|
case SHARED_MAP: \ |
|
map_notSemaphore((s)); \ |
|
break; \ |
|
} \ |
|
} while(0) |
|
|
|
#define IS_SEMAPHORED(s, ret) do { assert((s)); \ |
|
switch ((s)->type) { \ |
|
case SHARED_IPC: \ |
|
(ret) = ipc_isSemaphored((s)); \ |
|
break; \ |
|
case SHARED_MAP: \ |
|
(ret) = map_isSemaphored((s)); \ |
|
break; \ |
|
default: \ |
|
(ret) = -1; \ |
|
} \ |
|
} while(0) |
|
|
|
#define ADD_SEMAPHORE(s, ret) do { assert((s)); \ |
|
switch ((s)->type) { \ |
|
case SHARED_IPC: \ |
|
(ret) = ipc_addSemaphore((s)); \ |
|
break; \ |
|
case SHARED_MAP: \ |
|
(ret) = map_addSemaphore((s)); \ |
|
break; \ |
|
default: \ |
|
(ret) = -1; \ |
|
} \ |
|
} while(0) |
|
|
|
#define DEC_SEMAPHORE(s, ret) do { assert((s)); \ |
|
switch ((s)->type) { \ |
|
case SHARED_IPC: \ |
|
(ret) = ipc_decSemaphore((s)); \ |
|
break; \ |
|
case SHARED_MAP: \ |
|
(ret) = map_decSemaphore((s)); \ |
|
break; \ |
|
default: \ |
|
(ret) = -1; \ |
|
} \ |
|
} while(0) |
|
|
|
// --------------------------------------------------------- |
|
|
|
/* |
/* |
* sess_FreeValues() Free all values from value array allocated from sess_GetValues() |
* sess_FreeValues() Free all values from value array allocated from sess_GetValues() |
|
* |
* @ppsVals = Array strings |
* @ppsVals = Array strings |
* return: none |
* return: none |
*/ | */ |
inline void sess_FreeValues(char *** __restrict ppsVals); |
inline void sess_FreeValues(char *** __restrict ppsVals); |
/* |
/* |
* sess_GetValues() Get all values from session shared memory |
* sess_GetValues() Get all values from session shared memory |
|
* |
* @s = Session item |
* @s = Session item |
* @ppsVals = Return array strings |
* @ppsVals = Return array strings |
* return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!) |
* return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!) |
*/ | */ |
int sess_GetValues(tagSess * __restrict s, char ***ppsVals); | int sess_GetValues(ait_sess_t * __restrict s, char ***ppsVals); |
/* |
/* |
* sess_GetValue() Get value from session shared memory from attribute |
* sess_GetValue() Get value from session shared memory from attribute |
|
* |
* @s = Session item |
* @s = Session item |
* @csAttr = Attribute for search |
* @csAttr = Attribute for search |
* @psVal = Return string buffer |
* @psVal = Return string buffer |
* @pnLen = Length of return string buffer, |
* @pnLen = Length of return string buffer, |
// *{pnLen} input is max_size of buffer & output is really taken bytes |
// *{pnLen} input is max_size of buffer & output is really taken bytes |
* return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF |
* return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF |
*/ | */ |
int sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen); | int sess_GetValue(ait_sess_t * __restrict s, const char *csAttr, char *psVal, int *pnLen); |
/* |
/* |
* sess_DelValue() Delete item from session shared memory |
* sess_DelValue() Delete item from session shared memory |
|
* |
* @s = Session item |
* @s = Session item |
* @csAttr = Attribute for erasing |
* @csAttr = Attribute for erasing |
* return: 0 Ok, -1 error: in parameter |
* return: 0 Ok, -1 error: in parameter |
*/ | */ |
int sess_DelValue(tagSess * __restrict s, const char *csAttr); | int sess_DelValue(ait_sess_t * __restrict s, const char *csAttr); |
/* |
/* |
* sess_SetValue() Set item into session shared memory or update if find it |
* sess_SetValue() Set item into session shared memory or update if find it |
|
* |
* @s = Session item |
* @s = Session item |
* @csAttr = Attribute |
* @csAttr = Attribute |
* @psVal = Value |
* @psVal = Value |
* return: 0 nothing, -1 error: in parameter, |
* return: 0 nothing, -1 error: in parameter, |
>0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF |
>0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF |
*/ | */ |
int sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal); | int sess_SetValue(ait_sess_t * __restrict s, const char *csAttr, const char *psVal); |
|
|
/* |
/* |
* sess_prepareSession() Attach to shared memory and de-marshaling data |
* sess_prepareSession() Attach to shared memory and de-marshaling data |
|
* |
* @s = Session |
* @s = Session |
* @useDirect = Use direct shared memory if !=0 or snapshot of data to array |
* @useDirect = Use direct shared memory if !=0 or snapshot of data to array |
* return: NULL error or no data, !=NULL array with variables, |
* return: NULL error or no data, !=NULL array with variables, |
* after use must free resources with sess_doneSession() |
* after use must free resources with sess_doneSession() |
*/ |
*/ |
array_t *sess_prepareSession(tagSess * __restrict s, unsigned char useDirect); | array_t *sess_prepareSession(ait_sess_t * __restrict s, char useDirect); |
/* |
/* |
* sess_doneSession() Free resources allocated with sess_prepareSession() |
* sess_doneSession() Free resources allocated with sess_prepareSession() |
|
* |
* @s = Session |
* @s = Session |
* @arr = Array with variables for free |
* @arr = Array with variables for free |
* return: none |
* return: none |
*/ |
*/ |
void sess_doneSession(tagSess * __restrict s, array_t ** __restrict arr); | void sess_doneSession(ait_sess_t * __restrict s, array_t ** __restrict arr); |
/* |
/* |
* sess_commitSession() Commit data to shared memory |
* sess_commitSession() Commit data to shared memory |
|
* |
* @s = Session |
* @s = Session |
* @arr = Array with variables for save |
* @arr = Array with variables for save |
* return -1 error or !=-1 size of stored variables into shared memory |
* return -1 error or !=-1 size of stored variables into shared memory |
*/ |
*/ |
int sess_commitSession(tagSess * __restrict s, array_t * __restrict arr); | int sess_commitSession(ait_sess_t * __restrict s, array_t * __restrict arr); |
|
|
|
|
#endif |
#endif |