summaryrefslogtreecommitdiffstats
path: root/clib/memory_leak_detection.h
blob: 5c1afb13758784e364f782a3954fd463710026cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright (c) International Business Machines Corp., 2011
 *
 * This program 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.
 *
 * This program 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
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Module: memory_leak_detection.h
 *
 *
 * Functions:
 *
 * Description: This module defines macros which should be used in place
 *  						of malloc and free.  These macros default to malloc and
 *  						free, but, when FIND_MEMORY_LEAK is defined, they
 *  						default to functions which wrapper malloc and free to
 *  						track memory allocations and find memory leaks.  Several
 *  						other useful functions are defined as well.
 *
 * Author: B. Rafanello
 *
 * Version 0.0.0.1
 */

#ifndef MEMORY_LEAK_DETECTION_H

#define MEMORY_LEAK_DETECTION_H 1

#if ( FIND_MEMORY_LEAK > 0 )

#warning Memory Leak Detection enabled!

/* ================================================================= */
/* ================================================================= */
/*                                                                   */
/* The following functions are not meant to be called directly!  Use */
/* macros defined after the functions instead of the functions       */
/* themselves.                                                       */
/* ================================================================= */
/* ================================================================= */

/*********************************************************************/
/*                                                                   */
/*   Function Name: MEMORY_func                                      */
/*                                                                   */
/*   Descriptive Name: This function acts as a replacement for       */
/*                     malloc.  It allocates memory (using malloc)   */
/*                     and enters that memory into a tracking        */
/*                     structure so that memory leaks, if any, may   */
/*                     be found.                                     */
/*                                                                   */
/*   Input: size_t sz - The number of bytes to be allocated.         */
/*          unsigned int Alignment - 0 for non-aligned (normal)      */
/*                                   malloc, > 0 to return an        */
/*                                   address aligned on a specific   */
/*                                   memory boundary.  If > 0, then  */
/*                                   Alignment must be a power of 2  */
/*                                   and a multiple of sizeof(void *)*/
/*          void ** Memory_Location - The address of a variable which*/
/*                                   will hold the address of the    */
/*                                   allocated by this function.     */
/*          const char * mod_name - The name of the module from which*/
/*                                  this function was called.        */
/*          const char * func - The name of the function from which  */
/*                              this function was called.            */
/*          const int line - The line number of the code containing  */
/*                           the call to this function.              */
/*                                                                   */
/*   Output: If Success : The function return value will be 0.       */
/*                        *Memory_Location will be set to the address*/
/*                        of the first byte of the user portion of   */
/*                        any memory that was allocated.             */
/*                                                                   */
/*           If Failure : The function return value will be EINVAL or*/
/*                        ENOMEM.  Errors may be reported on stderr. */
/*                        *Memory_Location will be set to NULL.      */
/*                                                                   */
/*   Error Handling: This function will abort if an error is         */
/*                   is detected.  Errors could be lack of memory, or*/
/*                   corruption of the internal structures used to   */
/*                   track allocated blocks of memory.               */
/*                                                                   */
/*   Side Effects: Memory may be allocated, errors may be reported   */
/*                 on stderr.                                        */
/*                                                                   */
/*   Notes:                                                          */
/*                                                                   */
/*********************************************************************/
int MEMORY_func(size_t sz,
		unsigned int Alignment,
		void **Memory_Location,
		const char *mod_name, const char *func, const int line);

/*********************************************************************/
/*                                                                   */
/*   Function Name: MALLOC_func                                      */
/*                                                                   */
/*   Descriptive Name: This function acts as a replacement for       */
/*                     malloc.  It allocates memory (using malloc)   */
/*                     and enters that memory into a tracking        */
/*                     structure so that memory leaks, if any, may   */
/*                     be found.                                     */
/*                                                                   */
/*   Input: size_t sz - The number of bytes to be allocated.         */
/*          const char * mod_name - The name of the module from which*/
/*                                  this function was called.        */
/*          const char * func - The name of the function from which  */
/*                              this function was called.            */
/*          const int line - The line number of the code containing  */
/*                           the call to this function.              */
/*                                                                   */
/*   Output: If Success : The function return value will be non-NULL.*/
/*                                                                   */
/*           If Failure : The function return value will be NULL.    */
/*                        Errors may be reported on stderr.          */
/*                                                                   */
/*   Error Handling: This function will abort if an error is         */
/*                   is detected.  Errors could be lack of memory, or*/
/*                   corruption of the internal structures used to   */
/*                   track allocated blocks of memory.               */
/*                                                                   */
/*   Side Effects: Memory may be allocated, errors may be reported   */
/*                 on stderr.                                        */
/*                                                                   */
/*   Notes:                                                          */
/*                                                                   */
/*********************************************************************/
void *MALLOC_func(size_t sz, const char *mod_name, const char *func,
		  const int line);

