--- libaitsess/inc/aitsess.h 2012/02/28 13:00:24 1.5 +++ libaitsess/inc/aitsess.h 2013/01/17 13:26:37 1.6.4.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitsess.h,v 1.5 2012/02/28 13:00:24 misho Exp $ +* $Id: aitsess.h,v 1.6.4.2 2013/01/17 13:26:37 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -50,7 +50,7 @@ SUCH DAMAGE. #include #include #include -#include +#include #define SHARED_UNKNOWN -1 #define SHARED_IPC 0 @@ -66,47 +66,7 @@ SUCH DAMAGE. #define MAX_ATTRIBUTE 64 #define MAX_SEMNAME 14 -/* Memory pool */ -#define MEM_BUCKETS 28 /* 32 bits - 4 bits = 28 items in bucket array */ - -struct tagAlloc { - char alloc_name[MAX_ATTRIBUTE]; - - unsigned int *alloc_mem; - - TAILQ_ENTRY(tagAlloc) alloc_node; -}; -typedef TAILQ_HEAD(, tagAlloc) mpool_bucket_t; - -typedef struct _tagMPool { - pthread_mutex_t pool_mtx; - - struct { - unsigned long alloc; - unsigned long free; - unsigned long cache; - } pool_calls; - struct { - unsigned long alloc; - unsigned long free; - unsigned long cache; - } pool_bytes; - struct { - unsigned long max; - unsigned long curr; - } pool_quota; - - /* pool buckets */ - mpool_bucket_t pool_active[MEM_BUCKETS]; - mpool_bucket_t pool_inactive[MEM_BUCKETS]; -} mpool_t; -#define mpool_lock(x) (assert((x)), pthread_mutex_lock(&(x)->pool_mtx)) -#define mpool_unlock(x) (assert((x)), pthread_mutex_unlock(&(x)->pool_mtx)) - -typedef void (*mpool_stat_cb)(unsigned int, unsigned int, unsigned int); - - /* Shared memory session */ typedef struct tagSess { @@ -140,148 +100,34 @@ typedef struct tagSess { } ait_sess_t; -// ------------------------------------------------------- // 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(); // sess_SetErr() Set error to variables for internal use!!! inline void sess_SetErr(int eno, char *estr, ...); -// ------------------------------------------------------- /* - * mpool_init() - Init memory pool + * sess_initSession() - Initializing session structure, + * if session file not exists creating with specified tech * - * @maxmem = If !=0 set maximum memory quota - * return: =NULL error or !=NULL new allocated pool - */ -mpool_t *mpool_init(unsigned long maxmem); -/* - * mpool_destroy() - Destroy memory pool - * - * @mp = Memory pool - * return: none - */ -void mpool_destroy(mpool_t ** __restrict mp); -/* - * mpool_purge() - Purge memory block cache and release resources - * - * @mp = Memory pool - * @atmost = Free at most in buckets - * return: -1 error or 0 ok - */ -int mpool_purge(mpool_t * __restrict mp, unsigned int atmost); -/* - * mpool_malloc() - Memory allocation - * - * @mp = Memory pool - * @size = Size - * @memname = Optional memory block name - * return: NULL error or !=NULL ok allocated memory - */ -void *mpool_malloc(mpool_t * __restrict mp, unsigned int size, const char *memname); -/* - * mpool_free() Free allocated memory with mpool_alloc() - * - * @mp = Memory pool - * @data = Allocated memory data - * @purge = if !=0 force release memory block - * return: <0 error or 0 ok released memory block - */ -int mpool_free(mpool_t * __restrict mp, void * __restrict data, int purge); -/* - * mpool_free2() Free allocated memory with mpool_alloc() by size and memory name - * - * @mp = Memory pool - * @size = Allocated memory data size - * @memname = Memory name - * @purge = if !=0 force release memory block - * return: <0 error or 0 ok released memory block - */ -int mpool_free2(mpool_t * __restrict mp, unsigned int size, const char *memname, int purge); -/* - * mpool_realloc() Reallocate memory block with new size - * - * @mp = Memory pool - * @data = Allocated memory data - * @newsize = New size of memory block - * @memname = Optional new memory block name - * return: NULL error or !=NULL new reallocated memory block - */ -void *mpool_realloc(mpool_t * __restrict mp, void * __restrict data, - unsigned int newsize, const char *memname); -/* - * mpool_getmembynam() Find allocated memory block by size and memory name - * - * @mp = Memory pool - * @size = Memory size - * @memname = Memory name - * return: NULL error or not found and !=NULL allocated memory - */ -inline struct tagAlloc *mpool_getmembynam(mpool_t * __restrict mp, unsigned int size, const char *memname); -/* - * mpool_getsizebyaddr() - Get size of allocated memory block by address - * - * @data = allocated memory from mpool_malloc() - * return: usable size of allocated memory block - */ -inline unsigned int mpool_getsizebyaddr(void * __restrict data); -/* - * mpool_chkaddr() - Check validity of given address - * - * @data = allocated memory from mpool_malloc() - * return: -1 bad address, 1 corrupted address or 0 ok - */ -inline int mpool_chkaddr(void * __restrict data); -/* - * mpool_setquota() - Change maximum memory quota - * - * @mp = Memory pool - * @maxmem = New max quota size - * return: old maximum memory quota size - */ -inline unsigned long mpool_setquota(mpool_t * __restrict mp, unsigned long maxmem); -/* - * mpool_getquota() - Get memory quota - * - * @mp = Memory pool - * @currmem = Return current memory - * @maxmem = Return max quota size - * return: none - */ -inline void mpool_getquota(mpool_t * __restrict mp, unsigned long *currmem, - unsigned long *maxmem); -/* - * mpool_statistics() - Dump statistics from memory pool buckets - * - * @mp = Memory pool - * @cb = Export statistics to callback - * return: none - */ -void mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb); - - -/* - * sess_initSession() Initializing session structure, if session file not exists creating with specified tech - * - * @id = Technology using in session. SHARED_IPC IPC tech orSHARED_MAP BSD MemoryMap tech + * @id = Technology using in session. SHARED_IPC IPC tech or SHARED_MAP BSD MemoryMap tech * @csFName = Session filename for build key and identified * @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 */ int sess_initSession(int id, const char *csFName, ait_sess_t ** __restrict Sess); /* - * 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 * * @Sess = Session item * return: none */ void sess_freeSession(ait_sess_t ** __restrict Sess); - /* - * map_createSession() MMAP Created session and allocated resources + * map_createSession() - MMAP Created session and allocated resources * * @nSeed = Seed for securing key, if =-1 must add ready for use key * @nSize = Allocated shared memory size in bytes @@ -291,7 +137,7 @@ void sess_freeSession(ait_sess_t ** __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 * * @Sess = Session item * return: none @@ -299,7 +145,7 @@ int map_createSession(int nSeed, long nSize, ait_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 * * @nSeed = Seed for securing key, if =-1 must add ready for use key * @nSize = Allocated shared memory size in bytes @@ -309,7 +155,7 @@ void map_destroySession(ait_sess_t * __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 * * @Sess = Session item * return: none @@ -317,7 +163,7 @@ int ipc_createSession(int nSeed, long nSize, ait_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 * @procMem = Custom start address (optionl) *default must be 0* @@ -325,7 +171,7 @@ void ipc_destroySession(ait_sess_t * __restrict Sess); */ 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 * return: none @@ -333,7 +179,7 @@ void *map_attachSession(ait_sess_t * __restrict s, voi 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 * @procMem = Custom start address (optionl) *default must be 0* @@ -341,7 +187,7 @@ void map_detachSession(ait_sess_t * __restrict s); */ 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 * return: none @@ -349,7 +195,7 @@ void *ipc_attachSession(ait_sess_t * __restrict s, voi void ipc_detachSession(ait_sess_t * __restrict s); /* - * sess_isAttached() Check for attached shared memory + * sess_isAttached() - Check for attached shared memory * * @s = Session item * return: -1 null session item, 0 not attached, 1 attached memory @@ -358,28 +204,28 @@ 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 * return: none */ void map_notSemaphore(ait_sess_t * __restrict s); /* - * map_isSemaphoreOK() MMAP Check semaphore + * map_isSemaphoreOK() - MMAP Check semaphore * * @s = Session item * return: -1 error: can`t return semaphore, 0 = false, 1 = true */ int map_isSemaphoreOK(ait_sess_t * __restrict s); /* - * map_incSemaphore() MMAP unblock semaphore, increment semaphore + * map_incSemaphore() - MMAP unblock semaphore, increment semaphore * * @s = Session item * return: 0 Ok, -1 error: can`t increment */ 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 * return: 0 Ok, -1 error: can`t decrement @@ -387,28 +233,28 @@ int map_incSemaphore(ait_sess_t * __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 * return: none */ void ipc_notSemaphore(ait_sess_t * __restrict s); /* - * ipc_isSemaphoreOK() IPC Check semaphore + * ipc_isSemaphoreOK() - IPC Check semaphore * * @s = Session item * return: -1 error: can`t return semaphore, 0 = false, 1 = true */ int ipc_isSemaphoreOK(ait_sess_t * __restrict s); /* - * ipc_incSemaphore() IPC unblock semaphore, increment semaphore + * ipc_incSemaphore() - IPC unblock semaphore, increment semaphore * * @s = Session item * return: 0 Ok, -1 error: can`t increment */ 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 * return: 0 Ok, -1 error: can`t decrement @@ -432,33 +278,35 @@ int ipc_decSemaphore(ait_sess_t * __restrict s); /* --------------------------------------------------------- */ /* - * 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 * return: none */ 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 * @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 sess_FreeValues after use!) */ 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 * @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 + * return: 0 not found, -1 error: in parameter, >0 get position, + * if define item merged with IS_DEF */ 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 * @csAttr = Attribute for erasing @@ -466,7 +314,7 @@ int sess_GetValue(ait_sess_t * __restrict s, const cha */ 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 exists * * @s = Session item * @csAttr = Attribute @@ -477,7 +325,7 @@ int sess_DelValue(ait_sess_t * __restrict s, const cha 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 * @useDirect = Use direct shared memory if !=0 or snapshot of data to array @@ -486,7 +334,7 @@ int sess_SetValue(ait_sess_t * __restrict s, const cha */ 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 * @arr = Array with variables for free @@ -494,7 +342,7 @@ array_t *sess_prepareSession(ait_sess_t * __restrict s */ 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 * @arr = Array with variables for save