Skip to content

Commit 62edb20

Browse files
committed
Ditch --enable-debug-malloc and --enable-debug-alignment
We wrote DEBUG_MALLOC in 1997 to debug memory leaks. Nowadays DEBUG_MALLOC is just confusing. Better tools are available, and DEBUG_MALLOC is not thread-safe and it does not respect SIMD alignment. It confused at least one user. In the gcc-2.SOMETHING days, gcc would allocate doubles on the stack at 4-byte boundary (vs. 8) reducing performance by a factor of 3. That's when we introduced --enable-debug-alignment, which is totally obsolete by now.
1 parent 6ed4297 commit 62edb20

File tree

6 files changed

+1
-301
lines changed

6 files changed

+1
-301
lines changed

cmake.config.h.in

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@
3737
/* Define to enable extra FFTW debugging code. */
3838
/* #undef FFTW_DEBUG */
3939

40-
/* Define to enable alignment debugging hacks. */
41-
/* #undef FFTW_DEBUG_ALIGNMENT */
42-
43-
/* Define to enable debugging malloc. */
44-
/* #undef FFTW_DEBUG_MALLOC */
45-
4640
/* Define to enable the use of alloca(). */
4741
#define FFTW_ENABLE_ALLOCA 1
4842

configure.ac

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,11 @@ esac
4545
AC_ARG_ENABLE(debug, [AC_HELP_STRING([--enable-debug],[compile fftw with extra runtime checks for debugging])], ok=$enableval, ok=no)
4646
if test "$ok" = "yes"; then
4747
AC_DEFINE(FFTW_DEBUG,1,[Define to enable extra FFTW debugging code.])
48-
debug_malloc=yes
49-
else
50-
debug_malloc=no
5148
fi
5249

5350
AC_ARG_ENABLE(doc, [AC_HELP_STRING([--disable-doc],[disable building the documentation])], build_doc=$enableval, build_doc=yes)
5451
AM_CONDITIONAL(BUILD_DOC, test x"$build_doc" = xyes)
5552

56-
AC_ARG_ENABLE(debug-malloc, [AC_HELP_STRING([--enable-debug-malloc],[enable malloc debugging version])], ok=$enableval, ok=$debug_malloc)
57-
if test "$ok" = "yes"; then
58-
AC_DEFINE(FFTW_DEBUG_MALLOC,1,[Define to enable debugging malloc.])
59-
fi
60-
61-
AC_ARG_ENABLE(debug-alignment, [AC_HELP_STRING([--enable-debug-alignment],[enable alignment debugging hacks])], ok=$enableval, ok=no)
62-
if test "$ok" = "yes"; then
63-
AC_DEFINE(FFTW_DEBUG_ALIGNMENT,1,[Define to enable alignment debugging hacks.])
64-
fi
65-
6653
AC_ARG_ENABLE(random-estimator, [AC_HELP_STRING([--enable-random-estimator],[enable pseudorandom estimator (debugging hack)])], ok=$enableval, ok=no)
6754
if test "$ok" = "yes"; then
6855
AC_DEFINE(FFTW_RANDOM_ESTIMATOR,1,[Define to enable pseudorandom estimate planning for debugging.])

kernel/alloc.c

Lines changed: 0 additions & 242 deletions
Original file line numberDiff line numberDiff line change
@@ -20,246 +20,6 @@
2020

2121
#include "kernel/ifftw.h"
2222

