shm_mem.h

Go to the documentation of this file.
00001 /*
00002  * shared mem stuff
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of sip-router, a free SIP server.
00007  *
00008  * Permission to use, copy, modify, and distribute this software for any
00009  * purpose with or without fee is hereby granted, provided that the above
00010  * copyright notice and this permission notice appear in all copies.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00013  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00014  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00015  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00016  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00017  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00018  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00019  */
00020 
00021 /*
00022  * History:
00023  * --------
00024  *  2003-06-29  added shm_realloc & replaced shm_resize (andrei)
00025  *  2003-11-19  reverted shm_resize to the old version, using
00026  *               realloc causes terrible fragmentation  (andrei)
00027  *  2005-03-02   added shm_info() & re-eneabled locking on shm_status (andrei)
00028  *  2007-02-23   added shm_available() (andrei)
00029  *  2007-06-10   support for sf_malloc (andrei)
00030  */
00031 
00039 #ifdef SHM_MEM
00040 
00041 #ifndef shm_mem_h
00042 #define shm_mem_h
00043 
00044 #include <string.h>
00045 #include <errno.h>
00046 #include <sys/types.h>
00047 #include <sys/ipc.h>
00048 
00049 #ifndef SHM_MMAP
00050 
00051 #include <sys/shm.h>
00052 
00053 #endif
00054 
00055 #include <sys/sem.h>
00056 #include <string.h>
00057 #include <errno.h>
00058 
00059 /* fix DBG MALLOC stuff */
00060 
00061 /* fix debug defines, DBG_F_MALLOC <=> DBG_QM_MALLOC */
00062 #ifdef F_MALLOC
00063         #ifdef DBG_F_MALLOC
00064                 #ifndef DBG_QM_MALLOC
00065                         #define DBG_QM_MALLOC
00066                 #endif
00067         #elif defined(DBG_QM_MALLOC)
00068                 #define DBG_F_MALLOC
00069         #endif
00070 #endif
00071 
00072 
00073 
00074 #include "../dprint.h"
00075 #include "../lock_ops.h" /* we don't include locking.h on purpose */
00076 
00077 #ifdef LL_MALLOC
00078 #       include "ll_malloc.h"
00079 #       define SHM_SAFE_MALLOC /* no need to lock */
00080         extern struct sfm_block* shm_block;
00081 #ifdef __SUNPRO_C
00082 #       define shm_malloc(...) sfm_malloc(shm_block, __VA_ARGS__)
00083 #       define shm_free(...) sfm_free(shm_block, __VA_ARGS__)
00084 #       define shm_realloc(...) sfm_malloc(shm_block, __VA_ARGS__)
00085         /* WARNING: test, especially if switched to real realloc */
00086 #       define shm_resize(...)  sfm_realloc(shm_block, __VA_ARGS__)
00087 #       define shm_info(...) sfm_info(shm_block, __VA_ARGS__)
00088 #else /* __SUNPRO_C */
00089 #       define shm_malloc(args...) sfm_malloc(shm_block, ## args)
00090 #       define shm_free(args...) sfm_free(shm_block, ## args)
00091 #       define shm_realloc(args...) sfm_malloc(shm_block, ## args)
00092         /* WARNING: test, especially if switched to real realloc */
00093 #       define shm_resize(args...)      sfm_realloc(shm_block, ## args)
00094 #       define shm_info(args...) sfm_info(shm_block, ## args)
00095 #endif /* __SUNPRO_C */
00096 #       define shm_malloc_unsafe  shm_malloc
00097 #       define shm_free_unsafe shm_free
00098 #       define shm_available    sfm_available(shm_block)
00099 #       define shm_status() sfm_status(shm_block)
00100 #       define shm_sums() do{}while(0)
00101 #       define shm_malloc_init sfm_malloc_init
00102 #       define shm_malloc_destroy(b) sfm_malloc_destroy(b)
00103 #       define shm_malloc_on_fork()     sfm_pool_reset()
00104 #elif SF_MALLOC
00105 #       include "sf_malloc.h"
00106 #       define SHM_SAFE_MALLOC /* no need to lock */
00107         extern struct sfm_block* shm_block;
00108 #ifdef __SUNPRO_C
00109 #       define shm_malloc(...) sfm_malloc(shm_block, __VA_ARGS__)
00110 #       define shm_free(...) sfm_free(shm_block, __VA_ARGS__)
00111 #       define shm_realloc(...) sfm_malloc(shm_block, __VA_ARGS__)
00112         /* WARNING: test, especially if switched to real realloc */
00113 #       define shm_resize(...)  sfm_realloc(shm_block, __VA_ARGS__)
00114 #       define shm_info(...) sfm_info(shm_block, __VA_ARGS__)
00115 #else /* __SUNPRO_C */
00116 #       define shm_malloc(args...) sfm_malloc(shm_block, ## args)
00117 #       define shm_free(args...) sfm_free(shm_block, ## args)
00118 #       define shm_realloc(args...) sfm_malloc(shm_block, ## args)
00119         /* WARNING: test, especially if switched to real realloc */
00120 #       define shm_resize(args...)      sfm_realloc(shm_block, ## args)
00121 #       define shm_info(args...) sfm_info(shm_block, ## args)
00122 #endif /* __SUNPRO_C */
00123 #       define shm_malloc_unsafe  shm_malloc
00124 #       define shm_free_unsafe shm_free
00125 #       define shm_available    sfm_available(shm_block)
00126 #       define shm_status() sfm_status(shm_block)
00127 #       define shm_sums() do{}while(0)
00128 #       define shm_malloc_init sfm_malloc_init
00129 #       define shm_malloc_destroy(b) sfm_malloc_destroy(b)
00130 #       define shm_malloc_on_fork()     sfm_pool_reset()
00131 #elif defined F_MALLOC
00132 #       include "f_malloc.h"
00133         extern struct fm_block* shm_block;
00134 #       define MY_MALLOC fm_malloc
00135 #       define MY_FREE fm_free
00136 #       define MY_REALLOC fm_realloc
00137 #       define MY_STATUS fm_status
00138 #       define MY_MEMINFO       fm_info
00139 #       define MY_SUMS  fm_sums
00140 #       define  shm_malloc_init fm_malloc_init
00141 #       define shm_malloc_destroy(b) do{}while(0)
00142 #       define shm_available() fm_available(shm_block)
00143 #       define shm_malloc_on_fork() do{}while(0)
00144 #elif defined DL_MALLOC
00145 #       include "dl_malloc.h"
00146         extern mspace shm_block;
00147 #       define MY_MALLOC mspace_malloc
00148 #       define MY_FREE mspace_free
00149 #       define MY_REALLOC mspace_realloc
00150 #       define MY_STATUS(...) 0
00151 #       define MY_SUMS do{}while(0)
00152 #       define MY_MEMINFO       mspace_info
00153 #       define  shm_malloc_init(buf, len) create_mspace_with_base(buf, len, 0)
00154 #       define shm_malloc_destroy(b) do{}while(0)
00155 #       define shm_malloc_on_fork() do{}while(0)
00156 #else
00157 #       include "q_malloc.h"
00158         extern struct qm_block* shm_block;
00159 #       define MY_MALLOC qm_malloc
00160 #       define MY_FREE qm_free
00161 #       define MY_REALLOC qm_realloc
00162 #       define MY_STATUS qm_status
00163 #       define MY_MEMINFO       qm_info
00164 #       define MY_SUMS  qm_sums
00165 #       define  shm_malloc_init qm_malloc_init
00166 #       define shm_malloc_destroy(b) do{}while(0)
00167 #       define shm_available() qm_available(shm_block)
00168 #       define shm_malloc_on_fork() do{}while(0)
00169 #endif
00170 
00171 #ifndef SHM_SAFE_MALLOC
00172         extern gen_lock_t* mem_lock;
00173 #endif
00174 
00175 
00176 int shm_mem_init(int); /* calls shm_getmem & shm_mem_init_mallocs */
00177 int shm_getmem(void);   /* allocates the memory (mmap or sysv shmap) */
00178 int shm_mem_init_mallocs(void* mempool, unsigned long size); /* initialize
00179                                                                                                                                 the mallocs
00180                                                                                                                                 & the lock */
00181 void shm_mem_destroy(void);
00182 
00183 
00184 
00185 #ifdef SHM_SAFE_MALLOC
00186 #define shm_lock() do{}while(0)
00187 #define shm_unlock() do{}while(0)
00188 
00189 #else /* ! SHM_SAFE_MALLOC */
00190 
00191 #define shm_lock()    lock_get(mem_lock)
00192 #define shm_unlock()  lock_release(mem_lock)
00193 
00194 #ifdef DBG_QM_MALLOC
00195 
00196 #include "src_loc.h"
00197 
00198 #define shm_malloc_unsafe(_size ) \
00199         MY_MALLOC(shm_block, (_size), _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_ )
00200 
00201 
00202 inline static void* _shm_malloc(unsigned int size, 
00203         const char *file, const char *function, int line )
00204 {
00205         void *p;
00206         
00207         shm_lock();
00208         p=MY_MALLOC(shm_block, size, file, function, line );
00209         shm_unlock();
00210         return p; 
00211 }
00212 
00213 
00214 inline static void* _shm_realloc(void *ptr, unsigned int size, 
00215                 const char* file, const char* function, int line )
00216 {
00217         void *p;
00218         shm_lock();
00219         p=MY_REALLOC(shm_block, ptr, size, file, function, line);
00220         shm_unlock();
00221         return p;
00222 }
00223 
00224 #define shm_malloc( _size ) _shm_malloc((_size), \
00225         _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_ )
00226 
00227 #define shm_realloc( _ptr, _size ) _shm_realloc( (_ptr), (_size), \
00228         _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_ )
00229 
00230 
00231 
00232 #define shm_free_unsafe( _p  ) \
00233         MY_FREE( shm_block, (_p), _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_ )
00234 
00235 #define shm_free(_p) \
00236 do { \
00237                 shm_lock(); \
00238                 shm_free_unsafe( (_p)); \
00239                 shm_unlock(); \
00240 }while(0)
00241 
00242 
00243 
00244 void* _shm_resize(void* ptr, unsigned int size, const char* f, const char* fn,
00245                                         int line);
00246 #define shm_resize(_p, _s ) _shm_resize((_p), (_s), \
00247                 _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_ )
00248 /*#define shm_resize(_p, _s ) shm_realloc( (_p), (_s))*/
00249 
00250 
00251 
00252 #else /*DBQ_QM_MALLOC*/
00253 
00254 
00255 #define shm_malloc_unsafe(_size) MY_MALLOC(shm_block, (_size))
00256 
00257 inline static void* shm_malloc(unsigned int size)
00258 {
00259         void *p;
00260         
00261         shm_lock();
00262         p=shm_malloc_unsafe(size);
00263         shm_unlock();
00264          return p; 
00265 }
00266 
00267 
00268 inline static void* shm_realloc(void *ptr, unsigned int size)
00269 {
00270         void *p;
00271         shm_lock();
00272         p=MY_REALLOC(shm_block, ptr, size);
00273         shm_unlock();
00274         return p;
00275 }
00276 
00277 
00278 
00279 #define shm_free_unsafe( _p ) MY_FREE(shm_block, (_p))
00280 
00281 #define shm_free(_p) \
00282 do { \
00283                 shm_lock(); \
00284                 shm_free_unsafe( _p ); \
00285                 shm_unlock(); \
00286 }while(0)
00287 
00288 
00289 
00290 void* _shm_resize(void* ptr, unsigned int size);
00291 #define shm_resize(_p, _s) _shm_resize( (_p), (_s))
00292 /*#define shm_resize(_p, _s) shm_realloc( (_p), (_s))*/
00293 
00294 
00295 #endif  /* DBG_QM_MALLOC */
00296 
00297 
00298 #define shm_status() \
00299 do { \
00300                 shm_lock(); \
00301                 MY_STATUS(shm_block); \
00302                 shm_unlock(); \
00303 }while(0)
00304 
00305 
00306 #define shm_info(mi) \
00307 do{\
00308         shm_lock(); \
00309         MY_MEMINFO(shm_block, mi); \
00310         shm_unlock(); \
00311 }while(0)
00312 
00313 #ifdef MY_SUMS
00314 #define shm_sums() \
00315         do { \
00316                 shm_lock(); \
00317                 MY_SUMS(shm_block); \
00318                 shm_unlock(); \
00319         }while(0)
00320         
00321 #endif /* MY_SUMS */
00322 
00323 #endif /* ! SHM_SAFE_MALLOC */
00324 
00325 #endif /* shm_mem_h */
00326 
00327 #endif /* SHM_MEM */
00328