/*********************************************************************/
/*                                                                   */
/*   Function Name: FREE_func                                        */
/*                                                                   */
/*   Descriptive Name: This function frees a block of memory being   */
/*                     tracked by this module and removes the block  */
/*                     from its tracking structures.                 */
/*                                                                   */
/*   Input: const void * p - The address of the block of memory to   */
/*                           be freed.                               */
/*          const char * mod_name - The name of the module requesting*/
/*                                  the block of memory be freed.    */
/*          const char * func - The name of the function requesting  */
/*                              the block of memory be freed.        */
/*          const int line - The line number of the line of code in  */
/*                           module calling this function.           */
/*                                                                   */
/*   Output: If Success : None.                                      */
/*                                                                   */
/*           If Failure : Errors may be reported to stderr.          */
/*                                                                   */
/*   Error Handling: This function causes the internal structures    */
/*                   of this module to be checked as part of the     */
/*                   process of freeing the address p.  This may     */
/*                   cause errors to be reported on stderr. If any   */
/*                   errors are found, then the address p may not be */
/*                   freed.                                          */
/*                                                                   */
/*   Side Effects: The block of memory associated with the address p */
/*                 will be freed and available for reallocation.     */
/*                 Also, the memory tracking structures in this      */
/*                 module will undergo a series of checks.           */
/*                                                                   */
/*   Notes: This function was not intended to be called directly but */
/*          rather through the macro FREE.                           */
/*                                                                   */
/*********************************************************************/
void FREE_func(const void *p, const char *mod_name, const char *func,
	       const int line);

/*********************************************************************/
/*                                                                   */
/*   Function Name: Print_Leak_List                                  */
/*                                                                   */
/*   Descriptive Name: This function walks the list of allocated     */
/*                     memory blocks and prints information about    */
/*                     each one.  If this is done at program exit,   */
/*                     the resulting list of memory blocks most      */
/*                     likely represents leaked memory.              */
/*                                                                   */
/*   Input: None.                                                    */
/*                                                                   */
/*   Output: If Success : If there are any memory blocks being       */
/*                        tracked by this module, information about  */
/*                        block still being tracked will be sent to  */
/*                        stderr.                                    */
/*                                                                   */
/*           If Failure : Error messages may be sent to stderr.      */
/*                                                                   */
/*   Error Handling: If errors are detected, then error messages are */
/*                   output on stderr.                               */
/*                                                                   */
/*   Side Effects: The internal structures of this module are checked*/
/*                 for errors with any errors being reported on      */
/*                 stderr.                                           */
/*                                                                   */
/*   Notes:                                                          */
/*                                                                   */
/*********************************************************************/
void Print_Leak_List(void);

/*********************************************************************/
/*                                                                   */
/*   Function Name: Test_Address_Allocation                          */
/*                                                                   */
/*   Descriptive Name: This function tests the specified address to  */
/*                     to see if it lies within an allocated block   */
/*                     tracked by this module.                       */
/*                                                                   */
/*   Input: void * p - The address to be tested.                     */
/*                                                                   */
/*   Output: If Success : If the address p was found, then 0 will be */
/*                        returned if the address is the start of    */
/*                        a block of allocated memory.  If the       */
/*                        address p was found within an allocated    */
/*                        block of memory, then 1 is returned.       */
/*                                                                   */
/*           If Failure : If the address p was NOT found, then 2 is  */
/*                        returned.  If there was an error in the    */
/*                        memory tracking system then 3 will be      */
/*                        returned.                                  */
/*                                                                   */
/*   Error Handling: This function relies on the error handling      */
/*                   built into the Check_Leak_List function and     */
/*                   has no error handling of its own.               */
/*                                                                   */
/*   Side Effects: If the list of memory allocations contains errors */
/*                 then those errors will be detected and reported   */
/*                 on stderr.                                        */
/*                                                                   */
/*   Notes: If NULL is passed in as the address to test, then the    */
/*          integrity of the internal tracking structures will be    */
/*          checked, in which case a return value of 0 signifies     */
/*          that the internal tracking structures have passed the    */
/*          checks and a return value of 3 indicates that errors     */
/*          were found.                                              */
/*                                                                   */
/*********************************************************************/
unsigned int Test_Address_Allocation(void *p);

