mem/shm_mem.h
47f59303
 /*
628e3a5a
  * shared mem stuff
7dd0b342
  *
84d8e165
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
4c4e5766
  * This file is part of Kamailio, a free SIP server.
7dd0b342
  *
e4f42ce1
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  * copyright notice and this permission notice appear in all copies.
7dd0b342
  *
e4f42ce1
  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
628e3a5a
  */
47f59303
 
628e3a5a
 
47f59303
 /**
  * \file
  * \brief  Shared memory functions
  * \ingroup mem
  */
 
7dd0b342
 
628e3a5a
 #ifdef SHM_MEM
 
 #ifndef shm_mem_h
 #define shm_mem_h
 
bf3e80bd
 #include "shm.h"
 
 #if 0
 
628e3a5a
 #include <string.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/ipc.h>
dcb5f364
 
 #ifndef SHM_MMAP
 
628e3a5a
 #include <sys/shm.h>
dcb5f364
 
 #endif
 
628e3a5a
 #include <sys/sem.h>
 #include <string.h>
 #include <errno.h>
 
5e3b7958
 /* fix DBG MALLOC stuff */
 
 /* fix debug defines, DBG_F_MALLOC <=> DBG_QM_MALLOC */
 #ifdef F_MALLOC
 	#ifdef DBG_F_MALLOC
 		#ifndef DBG_QM_MALLOC
 			#define DBG_QM_MALLOC
 		#endif
 	#elif defined(DBG_QM_MALLOC)
 		#define DBG_F_MALLOC
 	#endif
8810c00f
 #elif defined TLSF_MALLOC
 	#ifdef DBG_TLSF_MALLOC
 		#ifndef DBG_QM_MALLOC
 			#define DBG_QM_MALLOC
 		#endif
 	#elif defined(DBG_QM_MALLOC)
 		#define DBG_TLSF_MALLOC
 	#endif
5e3b7958
 #endif
 
628e3a5a
 
 
dda9dab1
 #include "../dprint.h"
ba98af46
 #include "../lock_ops.h" /* we don't include locking.h on purpose */
dda9dab1
 
fbe11914
 #ifdef LL_MALLOC
 #	include "ll_malloc.h"
 #	define SHM_SAFE_MALLOC /* no need to lock */
 	extern struct sfm_block* shm_block;
 #ifdef __SUNPRO_C
 #	define shm_malloc(...) sfm_malloc(shm_block, __VA_ARGS__)
 #	define shm_free(...) sfm_free(shm_block, __VA_ARGS__)
 #	define shm_realloc(...) sfm_malloc(shm_block, __VA_ARGS__)
 	/* WARNING: test, especially if switched to real realloc */
 #	define shm_resize(...)	sfm_realloc(shm_block, __VA_ARGS__)
 #	define shm_info(...) sfm_info(shm_block, __VA_ARGS__)
 #else /* __SUNPRO_C */
 #	define shm_malloc(args...) sfm_malloc(shm_block, ## args)
 #	define shm_free(args...) sfm_free(shm_block, ## args)
 #	define shm_realloc(args...) sfm_malloc(shm_block, ## args)
 	/* WARNING: test, especially if switched to real realloc */
 #	define shm_resize(args...)	sfm_realloc(shm_block, ## args)
 #	define shm_info(args...) sfm_info(shm_block, ## args)
 #endif /* __SUNPRO_C */
 #	define shm_malloc_unsafe  shm_malloc
 #	define shm_free_unsafe shm_free
 #	define shm_available	sfm_available(shm_block)
 #	define shm_status() sfm_status(shm_block)
ed20ee1d
 #	define shm_sums() do{}while(0)
fbe11914
 #	define shm_malloc_init sfm_malloc_init
 #	define shm_malloc_destroy(b) sfm_malloc_destroy(b)
 #	define shm_malloc_on_fork()	sfm_pool_reset()
 #elif SF_MALLOC
 #	include "sf_malloc.h"
 #	define SHM_SAFE_MALLOC /* no need to lock */
 	extern struct sfm_block* shm_block;
 #ifdef __SUNPRO_C
 #	define shm_malloc(...) sfm_malloc(shm_block, __VA_ARGS__)
 #	define shm_free(...) sfm_free(shm_block, __VA_ARGS__)
 #	define shm_realloc(...) sfm_malloc(shm_block, __VA_ARGS__)
 	/* WARNING: test, especially if switched to real realloc */
 #	define shm_resize(...)	sfm_realloc(shm_block, __VA_ARGS__)
 #	define shm_info(...) sfm_info(shm_block, __VA_ARGS__)
 #else /* __SUNPRO_C */
 #	define shm_malloc(args...) sfm_malloc(shm_block, ## args)
 #	define shm_free(args...) sfm_free(shm_block, ## args)
 #	define shm_realloc(args...) sfm_malloc(shm_block, ## args)
 	/* WARNING: test, especially if switched to real realloc */
 #	define shm_resize(args...)	sfm_realloc(shm_block, ## args)
 #	define shm_info(args...) sfm_info(shm_block, ## args)
 #endif /* __SUNPRO_C */
 #	define shm_malloc_unsafe  shm_malloc
 #	define shm_free_unsafe shm_free
 #	define shm_available	sfm_available(shm_block)
 #	define shm_status() sfm_status(shm_block)
