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