mem.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 FhG Fokus
00003  *
00004  * This file is part of sip-router, a free SIP server.
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  */
00018 
00019 /*
00020  * History:
00021  * --------
00022  *  2003-03-10  __FUNCTION__ is a gcc-ism, defined it to "" for sun cc
00023  *               (andrei)
00024  *  2003-03-07  split init_malloc into init_pkg_mallocs & init_shm_mallocs 
00025  *               (andrei)
00026  *  2007-02-23   added pkg_info() and pkg_available() (andrei)
00027  */
00028 
00048 #ifndef mem_h
00049 #define mem_h
00050 #include "../config.h"
00051 #include "../dprint.h"
00052 
00053 /* fix debug defines, DBG_F_MALLOC <=> DBG_QM_MALLOC */
00054 #ifdef F_MALLOC
00055         #ifdef DBG_F_MALLOC
00056                 #ifndef DBG_QM_MALLOC
00057                         #define DBG_QM_MALLOC
00058                 #endif
00059         #elif defined(DBG_QM_MALLOC)
00060                 #define DBG_F_MALLOC
00061         #endif
00062 #endif
00063 
00064 #ifdef PKG_MALLOC
00065 #       ifdef F_MALLOC
00066 #               include "f_malloc.h"
00067                 extern struct fm_block* mem_block;
00068 #       elif defined DL_MALLOC
00069 #               include "dl_malloc.h"
00070 #   else
00071 #               include "q_malloc.h"
00072                 extern struct qm_block* mem_block;
00073 #       endif
00074 
00075         extern char* mem_pool;
00076 
00077 
00078 #       ifdef DBG_QM_MALLOC
00079 
00080 #       include "src_loc.h" /* src location macros: _SRC_* */
00081 #               ifdef F_MALLOC
00082 #                       define pkg_malloc(s) fm_malloc(mem_block, (s), _SRC_LOC_, \
00083                                 _SRC_FUNCTION_, _SRC_LINE_)
00084 #                       define pkg_free(p)   fm_free(mem_block, (p), _SRC_LOC_,  \
00085                                 _SRC_FUNCTION_, _SRC_LINE_)
00086 #                       define pkg_realloc(p, s) fm_realloc(mem_block, (p), (s), \
00087                                         _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_)
00088 #               else
00089 #                       define pkg_malloc(s) qm_malloc(mem_block, (s),_SRC_LOC_, \
00090                                 _SRC_FUNCTION_, _SRC_LINE_)
00091 #                       define pkg_realloc(p, s) qm_realloc(mem_block, (p), (s), \
00092                                 _SRC_LOC_, _SRC_FUNCTION_, _SRC_LINE_)
00093 #                       define pkg_free(p)   qm_free(mem_block, (p), _SRC_LOC_,  \
00094                                 _SRC_FUNCTION_, _SRC_LINE_)
00095 #               endif
00096 #       else
00097 #               ifdef F_MALLOC
00098 #                       define pkg_malloc(s) fm_malloc(mem_block, (s))
00099 #                       define pkg_realloc(p, s) fm_realloc(mem_block, (p), (s))
00100 #                       define pkg_free(p)   fm_free(mem_block, (p))
00101 #               elif defined DL_MALLOC
00102 #                       define pkg_malloc(s) dlmalloc((s))
00103 #                       define pkg_realloc(p, s) dlrealloc((p), (s))
00104 #                       define pkg_free(p)   dlfree((p))
00105 #               else
00106 #                       define pkg_malloc(s) qm_malloc(mem_block, (s))
00107 #                       define pkg_realloc(p, s) qm_realloc(mem_block, (p), (s))
00108 #                       define pkg_free(p)   qm_free(mem_block, (p))
00109 #               endif
00110 #       endif
00111 #       ifdef F_MALLOC
00112 #               define pkg_status()    fm_status(mem_block)
00113 #               define pkg_info(mi)    fm_info(mem_block, mi)
00114 #               define pkg_available() fm_available(mem_block)
00115 #               define pkg_sums()      fm_sums(mem_block)
00116 #       elif defined DL_MALLOC
00117 #               define pkg_status()  0
00118 #               define pkg_info(mi)  0
00119 #               define pkg_available()  0
00120 #               define pkg_sums()  0
00121 #       else
00122 #               define pkg_status()    qm_status(mem_block)
00123 #               define pkg_info(mi)    qm_info(mem_block, mi)
00124 #               define pkg_available() qm_available(mem_block)
00125 #               define pkg_sums()      qm_sums(mem_block)
00126 #       endif
00127 #elif defined(SHM_MEM) && defined(USE_SHM_MEM)
00128 #       include "shm_mem.h"
00129 #       define pkg_malloc(s) shm_malloc((s))
00130 #       define pkg_free(p)   shm_free((p))
00131 #       define pkg_status()  shm_status()
00132 #       define pkg_sums()    shm_sums()
00133 #else
00134 #       include <stdlib.h>
00135 #       include "memdbg.h"
00136 #       define pkg_malloc(s) \
00137         (  { void *____v123; ____v123=malloc((s)); \
00138            MDBG("malloc %p size %lu end %p\n", ____v123, (unsigned long)(s), (char*)____v123+(s));\
00139            ____v123; } )
00140 #       define pkg_realloc(p, s) \
00141         (  { void *____v123; ____v123=realloc(p, s); \
00142            MDBG("realloc %p size %lu end %p\n", ____v123, (unsigned long)(s), (char*)____v123+(s));\
00143             ____v123; } )
00144 #       define pkg_free(p)  do{ MDBG("free %p\n", (p)); free((p)); }while(0);
00145 #       define pkg_status()
00146 #       define pkg_sums()
00147 #endif
00148 
00153 int init_pkg_mallocs(void);
00154 
00158 void destroy_pkg_mallocs(void);
00159 
00165 int init_shm_mallocs(int force_alloc);
00166 
00168 #ifdef SYSTEM_MALLOC
00169 #define PKG_MEM_ERROR LM_ERR("could not allocate private memory from system")
00170 #else
00171 #define PKG_MEM_ERROR LM_ERR("could not allocate private memory from available pool")
00172 #endif
00173 
00174 #define SHM_MEM_ERROR LM_ERR("could not allocate shared memory from available pool")
00175 
00176 #endif