lib/cds/memory.h
1c24f8eb
 /* 
  * Copyright (C) 2005 iptelorg GmbH
  *
  * 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
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
1c24f8eb
  */
 
 #ifndef __CDS_MEMORY_H
 #define __CDS_MEMORY_H
 
abab9ea7
 /* #define TRACE_CDS_MEMORY */
 
1c24f8eb
 #ifdef __cplusplus
 extern "C" {
 #endif
 
c8d7930f
 /** \ingroup cds
  * @{ 
  *
  * \defgroup cds_memory Memory management
  *
  * Memory operations are common for whole CDS library. Because it must work
  together with SER's memory management and must work without it too, there are
  wrapper macros for memory allocation/deallocation.
  * 
  * @{ */
 
abab9ea7
 /* typedef void*(*cds_malloc_func)(unsigned int size);
1c24f8eb
 typedef void(*cds_free_func)(void *ptr);
 
 extern cds_malloc_func cds_malloc;
 extern cds_free_func cds_free;
 
abab9ea7
 void cds_set_memory_functions(cds_malloc_func _malloc, cds_free_func _free); */
 
c8d7930f
 /** \def cds_malloc(s)
  * Function/macro for memory allocation. Which function is choosen depends on
  * SER and TRACE_CDS_MEMORY defines. 
  *
  * When SER is defined shm_malloc is choosen, standard malloc otherwise. */
 
 /** \def cds_free(p)
  * Function/macro for memory deallocation. Which function is choosen depends
  * on SER and TRACE_CDS_MEMORY defines. 
  *
  * If SER is defined shm_free is choosen, standard free otherwise. */
 
 /** \def cds_malloc_ptr
  * Function/macro for memory allocation when pointer to function needed. Which
  * function is choosen depends on SER and TRACE_CDS_MEMORY defines. 
  *
  * If SER is defined shm_malloc is choosen, standard malloc otherwise.  */
 
 /** \def cds_free_ptr
  * Function/macro for memory deallocation when pointer to function needed.
  * Which function is choosen depends on SER and TRACE_CDS_MEMORY defines. 
  *
  * If SER is defined shm_free is choosen, standard free otherwise.  */
 
 /** \def cds_malloc_pkg(s)
  * Function/macro for 'local' memory allocation. Which function is choosen
  * depends on SER and TRACE_CDS_MEMORY defines. 
  *
  * When SER is defined pkg_malloc is choosen, standard malloc otherwise. */
 
 /** \def cds_free_pkg(p)
  * Function/macro for 'local' memory deallocation. Which function is choosen
  * depends on SER and TRACE_CDS_MEMORY defines. 
  *
  * When SER is defined pkg_free is choosen, standard free otherwise. */
 
abab9ea7
 #ifdef TRACE_CDS_MEMORY
 
c8d7930f
 /** \internal Debugging variant of alloc function */
abab9ea7
 void *debug_malloc(int size, const char *file, int line);
c8d7930f
 
 /** \internal Debugging variant of free function */
abab9ea7
 void debug_free(void *block, const char *file, int line);
c8d7930f
 
 /** \internal Another debugging variant of alloc function - used when pointer
  * to function needed. */
abab9ea7
 void *debug_malloc_ex(unsigned int size);
c8d7930f
 
 /** \internal Another debugging variant of free function - used when pointer to
  * function needed. */
abab9ea7
 void debug_free_ex(void *block);
 
c8d7930f
 /* \internal Helper function for debugging - shows some debugging information about
  * memory allocations (currently only the number of allocated blocks). */
abab9ea7
 void cds_memory_trace(char *dst, int dst_len);
c8d7930f
 
 /** \internal Helper function which is useful for memory debugging only - initializes
  * internal variables for memory tracing */
abab9ea7
 void cds_memory_trace_init();
 
 #define cds_malloc(s)	debug_malloc(s,__FILE__, __LINE__)
 #define cds_free(p)		debug_free(p,__FILE__, __LINE__)
 #define cds_free_ptr	debug_free_ex
 #define cds_malloc_ptr	debug_malloc_ex
3bf618cc
 #define cds_malloc_pkg(s)	debug_malloc(s,__FILE__, __LINE__)
 #define cds_free_pkg(p)		debug_free(p,__FILE__, __LINE__)
abab9ea7
 
 #else /* !TRACE */
 
 #ifdef SER
 
 #include <mem/mem.h>
 #include <mem/shm_mem.h>
 
 void* shm_malloc_x(unsigned int size);
 void shm_free_x(void *ptr);
 
 #define cds_malloc(s)	shm_malloc(s)
 #define cds_free(p)		shm_free(p)
 #define cds_malloc_ptr	shm_malloc_x
 #define cds_free_ptr	shm_free_x
3bf618cc
 #define cds_malloc_pkg(s)	pkg_malloc(s)
 #define cds_free_pkg(p)		pkg_free(p)
abab9ea7
 
 #else /* !SER */
 
 #include <stdlib.h>
 
 #define cds_malloc(s)	malloc(s)
 #define cds_free(p)		free(p)
 #define cds_malloc_ptr	malloc
 #define cds_free_ptr	free
3bf618cc
 #define cds_malloc_pkg(s)	malloc(s)
 #define cds_free_pkg(p)		free(p)
abab9ea7
 
 #endif /* !SER */
 
 #endif /* !TRACE_CDS_MEMORY */
1c24f8eb
 
 #ifdef __cplusplus
 }
 #endif
 
c8d7930f
 /** @} 
  * @} */
1c24f8eb
 
 #endif