shm_mem.h

Go to the documentation of this file.
00001 /* $Id: shm_mem.h 5035 2008-10-06 11:01:24Z henningw $*
00002  *
00003  * shared mem stuff
00004  *
00005  * Copyright (C) 2001-2003 FhG Fokus
00006  *
00007  * This file is part of Kamailio, a free SIP server.
00008  *
00009  * Kamailio is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * Kamailio is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License 
00020  * along with this program; if not, write to the Free Software 
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  * History:
00024  * --------
00025  *  2003-06-29  added shm_realloc & replaced shm_resize (andrei)
00026  *  2003-11-19  reverted shm_resize to the old version, using
00027  *               realloc causes terrible fragmentation  (andrei)
00028  */
00029 
00030 
00031 #include "../statistics.h"
00032 
00033 #ifndef shm_mem_h
00034 #define shm_mem_h
00035 
00036 #include <string.h>
00037 #include <errno.h>
00038 #include <sys/types.h>
00039 #include <sys/ipc.h>
00040 
00041 #ifndef SHM_MMAP
00042 
00043 #include <sys/shm.h>
00044 
00045 #endif
00046 
00047 #include <sys/sem.h>
00048 #include <string.h>
00049 #include <errno.h>
00050 
00051 /* fix DBG MALLOC stuff */
00052 
00053 /* fix debug defines, DBG_F_MALLOC <=> DBG_QM_MALLOC */
00054 #ifdef F_MALLOC
00055    #ifdef DBG_F_MALLOC
00056       #ifndef DBG_QM_MALLOC
00057          #define DBG_QM_MALLOC
00058       #endif
00059    #elif defined(DBG_QM_MALLOC)
00060       #define DBG_F_MALLOC
00061    #endif
00062 #endif
00063 
00064 
00065 #include "../dprint.h"
00066 #include "../lock_ops.h" /* we don't include locking.h on purpose */
00067 
00068 #ifdef F_MALLOC
00069 #  include "f_malloc.h"
00070    extern struct fm_block* shm_block;
00071 #  define MY_MALLOC fm_malloc
00072 #  define MY_FREE fm_free
00073 #  define MY_REALLOC fm_realloc
00074 #  define MY_STATUS fm_status
00075 #  define MY_MEMINFO fm_info
00076 #  ifdef STATISTICS
00077 #     define MY_GET_SIZE   fm_get_size
00078 #     define MY_GET_USED   fm_get_used
00079 #     define MY_GET_RUSED  fm_get_real_used
00080 #     define MY_GET_MUSED  fm_get_max_real_used
00081 #     define MY_GET_FREE   fm_get_free
00082 #     define MY_GET_FRAGS  fm_get_frags
00083 #  endif
00084 #  define  shm_malloc_init fm_malloc_init
00085 #else
00086 #  include "q_malloc.h"
00087    extern struct qm_block* shm_block;
00088 #  define MY_MALLOC qm_malloc
00089 #  define MY_FREE qm_free
00090 #  define MY_REALLOC qm_realloc
00091 #  define MY_STATUS qm_status
00092 #  define MY_MEMINFO qm_info
00093 #  ifdef STATISTICS
00094 #     define MY_GET_SIZE   qm_get_size
00095 #     define MY_GET_USED   qm_get_used
00096 #     define MY_GET_RUSED  qm_get_real_used
00097 #     define MY_GET_MUSED  qm_get_max_real_used
00098 #     define MY_GET_FREE   qm_get_free
00099 #     define MY_GET_FRAGS  qm_get_frags
00100 #  endif
00101 #  define  shm_malloc_init qm_malloc_init
00102 #endif
00103 
00104    
00105    extern gen_lock_t* mem_lock;
00106 
00107 
00108 int shm_mem_init(void); /* calls shm_getmem & shm_mem_init_mallocs */
00109 int shm_getmem(void);   /* allocates the memory (mmap or sysv shmap) */
00110 int shm_mem_init_mallocs(void* mempool, unsigned long size); /* initialize
00111                                                 the mallocs
00112                                                 & the lock */
00113 void shm_mem_destroy(void);
00114 
00115 
00116 
00117 #define shm_lock()    lock_get(mem_lock)
00118 #define shm_unlock()  lock_release(mem_lock)
00119 
00120 
00121 #ifdef DBG_QM_MALLOC
00122 
00123 #ifdef __SUNPRO_C
00124       #define __FUNCTION__ ""  /* gcc specific */
00125 #endif
00126 
00127 
00128 #define shm_malloc_unsafe(_size ) \
00129    MY_MALLOC(shm_block, (_size), __FILE__, __FUNCTION__, __LINE__ )
00130 
00131 
00132 inline static void* _shm_malloc(unsigned int size, 
00133    const char *file, const char *function, int line )
00134 {
00135    void *p;
00136    
00137    shm_lock();
00138    p=MY_MALLOC(shm_block, size, file, function, line );
00139    shm_unlock();
00140    return p; 
00141 }
00142 
00143 
00144 inline static void* _shm_realloc(void *ptr, unsigned int size, 
00145       const char* file, const char* function, int line )
00146 {
00147    void *p;
00148    shm_lock();
00149    p=MY_REALLOC(shm_block, ptr, size, file, function, line);
00150    shm_unlock();
00151    return p;
00152 }
00153 
00154 #define shm_malloc( _size ) _shm_malloc((_size), \
00155    __FILE__, __FUNCTION__, __LINE__ )
00156 
00157 #define shm_realloc( _ptr, _size ) _shm_realloc( (_ptr), (_size), \
00158    __FILE__, __FUNCTION__, __LINE__ )
00159 
00160 
00161 
00162 #define shm_free_unsafe( _p  ) \
00163    MY_FREE( shm_block, (_p), __FILE__, __FUNCTION__, __LINE__ )
00164 
00165 #define shm_free(_p) \
00166 do { \
00167       shm_lock(); \
00168       shm_free_unsafe( (_p)); \
00169       shm_unlock(); \
00170 }while(0)
00171 
00172 
00173 
00174 void* _shm_resize(void* ptr, unsigned int size, const char* f, const char* fn,
00175                int line);
00176 #define shm_resize(_p, _s ) _shm_resize((_p), (_s), \
00177       __FILE__, __FUNCTION__, __LINE__ )
00178 /*#define shm_resize(_p, _s ) shm_realloc( (_p), (_s))*/
00179 
00180 
00181 
00182 #else /*DBQ_QM_MALLOC*/
00183 
00184 
00185 #define shm_malloc_unsafe(_size) MY_MALLOC(shm_block, (_size))
00186 
00187 inline static void* shm_malloc(unsigned int size)
00188 {
00189    void *p;
00190    
00191    shm_lock();
00192    p=shm_malloc_unsafe(size);
00193    shm_unlock();
00194     return p; 
00195 }
00196 
00197 
00198 inline static void* shm_realloc(void *ptr, unsigned int size)
00199 {
00200    void *p;
00201    shm_lock();
00202    p=MY_REALLOC(shm_block, ptr, size);
00203    shm_unlock();
00204    return p;
00205 }
00206 
00207 
00208 
00209 #define shm_free_unsafe( _p ) MY_FREE(shm_block, (_p))
00210 
00211 #define shm_free(_p) \
00212 do { \
00213       shm_lock(); \
00214       shm_free_unsafe( _p ); \
00215       shm_unlock(); \
00216 }while(0)
00217 
00218 
00219 
00220 void* _shm_resize(void* ptr, unsigned int size);
00221 #define shm_resize(_p, _s) _shm_resize( (_p), (_s))
00222 /*#define shm_resize(_p, _s) shm_realloc( (_p), (_s))*/
00223 
00224 
00225 #endif
00226 
00227 
00228 #define shm_status() \
00229 do { \
00230       shm_lock(); \
00231       MY_STATUS(shm_block); \
00232       shm_unlock(); \
00233 }while(0)
00234 
00235 
00236 #define shm_info(mi) \
00237 do{\
00238    shm_lock(); \
00239    MY_MEMINFO(shm_block, mi); \
00240    shm_unlock(); \
00241 }while(0)
00242 
00243 
00244 #ifdef STATISTICS
00245 extern stat_export_t shm_stats[];
00246 
00247 inline static unsigned long shm_get_size(void) {
00248    return MY_GET_SIZE(shm_block);
00249 }
00250 inline static unsigned long shm_get_used(void) {
00251    return MY_GET_USED(shm_block);
00252 }
00253 inline static unsigned long shm_get_rused(void) {
00254    return MY_GET_RUSED(shm_block);
00255 }
00256 inline static unsigned long shm_get_mused(void) {
00257    return MY_GET_MUSED(shm_block);
00258 }
00259 inline static unsigned long shm_get_free(void) {
00260    return MY_GET_FREE(shm_block);
00261 }
00262 inline static unsigned long shm_get_frags(void) {
00263    return MY_GET_FRAGS(shm_block);
00264 }
00265 #endif /*STATISTICS*/
00266 
00267 #endif
00268 

Generated on Thu May 24 12:00:30 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6