mem/mem.c
dda9dab1
 /*
84d8e165
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
4c4e5766
  * This file is part of Kamailio, a free SIP server.
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.
  *
  * 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.
90907c3f
  *
  *
dda9dab1
  */
 
47f59303
 /**
  * \file
  * \brief Main definitions for memory manager
  * 
  * Main definitions for memory manager, like malloc, free and realloc
  * \ingroup mem
  */
 
7dd0b342
 
caf80ae6
 #include <stdio.h>
7b3a6eca
 #include <stdlib.h>
dda9dab1
 #include "../config.h"
 #include "../dprint.h"
caf80ae6
 #include "../globals.h"
dda9dab1
 #include "mem.h"
 
 #ifdef PKG_MALLOC
f92113d8
 #include "q_malloc.h"
e22bbdb8
 #endif
 
 #ifdef SHM_MEM
 #include "shm_mem.h"
dda9dab1
 #endif
 
 #ifdef PKG_MALLOC
51ea5ec2
 	#ifndef DL_MALLOC
7b3a6eca
 	char* mem_pool = 0;
51ea5ec2
 	#endif
 
f92113d8
 	#ifdef F_MALLOC
7b3a6eca
 		struct fm_block* mem_block = 0;
51ea5ec2
 	#elif defined DL_MALLOC
 		/* don't need this */
63b7c711
 	#elif defined TLSF_MALLOC
 		tlsf_t mem_block = 0;
dda9dab1
 	#else
7b3a6eca
 		struct qm_block* mem_block = 0;
dda9dab1
 	#endif
 #endif
 
90907c3f
 
47f59303
 /**
  * \brief Initialize private memory pool
  * \return 0 if the memory allocation was successful, -1 otherwise
  */
9f8a2ff6
 int init_pkg_mallocs(void)
dda9dab1
 {
 #ifdef PKG_MALLOC
c4217e61
 	/*init mem*/
7b3a6eca
 	#ifndef DL_MALLOC
 		if (pkg_mem_size == 0)
 			pkg_mem_size = PKG_MEM_POOL_SIZE;
 		mem_pool = malloc(pkg_mem_size);
 	#endif
f92113d8
 	#ifdef F_MALLOC
7b3a6eca
 		if (mem_pool)
8ef30f91
 			mem_block=fm_malloc_init(mem_pool, pkg_mem_size, MEM_TYPE_PKG);
51ea5ec2
 	#elif DL_MALLOC
 		/* don't need this */
63b7c711
 	#elif TLSF_MALLOC
 		mem_block = tlsf_create_with_pool(mem_pool, pkg_mem_size);
dda9dab1
 	#else
7b3a6eca
 		if (mem_pool)
8ef30f91
 			mem_block=qm_malloc_init(mem_pool, pkg_mem_size, MEM_TYPE_PKG);
dda9dab1
 	#endif
51ea5ec2
 	#ifndef DL_MALLOC
c4217e61
 	if (mem_block==0){
 		LOG(L_CRIT, "could not initialize memory pool\n");
7b3a6eca
 		fprintf(stderr, "Too much pkg memory demanded: %ld bytes\n",
 						pkg_mem_size);
dda9dab1
 		return -1;
c4217e61
 	}
51ea5ec2
 	#endif
dda9dab1
 #endif
90907c3f
 	return 0;
 }
dda9dab1
 
90907c3f
 
 
47f59303
 /**
  * \brief Destroy private memory pool
  */
9f8a2ff6
 void destroy_pkg_mallocs(void)
7b3a6eca
 {
 #ifdef PKG_MALLOC
 	#ifndef DL_MALLOC
 		if (mem_pool) {
 			free(mem_pool);
 			mem_pool = 0;
 		}
 	#endif
 #endif /* PKG_MALLOC */
 }
 
 
47f59303
 /**
  * \brief Initialize shared memory pool
  * \param force_alloc Force allocation of memory, e.g. initialize complete block with zero
  * \return 0 if the memory allocation was successful, -1 otherwise
  */
7c855aa0
 int init_shm_mallocs(int force_alloc)
90907c3f
 {
dda9dab1
 #ifdef SHM_MEM
7c855aa0
 	if (shm_mem_init(force_alloc)<0) {
c4217e61
 		LOG(L_CRIT, "could not initialize shared memory pool, exiting...\n");
c082177a
 		 fprintf(stderr, "Too much shared memory demanded: %ld\n",
caf80ae6
 			shm_mem_size );
c4217e61
 		return -1;
 	}
dda9dab1
 #endif
 	return 0;
 }