23-
/**********************************************************
24-
* DEBUGGING CODE
25-
**********************************************************/
26-
#if defined(FFTW_DEBUG_MALLOC)
27-
28-
#include <stdio.h>
29-
30-
/*
31-
debugging malloc/free.
32-
33-
1) Initialize every malloced and freed area to random values, just
34-
to make sure we are not using uninitialized pointers.
35-
36-
2) check for blocks freed twice.
37-
38-
3) Check for writes past the ends of allocated blocks
39-
40-
4) destroy contents of freed blocks in order to detect incorrect reuse.
41-
42-
5) keep track of who allocates what and report memory leaks
43-
44-
This code is a quick and dirty hack. May be nonportable.
45-
Use at your own risk.
46-
47-
*/
48-
49-
#define MAGIC ((size_t)0xABadCafe)
50-
#define PAD_FACTOR 2
51-
#define SZ_HEADER (4 * sizeof(size_t))
52-
#define HASHSZ 1031
53-
54-
static unsigned int hashaddr(void *p)
55-
{
56-
return ((unsigned long)p) % HASHSZ;
57-
}
58-
59-
struct mstat {
60-
int siz;
61-
int maxsiz;
62-
int cnt;
63-
int maxcnt;
64-
};
65-
66-
static struct mstat mstat[MALLOC_WHAT_LAST];
67-
68-
struct minfo {
69-
const char *file;
70-
int line;
71-
size_t n;
72-
void *p;
73-
struct minfo *next;
74-
};
75-
76-
static struct minfo *minfo[HASHSZ] = {0};
77-
78-
#if defined(HAVE_THREADS) || defined(HAVE_OPENMP)
79-
int X(in_thread) = 0;
80-
#endif
81-
82-
void *X(malloc_debug)(size_t n, enum malloc_tag what,
83-
const char *file, int line)
84-
{
85-
char *p;
86-
size_t i;
87-
struct minfo *info;
88-
struct mstat *stat = mstat + what;
89-
struct mstat *estat = mstat + EVERYTHING;
90-
91-
if (n == 0)
92-
n = 1;
93-
94-
if (!IN_THREAD) {
95-
stat->siz += n;
96-
if (stat->siz > stat->maxsiz)
97-
stat->maxsiz = stat->siz;
98-
estat->siz += n;
99-
if (estat->siz > estat->maxsiz)
100-
estat->maxsiz = estat->siz;
101-
}
102-
103-
p = (char *) X(kernel_malloc)(PAD_FACTOR * n + SZ_HEADER);
104-
A(p);
105-
106-
/* store the sz in a known position */
107-
((size_t *) p)[0] = n;
108-
((size_t *) p)[1] = MAGIC;
109-
((size_t *) p)[2] = what;
110-
111-
/* fill with junk */
112-
for (i = 0; i < PAD_FACTOR * n; i++)
113-
p[i + SZ_HEADER] = (char) (i ^ 0xEF);
114-
115-
if (!IN_THREAD) {
116-
++stat->cnt;
117-
++estat->cnt;
118-
119-
if (stat->cnt > stat->maxcnt)
120-
stat->maxcnt = stat->cnt;
121-
if (estat->cnt > estat->maxcnt)
122-
estat->maxcnt = estat->cnt;
123-
}
124-
125-
/* skip the info we stored previously */
126-
p = p + SZ_HEADER;
127-
128-
if (!IN_THREAD) {
129-
unsigned int h = hashaddr(p);
130-
/* record allocation in allocation list */
131-
info = (struct minfo *) malloc(sizeof(struct minfo));
132-
info->n = n;
133-
info->file = file;
134-
info->line = line;
135-
info->p = p;
136-
info->next = minfo[h];
137-
minfo[h] = info;
138-
}
139-
140-
return (void *) p;
141-
}
142-
143-
void X(ifree)(void *p)
144-
{
145-
char *q;
146-
147-
A(p);
148-
149-
q = ((char *) p) - SZ_HEADER;
150-
A(q);
151-
152-
{
153-
size_t n = ((size_t *) q)[0];
154-
size_t magic = ((size_t *) q)[1];
155-
int what = ((size_t *) q)[2];
156-
size_t i;
157-
struct mstat *stat = mstat + what;
158-
struct mstat *estat = mstat + EVERYTHING;
159-
160-
/* set to zero to detect duplicate free's */
161-
((size_t *) q)[0] = 0;
162-
163-
A(magic == MAGIC);
164-
((size_t *) q)[1] = ~MAGIC;
165-
166-
if (!IN_THREAD) {
167-
stat->siz -= n;
168-
A(stat->siz >= 0);
169-
estat->siz -= n;
170-
A(estat->siz >= 0);
171-
}
172-
173-
/* check for writing past end of array: */
174-
for (i = n; i < PAD_FACTOR * n; ++i)
175-
if (q[i + SZ_HEADER] != (char) (i ^ 0xEF)) {
176-
A(0 /* array bounds overwritten */ );
177-
}
178-
for (i = 0; i < PAD_FACTOR * n; ++i)
179-
q[i + SZ_HEADER] = (char) (i ^ 0xAD);
180-
181-
if (!IN_THREAD) {
182-
--stat->cnt;
183-
--estat->cnt;
184-
185-
A(stat->cnt >= 0);
186-
A((stat->cnt == 0 && stat->siz == 0) ||
187-
(stat->cnt > 0 && stat->siz > 0));
188-
A(estat->cnt >= 0);
189-
A((estat->cnt == 0 && estat->siz == 0) ||
190-
(estat->cnt > 0 && estat->siz > 0));
191-
}
192-
193-
X(kernel_free)(q);
194-
}
195-
196-
if (!IN_THREAD) {
197-
/* delete minfo entry */
198-
unsigned int h = hashaddr(p);
199-
struct minfo **i;
200-
201-
for (i = minfo + h; *i; i = &((*i)->next)) {
202-
if ((*i)->p == p) {
203-
struct minfo *i0 = (*i)->next;
204-
free(*i);
205-
*i = i0;
206-
return;
207-
}
208-
}
209-
210-
A(0 /* no entry in minfo list */ );
211-
}
212-
}
213-
214-
void X(malloc_print_minfo)(int verbose)
215-
{
216-
struct minfo *info;
217-
int what;
218-
unsigned int h;
219-
int leak = 0;
220-
221-
if (verbose > 2) {
222-
static const char *names[MALLOC_WHAT_LAST] = {
223-
"EVERYTHING",
224-
"PLANS", "SOLVERS", "PROBLEMS", "BUFFERS",
225-
"HASHT", "TENSORS", "PLANNERS", "SLVDSC", "TWIDDLES",
226-
"STRIDES", "OTHER"
227-
};
228-
229-
printf("%12s %8s %8s %10s %10s\n",
230-
"what", "cnt", "maxcnt", "siz", "maxsiz");
231-
232-
for (what = 0; what < MALLOC_WHAT_LAST; ++what) {
233-
struct mstat *stat = mstat + what;
234-
printf("%12s %8d %8d %10d %10d\n",
235-
names[what], stat->cnt, stat->maxcnt,
236-
stat->siz, stat->maxsiz);
237-
}
238-
}
239-
240-
for (h = 0; h < HASHSZ; ++h)
241-
if (minfo[h]) {
242-
printf("\nUnfreed allocations:\n");
243-
break;
244-
}
245-
246-
for (h = 0; h < HASHSZ; ++h)
247-
for (info = minfo[h]; info; info = info->next) {
248-
leak = 1;
249-
printf("%s:%d: %zd bytes at %p\n",
250-
info->file, info->line, info->n, info->p);
251-
}
252-
253-
if (leak)
254-
abort();
255-
}
256-
257-
#else
258-
/**********************************************************
259-
* NON DEBUGGING CODE
260-
**********************************************************/
261-
/* production version, no hacks */
262-
26323
void *X(malloc_plain)(size_t n)
26424
{
26525
void *p;
@@ -280,8 +40,6 @@ void X(ifree)(void *p)
28040
X(kernel_free)(p);
28141
}
28242

