Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

gwmem.h

Go to the documentation of this file.
00001 /* ==================================================================== 
00002  * The Kannel Software License, Version 1.0 
00003  * 
00004  * Copyright (c) 2001-2008 Kannel Group  
00005  * Copyright (c) 1998-2001 WapIT Ltd.   
00006  * All rights reserved. 
00007  * 
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions 
00010  * are met: 
00011  * 
00012  * 1. Redistributions of source code must retain the above copyright 
00013  *    notice, this list of conditions and the following disclaimer. 
00014  * 
00015  * 2. Redistributions in binary form must reproduce the above copyright 
00016  *    notice, this list of conditions and the following disclaimer in 
00017  *    the documentation and/or other materials provided with the 
00018  *    distribution. 
00019  * 
00020  * 3. The end-user documentation included with the redistribution, 
00021  *    if any, must include the following acknowledgment: 
00022  *       "This product includes software developed by the 
00023  *        Kannel Group (http://www.kannel.org/)." 
00024  *    Alternately, this acknowledgment may appear in the software itself, 
00025  *    if and wherever such third-party acknowledgments normally appear. 
00026  * 
00027  * 4. The names "Kannel" and "Kannel Group" must not be used to 
00028  *    endorse or promote products derived from this software without 
00029  *    prior written permission. For written permission, please  
00030  *    contact org@kannel.org. 
00031  * 
00032  * 5. Products derived from this software may not be called "Kannel", 
00033  *    nor may "Kannel" appear in their name, without prior written 
00034  *    permission of the Kannel Group. 
00035  * 
00036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
00037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00039  * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 
00040  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
00041  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  
00042  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  
00043  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
00044  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  
00045  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  
00046  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00047  * ==================================================================== 
00048  * 
00049  * This software consists of voluntary contributions made by many 
00050  * individuals on behalf of the Kannel Group.  For more information on  
00051  * the Kannel Group, please see <http://www.kannel.org/>. 
00052  * 
00053  * Portions of this software are based upon software originally written at  
00054  * WapIT Ltd., Helsinki, Finland for the Kannel project.  
00055  */ 
00056 
00057 /*
00058  * gwmem.h
00059  *
00060  * This is a simple malloc()-wrapper. It does not return NULLs but
00061  * instead panics.
00062  *
00063  * We have two wrappers. One that just checks for allocation failures and
00064  * panics if they happen and one that tries to find allocation problems,
00065  * such as using an area after it has been freed.
00066  *
00067  * Kalle Marjola
00068  * Lars Wirzenius
00069  */
00070 
00071 #ifndef GWMEM_H
00072 #define GWMEM_H
00073 
00074 
00075 void *gw_native_noop(void *ptr);
00076 void gw_native_init(void);
00077 void gw_native_check_leaks(void);
00078 void *gw_native_malloc(size_t size);
00079 void *gw_native_realloc(void *ptr, size_t size);
00080 void gw_native_free(void *ptr);
00081 char *gw_native_strdup(const char *str);
00082 void gw_native_shutdown(void);
00083 
00084 
00085 void gw_check_init_mem(int slow_flag);
00086 void gw_check_check_leaks(void);
00087 void *gw_check_malloc(size_t size, 
00088     const char *filename, long line, const char *function);
00089 void *gw_check_realloc(void *p, size_t size, 
00090     const char *filename, long line, const char *function);
00091 void  gw_check_free(void *p, 
00092     const char *filename, long line, const char *function);
00093 char *gw_check_strdup(const char *str, 
00094     const char *filename, long line, const char *function);
00095 int gw_check_is_allocated(void *p);
00096 long gw_check_area_size(void *p);
00097 void *gw_check_claim_area(void *p,
00098     const char *filename, long line, const char *function);
00099 void gw_check_shutdown(void);
00100 
00101 
00102 
00103 /*
00104  * "slow" == "checking" with a small variation.
00105  */
00106 #if USE_GWMEM_SLOW
00107 #define USE_GWMEM_CHECK 1
00108 #endif
00109 
00110 
00111 #if USE_GWMEM_NATIVE
00112 
00113 /*
00114  * The `native' wrapper.
00115  */
00116 
00117 #define gw_init_mem()
00118 #define gw_check_leaks()
00119 #define gw_malloc(size) (gw_native_malloc(size))
00120 #define gw_malloc_trace(size, file, line, func) (gw_native_malloc(size))
00121 #define gw_realloc(ptr, size) (gw_native_realloc(ptr, size))
00122 #define gw_free(ptr) (gw_native_free(ptr))
00123 #define gw_strdup(str) (gw_native_strdup(str))
00124 #define gw_assert_allocated(ptr, file, line, function)
00125 #define gw_claim_area(ptr) (gw_native_noop(ptr))
00126 #define gw_claim_area_for(ptr, file, line, func) (gw_native_noop(ptr))
00127 #define gwmem_shutdown()
00128 #define gwmem_type() (octstr_imm("native"))
00129 
00130 #elif USE_GWMEM_CHECK
00131 
00132 /*
00133  * The `check' wrapper.
00134  */
00135 
00136 #ifdef USE_GWMEM_SLOW
00137 #define gw_init_mem() (gw_check_init_mem(1))
00138 #define gwmem_type() (octstr_imm("slow"))
00139 #else
00140 #define gw_init_mem() (gw_check_init_mem(0))
00141 #define gwmem_type() (octstr_imm("checking"))
00142 #endif
00143 
00144 #define gw_check_leaks() (gw_check_check_leaks())
00145 #define gw_malloc_trace(size, file, line, func) \
00146     (gw_check_malloc(size, file, line, func))
00147 #define gw_malloc(size) \
00148     (gw_check_malloc(size, __FILE__, __LINE__, __func__))
00149 #define gw_realloc(ptr, size) \
00150     (gw_check_realloc(ptr, size, __FILE__, __LINE__, __func__))
00151 #define gw_free(ptr) \
00152     (gw_check_free(ptr, __FILE__, __LINE__, __func__))
00153 #define gw_strdup(str) \
00154     (gw_check_strdup(str, __FILE__, __LINE__, __func__))
00155 #define gw_assert_allocated(ptr, file, line, function) \
00156     (gw_assert_place(gw_check_is_allocated(ptr), file, line, function))
00157 #define gw_claim_area(ptr) \
00158     (gw_check_claim_area(ptr, __FILE__, __LINE__, __func__))
00159 #define gw_claim_area_for(ptr, file, line, func) \
00160     (gw_check_claim_area(ptr, file, line, func))
00161 #define gwmem_shutdown() (gw_check_shutdown())
00162 
00163 #else
00164 
00165 /*
00166  * Unknown wrapper. Oops.
00167  */
00168 #error "Unknown malloc wrapper."
00169 
00170 
00171 #endif
00172 
00173 
00174 /*
00175  * Make sure no-one uses the unwrapped functions by mistake.
00176  */
00177 
00178 #define malloc(n)   do_not_call_malloc_directly
00179 #define calloc(a, b)    do_not_use_calloc
00180 #define realloc(p, n)   do_not_call_realloc_directly
00181 #define free(p)         do_not_call_free_directly
00182 
00183 
00184 #endif
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.