File:  [ELWIX - Embedded LightWeight unIX -] / libaitsess / inc / aitsess.h
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Thu Aug 28 13:18:30 2008 UTC (15 years, 9 months ago) by misho
Branches: MAIN
CVS tags: HEAD
Initial revision

#ifndef __AITSESS_H
#define __AITSESS_H


#include <semaphore.h>
#include <sys/types.h>

#define SHARED_IPC	1
#define SHARED_MAP	2

#define IS_VAL		0x0
#define IS_ADD		0x40000000
#define IS_DEF		0x80000000

#define ISNEW(x)	(((x) & IS_ADD) == IS_ADD)
#define ISDEF(x)	(((x) & IS_DEF) == IS_DEF)

#define MAX_ATTRIBUTE	63
#define MAX_SEMNAME	14


typedef struct _tagSess {
	key_t	key;
	u_char	type;
	off_t	eom;
	void	*addr;
	off_t	offset;
	union {
		int	shmid;
		int	fd;
	} mem;
	union {
		int	semid;
		sem_t	*sid;
	} id;
} tagSess;


// -------------------------------------------------------
// sess_GetErrno() Get error code of last operation
inline int sess_GetErrno();
// sess_GetError() Get error text of last operation
inline const char *sess_GetError();
// -------------------------------------------------------


/*
 * initSession() Initializing session structure, if session file not exists creating with specified tech
 * @cnID = Technology using in session. SHARED_IPC IPC tech; SHARED_MAP BSD MemoryMap tech
 * @csFName = Session filename for build key and identified
 * @Sess = Session item
 * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
*/
inline int initSession(const int cnID, const char *csFName, tagSess ** __restrict Sess);
/*
 * freeSession() Free allocated memory for session item and delete session file if present name
 * @csFName = Session filename for delete, if NULL nothing delete
 * @Sess = Session item
*/
inline void freeSession(const char *csFName, tagSess ** __restrict Sess);

/*
 * map_createSession() MMAP Created session and allocated resources
 * @csFName = Session name for identified
 * @cnSeed = Seed for securing key
 * @cnSize = Allocated shared memory size in bytes
 * @Sess = Session item
 * 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);
/*
 * map_destroySession() MMAP free shared resources
 * @csFName = Session name for delete
 * @Sess = Session item
*/
void map_destroySession(const char *csFName, tagSess ** __restrict Sess);

/*
 * ipc_createSession() IPC Created session and allocated resources
 * @csFName = Session name for identified
 * @cnSeed = Seed for securing key
 * @cnSize = Allocated shared memory size in bytes
 * @Sess = Session item
 * 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);
/*
 * ipc_destroySession() IPC free shared resources
 * @csFName = Session name for delete
 * @Sess = Session item
*/
void ipc_destroySession(const char *csFName, tagSess ** __restrict Sess);

/*
 * map_attachSession() MMAP Attach to shared memory & return begin address
 * @s = Session item
 * @procMem = Custom start address (optionl) *default must be 0*
 * return: NULL failed attach, !=NULL begin address of memory
*/
inline void *map_attachSession(tagSess * __restrict s, void *procMem);
/*
 * map_detachSession() MMAP Detach from shared memory
 * @s = Session item
*/
inline void map_detachSession(tagSess * __restrict s);

/*
 * ipc_attachSession() IPC Attach to shared memory & return begin address
 * @s = Session item
 * @procMem = Custom start address (optionl) *default must be 0*
 * return: NULL failed attach, !=NULL begin address of memory
*/
inline void *ipc_attachSession(tagSess * __restrict s, void *procMem);
/*
 * ipc_detachSession() IPC Detach from shared memory
 * @s = Session item
*/
inline void ipc_detachSession(tagSess * __restrict s);

/*
 * map_notSemaphore() MMAP negative block if semaphore isn`t signaled
 * @s = Session item
*/
inline void map_notSemaphore(tagSess * __restrict s);
/*
 * map_isSemaphored() MMAP Check semaphore
 * @s = Session item
 * return: -1 error: can`t return semaphore, 0 = false, 1 = true
*/
inline int map_isSemaphored(tagSess * __restrict s);
/*
 * map_addSemaphore() MMAP unblock semaphore, increment semaphore
 * @s = Session item
 * return: 0 Ok, -1 error: can`t increment 
*/
inline int map_addSemaphore(tagSess * __restrict s);
/*
 * map_decSemaphore() MMAP block semaphore, decrement semaphore
 * @s = Session item
 * return: 0 Ok, -1 error: can`t decrement 
*/
inline int map_decSemaphore(tagSess * __restrict s);

/*
 * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled
 * @s = Session item
*/
inline void ipc_notSemaphore(tagSess * __restrict s);
/*
 * ipc_isSemaphored() IPC Check semaphore
 * @s = Session item
 * return: -1 error: can`t return semaphore, 0 = false, 1 = true
*/
inline int ipc_isSemaphored(tagSess * __restrict s);
/*
 * ipc_addSemaphore() IPC unblock semaphore, increment semaphore
 * @s = Session item
 * return: 0 Ok, -1 error: can`t increment 
*/
inline int ipc_addSemaphore(tagSess * __restrict s);
/*
 * ipc_decSemaphore() IPC block semaphore, decrement semaphore
 * @s = Session item
 * return: 0 Ok, -1 error: can`t decrement 
*/
inline int ipc_decSemaphore(tagSess * __restrict s);

// ---------------------------------------------------------
//
//  Lazy macros for lazy programmers :-) by Michael Pounov; Optimizing work with sessions!
//

#define DESTROY_SESSION(fname, s)	do { \
						switch ((s)->type) { \
							case SHARED_IPC: \
									 ipc_destroySession((fname), (s)); \
									 break; \
							case SHARED_MAP: \
									 map_destroySession((fname), (s)); \
									 break; \
						} \
					} while(0)

#define ATTACH_MEMORY(s, shared)	do { \
						switch ((s)->type) { \
							case SHARED_IPC: \
									 (shared) = ipc_attachSession((s), 0); \
									 break; \
							case SHARED_MAP: \
									 (shared) = map_attachSession((s), 0); \
									 break; \
						} \
					} while(0)

#define DETACH_MEMORY(s)		do { \
						switch ((s)->type) { \
							case SHARED_IPC: \
									 ipc_detachSession((s)); \
									 break; \
							case SHARED_MAP: \
									 map_detachSession((s)); \
									 break; \
						} \
					} while(0)

#define NOT_SEMAPHORE(s)	do { \
					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 { \
					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 { \
					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 { \
					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_GetValue() Get value from session shared memory from attribute
 * @s = Session item
 * @csAttr = Attribute for search
 * @psVal = Return string buffer
 * @pnLen = Length of return string buffer, 
	// *{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
*/
int sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen);
/*
 * sess_DelValue() Delete item from session shared memory
 * @s = Session item
 * @csAttr = Attribute for erasing
 * return: 0 Ok, -1 error: in parameter
*/
int sess_DelValue(tagSess * __restrict s, const char *csAttr);
/*
 * sess_SetValue() Set item into session shared memory or update if find it
 * @s = Session item
 * @csAttr = Attribute
 * @psVal = Value
 * 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
*/
int sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal);


#endif

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