/*********************************************************************/
/*                                                                   */
/*   Function Name: Duplicate_String                                 */
/*                                                                   */
/*   Descriptive Name: This function duplicates a string.  The memory*/
/*                     allocated for the duplicate is allocated      */
/*                     using the MALLOC_func routine in this module  */
/*                     and is thus tracked by this module.           */
/*                                                                   */
/*   Input: const char * Source - The string to be copied.           */
/*          const char * mod_name - The name of the module containing*/
/*                                  the function which called this   */
/*                                   function.                       */
/*          const char * func - The name of the function calling     */
/*                              this function.                       */
/*          const int line - The line number of the line of code in  */
/*                           module calling this function.           */
/*                                                                   */
/*   Output: If Success : The function return value will be non-NULL */
/*                        and will point to a duplicate of the       */
/*                        string given in Source.                    */
/*                                                                   */
/*           If Failure : The function return value will be NULL.    */
/*                                                                   */
/*   Error Handling: Any errors detected by this function result in  */
/*                   a function return value of NULL.                */
/*                                                                   */
/*   Side Effects: The memory tracking features of this module are   */
/*                 employed to allocate memory for the duplicate     */
/*                 string produced by this funciton.                 */
/*                                                                   */
/*   Notes:                                                          */
/*                                                                   */
/*********************************************************************/
char *Duplicate_String(const char *Source,
		       const char *mod_name, const char *func, const int line);

/*********************************************************************/
/*                                                                   */
/*   Function Name: Realloc_func                                     */
/*                                                                   */
/*   Descriptive Name: This function performs the same function as   */
/*                     the realloc function in the ANSI C library.   */
/*                                                                   */
/*   Input: const void * p - The address of the block of memory to   */
/*                           be reallocated.                         */
/*          size_t size - The size of the memory block to return.    */
/*          const char * mod_name - The name of the module requesting*/
/*                                  the block of memory be freed.    */
/*          const char * func - The name of the function requesting  */
/*                              the block of memory be freed.        */
/*          const int line - The line number of the line of code in  */
/*                           module calling this function.           */
/*                                                                   */
/*   Output: If Success : The function return value will be a pointer*/
/*                        to the new block of memory.                */
/*                                                                   */
/*           If Failure : NULL will be returned and errno will be set*/
/*                        to a non-null error code.                  */
/*                                                                   */
/*   Error Handling: This function causes the internal structures    */
/*                   of this module to be checked.  This may         */
/*                   cause errors to be reported on stderr. If any   */
/*                   errors are found, then the address p may not be */
/*                   freed.                                          */
/*                                                                   */
/*   Side Effects: A new block of memory of size bytes will be       */
/*                 allocated, the contents of the current block will */
/*                 be copied to the new block (at least as much as   */
/*                 will fit, and the current block will be freed.    */
/*                 This will cause internal structures in this module*/
/*                 to be modified accordingly.                       */
/*                                                                   */
/*   Notes: This function was not intended to be called directly but */
/*          rather through the macro REALLOC.                        */
/*                                                                   */
/*          If p is NULL, then this will cause this function to      */
/*          behave like malloc.                                      */
/*                                                                   */
/*          If size is 0, then this will cause this function to      */
/*          behave like free.                                        */
/*                                                                   */
/*********************************************************************/
void *Realloc_func(void *p,
		   size_t size,
		   const char *mod_name, const char *func, const int line);

/* ================================================================= */
/* ================================================================= */
/*                                                                   */
/* Macros to use for accessing the functions in this module.         */
/*                                                                   */
/* ================================================================= */
/* ================================================================= */

#ifdef MALLOC
#undef MALLOC
#endif
#ifdef FREE
#undef FREE
#endif

/* This macro should be used in place of posix_memalign. */
#define POSIX_MEMALIGN(a, b, c )  MEMORY_func( c, b, a, __FILE__, __func__, __LINE__ )

/* This macro should be used in place of the malloc function.             */
#define MALLOC( a )              MALLOC_func( a, __FILE__, __func__, __LINE__ )

/* This macro should be used in place of the free function.               */
#define FREE( a )                FREE_func( a,  __FILE__, __func__, __LINE__ )

/* This macro should be used to print a list of allocated memory blocks.  */
#define PRINT_ALLOCATION_LIST		 Print_Leak_List()

/* This macro should be used to see if an address lies within a block of
   memory being tracked by this module.                                   */
#define ADDRESS_CHECK( p )       Test_Address_Allocation( p )

/* This macro should be used in place of the strdup function. */
#define STRDUP( p )              Duplicate_String( p, __FILE__, __func__, __LINE__ )

/* This macro should be used in place of the realloc function. */
#define REALLOC( p, s )          Realloc_func( p, s, __FILE__, __func__, __LINE__ )

#else

#define POSIX_MEMALIGN(a, b, c ) posix_memalign( a, b, c )
#define MALLOC(s)                malloc(s)
#define FREE(p)                  free(p)
#define PRINT_ALLOCATION_LIST
#define ADDRESS_CHECK( p )       0
#define STRDUP( p )              strdup( p )
#define REALLOC( p, s )          realloc( p, s )

#endif

#endif
OpenPOWER on IntegriCloud