058ede7145fc68c75ef6890583e251749bb53130
[sip-router] / mem / shm_mem.h
1 /* $Id$*
2  *
3  * shared mem stuff
4  */
5
6 #ifdef SHM_MEM
7
8 #ifndef shm_mem_h
9 #define shm_mem_h
10
11 #include <string.h>
12 #include <errno.h>
13 #include <sys/types.h>
14 #include <sys/ipc.h>
15
16 #ifndef SHM_MMAP
17
18 #include <sys/shm.h>
19
20 #endif
21
22 #include <sys/sem.h>
23 #include <string.h>
24 #include <errno.h>
25
26
27
28 #include "../dprint.h"
29
30 #ifdef VQ_MALLOC
31 #       include "vq_malloc.h"
32         extern struct vqm_block* shm_block;
33 #       define MY_MALLOC vqm_malloc
34 #       define MY_FREE vqm_free
35 #       define MY_STATUS vqm_status
36 #elif defined F_MALLOC
37 #       include "f_malloc.h"
38         extern struct fm_block* shm_block;
39 #       define MY_MALLOC fm_malloc
40 #       define MY_FREE fm_free
41 #       define MY_STATUS fm_status
42 #else
43 #       include "q_malloc.h"
44         extern struct qm_block* shm_block;
45 #       define MY_MALLOC qm_malloc
46 #       define MY_FREE qm_free
47 #       define MY_STATUS qm_status
48 #endif
49
50 #ifdef FAST_LOCK
51 #include "../fastlock.h"
52         
53         extern fl_lock_t* mem_lock;
54 #else
55 extern  int shm_semid;
56 #endif
57
58
59 int shm_mem_init();
60 void shm_mem_destroy();
61
62
63 #ifdef FAST_LOCK
64
65 #define shm_lock()    get_lock(mem_lock)
66 #define shm_unlock()  release_lock(mem_lock)
67
68 #else
69 /* inline functions (do not move them to *.c, they won't be inlined anymore) */
70 static inline void shm_lock()
71 {
72
73         struct sembuf sop;
74         int ret;
75         
76         sop.sem_num=0;
77         sop.sem_op=-1; /*down*/
78         sop.sem_flg=0 /*SEM_UNDO*/;
79 again:
80         ret=semop(shm_semid, &sop, 1);
81
82         switch(ret){
83                 case 0: /*ok*/
84                         break;
85                 case EINTR: /*interrupted by signal, try again*/
86                         DBG("sh_lock: interrupted by signal, trying again...\n");
87                         goto again;
88                 default:
89                         LOG(L_ERR, "ERROR: sh_lock: error waiting on semaphore: %s\n",
90                                         strerror(errno));
91         }
92 }
93
94
95
96 static inline void shm_unlock()
97 {
98         struct sembuf sop;
99         int ret;
100         
101         sop.sem_num=0;
102         sop.sem_op=1; /*up*/
103         sop.sem_flg=0 /*SEM_UNDO*/;
104 again:
105         ret=semop(shm_semid, &sop, 1);
106         /*should ret immediately*/
107         switch(ret){
108                 case 0: /*ok*/
109                         break;
110                 case EINTR: /*interrupted by signal, try again*/
111                         DBG("sh_lock: interrupted by signal, trying again...\n");
112                         goto again;
113                 default:
114                         LOG(L_ERR, "ERROR: sh_lock: error waiting on semaphore: %s\n",
115                                         strerror(errno));
116         }
117 }
118
119 /* ret -1 on erro*/
120 #endif
121
122
123
124 #ifdef DBG_QM_MALLOC
125
126 #define shm_malloc_unsafe(_size ) \
127         MY_MALLOC(shm_block, (_size), __FILE__, __FUNCTION__, __LINE__ )
128
129
130 inline static void* _shm_malloc(unsigned int size, 
131         char *file, char *function, int line )
132 {
133         void *p;
134         
135         shm_lock();\
136         p=MY_MALLOC(shm_block, size, file, function, line );
137         shm_unlock();
138         return p; 
139 }
140
141 #define shm_malloc( _size ) _shm_malloc((_size), \
142         __FILE__, __FUNCTION__, __LINE__ )
143
144
145
146 #define shm_free_unsafe( _p  ) \
147         MY_FREE( shm_block, (_p), __FILE__, __FUNCTION__, __LINE__ )
148
149 #define shm_free(_p) \
150 do { \
151                 shm_lock(); \
152                 shm_free_unsafe( (_p)); \
153                 shm_unlock(); \
154 }while(0)
155
156 void* _shm_resize( void*, unsigned int, char*, char*, unsigned int);
157 #define shm_resize(_p, _s ) \
158         _shm_resize( (_p), (_s),   __FILE__, __FUNCTION__, __LINE__)
159
160
161
162 #else
163
164
165 #define shm_malloc_unsafe(_size) MY_MALLOC(shm_block, (_size))
166
167 inline static void* shm_malloc(unsigned int size)
168 {
169         void *p;
170         
171         shm_lock();
172         p=shm_malloc_unsafe(size);
173         shm_unlock();
174          return p; 
175 }
176
177
178 void* _shm_resize( void*, unsigned int);
179
180 #define shm_free_unsafe( _p ) MY_FREE(shm_block, (_p))
181
182 #define shm_free(_p) \
183 do { \
184                 shm_lock(); \
185                 shm_free_unsafe( _p ); \
186                 shm_unlock(); \
187 }while(0)
188
189
190
191 #define shm_resize(_p, _s) _shm_resize( (_p), (_s))
192
193
194 #endif
195
196
197 #define shm_status() \
198 do { \
199                 /*shm_lock();*/ \
200                 MY_STATUS(shm_block); \
201                 /*shm_unlock();*/ \
202 }while(0)
203
204
205
206
207 #endif
208
209 #endif
210