Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
gwmem.h
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * gwmem.h
59  *
60  * This is a simple malloc()-wrapper. It does not return NULLs but
61  * instead panics.
62  *
63  * We have two wrappers. One that just checks for allocation failures and
64  * panics if they happen and one that tries to find allocation problems,
65  * such as using an area after it has been freed.
66  *
67  * Kalle Marjola
68  * Lars Wirzenius
69  */
70 
71 #ifndef GWMEM_H
72 #define GWMEM_H
73 
74 
75 void *gw_native_noop(void *ptr);
76 void gw_native_init(void);
77 void gw_native_check_leaks(void);
78 void *gw_native_malloc(size_t size);
79 void *gw_native_calloc(int nmemb, size_t size);
80 void *gw_native_realloc(void *ptr, size_t size);
81 void gw_native_free(void *ptr);
82 char *gw_native_strdup(const char *str);
83 void gw_native_shutdown(void);
84 
85 
86 void gw_check_init_mem(int slow_flag);
87 void gw_check_check_leaks(void);
88 void *gw_check_malloc(size_t size,
89  const char *filename, long line, const char *function);
90 void *gw_check_calloc(int nmemb, size_t size,
91  const char *filename, long line, const char *function);
92 void *gw_check_realloc(void *p, size_t size,
93  const char *filename, long line, const char *function);
94 void gw_check_free(void *p,
95  const char *filename, long line, const char *function);
96 char *gw_check_strdup(const char *str,
97  const char *filename, long line, const char *function);
98 int gw_check_is_allocated(void *p);
99 long gw_check_area_size(void *p);
100 void *gw_check_claim_area(void *p,
101  const char *filename, long line, const char *function);
102 void gw_check_shutdown(void);
103 
104 
105 
106 /*
107  * "slow" == "checking" with a small variation.
108  */
109 #if USE_GWMEM_SLOW
110 #define USE_GWMEM_CHECK 1
111 #endif
112 
113 
114 #if USE_GWMEM_NATIVE
115 
116 /*
117  * The `native' wrapper.
118  */
119 
120 #define gw_init_mem()
121 #define gw_check_leaks()
122 #define gw_malloc(size) (gw_native_malloc(size))
123 #define gw_malloc_trace(size, file, line, func) (gw_native_malloc(size))
124 #define gw_calloc(nmemb, size) (gw_native_calloc(nmemb, size))
125 #define gw_realloc(ptr, size) (gw_native_realloc(ptr, size))
126 #define gw_free(ptr) (gw_native_free(ptr))
127 #define gw_strdup(str) (gw_native_strdup(str))
128 #define gw_assert_allocated(ptr, file, line, function)
129 #define gw_claim_area(ptr) (gw_native_noop(ptr))
130 #define gw_claim_area_for(ptr, file, line, func) (gw_native_noop(ptr))
131 #define gwmem_shutdown()
132 #define gwmem_type() (octstr_imm("native"))
133 
134 #elif USE_GWMEM_CHECK
135 
136 /*
137  * The `check' wrapper.
138  */
139 
140 #ifdef USE_GWMEM_SLOW
141 #define gw_init_mem() (gw_check_init_mem(1))
142 #define gwmem_type() (octstr_imm("slow"))
143 #else
144 #define gw_init_mem() (gw_check_init_mem(0))
145 #define gwmem_type() (octstr_imm("checking"))
146 #endif
147 
148 #define gw_check_leaks() (gw_check_check_leaks())
149 #define gw_malloc_trace(size, file, line, func) \
150  (gw_check_malloc(size, file, line, func))
151 #define gw_malloc(size) \
152  (gw_check_malloc(size, __FILE__, __LINE__, __func__))
153 #define gw_calloc(nmemb, size) \
154  (gw_check_calloc(nmemb, size, __FILE__, __LINE__, __func__))
155 #define gw_realloc(ptr, size) \
156  (gw_check_realloc(ptr, size, __FILE__, __LINE__, __func__))
157 #define gw_free(ptr) \
158  (gw_check_free(ptr, __FILE__, __LINE__, __func__))
159 #define gw_strdup(str) \
160  (gw_check_strdup(str, __FILE__, __LINE__, __func__))
161 #define gw_assert_allocated(ptr, file, line, function) \
162  (gw_assert_place(gw_check_is_allocated(ptr), file, line, function))
163 #define gw_claim_area(ptr) \
164  (gw_check_claim_area(ptr, __FILE__, __LINE__, __func__))
165 #define gw_claim_area_for(ptr, file, line, func) \
166  (gw_check_claim_area(ptr, file, line, func))
167 #define gwmem_shutdown() (gw_check_shutdown())
168 
169 #else
170 
171 /*
172  * Unknown wrapper. Oops.
173  */
174 #error "Unknown malloc wrapper."
175 
176 
177 #endif
178 
179 
180 /*
181  * Make sure no-one uses the unwrapped functions by mistake.
182  */
183 
184 /* undefine first to avoid compiler warnings about redefines¬†*/
185 #undef malloc
186 #undef calloc
187 #undef realloc
188 #undef free
189 #undef strdup
190 
191 #define malloc(n) do_not_call_malloc_directly
192 #define calloc(a, b) do_not_use_calloc
193 #define realloc(p, n) do_not_call_realloc_directly
194 #define free(p) do_not_call_free_directly
195 #define strdup(p) do_not_call_strdup_directly
196 
197 
198 #endif
void gw_check_free(void *p, const char *filename, long line, const char *function)
Definition: gwmem-check.c:619
void * gw_native_noop(void *ptr)
Definition: gwmem-native.c:78
int size
Definition: wsasm.c:84
char * gw_check_strdup(const char *str, const char *filename, long line, const char *function)
Definition: gwmem-check.c:639
void gw_native_init(void)
void gw_check_shutdown(void)
Definition: gwmem-check.c:508
void * gw_check_claim_area(void *p, const char *filename, long line, const char *function)
Definition: gwmem-check.c:654
void * gw_check_malloc(size_t size, const char *filename, long line, const char *function)
Definition: gwmem-check.c:514
void gw_native_shutdown(void)
void gw_native_check_leaks(void)
void * gw_native_calloc(int nmemb, size_t size)
Definition: gwmem-native.c:94
char * gw_native_strdup(const char *str)
Definition: gwmem-native.c:129
void gw_check_check_leaks(void)
Definition: gwmem-check.c:679
int gw_check_is_allocated(void *p)
Definition: gwmem-check.c:713
void gw_native_free(void *ptr)
Definition: gwmem-native.c:123
char filename[FILENAME_MAX+1]
Definition: log.c:135
void * gw_check_realloc(void *p, size_t size, const char *filename, long line, const char *function)
Definition: gwmem-check.c:561
void * gw_check_calloc(int nmemb, size_t size, const char *filename, long line, const char *function)
Definition: gwmem-check.c:537
long gw_check_area_size(void *p)
Definition: gwmem-check.c:723
void * gw_native_malloc(size_t size)
Definition: gwmem-native.c:80
void * gw_native_realloc(void *ptr, size_t size)
Definition: gwmem-native.c:109
void gw_check_init_mem(int slow_flag)
Definition: gwmem-check.c:501
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.