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 #ifdef MEM_DEBUG
# define XP_MALLOC(pool,nbytes) \ # define XP_MALLOC(pool,nbytes) \
mpool_alloc((pool),(nbytes),__FILE__,__LINE__) mpool_alloc((pool),(nbytes),__FILE__,__func__, __LINE__)
# define XP_REALLOC(pool,p,s) mpool_realloc((pool),(p),(s),__FILE__,__LINE__) # define XP_REALLOC(pool,p,s) \
# define XP_FREE(pool,p) mpool_free((pool), (p),__FILE__,__LINE__) 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_NOCOMMA MemPoolCtx* mpool
# define MPFORMAL MPFORMAL_NOCOMMA, # define MPFORMAL MPFORMAL_NOCOMMA,

View file

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

View file

@ -1,6 +1,7 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */ /* -*-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 * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * 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_destroy( MemPoolCtx* mpool );
void* mpool_alloc( MemPoolCtx* mpool, XP_U32 size, 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, void* mpool_realloc( MemPoolCtx* mpool, void* ptr, XP_U32 newsize,
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, 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 ); void mpool_stats( MemPoolCtx* mpool, XWStreamCtxt* stream );
XP_U16 mpool_getNUsed( MemPoolCtx* mpool ); XP_U16 mpool_getNUsed( MemPoolCtx* mpool );

View file

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

View file

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