Annotation of libaitsess/inc/aitsess.h, revision 1.1.1.1
1.1 misho 1: #ifndef __AITSESS_H
2: #define __AITSESS_H
3:
4:
5: #include <semaphore.h>
6: #include <sys/types.h>
7:
8: #define SHARED_IPC 1
9: #define SHARED_MAP 2
10:
11: #define IS_VAL 0x0
12: #define IS_ADD 0x40000000
13: #define IS_DEF 0x80000000
14:
15: #define ISNEW(x) (((x) & IS_ADD) == IS_ADD)
16: #define ISDEF(x) (((x) & IS_DEF) == IS_DEF)
17:
18: #define MAX_ATTRIBUTE 63
19: #define MAX_SEMNAME 14
20:
21:
22: typedef struct _tagSess {
23: key_t key;
24: u_char type;
25: off_t eom;
26: void *addr;
27: off_t offset;
28: union {
29: int shmid;
30: int fd;
31: } mem;
32: union {
33: int semid;
34: sem_t *sid;
35: } id;
36: } tagSess;
37:
38:
39: // -------------------------------------------------------
40: // sess_GetErrno() Get error code of last operation
41: inline int sess_GetErrno();
42: // sess_GetError() Get error text of last operation
43: inline const char *sess_GetError();
44: // -------------------------------------------------------
45:
46:
47: /*
48: * initSession() Initializing session structure, if session file not exists creating with specified tech
49: * @cnID = Technology using in session. SHARED_IPC IPC tech; SHARED_MAP BSD MemoryMap tech
50: * @csFName = Session filename for build key and identified
51: * @Sess = Session item
52: * return: 0 OK new key created, -1 error: no memory or file not created, 1 OK key finded
53: */
54: inline int initSession(const int cnID, const char *csFName, tagSess ** __restrict Sess);
55: /*
56: * freeSession() Free allocated memory for session item and delete session file if present name
57: * @csFName = Session filename for delete, if NULL nothing delete
58: * @Sess = Session item
59: */
60: inline void freeSession(const char *csFName, tagSess ** __restrict Sess);
61:
62: /*
63: * map_createSession() MMAP Created session and allocated resources
64: * @csFName = Session name for identified
65: * @cnSeed = Seed for securing key
66: * @cnSize = Allocated shared memory size in bytes
67: * @Sess = Session item
68: * return: 0 Ok successful, -1 error: not allocated resources
69: */
70: int map_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess);
71: /*
72: * map_destroySession() MMAP free shared resources
73: * @csFName = Session name for delete
74: * @Sess = Session item
75: */
76: void map_destroySession(const char *csFName, tagSess ** __restrict Sess);
77:
78: /*
79: * ipc_createSession() IPC Created session and allocated resources
80: * @csFName = Session name for identified
81: * @cnSeed = Seed for securing key
82: * @cnSize = Allocated shared memory size in bytes
83: * @Sess = Session item
84: * return: 0 Ok successful, -1 error: not allocated resources
85: */
86: int ipc_createSession(const char *csFName, const int cnSeed, const u_int cnSize, tagSess ** __restrict Sess);
87: /*
88: * ipc_destroySession() IPC free shared resources
89: * @csFName = Session name for delete
90: * @Sess = Session item
91: */
92: void ipc_destroySession(const char *csFName, tagSess ** __restrict Sess);
93:
94: /*
95: * map_attachSession() MMAP Attach to shared memory & return begin address
96: * @s = Session item
97: * @procMem = Custom start address (optionl) *default must be 0*
98: * return: NULL failed attach, !=NULL begin address of memory
99: */
100: inline void *map_attachSession(tagSess * __restrict s, void *procMem);
101: /*
102: * map_detachSession() MMAP Detach from shared memory
103: * @s = Session item
104: */
105: inline void map_detachSession(tagSess * __restrict s);
106:
107: /*
108: * ipc_attachSession() IPC Attach to shared memory & return begin address
109: * @s = Session item
110: * @procMem = Custom start address (optionl) *default must be 0*
111: * return: NULL failed attach, !=NULL begin address of memory
112: */
113: inline void *ipc_attachSession(tagSess * __restrict s, void *procMem);
114: /*
115: * ipc_detachSession() IPC Detach from shared memory
116: * @s = Session item
117: */
118: inline void ipc_detachSession(tagSess * __restrict s);
119:
120: /*
121: * map_notSemaphore() MMAP negative block if semaphore isn`t signaled
122: * @s = Session item
123: */
124: inline void map_notSemaphore(tagSess * __restrict s);
125: /*
126: * map_isSemaphored() MMAP Check semaphore
127: * @s = Session item
128: * return: -1 error: can`t return semaphore, 0 = false, 1 = true
129: */
130: inline int map_isSemaphored(tagSess * __restrict s);
131: /*
132: * map_addSemaphore() MMAP unblock semaphore, increment semaphore
133: * @s = Session item
134: * return: 0 Ok, -1 error: can`t increment
135: */
136: inline int map_addSemaphore(tagSess * __restrict s);
137: /*
138: * map_decSemaphore() MMAP block semaphore, decrement semaphore
139: * @s = Session item
140: * return: 0 Ok, -1 error: can`t decrement
141: */
142: inline int map_decSemaphore(tagSess * __restrict s);
143:
144: /*
145: * ipc_notSemaphore() IPC negative block if semaphore isn`t signaled
146: * @s = Session item
147: */
148: inline void ipc_notSemaphore(tagSess * __restrict s);
149: /*
150: * ipc_isSemaphored() IPC Check semaphore
151: * @s = Session item
152: * return: -1 error: can`t return semaphore, 0 = false, 1 = true
153: */
154: inline int ipc_isSemaphored(tagSess * __restrict s);
155: /*
156: * ipc_addSemaphore() IPC unblock semaphore, increment semaphore
157: * @s = Session item
158: * return: 0 Ok, -1 error: can`t increment
159: */
160: inline int ipc_addSemaphore(tagSess * __restrict s);
161: /*
162: * ipc_decSemaphore() IPC block semaphore, decrement semaphore
163: * @s = Session item
164: * return: 0 Ok, -1 error: can`t decrement
165: */
166: inline int ipc_decSemaphore(tagSess * __restrict s);
167:
168: // ---------------------------------------------------------
169: //
170: // Lazy macros for lazy programmers :-) by Michael Pounov; Optimizing work with sessions!
171: //
172:
173: #define DESTROY_SESSION(fname, s) do { \
174: switch ((s)->type) { \
175: case SHARED_IPC: \
176: ipc_destroySession((fname), (s)); \
177: break; \
178: case SHARED_MAP: \
179: map_destroySession((fname), (s)); \
180: break; \
181: } \
182: } while(0)
183:
184: #define ATTACH_MEMORY(s, shared) do { \
185: switch ((s)->type) { \
186: case SHARED_IPC: \
187: (shared) = ipc_attachSession((s), 0); \
188: break; \
189: case SHARED_MAP: \
190: (shared) = map_attachSession((s), 0); \
191: break; \
192: } \
193: } while(0)
194:
195: #define DETACH_MEMORY(s) do { \
196: switch ((s)->type) { \
197: case SHARED_IPC: \
198: ipc_detachSession((s)); \
199: break; \
200: case SHARED_MAP: \
201: map_detachSession((s)); \
202: break; \
203: } \
204: } while(0)
205:
206: #define NOT_SEMAPHORE(s) do { \
207: switch ((s)->type) { \
208: case SHARED_IPC: \
209: ipc_notSemaphore((s)); \
210: break; \
211: case SHARED_MAP: \
212: map_notSemaphore((s)); \
213: break; \
214: } \
215: } while(0)
216:
217: #define IS_SEMAPHORED(s, ret) do { \
218: switch ((s)->type) { \
219: case SHARED_IPC: \
220: (ret) = ipc_isSemaphored((s)); \
221: break; \
222: case SHARED_MAP: \
223: (ret) = map_isSemaphored((s)); \
224: break; \
225: default: \
226: (ret) = -1; \
227: } \
228: } while(0)
229:
230: #define ADD_SEMAPHORE(s, ret) do { \
231: switch ((s)->type) { \
232: case SHARED_IPC: \
233: (ret) = ipc_addSemaphore((s)); \
234: break; \
235: case SHARED_MAP: \
236: (ret) = map_addSemaphore((s)); \
237: break; \
238: default: \
239: (ret) = -1; \
240: } \
241: } while(0)
242:
243: #define DEC_SEMAPHORE(s, ret) do { \
244: switch ((s)->type) { \
245: case SHARED_IPC: \
246: (ret) = ipc_decSemaphore((s)); \
247: break; \
248: case SHARED_MAP: \
249: (ret) = map_decSemaphore((s)); \
250: break; \
251: default: \
252: (ret) = -1; \
253: } \
254: } while(0)
255:
256: // ---------------------------------------------------------
257:
258: /*
259: * sess_GetValue() Get value from session shared memory from attribute
260: * @s = Session item
261: * @csAttr = Attribute for search
262: * @psVal = Return string buffer
263: * @pnLen = Length of return string buffer,
264: // *{pnLen} input is max_size of buffer & output is really taken bytes
265: * return: 0 not found, -1 error: in parameter, >0 get position, if define item merged with IS_DEF
266: */
267: int sess_GetValue(tagSess * __restrict s, const char *csAttr, char *psVal, int *pnLen);
268: /*
269: * sess_DelValue() Delete item from session shared memory
270: * @s = Session item
271: * @csAttr = Attribute for erasing
272: * return: 0 Ok, -1 error: in parameter
273: */
274: int sess_DelValue(tagSess * __restrict s, const char *csAttr);
275: /*
276: * sess_SetValue() Set item into session shared memory or update if find it
277: * @s = Session item
278: * @csAttr = Attribute
279: * @psVal = Value
280: * return: 0 nothing, -1 error: in parameter,
281: >0 set position, if add item merged with IS_ADD and if define item merged with IS_DEF
282: */
283: int sess_SetValue(tagSess * __restrict s, const char *csAttr, const char *psVal);
284:
285:
286: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>