ed20ee1d
 #	define shm_sums() do{}while(0)
fbe11914
 #	define shm_malloc_init sfm_malloc_init
 #	define shm_malloc_destroy(b) sfm_malloc_destroy(b)
 #	define shm_malloc_on_fork()	sfm_pool_reset()
b2dec9c6
 #elif defined F_MALLOC
 #	include "f_malloc.h"
 	extern struct fm_block* shm_block;
 #	define MY_MALLOC fm_malloc
 #	define MY_FREE fm_free
9c01c860
 #	define MY_REALLOC fm_realloc
b2dec9c6
 #	define MY_STATUS fm_status
51dffb0a
 #	define MY_MEMINFO	fm_info
ed20ee1d
 #	define MY_SUMS	fm_sums
3ef4a0eb
 #	define  shm_malloc_init fm_malloc_init
fbe11914
 #	define shm_malloc_destroy(b) do{}while(0)
8799e028
 #	define shm_available() fm_available(shm_block)
fbe11914
 #	define shm_malloc_on_fork() do{}while(0)
51ea5ec2
 #elif defined DL_MALLOC
 #	include "dl_malloc.h"
 	extern mspace shm_block;
 #	define MY_MALLOC mspace_malloc
 #	define MY_FREE mspace_free
 #	define MY_REALLOC mspace_realloc
 #	define MY_STATUS(...) 0
ed20ee1d
 #	define MY_SUMS do{}while(0)
51ea5ec2
 #	define MY_MEMINFO	mspace_info
c996d5dd
 #	define  shm_malloc_init(buf, len, type) create_mspace_with_base(buf, len, 0)
fbe11914
 #	define shm_malloc_destroy(b) do{}while(0)
 #	define shm_malloc_on_fork() do{}while(0)
92dcf770
 #elif defined TLSF_MALLOC
 #	include "tlsf.h"
 	extern pool_t shm_block;
 #	define MY_MALLOC tlsf_malloc
 #	define MY_FREE tlsf_free
 #	define MY_REALLOC tlsf_realloc
8810c00f
 #	define MY_STATUS tlsf_status
92dcf770
 #	define MY_MEMINFO	tlsf_meminfo
8810c00f
 #	define MY_SUMS tlsf_sums
92dcf770
 #	define shm_malloc_init(mem, bytes, type) tlsf_create_with_pool((void*) mem, bytes)
 #	define shm_malloc_destroy(b) do{}while(0)
41b62951
 #	define shm_available() tlsf_available(shm_block)
92dcf770
 #	define shm_malloc_on_fork() do{}while(0)
dda9dab1
 #else
 #	include "q_malloc.h"
 	extern struct qm_block* shm_block;
 #	define MY_MALLOC qm_malloc
 #	define MY_FREE qm_free
f22b996b
 #	define MY_REALLOC qm_realloc
dda9dab1
 #	define MY_STATUS qm_status
51dffb0a
 #	define MY_MEMINFO	qm_info
ed20ee1d
 #	define MY_SUMS	qm_sums
3ef4a0eb
 #	define  shm_malloc_init qm_malloc_init
fbe11914
 #	define shm_malloc_destroy(b) do{}while(0)
8799e028
 #	define shm_available() qm_available(shm_block)
fbe11914
 #	define shm_malloc_on_fork() do{}while(0)
dda9dab1
 #endif
eca7f442
 
fbe11914
 #ifndef SHM_SAFE_MALLOC
0bd53297
 	extern gen_lock_t* mem_lock;
fbe11914
 #endif
eca7f442
 
628e3a5a
 
7c855aa0
 int shm_mem_init(int); /* calls shm_getmem & shm_mem_init_mallocs */
9f8a2ff6
 int shm_getmem(void);   /* allocates the memory (mmap or sysv shmap) */
c082177a
 int shm_mem_init_mallocs(void* mempool, unsigned long size); /* initialize
 																the mallocs
 																& the lock */
9f8a2ff6
 void shm_mem_destroy(void);
628e3a5a
 
 
8bb87b96
 
fbe11914
 #ifdef SHM_SAFE_MALLOC
 #define shm_lock() do{}while(0)
 #define shm_unlock() do{}while(0)
 
 #else /* ! SHM_SAFE_MALLOC */
 
e0135488
 #define shm_lock()    lock_get(mem_lock)
 #define shm_unlock()  lock_release(mem_lock)
8bb87b96
 
b4d048d4
 #ifdef DBG_QM_MALLOC
ea6721d7
 
220ac6e2
 #include "src_loc.h"
6419a43f
 
ea6721d7
 #define shm_malloc_unsafe(_size ) \
