Add __func__ to what's tracked for allocation in debug builds

This commit is contained in:
ehouse 2009-01-18 16:37:44 +00:00
parent 40fcb03441
commit 53808a7098
5 changed files with 62 additions and 41 deletions

View file

@ -162,9 +162,11 @@ typedef struct CommonPrefs {
#ifdef MEM_DEBUG
# define XP_MALLOC(pool,nbytes) \
mpool_alloc((pool),(nbytes),__FILE__,__LINE__)
# define XP_REALLOC(pool,p,s) mpool_realloc((pool),(p),(s),__FILE__,__LINE__)
# define XP_FREE(pool,p) mpool_free((pool), (p),__FILE__,__LINE__)
mpool_alloc((pool),(nbytes),__FILE__,__func__, __LINE__)
# define XP_REALLOC(pool,p,s) \
mpool_realloc((pool),(p),(s),__FILE__,__func__,__LINE__)
# define XP_FREE(pool,p) \
mpool_free((pool), (p),__FILE__,__func__,__LINE__)
# define MPFORMAL_NOCOMMA MemPoolCtx* mpool
# define MPFORMAL MPFORMAL_NOCOMMA,

View file

@ -1,6 +1,7 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/*
* Copyright 2001 by Eric House (xwords@eehouse.org). All rights reserved.
* Copyright 2001-2009 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -32,6 +33,7 @@ extern "C" {
typedef struct MemPoolEntry {
struct MemPoolEntry* next;
const char* fileName;
const char* func;
XP_U32 lineNo;
XP_U32 size;
void* ptr;
@ -101,8 +103,8 @@ mpool_destroy( MemPoolCtx* mpool )
MemPoolEntry* entry;
for ( entry = mpool->usedList; !!entry; entry = entry->next ) {
#ifndef FOR_GREMLINS /* I don't want to hear about this right now */
XP_LOGF( "%s: " XP_P " from ln %ld of %s\n", __func__,
entry->ptr, entry->lineNo, entry->fileName );
XP_LOGF( "%s: " XP_P " in %s, ln %ld of %s\n", __func__,
entry->ptr, entry->func, entry->lineNo, entry->fileName );
#ifdef DEBUG
{
char* tryTxt;
@ -125,7 +127,8 @@ mpool_destroy( MemPoolCtx* mpool )
} /* mpool_destroy */
void*
mpool_alloc( MemPoolCtx* mpool, XP_U32 size, const char* file, XP_U32 lineNo )
mpool_alloc( MemPoolCtx* mpool, XP_U32 size, const char* file,
const char* func, XP_U32 lineNo )
{
MemPoolEntry* entry;
@ -141,6 +144,7 @@ mpool_alloc( MemPoolCtx* mpool, XP_U32 size, const char* file, XP_U32 lineNo )
mpool->usedList = entry;
entry->fileName = file;
entry->func = func;
entry->lineNo = lineNo;
entry->size = size;
entry->ptr = XP_PLATMALLOC( size );
@ -179,7 +183,8 @@ findEntryFor( MemPoolCtx* mpool, void* ptr, MemPoolEntry** prevP )
} /* findEntryFor */
void*
mpool_realloc( MemPoolCtx* mpool, void* ptr, XP_U32 newsize, const char* file, XP_U32 lineNo )
mpool_realloc( MemPoolCtx* mpool, void* ptr, XP_U32 newsize, const char* file,
const char* func, XP_U32 lineNo )
{
MemPoolEntry* entry = findEntryFor( mpool, ptr, (MemPoolEntry**)NULL );
@ -190,13 +195,15 @@ mpool_realloc( MemPoolCtx* mpool, void* ptr, XP_U32 newsize, const char* file, X
entry->ptr = XP_PLATREALLOC( entry->ptr, newsize );
XP_ASSERT( !!entry->ptr );
entry->fileName = file;
entry->func = func;
entry->lineNo = lineNo;
}
return entry->ptr;
} /* mpool_realloc */
void
mpool_free( MemPoolCtx* mpool, void* ptr, const char* file, XP_U32 lineNo )
mpool_free( MemPoolCtx* mpool, void* ptr, const char* file,
const char* func, XP_U32 lineNo )
{
MemPoolEntry* entry;
MemPoolEntry* prev;
@ -204,13 +211,14 @@ mpool_free( MemPoolCtx* mpool, void* ptr, const char* file, XP_U32 lineNo )
entry = findEntryFor( mpool, ptr, &prev );
if ( !entry ) {
XP_LOGF( "findEntryFor failed; called from %s, line %ld",
file, lineNo );
XP_LOGF( "findEntryFor failed; called from %s, line %ld in %s",
func, lineNo, file );
} else {
#ifdef MPOOL_DEBUG
XP_LOGF( "%s(ptr=%p):size=%ld,file=%s,lineNo=%ld)", __func__,
entry->ptr, entry->size, entry->fileName, entry->lineNo );
XP_LOGF( "%s(ptr=%p):size=%ld,func=%s,file=%s,lineNo=%ld)", __func__,
entry->ptr, entry->size, entry->func, entry->fileName,
entry->lineNo );
#endif
if ( !!prev ) {
@ -235,32 +243,38 @@ mpool_free( MemPoolCtx* mpool, void* ptr, const char* file, XP_U32 lineNo )
XP_ASSERT( 0 );
} /* mpool_free */
#define STREAM_OR_LOG(stream,buf) \
if ( !!stream ) { \
stream_putString( stream, buf ); \
} else { \
XP_LOGF( "%s", buf ); \
} \
void
mpool_stats( MemPoolCtx* mpool, XWStreamCtxt* stream )
{
XP_UCHAR buf[128];
MemPoolEntry* entry;
XP_U32 total = 0;
XP_SNPRINTF( buf, sizeof(buf), (XP_UCHAR*)"Number of blocks in use: %d\n"
"Number of free blocks: %d\n"
"Total number of blocks allocated: %d\n",
mpool->nUsed, mpool->nFree, mpool->nAllocs );
if ( !!stream ) {
stream_putString( stream, buf );
} else {
XP_LOGF( "%s", buf );
}
STREAM_OR_LOG( stream, buf );
for ( entry = mpool->usedList; !!entry; entry = entry->next ) {
XP_SNPRINTF( buf, sizeof(buf),
(XP_UCHAR*)"%ld byte block allocated at %p, %s: line %ld\n",
entry->size, entry->ptr, entry->fileName, entry->lineNo );
if ( !!stream ) {
stream_putString( stream, buf );
} else {
XP_LOGF( "%s", buf );
}
(XP_UCHAR*)"%ld byte block allocated at %p, at line %ld "
"in %s, %s\n", entry->size, entry->ptr, entry->lineNo,
entry->func, entry->fileName );
STREAM_OR_LOG( stream, buf );
total += entry->size;
}
XP_SNPRINTF( buf, sizeof(buf), "total bytes allocated: %ld\n", total );
STREAM_OR_LOG( stream, buf );
} /* mpool_stats */
XP_U16

View file

@ -1,6 +1,7 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/*
* Copyright 2001 by Eric House (xwords@eehouse.org). All rights reserved.
* Copyright 2001-2009 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -34,10 +35,11 @@ MemPoolCtx* mpool_make(void);
void mpool_destroy( MemPoolCtx* mpool );
void* mpool_alloc( MemPoolCtx* mpool, XP_U32 size,
const char* file, XP_U32 lineNo );
const char* file, const char* func, XP_U32 lineNo );
void* mpool_realloc( MemPoolCtx* mpool, void* ptr, XP_U32 newsize,
const char* file, XP_U32 lineNo );
void mpool_free( MemPoolCtx* mpool, void* ptr, const char* file, XP_U32 lineNo );
const char* file, const char* func, XP_U32 lineNo );
void mpool_free( MemPoolCtx* mpool, void* ptr, const char* file,
const char* func, XP_U32 lineNo );
void mpool_stats( MemPoolCtx* mpool, XWStreamCtxt* stream );
XP_U16 mpool_getNUsed( MemPoolCtx* mpool );

View file

@ -101,7 +101,7 @@ signedFromStream( XWStreamCtxt* stream, XP_U16 nBits )
XP_UCHAR*
p_stringFromStream( MPFORMAL XWStreamCtxt* stream
#ifdef MEM_DEBUG
, const char* file, XP_U32 lineNo
, const char* file, const char* func, XP_U32 lineNo
#endif
)
{
@ -111,7 +111,7 @@ p_stringFromStream( MPFORMAL XWStreamCtxt* stream
if ( len > 0 ) {
#ifdef MEM_DEBUG
str = mpool_alloc( mpool, len + 1, file, lineNo );
str = mpool_alloc( mpool, len + 1, file, func, lineNo );
#else
str = (XP_UCHAR*)XP_MALLOC( mpool, len + 1 ); /* leaked */
#endif
@ -151,7 +151,7 @@ stringToStream( XWStreamCtxt* stream, const XP_UCHAR* str )
XP_UCHAR*
p_copyString( MPFORMAL const XP_UCHAR* instr
#ifdef MEM_DEBUG
, const char* file, XP_U32 lineNo
, const char* file, const char* func, XP_U32 lineNo
#endif
)
{
@ -159,7 +159,7 @@ p_copyString( MPFORMAL const XP_UCHAR* instr
if ( !!instr ) {
XP_U16 len = 1 + XP_STRLEN( (const char*)instr );
#ifdef MEM_DEBUG
result = mpool_alloc( mpool, len, file, lineNo );
result = mpool_alloc( mpool, len, file, func, lineNo );
#else
result = XP_MALLOC( ignore, len );
#endif
@ -173,7 +173,7 @@ p_copyString( MPFORMAL const XP_UCHAR* instr
void
p_replaceStringIfDifferent( MPFORMAL XP_UCHAR** curLoc, const XP_UCHAR* newStr
#ifdef MEM_DEBUG
, const char* file, XP_U32 lineNo
, const char* file, const char* func, XP_U32 lineNo
#endif
)
{
@ -187,7 +187,7 @@ p_replaceStringIfDifferent( MPFORMAL XP_UCHAR** curLoc, const XP_UCHAR* newStr
XP_FREE( mpool, curStr );
}
#ifdef MEM_DEBUG
curStr = p_copyString( mpool, newStr, file, lineNo );
curStr = p_copyString( mpool, newStr, file, func, lineNo );
#else
curStr = p_copyString( newStr );
#endif

View file

@ -40,11 +40,13 @@ void signedToStream( XWStreamCtxt* stream, XP_U16 nBits, XP_S32 num );
XP_UCHAR* p_stringFromStream( MPFORMAL XWStreamCtxt* stream
#ifdef MEM_DEBUG
, const char* file, XP_U32 lineNo
, const char* file, const char* func,
XP_U32 lineNo
#endif
);
#ifdef MEM_DEBUG
# define stringFromStream( p, in ) p_stringFromStream( (p), (in), __FILE__, __LINE__ )
# define stringFromStream( p, in ) \
p_stringFromStream( (p), (in), __FILE__,__func__,__LINE__ )
#else
# define stringFromStream( p, in ) p_stringFromStream( in )
#endif
@ -54,11 +56,12 @@ void stringToStream( XWStreamCtxt* stream, const XP_UCHAR* str );
XP_UCHAR* p_copyString( MPFORMAL const XP_UCHAR* instr
#ifdef MEM_DEBUG
, const char* file, XP_U32 lineNo
, const char* file, const char* func, XP_U32 lineNo
#endif
);
#ifdef MEM_DEBUG
# define copyString( p, in ) p_copyString( (p), (in), __FILE__, __LINE__ )
# define copyString( p, in ) \
p_copyString( (p), (in), __FILE__, __func__, __LINE__ )
#else
# define copyString( p, in ) p_copyString( in )
#endif
@ -67,12 +70,12 @@ XP_UCHAR* p_copyString( MPFORMAL const XP_UCHAR* instr
void p_replaceStringIfDifferent( MPFORMAL XP_UCHAR** curLoc,
const XP_UCHAR* newStr
#ifdef MEM_DEBUG
, const char* file, XP_U32 lineNo
, const char* file, const char* func, XP_U32 lineNo
#endif
);
#ifdef MEM_DEBUG
# define replaceStringIfDifferent(p, sp, n) \
p_replaceStringIfDifferent( (p), (sp), (n), __FILE__, __LINE__ )
p_replaceStringIfDifferent( (p), (sp), (n), __FILE__, __func__, __LINE__ )
#else
# define replaceStringIfDifferent(p, sp, n) p_replaceStringIfDifferent((sp),(n))
#endif