55 #include <lal/LALStdio.h>
56 #include <lal/LALStdlib.h>
61 char caughtMessage[1024];
66 static int TestRaise(
int sig,
const char *fmt, ... )
70 vsnprintf( caughtMessage,
sizeof( caughtMessage ), fmt, ap );
77 #define XSTR( a ) STR( a )
78 #define LINE ":" XSTR( __LINE__ ) ")\n"
79 #define trial( func, sig, msg ) \
82 if ( ! ( val = setjmp( jump ) ) ) \
87 fprintf( mystderr, "Error: no signal raised! (" #func LINE ); \
95 fprintf( mystderr, "Error: wrong signal raised! (" #func LINE ); \
96 fprintf( mystderr, "Received: %d %s", val, caughtMessage ); \
97 fprintf( mystderr, "Expected: %d %s\n", sig, msg ); \
100 if ( NULL == strstr( caughtMessage, msg ) ) \
102 fprintf( mystderr, "Error: wrong message! (" #func LINE ); \
103 fprintf( mystderr, "Received: %d %s", val, caughtMessage ); \
104 fprintf( mystderr, "Expected: %d %s\n", sig, msg ); \
112 fputs("Error: " #msg "\n", mystderr); \
126 static int testOK(
void )
131 trial(
p =
LALMalloc( 1024 *
sizeof( *
p ) ), 0,
"" );
132 for ( i = 0; i < 1024; ++i )
p[i] = i;
133 trial(
q =
LALCalloc( 1024,
sizeof( *
q ) ), 0,
"" );
134 for ( i = 0; i < 1024; ++i )
if (
q[i] ) die( memory not blanked );
136 for ( i = 0; i < 1024; ++i )
if (
p[i] != i ) die( memory not copied );
138 if (
q ) die( memory not freed );
140 if ( *(
p - 1 ) != (
size_t)0xABadCafe ) die( wrong
magic );
141 if ( *(
p - 2 ) != 4096 *
sizeof( *
p ) ) die( wrong size );
146 trial(
p =
LALMalloc( 1024 *
sizeof( *
p ) ), 0,
"" );
147 for ( i = 0; i < 1024; ++i )
p[i] = i;
148 trial(
q =
LALCalloc( 1024,
sizeof( *
q ) ), 0,
"" );
149 for ( i = 0; i < 1024; ++i )
if (
q[i] ) die( memory not blanked );
151 for ( i = 0; i < 1024; ++i )
if (
p[i] != i ) die( memory not copied );
153 if (
q ) die( memory not freed );
160 trial(
p =
LALMalloc( 1024 *
sizeof( *
p ) ), 0,
"" );
161 for ( i = 0; i < 1024; ++i )
p[i] = i;
162 trial(
q =
LALCalloc( 1024,
sizeof( *
q ) ), 0,
"" );
163 for ( i = 0; i < 1024; ++i )
if (
q[i] ) die( memory not blanked );
165 for ( i = 0; i < 1024; ++i )
if (
p[i] != i ) die( memory not copied );
167 if (
q ) die( memory not freed );
169 if ( *(
p - 1 ) != (
size_t)0xABadCafe ) die( wrong
magic );
170 if ( *(
p - 2 ) != 4096 *
sizeof( *
p ) ) die( wrong size );
175 trial(
p =
LALMalloc( 1024 *
sizeof( *
p ) ), 0,
"" );
176 for ( i = 0; i < 1024; ++i )
p[i] = i;
177 trial(
q =
LALCalloc( 1024,
sizeof( *
q ) ), 0,
"" );
178 for ( i = 0; i < 1024; ++i )
if (
q[i] ) die( memory not blanked );
180 for ( i = 0; i < 1024; ++i )
if (
p[i] != i ) die( memory not copied );
193 static int testPadding(
void )
215 trial(
LALFree(
p ), SIGSEGV,
"error: wrong magic" );
224 trial(
LALFree(
p ), SIGSEGV,
"error: corrupt size descriptor" );
233 trial(
LALFree(
p ), SIGSEGV,
"error: array bounds overwritten" );
239 q = malloc( 4 *
sizeof( *
p ) );
241 memcpy(
q,
p - 2, 4 *
sizeof( *
p ) );
243 trial(
LALFree(
q + 2 ), SIGSEGV,
"error: lalMallocTotal too small" );
252 static int testAllocList(
void )
256 s = malloc(
sizeof( *s ) );
263 trial(
LALFree( s ), SIGSEGV,
"not found" );
269 trial(
LALFree( s ), SIGSEGV,
"not found" );
279 trial(
p =
LALRealloc( NULL, 2 *
sizeof( *
p ) ), 0,
"" );
290 static int stressTestRealloc(
void )
292 const size_t nmax = 256;
301 for ( n = 1; n <= nmax; ++n )
304 trial( v =
LALRealloc( v, n *
sizeof( *v ) ), 0,
"" );
305 trial( u = v[n - 1] =
LALRealloc( NULL, n *
sizeof( **v ) ), 0,
"" );
306 for ( i = 0; i < n; ++i ) u[i] = n - 1;
307 for ( i = 0; i < n; ++i )
309 trial( u = v[i] =
LALRealloc( v[i], n *
sizeof( *u ) ), 0,
"" );
310 for ( j = 0; j < n - 1; ++j )
311 if ( u[j] != n - 1 ) die( wrong contents );
312 for ( j = 0; j < n; ++j )
317 for ( n = 0; n < nmax; ++n )
335 setvbuf( mystderr = stdout, NULL, _IONBF, 0 );
336 FILE *
fp = freopen(
"/dev/null",
"w", stderr );
337 if (
fp == NULL) die ( unable to open /dev/
null );
341 if ( testOK() )
return 1;
342 if ( testPadding() )
return 1;
343 if ( testAllocList() )
return 1;
344 if ( stressTestRealloc() )
return 1;
int(* lalRaiseHook)(int, const char *,...)
static const size_t magic
void LALCheckMemoryLeaks(void)
int main(int argc, char *argv[])
void XLALClobberDebugLevel(int level)
int XLALGetDebugLevel(void)
@ LALMEMDBG
enable memory debugging tools
@ LALMEMINFOBIT
enable memory info messages
@ LALMEMPADBIT
enable memory padding
@ LALMEMTRKBIT
enable memory tracking
@ LALMEMDBGBIT
enable memory debugging routines