1755c1b7
 	MY_MALLOC(shm_block, (_size), _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_, _SRC_MODULE_)
51eadd0c
 
 
359ec1d0
 inline static void* _shm_malloc(unsigned int size, 
1755c1b7
 	const char *file, const char *function, int line, const char *mname)
51eadd0c
 {
 	void *p;
 	
f22b996b
 	shm_lock();
1755c1b7
 	p=MY_MALLOC(shm_block, size, file, function, line, mname);
51eadd0c
 	shm_unlock();
 	return p; 
 }
 
f22b996b
 
 inline static void* _shm_realloc(void *ptr, unsigned int size, 
1755c1b7
 		const char* file, const char* function, int line, const char *mname)
f22b996b
 {
 	void *p;
 	shm_lock();
1755c1b7
 	p=MY_REALLOC(shm_block, ptr, size, file, function, line, mname);
f22b996b
 	shm_unlock();
 	return p;
 }
 
359ec1d0
 #define shm_malloc( _size ) _shm_malloc((_size), \
1755c1b7
 	_SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_ , _SRC_MODULE_)
359ec1d0
 
f22b996b
 #define shm_realloc( _ptr, _size ) _shm_realloc( (_ptr), (_size), \
1755c1b7
 	_SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_, _SRC_MODULE_)
f22b996b
 
51eadd0c
 
b4d048d4
 
ea6721d7
 #define shm_free_unsafe( _p  ) \
1755c1b7
 	MY_FREE( shm_block, (_p), _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_, _SRC_MODULE_)
51eadd0c
 
ea6721d7
 #define shm_free(_p) \
b4d048d4
 do { \
 		shm_lock(); \
ea6721d7
 		shm_free_unsafe( (_p)); \
b4d048d4
 		shm_unlock(); \
 }while(0)
 
f22b996b
 
 
4168f707
 void* _shm_resize(void* ptr, unsigned int size, const char* f, const char* fn,
1755c1b7
 					int line, const char *mname);
e85f0834
 #define shm_resize(_p, _s ) _shm_resize((_p), (_s), \
1755c1b7
 		_SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_, _SRC_MODULE_)
e85f0834
 /*#define shm_resize(_p, _s ) shm_realloc( (_p), (_s))*/
b4d048d4
 
51eadd0c
 
 
e85f0834
 #else /*DBQ_QM_MALLOC*/
b4d048d4
 
51eadd0c
 
ea6721d7
 #define shm_malloc_unsafe(_size) MY_MALLOC(shm_block, (_size))
51eadd0c
 
 inline static void* shm_malloc(unsigned int size)
 {
 	void *p;
 	
 	shm_lock();
 	p=shm_malloc_unsafe(size);
 	shm_unlock();
 	 return p; 
 }
 
628e3a5a
 
f22b996b
 inline static void* shm_realloc(void *ptr, unsigned int size)
 {
 	void *p;
 	shm_lock();
 	p=MY_REALLOC(shm_block, ptr, size);
 	shm_unlock();
 	return p;
 }
 
 
628e3a5a
 
ea6721d7
 #define shm_free_unsafe( _p ) MY_FREE(shm_block, (_p))
51eadd0c
 
ea6721d7
 #define shm_free(_p) \
628e3a5a
 do { \
21f03122
 		shm_lock(); \
ea6721d7
 		shm_free_unsafe( _p ); \
21f03122
 		shm_unlock(); \
628e3a5a
 }while(0)
 
51eadd0c
 
 
e85f0834
 void* _shm_resize(void* ptr, unsigned int size);
 #define shm_resize(_p, _s) _shm_resize( (_p), (_s))
 /*#define shm_resize(_p, _s) shm_realloc( (_p), (_s))*/
628e3a5a
 
51eadd0c
 
fbe11914
 #endif  /* DBG_QM_MALLOC */
b4d048d4
 
628e3a5a
 
21f03122
 #define shm_status() \
628e3a5a
 do { \
51dffb0a
 		shm_lock(); \
dda9dab1
 		MY_STATUS(shm_block); \
51dffb0a
 		shm_unlock(); \
628e3a5a
 }while(0)
 
 
51dffb0a
 #define shm_info(mi) \
 do{\
 	shm_lock(); \
 	MY_MEMINFO(shm_block, mi); \
 	shm_unlock(); \
 }while(0)
ea6721d7
 
ed20ee1d
 #ifdef MY_SUMS
 #define shm_sums() \
 	do { \
 		shm_lock(); \
 		MY_SUMS(shm_block); \
 		shm_unlock(); \
 	}while(0)
 	
 #endif /* MY_SUMS */
 
fbe11914
 #endif /* ! SHM_SAFE_MALLOC */
628e3a5a
 
51e263de
 /* multi-process safe version of shm_available()
  */
c4d48f3a
 unsigned long shm_available_safe();
51e263de
 
bf3e80bd
 #endif
51e263de
 
fbe11914
 #endif /* shm_mem_h */
628e3a5a
 
fbe11914
 #endif /* SHM_MEM */
628e3a5a