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