283-
#endif
284-
28543
void X(ifree0)(void *p)
28644
{
28745
/* common pattern */

kernel/ifftw.h

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -288,31 +288,9 @@ enum malloc_tag {
288288
IFFTW_EXTERN void X(ifree)(void *ptr);
289289
extern void X(ifree0)(void *ptr);
290290

291-
#ifdef FFTW_DEBUG_MALLOC
292-
293-
IFFTW_EXTERN void *X(malloc_debug)(size_t n, enum malloc_tag what,
294-
const char *file, int line);
295-
#define MALLOC(n, what) X(malloc_debug)(n, what, __FILE__, __LINE__)
296-
IFFTW_EXTERN void X(malloc_print_minfo)(int vrbose);
297-
298-
#else /* ! FFTW_DEBUG_MALLOC */
299-
300291
IFFTW_EXTERN void *X(malloc_plain)(size_t sz);
301292
#define MALLOC(n, what) X(malloc_plain)(n)
302293

303-
#endif
304-
305-
#if defined(FFTW_DEBUG) && defined(FFTW_DEBUG_MALLOC) && (defined(HAVE_THREADS) || defined(HAVE_OPENMP))
306-
extern int X(in_thread);
307-
# define IN_THREAD X(in_thread)
308-
# define THREAD_ON { int in_thread_save = X(in_thread); X(in_thread) = 1
309-
# define THREAD_OFF X(in_thread) = in_thread_save; }
310-
#else
311-
# define IN_THREAD 0
312-
# define THREAD_ON
313-
# define THREAD_OFF
314-
#endif
315-
316294
/*-----------------------------------------------------------------------*/
317295
/* low-resolution clock */
318296

@@ -1063,16 +1041,7 @@ R *X(join_taint)(R *p1, R *p2);
10631041
#define JOIN_TAINT(p1, p2) p1
10641042
#endif
10651043

1066-
#ifdef FFTW_DEBUG_ALIGNMENT
1067-
# define ASSERT_ALIGNED_DOUBLE { \
1068-
double __foo; \
1069-
CK(!(((uintptr_t) &__foo) & 0x7)); \
1070-
}
1071-
#else
1072-
# define ASSERT_ALIGNED_DOUBLE
1073-
#endif /* FFTW_DEBUG_ALIGNMENT */
1074-
1075-
1044+
#define ASSERT_ALIGNED_DOUBLE /*unused, legacy*/
10761045

10771046
/*-----------------------------------------------------------------------*/
10781047
/* macros used in codelets to reduce source code size */

threads/openmp.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ void X(spawn_loop)(int loopmax, int nthr, spawn_function proc, void *data)
5858
block_size = (loopmax + nthr - 1) / nthr;
5959
nthr = (loopmax + block_size - 1) / block_size;
6060

61-
THREAD_ON; /* prevent debugging mode from failing under threads */
6261
#pragma omp parallel for private(d)
6362
for (i = 0; i < nthr; ++i) {
6463
d.max = (d.min = i * block_size) + block_size;
@@ -68,7 +67,6 @@ void X(spawn_loop)(int loopmax, int nthr, spawn_function proc, void *data)
6867
d.data = data;
6968
proc(&d);
7069
}
71-
THREAD_OFF; /* prevent debugging mode from failing under threads */
7270
}
7371

