File: debug_malloc.c

package info (click to toggle)
mpb 1.11.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,856 kB
  • sloc: ansic: 13,270; javascript: 9,901; makefile: 212; lisp: 44; sh: 4
file content (169 lines) | stat: -rw-r--r-- 4,528 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) 1999-2020 Massachusetts Institute of Technology.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* Debugging versions of malloc & free, to help catch common errors.
   The routines in this file were adapted from similar routines in the
   FFTW library (www.fftw.org), written by Matteo Frigo and Steven
   G. Johnson.  FFTW is also GPLed and is also copyrighted by MIT.  */

#include <stdio.h>
#include <stdlib.h>

#include "config.h"

#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif

#include "mpi_utils.h"
#include "check.h"

void* (*my_malloc_hook) (size_t) = 0;

/**********************************************************
 *   DEBUGGING CODE
 **********************************************************/

#ifdef DEBUG

void check_breakpoint(void)
{
     /* this function is only here so that we can drop into a breakpoint
	in a debugger when a CHECK macro fails... */
}

#endif

#ifdef DEBUG_MALLOC

#  undef malloc
#  undef free

/*
 * debugging malloc/free.  Initialize every malloced and freed area to
 * random values, just to make sure we are not using uninitialized
 * pointers.  Also check for writes past the ends of allocated blocks,
 * and a couple of other things.
 *
 */

static int debug_malloc_cnt = 0;
static int debug_malloc_total = 0;

#define MAGIC 0xABadCafe
#define MMAGIC (((int) MAGIC) < 0 ? ((int) MAGIC) : -((int) MAGIC))
#define PAD_FACTOR 2
#define TWOINTS (2 * sizeof(int))

#define VERBOSE_ALLOCATION 0

#if VERBOSE_ALLOCATION
#define WHEN_VERBOSE(a) a
#else
#define WHEN_VERBOSE(a)
#endif

void *debug_malloc(size_t n)
{
     char *p;
     int i;

     WHEN_VERBOSE(mpi_one_fprintf(stderr,"DEBUG_MALLOC %d\n", n));

     if (n == 0)
	  mpi_one_fprintf(stderr, "(Allocating a block of zero size.)\n");

     debug_malloc_total += n;

     p = (char *) malloc(PAD_FACTOR * n + TWOINTS);
     CHECK(p, "debug_malloc: out of memory\n");

     /* store the size in a known position */
     ((int *) p)[0] = n;
     ((int *) p)[1] = MAGIC;
     for (i = 0; i < PAD_FACTOR * n; ++i)
	  p[i + TWOINTS] = (char) (i ^ 0xDEADBEEF);

     ++debug_malloc_cnt;

     /* skip the size we stored previously */
     return (void *) (p + TWOINTS);
}

void debug_free(void *p)
{
     char *q = ((char *) p) - TWOINTS;

     CHECK(p, "debug_free: tried to free NULL pointer!\n");
     CHECK(q, "debug_free: tried to free NULL+TWOINTS pointer!\n");

     {
	  int n = ((int *) q)[0];
	  int magic = ((int *) q)[1];
	  int i;

	  WHEN_VERBOSE(mpi_one_fprintf(stderr,"DEBUG_FREE %d\n", n));
	  CHECK(n != MMAGIC, "Tried to free a freed pointer!\n");
	  *((int *) q) = MMAGIC; /* to detect duplicate free's */

	  CHECK(magic == MAGIC, "Wrong magic in debug_free()!\n");
	  ((int *) q)[1] = ~MAGIC;

	  CHECK(n >= 0, "Tried to free block with corrupt size descriptor!\n");

	  debug_malloc_total -= n;

	  CHECK(debug_malloc_total >= 0, 
		"debug_malloc_total went negative!\n");

	  /* check for writing past end of array: */
	  for (i = n; i < PAD_FACTOR * n; ++i)
	       if (q[i + TWOINTS] != (char) (i ^ 0xDEADBEEF))
		    mpi_die("Byte %d past end of array has changed!\n"
			    "Array bounds overwritten!\n",
			    i - n + 1);
	  for (i = 0; i < PAD_FACTOR * n; ++i)
	       q[i + TWOINTS] = (char) (i ^ 0xBEEFDEAD);

	  --debug_malloc_cnt;
	  free(q);
     }
}

#endif /* DEBUG */

/* output current memory usage: */
void debug_output_malloc_count(void)
{
#ifdef DEBUG_MALLOC
     mpi_one_fprintf(stderr, "malloc: %d blocks, %g kB\n", 
		     debug_malloc_cnt, debug_malloc_total / 1024.0);
#endif
}

/* check for memory leaks when debugging */
void debug_check_memory_leaks(void)
{
#ifdef DEBUG_MALLOC
     if (debug_malloc_cnt || debug_malloc_total)
	  mpi_die("MEMORY LEAK!!!\n"
		  "number of unbalanced malloc calls = %d\n"
		  "total leaked bytes = %d\n",
		  debug_malloc_cnt,
		  debug_malloc_total);
#endif
}