shm_mem.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * shared mem stuff
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of sip-router, a free SIP server.
  7. *
  8. * Permission to use, copy, modify, and distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. */
  20. /*
  21. * History:
  22. * --------
  23. * 2003-06-29 added shm_realloc & replaced shm_resize (andrei)
  24. * 2003-11-19 reverted shm_resize to the old version, using
  25. * realloc causes terrible fragmentation (andrei)
  26. * 2005-03-02 added shm_info() & re-eneabled locking on shm_status (andrei)
  27. * 2007-02-23 added shm_available() (andrei)
  28. * 2007-06-10 support for sf_malloc (andrei)
  29. */
  30. /**
  31. * \file
  32. * \brief Shared memory functions
  33. * \ingroup mem
  34. */
  35. #ifdef SHM_MEM
  36. #ifndef shm_mem_h
  37. #define shm_mem_h
  38. #include <string.h>
  39. #include <errno.h>
  40. #include <sys/types.h>
  41. #include <sys/ipc.h>
  42. #ifndef SHM_MMAP
  43. #include <sys/shm.h>
  44. #endif
  45. #include <sys/sem.h>
  46. #include <string.h>
  47. #include <errno.h>
  48. /* fix DBG MALLOC stuff */
  49. /* fix debug defines, DBG_F_MALLOC <=> DBG_QM_MALLOC */
  50. #ifdef F_MALLOC
  51. #ifdef DBG_F_MALLOC
  52. #ifndef DBG_QM_MALLOC
  53. #define DBG_QM_MALLOC
  54. #endif
  55. #elif defined(DBG_QM_MALLOC)
  56. #define DBG_F_MALLOC
  57. #endif
  58. #endif
  59. #include "../dprint.h"
  60. #include "../lock_ops.h" /* we don't include locking.h on purpose */
  61. #ifdef LL_MALLOC
  62. # include "ll_malloc.h"
  63. # define SHM_SAFE_MALLOC /* no need to lock */
  64. extern struct sfm_block* shm_block;
  65. #ifdef __SUNPRO_C
  66. # define shm_malloc(...) sfm_malloc(shm_block, __VA_ARGS__)
  67. # define shm_free(...) sfm_free(shm_block, __VA_ARGS__)
  68. # define shm_realloc(...) sfm_malloc(shm_block, __VA_ARGS__)
  69. /* WARNING: test, especially if switched to real realloc */
  70. # define shm_resize(...) sfm_realloc(shm_block, __VA_ARGS__)
  71. # define shm_info(...) sfm_info(shm_block, __VA_ARGS__)
  72. #else /* __SUNPRO_C */
  73. # define shm_malloc(args...) sfm_malloc(shm_block, ## args)
  74. # define shm_free(args...) sfm_free(shm_block, ## args)
  75. # define shm_realloc(args...) sfm_malloc(shm_block, ## args)
  76. /* WARNING: test, especially if switched to real realloc */
  77. # define shm_resize(args...) sfm_realloc(shm_block, ## args)
  78. # define shm_info(args...) sfm_info(shm_block, ## args)
  79. #endif /* __SUNPRO_C */
  80. # define shm_malloc_unsafe shm_malloc
  81. # define shm_free_unsafe shm_free
  82. # define shm_available sfm_available(shm_block)
  83. # define shm_status() sfm_status(shm_block)
  84. # define shm_sums() do{}while(0)
  85. # define shm_malloc_init sfm_malloc_init
  86. # define shm_malloc_destroy(b) sfm_malloc_destroy(b)
  87. # define shm_malloc_on_fork() sfm_pool_reset()
  88. #elif SF_MALLOC
  89. # include "sf_malloc.h"
  90. # define SHM_SAFE_MALLOC /* no need to lock */
  91. extern struct sfm_block* shm_block;
  92. #ifdef __SUNPRO_C
  93. # define shm_malloc(...) sfm_malloc(shm_block, __VA_ARGS__)
  94. # define shm_free(...) sfm_free(shm_block, __VA_ARGS__)
  95. # define shm_realloc(...) sfm_malloc(shm_block, __VA_ARGS__)
  96. /* WARNING: test, especially if switched to real realloc */
  97. # define shm_resize(...) sfm_realloc(shm_block, __VA_ARGS__)
  98. # define shm_info(...) sfm_info(shm_block, __VA_ARGS__)
  99. #else /* __SUNPRO_C */
  100. # define shm_malloc(args...) sfm_malloc(shm_block, ## args)
  101. # define shm_free(args...) sfm_free(shm_block, ## args)
  102. # define shm_realloc(args...) sfm_malloc(shm_block, ## args)
  103. /* WARNING: test, especially if switched to real realloc */
  104. # define shm_resize(args...) sfm_realloc(shm_block, ## args)
  105. # define shm_info(args...) sfm_info(shm_block, ## args)
  106. #endif /* __SUNPRO_C */
  107. # define shm_malloc_unsafe shm_malloc
  108. # define shm_free_unsafe shm_free
  109. # define shm_available sfm_available(shm_block)
  110. # define shm_status() sfm_status(shm_block)
  111. # define shm_sums() do{}while(0)
  112. # define shm_malloc_init sfm_malloc_init
  113. # define shm_malloc_destroy(b) sfm_malloc_destroy(b)
  114. # define shm_malloc_on_fork() sfm_pool_reset()
  115. #elif defined F_MALLOC
  116. # include "f_malloc.h"
  117. extern struct fm_block* shm_block;
  118. # define MY_MALLOC fm_malloc
  119. # define MY_FREE fm_free
  120. # define MY_REALLOC fm_realloc
  121. # define MY_STATUS fm_status
  122. # define MY_MEMINFO fm_info
  123. # define MY_SUMS fm_sums
  124. # define shm_malloc_init fm_malloc_init
  125. # define shm_malloc_destroy(b) do{}while(0)
  126. # define shm_available() fm_available(shm_block)
  127. # define shm_malloc_on_fork() do{}while(0)
  128. #elif defined DL_MALLOC
  129. # include "dl_malloc.h"
  130. extern mspace shm_block;
  131. # define MY_MALLOC mspace_malloc
  132. # define MY_FREE mspace_free
  133. # define MY_REALLOC mspace_realloc
  134. # define MY_STATUS(...) 0
  135. # define MY_SUMS do{}while(0)
  136. # define MY_MEMINFO mspace_info
  137. # define shm_malloc_init(buf, len) create_mspace_with_base(buf, len, 0)
  138. # define shm_malloc_destroy(b) do{}while(0)
  139. # define shm_malloc_on_fork() do{}while(0)
  140. #else
  141. # include "q_malloc.h"
  142. extern struct qm_block* shm_block;
  143. # define MY_MALLOC qm_malloc
  144. # define MY_FREE qm_free
  145. # define MY_REALLOC qm_realloc
  146. # define MY_STATUS qm_status
  147. # define MY_MEMINFO qm_info
  148. # define MY_SUMS qm_sums
  149. # define shm_malloc_init qm_malloc_init
  150. # define shm_malloc_destroy(b) do{}while(0)
  151. # define shm_available() qm_available(shm_block)
  152. # define shm_malloc_on_fork() do{}while(0)
  153. #endif
  154. #ifndef SHM_SAFE_MALLOC
  155. extern gen_lock_t* mem_lock;
  156. #endif
  157. int shm_mem_init(int); /* calls shm_getmem & shm_mem_init_mallocs */
  158. int shm_getmem(void); /* allocates the memory (mmap or sysv shmap) */
  159. int shm_mem_init_mallocs(void* mempool, unsigned long size); /* initialize
  160. the mallocs
  161. & the lock */
  162. void shm_mem_destroy(void);
  163. #ifdef SHM_SAFE_MALLOC
  164. #define shm_lock() do{}while(0)
  165. #define shm_unlock() do{}while(0)
  166. #else /* ! SHM_SAFE_MALLOC */
  167. #define shm_lock() lock_get(mem_lock)
  168. #define shm_unlock() lock_release(mem_lock)
  169. #ifdef DBG_QM_MALLOC
  170. #include "src_loc.h"
  171. #define shm_malloc_unsafe(_size ) \
  172. MY_MALLOC(shm_block, (_size), _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_ )
  173. inline static void* _shm_malloc(unsigned int size,
  174. const char *file, const char *function, int line )
  175. {
  176. void *p;
  177. shm_lock();
  178. p=MY_MALLOC(shm_block, size, file, function, line );
  179. shm_unlock();
  180. return p;
  181. }
  182. inline static void* _shm_realloc(void *ptr, unsigned int size,
  183. const char* file, const char* function, int line )
  184. {
  185. void *p;
  186. shm_lock();
  187. p=MY_REALLOC(shm_block, ptr, size, file, function, line);
  188. shm_unlock();
  189. return p;
  190. }
  191. #define shm_malloc( _size ) _shm_malloc((_size), \
  192. _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_ )
  193. #define shm_realloc( _ptr, _size ) _shm_realloc( (_ptr), (_size), \
  194. _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_ )
  195. #define shm_free_unsafe( _p ) \
  196. MY_FREE( shm_block, (_p), _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_ )
  197. #define shm_free(_p) \
  198. do { \
  199. shm_lock(); \
  200. shm_free_unsafe( (_p)); \
  201. shm_unlock(); \
  202. }while(0)
  203. void* _shm_resize(void* ptr, unsigned int size, const char* f, const char* fn,
  204. int line);
  205. #define shm_resize(_p, _s ) _shm_resize((_p), (_s), \
  206. _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_ )
  207. /*#define shm_resize(_p, _s ) shm_realloc( (_p), (_s))*/
  208. #else /*DBQ_QM_MALLOC*/
  209. #define shm_malloc_unsafe(_size) MY_MALLOC(shm_block, (_size))
  210. inline static void* shm_malloc(unsigned int size)
  211. {
  212. void *p;
  213. shm_lock();
  214. p=shm_malloc_unsafe(size);
  215. shm_unlock();
  216. return p;
  217. }
  218. inline static void* shm_realloc(void *ptr, unsigned int size)
  219. {
  220. void *p;
  221. shm_lock();
  222. p=MY_REALLOC(shm_block, ptr, size);
  223. shm_unlock();
  224. return p;
  225. }
  226. #define shm_free_unsafe( _p ) MY_FREE(shm_block, (_p))
  227. #define shm_free(_p) \
  228. do { \
  229. shm_lock(); \
  230. shm_free_unsafe( _p ); \
  231. shm_unlock(); \
  232. }while(0)
  233. void* _shm_resize(void* ptr, unsigned int size);
  234. #define shm_resize(_p, _s) _shm_resize( (_p), (_s))
  235. /*#define shm_resize(_p, _s) shm_realloc( (_p), (_s))*/
  236. #endif /* DBG_QM_MALLOC */
  237. #define shm_status() \
  238. do { \
  239. shm_lock(); \
  240. MY_STATUS(shm_block); \
  241. shm_unlock(); \
  242. }while(0)
  243. #define shm_info(mi) \
  244. do{\
  245. shm_lock(); \
  246. MY_MEMINFO(shm_block, mi); \
  247. shm_unlock(); \
  248. }while(0)
  249. #ifdef MY_SUMS
  250. #define shm_sums() \
  251. do { \
  252. shm_lock(); \
  253. MY_SUMS(shm_block); \
  254. shm_unlock(); \
  255. }while(0)
  256. #endif /* MY_SUMS */
  257. #endif /* ! SHM_SAFE_MALLOC */
  258. #endif /* shm_mem_h */
  259. #endif /* SHM_MEM */