7472
void X(threads_cleanup)(void)

threads/threads.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,6 @@ static void kill_workforce(void)
354354

355355
w.proc = 0;
356356

357-
THREAD_ON; /* needed for debugging mode: since make_worker
358-
is called from dequeue which is only called in
359-
thread_on mode, we need to unmake_worker in thread_on. */
360357
WITH_QUEUE_LOCK({
361358
/* tell all workers that they must terminate.
362359
@@ -374,7 +371,6 @@ static void kill_workforce(void)
374371
unmake_worker(q);
375372
}
376373
});
377-
THREAD_OFF;
378374
}
379375

380376
static os_static_mutex_t initialization_mutex = OS_STATIC_MUTEX_INITIALIZER;
@@ -420,7 +416,6 @@ void X(spawn_loop)(int loopmax, int nthr, spawn_function proc, void *data)
420416
block_size = (loopmax + nthr - 1) / nthr;
421417
nthr = (loopmax + block_size - 1) / block_size;
422418

423-
THREAD_ON; /* prevent debugging mode from failing under threads */
424419
STACK_MALLOC(struct work *, r, sizeof(struct work) * nthr);
425420

426421
/* distribute work: */
@@ -455,7 +450,6 @@ void X(spawn_loop)(int loopmax, int nthr, spawn_function proc, void *data)
455450
}
456451

457452
STACK_FREE(r);
458-
THREAD_OFF; /* prevent debugging mode from failing under threads */
459453
}
460454

461455
void X(threads_cleanup)(void)

0 commit comments

Comments
 (0)