00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
00060
00061
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"
00076
00077 #ifdef LL_MALLOC
00078 # include "ll_malloc.h"
00079 # define SHM_SAFE_MALLOC
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
00086 # define shm_resize(...) sfm_realloc(shm_block, __VA_ARGS__)
00087 # define shm_info(...) sfm_info(shm_block, __VA_ARGS__)
00088 #else
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
00093 # define shm_resize(args...) sfm_realloc(shm_block, ## args)
00094 # define shm_info(args...) sfm_info(shm_block, ## args)
00095 #endif
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
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
00113 # define shm_resize(...) sfm_realloc(shm_block, __VA_ARGS__)
00114 # define shm_info(...) sfm_info(shm_block, __VA_ARGS__)
00115 #else
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
00120 # define shm_resize(args...) sfm_realloc(shm_block, ## args)
00121 # define shm_info(args...) sfm_info(shm_block, ## args)
00122 #endif
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);
00177 int shm_getmem(void);
00178 int shm_mem_init_mallocs(void* mempool, unsigned long size);
00179
00180
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
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
00249
00250
00251
00252 #else
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
00293
00294
00295 #endif
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
00322
00323 #endif
00324
00325 #endif
00326
00327 #endif
00328