mem/shm_mem.h
628e3a5a
 /* $Id$*
  *
  * shared mem stuff
7dd0b342
  *
  * Copyright (C) 2001-2003 Fhg Fokus
  *
  * This file is part of ser, a free SIP server.
  *
  * ser is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
  * For a license to use the ser software under conditions
  * other than those described here, or to purchase support for this
  * software, please contact iptel.org by e-mail at the following addresses:
  *    info@iptel.org
  *
  * ser is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
628e3a5a
  */
 
7dd0b342
 
628e3a5a
 #ifdef SHM_MEM
 
 #ifndef shm_mem_h
 #define shm_mem_h
 
 #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>
 
 
 
dda9dab1
 #include "../dprint.h"
e0135488
 #include "../locking.h"
dda9dab1
 
 #ifdef VQ_MALLOC
 #	include "vq_malloc.h"
 	extern struct vqm_block* shm_block;
 #	define MY_MALLOC vqm_malloc
 #	define MY_FREE vqm_free
 #	define MY_STATUS vqm_status
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
 #	define MY_STATUS fm_status
dda9dab1
 #else
 #	include "q_malloc.h"
 	extern struct qm_block* shm_block;
 #	define MY_MALLOC qm_malloc
 #	define MY_FREE qm_free
 #	define MY_STATUS qm_status
 #endif
eca7f442
 
 	
e0135488
 	extern lock_t* mem_lock;
eca7f442
 
628e3a5a
 
 int shm_mem_init();
 void shm_mem_destroy();
 
 
8bb87b96
 
e0135488
 #define shm_lock()    lock_get(mem_lock)
 #define shm_unlock()  lock_release(mem_lock)
8bb87b96
 
 
b4d048d4
 
 #ifdef DBG_QM_MALLOC
ea6721d7
 
 #define shm_malloc_unsafe(_size ) \
 	MY_MALLOC(shm_block, (_size), __FILE__, __FUNCTION__, __LINE__ )
51eadd0c
 
 
359ec1d0
 inline static void* _shm_malloc(unsigned int size, 
 	char *file, char *function, int line )
51eadd0c
 {
 	void *p;
 	
ea6721d7
 	shm_lock();\
359ec1d0
 	p=MY_MALLOC(shm_block, size, file, function, line );
51eadd0c
 	shm_unlock();
 	return p; 
 }
 
359ec1d0
 #define shm_malloc( _size ) _shm_malloc((_size), \
 	__FILE__, __FUNCTION__, __LINE__ )
 
51eadd0c
 
b4d048d4
 
ea6721d7
 #define shm_free_unsafe( _p  ) \
 	MY_FREE( shm_block, (_p), __FILE__, __FUNCTION__, __LINE__ )
51eadd0c
 
ea6721d7
 #define shm_free(_p) \
b4d048d4
 do { \
 		shm_lock(); \
ea6721d7
 		shm_free_unsafe( (_p)); \
b4d048d4
 		shm_unlock(); \
 }while(0)
 
359ec1d0
 void* _shm_resize( void*, unsigned int, char*, char*, unsigned int);
ea6721d7
 #define shm_resize(_p, _s ) \
 	_shm_resize( (_p), (_s),   __FILE__, __FUNCTION__, __LINE__)
b4d048d4
 
51eadd0c
 
 
b4d048d4
 #else
 
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
 
6beabeca
 void* _shm_resize( void*, unsigned int);
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
 
 
ea6721d7
 #define shm_resize(_p, _s) _shm_resize( (_p), (_s))
628e3a5a
 
51eadd0c
 
b4d048d4
 #endif
 
628e3a5a
 
21f03122
 #define shm_status() \
628e3a5a
 do { \
391fa285
 		/*shm_lock();*/ \
dda9dab1
 		MY_STATUS(shm_block); \
391fa285
 		/*shm_unlock();*/ \
628e3a5a
 }while(0)
 
 
ea6721d7
 
628e3a5a
 
 #endif
 
 #endif