File:  [ELWIX - Embedded LightWeight unIX -] / libaitsess / inc / aitsess.h
Revision 1.4.2.4: download - view: text, annotated - select for diffs - revision graph
Tue Feb 28 00:13:26 2012 UTC (12 years, 4 months ago) by misho
Branches: sess3_1
Diff to: branchpoint 1.4: preferred, unified
add quota for purge

    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.4.2.4 2012/02/28 00:13:26 misho Exp $
    7: *
    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, 2012
   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: */
   46: #ifndef __AITSESS_H
   47: #define __AITSESS_H
   48: 
   49: 
   50: #include <pthread.h>
   51: #include <assert.h>
   52: #include <semaphore.h>
   53: #include <aitio.h>
   54: 
   55: #define SHARED_UNKNOWN	-1
   56: #define SHARED_IPC	0
   57: #define SHARED_MAP	1
   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: 
   66: #define MAX_ATTRIBUTE	64
   67: #define MAX_SEMNAME	14
   68: 
   69: /* Memory pool */
   70: 
   71: #define MEM_BUCKETS	28	/* 32 bits - 4 bits = 28 items in bucket array */
   72: 
   73: struct tagAlloc {
   74: 	char			alloc_name[MAX_ATTRIBUTE];
   75: 	volatile unsigned int	alloc_flags;
   76: 
   77: 	unsigned int		*alloc_mem;
   78: 
   79: 	TAILQ_ENTRY(tagAlloc)	alloc_node;
   80: };
   81: typedef TAILQ_HEAD(, tagAlloc) mpool_bucket_t;
   82: 
   83: typedef struct _tagMPool {
   84: 	pthread_mutex_t	pool_mtx;
   85: 
   86: 	struct {
   87: 		unsigned long alloc;
   88: 		unsigned long free;
   89: 		unsigned long cache;
   90: 	} pool_calls;
   91: 	struct {
   92: 		unsigned long alloc;
   93: 		unsigned long free;
   94: 		unsigned long cache;
   95: 	} pool_bytes;
   96: 	struct {
   97: 		unsigned long max;
   98: 		unsigned long curr;
   99: 	} pool_quota;
  100: 
  101: 	/* pool buckets */
  102: 	mpool_bucket_t	pool_active[MEM_BUCKETS];
  103: 	mpool_bucket_t	pool_inactive[MEM_BUCKETS];
  104: } mpool_t;
  105: #define mpool_lock(x)	(assert((x)), pthread_mutex_lock(&(x)->pool_mtx))
  106: #define mpool_unlock(x)	(assert((x)), pthread_mutex_unlock(&(x)->pool_mtx))
  107: 
  108: /* Shared memory session */
  109: 
  110: typedef struct tagSess {
  111: 	key_t	key;
  112: 	char	type;
  113: 	char	zcpy;
  114: 
  115: 	char	name[BUFSIZ];
  116: 	off_t	eom;
  117: 	void	*addr;
  118: 	off_t	offset;
  119: 	union {
  120: 		int	shmid;
  121: 		int	fd;
  122: 	} mem;
  123: 	union {
  124: 		int	semid;
  125: 		sem_t	*sid;
  126: 	} id;
  127: 
  128: 	struct {
  129: 		int (*create)(int, long, void *, ...);
  130: 		void (*destroy)(void *);
  131: 		void *(*attach)(void *, void *);
  132: 		void (*detach)(void *);
  133: 		void (*notSem)(void *);
  134: 		int (*isSemOK)(void *);
  135: 		int (*incSem)(void *);
  136: 		int (*decSem)(void *);
  137: 	} sess;
  138: } ait_sess_t;
  139: 
  140: 
  141: // -------------------------------------------------------
  142: // sess_GetErrno() Get error code of last operation
  143: inline int sess_GetErrno();
  144: // sess_GetError() Get error text of last operation
  145: inline const char *sess_GetError();
  146: // sess_SetErr() Set error to variables for internal use!!!
  147: inline void sess_SetErr(int eno, char *estr, ...);
  148: // -------------------------------------------------------
  149: 
  150: 
  151: /*
  152:  * mpool_init() - Init memory pool
  153:  *
  154:  * return: =NULL error or !=NULL new allocated pool
  155:  */
  156: mpool_t *mpool_init(void);
  157: /*
  158:  * mpool_destroy() - Destroy memory pool
  159:  *
  160:  * @mp = Memory pool
  161:  * return: none
  162:  */
  163: void mpool_destroy(mpool_t ** __restrict mp);
  164: /*
  165:  * mpool_purge() - Purge memory block cache and release resources
  166:  *
  167:  * @mp = Memory pool
  168:  * @atmost = Free at most in buckets
  169:  * return: -1 error or 0 ok
  170:  */
  171: int mpool_purge(mpool_t * __restrict mp, unsigned int atmost);
  172: /*
  173:  * mpool_malloc() - Memory allocation
  174:  *
  175:  * @mp = Memory pool
  176:  * @size = Size
  177:  * @memname = Optional memory block name
  178:  * return: NULL error or !=NULL ok allocated memory
  179:  */
  180: void *mpool_malloc(mpool_t * __restrict mp, unsigned int size, const char *memname);
  181: /*
  182:  * mpool_free() Free allocated memory with mpool_alloc()
  183:  *
  184:  * @mp = Memory pool
  185:  * @data = Allocated memory data
  186:  * @purge = if !=0 force release memory block
  187:  * return: <0 error or 0 ok released memory block
  188:  */
  189: int mpool_free(mpool_t * __restrict mp, void * __restrict data, int purge);
  190: /*
  191:  * mpool_free2() Free allocated memory with mpool_alloc() by size and memory name
  192:  *
  193:  * @mp = Memory pool
  194:  * @size = Allocated memory data size
  195:  * @memname = Memory name
  196:  * @purge = if !=0 force release memory block
  197:  * return: <0 error or 0 ok released memory block
  198:  */
  199: int mpool_free2(mpool_t * __restrict mp, unsigned int size, const char *memname, int purge);
  200: /*
  201:  * mpool_getmembynam() Find allocated memory block by size and memory name
  202:  *
  203:  * @mp = Memory pool
  204:  * @size = Memory size
  205:  * @memname = Memory name
  206:  * return: NULL error or not found and !=NULL allocated memory 
  207:  */
  208: inline struct tagAlloc *mpool_getmembynam(mpool_t * __restrict mp, unsigned int size, const char *memname);
  209: /*
  210:  * mpool_getsizebyaddr() - Get size of allocated memory block by address
  211:  *
  212:  * @data = allocated memory from mpool_malloc()
  213:  * return: usable size of allocated memory block
  214:  */
  215: inline unsigned int mpool_getsizebyaddr(void * __restrict data);
  216: /*
  217:  * mpool_chkaddr() - Check validity of given address
  218:  *
  219:  * @data = allocated memory from mpool_malloc()
  220:  * return: -1 bad address, 1 corrupted address or 0 ok
  221:  */
  222: inline int mpool_chkaddr(void * __restrict data);
  223: 
  224: 
  225: /*
  226:  * sess_initSession() Initializing session structure, if session file not exists creating with specified tech
  227:  *
  228:  * @id = Technology using in session. SHARED_IPC IPC tech orSHARED_MAP BSD MemoryMap tech
  229:  * @csFName = Session filename for build key and identified
  230:  * @Sess = Session item, if =NULL allocate memory for session after use must be free!
  231:  * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
  232:  */
  233: int sess_initSession(int id, const char *csFName, ait_sess_t ** __restrict Sess);
  234: /*
  235:  * sess_freeSession() Free allocated memory for session item and delete session file if present name
  236:  *
  237:  * @Sess = Session item
  238:  * return: none
  239:  */
  240: void sess_freeSession(ait_sess_t ** __restrict Sess);
  241: 
  242: 
  243: /*
  244:  * map_createSession() MMAP Created session and allocated resources
  245:  *
  246:  * @nSeed = Seed for securing key, if =-1 must add ready for use key
  247:  * @nSize = Allocated shared memory size in bytes
  248:  * @Sess = Session item
  249:  * @... = If nSeed == -1 add ready for use key value
  250:  * return: 0 Ok successful, -1 error: not allocated resources
  251:  */
  252: int map_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...);
  253: /*
  254:  * map_destroySession() MMAP free shared resources
  255:  *
  256:  * @Sess = Session item
  257:  * return: none
  258:  */
  259: void map_destroySession(ait_sess_t * __restrict Sess);
  260: 
  261: /*
  262:  * ipc_createSession() IPC Created session and allocated resources
  263:  *
  264:  * @nSeed = Seed for securing key, if =-1 must add ready for use key
  265:  * @nSize = Allocated shared memory size in bytes
  266:  * @Sess = Session item
  267:  * @... = If nSeed == -1 add ready for use key value
  268:  * return: 0 Ok successful, -1 error: not allocated resources
  269:  */
  270: int ipc_createSession(int nSeed, long nSize, ait_sess_t * __restrict Sess, ...);
  271: /*
  272:  * ipc_destroySession() IPC free shared resources
  273:  *
  274:  * @Sess = Session item
  275:  * return: none
  276:  */
  277: void ipc_destroySession(ait_sess_t * __restrict Sess);
  278: 
  279: /*
  280:  * map_attachSession() MMAP Attach to shared memory & return begin address
  281:  *
  282:  * @s = Session item
  283:  * @procMem = Custom start address (optionl) *default must be 0*
  284:  * return: NULL failed attach, !=NULL begin address of memory
  285:  */
  286: void *map_attachSession(ait_sess_t * __restrict s, void *procMem);
  287: /*
  288:  * map_detachSession() MMAP Detach from shared memory
  289:  *
  290:  * @s = Session item
  291:  * return: none
  292:  */
  293: void map_detachSession(ait_sess_t * __restrict s);
  294: 
  295: /*
  296:  * ipc_attachSession() IPC Attach to shared memory & return begin address
  297:  *
  298:  * @s = Session item
  299:  * @procMem = Custom start address (optionl) *default must be 0*
  300:  * return: NULL failed attach, !=NULL begin address of memory
  301:  */
  302: void *ipc_attachSession(ait_sess_t * __restrict s, void *procMem);
  303: /*
  304:  * ipc_detachSession() IPC Detach from shared memory
  305:  *
  306:  * @s = Session item
  307:  * return: none
  308:  */
  309: void ipc_detachSession(ait_sess_t * __restrict s);
  310: 
  311: /*
  312:  * sess_isAttached() Check for attached shared memory
  313:  *
  314:  * @s = Session item
  315:  * return: -1 null session item, 0 not attached, 1 attached memory
  316:  */
  317: inline int sess_isAttached(ait_sess_t * __restrict s);
  318: 
  319: 
  320: /*
  321:  * map_notSemaphore() MMAP negative block if semaphore isn`t signaled
  322:  *
  323:  * @s = Session item
  324:  * return: none
  325:  */
  326: void map_notSemaphore(ait_sess_t * __restrict s);
  327: /*
  328:  * map_isSemaphoreOK() MMAP Check semaphore
  329:  *
  330:  * @s = Session item
  331:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
  332:  */
  333: int map_isSemaphoreOK(ait_sess_t * __restrict s);
  334: /*
  335:  * map_incSemaphore() MMAP unblock semaphore, increment semaphore
  336:  *
  337:  * @s = Session item
  338:  * return: 0 Ok, -1 error: can`t increment 
  339:  */
  340: int map_incSemaphore(ait_sess_t * __restrict s);
  341: /*
  342:  * map_decSemaphore() MMAP block semaphore, decrement semaphore
  343:  *
  344:  * @s = Session item
  345:  * return: 0 Ok, -1 error: can`t decrement 
  346:  */
  347: int map_decSemaphore(ait_sess_t * __restrict s);
  348: 
  349: /*
  350:  * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled
  351:  *
  352:  * @s = Session item
  353:  * return: none
  354:  */
  355: void ipc_notSemaphore(ait_sess_t * __restrict s);
  356: /*
  357:  * ipc_isSemaphoreOK() IPC Check semaphore
  358:  *
  359:  * @s = Session item
  360:  * return: -1 error: can`t return semaphore, 0 = false, 1 = true
  361:  */
  362: int ipc_isSemaphoreOK(ait_sess_t * __restrict s);
  363: /*
  364:  * ipc_incSemaphore() IPC unblock semaphore, increment semaphore
  365:  *
  366:  * @s = Session item
  367:  * return: 0 Ok, -1 error: can`t increment 
  368:  */
  369: int ipc_incSemaphore(ait_sess_t * __restrict s);
  370: /*
  371:  * ipc_decSemaphore() IPC block semaphore, decrement semaphore
  372:  *
  373:  * @s = Session item
  374:  * return: 0 Ok, -1 error: can`t decrement 
  375:  */
  376: int ipc_decSemaphore(ait_sess_t * __restrict s);
  377: 
  378: 
  379: /* --------------------------------------------------------- */
  380: 
  381: #define ALLOC_MEMORY(sd, siz, s, ...)	(assert((s)), (s)->sess.create((sd), \
  382: 							(siz), (s), ## __VA_ARGS__))
  383: #define ATTACH_MEMORY(s)		(assert((s)), (s)->sess.attach((s), NULL))
  384: #define DETACH_MEMORY(s)		do { assert((s)); (s)->sess.detach((s)); } while(0)
  385: #define FREE_MEMORY(s)			do { assert((s)); (s)->sess.destroy((s)); } while(0)
  386: 
  387: #define IS_SEMOK(s)			(assert((s)), (s)->sess.isSemOK((s)))
  388: #define INC_SEM(s)			(assert((s)), (s)->sess.incSem((s)))
  389: #define DEC_SEM(s)			(assert((s)), (s)->sess.decSem((s)))
  390: #define NOT_SEM(s)			do { assert((s)); (s)->sess.notSem((s)); } while(0)
  391: 
  392: /* --------------------------------------------------------- */
  393: 
  394: /*
  395:  * sess_FreeValues() Free all values from value array allocated from sess_GetValues()
  396:  *
  397:  * @ppsVals = Array strings
  398:  * return: none
  399:  */
  400: inline void sess_FreeValues(char *** __restrict ppsVals);
  401: /*
  402:  * sess_GetValues() Get all values from session shared memory
  403:  *
  404:  * @s = Session item
  405:  * @ppsVals = Return array strings
  406:  * return: -1 error: in parameter, !=-1 count of returned strings in ppsVals (must be free after use!)
  407:  */
  408: int sess_GetValues(ait_sess_t * __restrict s, char ***ppsVals);
  409: /*
  410:  * sess_GetValue() Get value from session shared memory from attribute
  411:  *
  412:  * @s = Session item
  413:  * @csAttr = Attribute for search
  414:  * @psVal = Return string buffer
  415:  * @pnLen = Length of return string buffer, 
  416: 	// *{pnLen} input is max_size of buffer & output is really taken bytes
  417:  * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
  418:  */
  419: int sess_GetValue(ait_sess_t * __restrict s, const char *csAttr, char *psVal, int *pnLen);
  420: /*
  421:  * sess_DelValue() Delete item from session shared memory
  422:  *
  423:  * @s = Session item
  424:  * @csAttr = Attribute for erasing
  425:  * return: 0 Ok, -1 error: in parameter
  426:  */
  427: int sess_DelValue(ait_sess_t * __restrict s, const char *csAttr);
  428: /*
  429:  * sess_SetValue() Set item into session shared memory or update if find it
  430:  *
  431:  * @s = Session item
  432:  * @csAttr = Attribute
  433:  * @psVal = Value
  434:  * return: 0 nothing, -1 error: in parameter, 
  435:  	>0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
  436:  */
  437: int sess_SetValue(ait_sess_t * __restrict s, const char *csAttr, const char *psVal);
  438: 
  439: /*
  440:  * sess_prepareSession() Attach to shared memory and de-marshaling data
  441:  *
  442:  * @s = Session
  443:  * @useDirect = Use direct shared memory if !=0 or snapshot of data to array
  444:  * return: NULL error or no data, !=NULL array with variables, 
  445:  *		after use must free resources with sess_doneSession()
  446:  */
  447: array_t *sess_prepareSession(ait_sess_t * __restrict s, char useDirect);
  448: /*
  449:  * sess_doneSession() Free resources allocated with sess_prepareSession()
  450:  *
  451:  * @s = Session
  452:  * @arr = Array with variables for free
  453:  * return: none
  454:  */
  455: void sess_doneSession(ait_sess_t * __restrict s, array_t ** __restrict arr);
  456: /*
  457:  * sess_commitSession() Commit data to shared memory
  458:  *
  459:  * @s = Session
  460:  * @arr = Array with variables for save
  461:  * return -1 error or !=-1 size of stored variables into shared memory
  462:  */
  463: int sess_commitSession(ait_sess_t * __restrict s, array_t * __restrict arr);
  464: 
  465: 
  466: #endif

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