From 6e71f8197e1d9f303f6c6c80d7fd50b788673221 Mon Sep 17 00:00:00 2001 From: ehouse Date: Thu, 22 Nov 2007 05:04:40 +0000 Subject: [PATCH] Add file and line number to debug version of realloc and free. --- xwords4/common/comtypes.h | 4 ++-- xwords4/common/mempool.c | 21 +++++++++++++++------ xwords4/common/mempool.h | 5 +++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/xwords4/common/comtypes.h b/xwords4/common/comtypes.h index 5506a6db6..7c8921907 100644 --- a/xwords4/common/comtypes.h +++ b/xwords4/common/comtypes.h @@ -162,8 +162,8 @@ 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)) -# define XP_FREE(pool,p) mpool_free((pool), (p)) +# define XP_REALLOC(pool,p,s) mpool_realloc((pool),(p),(s),__FILE__,__LINE__) +# define XP_FREE(pool,p) mpool_free((pool), (p),__FILE__,__LINE__) # define MPFORMAL_NOCOMMA MemPoolCtx* mpool # define MPFORMAL MPFORMAL_NOCOMMA, diff --git a/xwords4/common/mempool.c b/xwords4/common/mempool.c index 4bbdb8f9a..ddc7e8af2 100644 --- a/xwords4/common/mempool.c +++ b/xwords4/common/mempool.c @@ -165,29 +165,38 @@ findEntryFor( MemPoolCtx* mpool, void* ptr, MemPoolEntry** prevP ) return entry; } } - XP_ASSERT(0); return (MemPoolEntry*)NULL; } /* findEntryFor */ void* -mpool_realloc( MemPoolCtx* mpool, void* ptr, XP_U32 newsize ) +mpool_realloc( MemPoolCtx* mpool, void* ptr, XP_U32 newsize, const char* file, XP_U32 lineNo ) { MemPoolEntry* entry = findEntryFor( mpool, ptr, (MemPoolEntry**)NULL ); - entry->ptr = XP_PLATREALLOC( entry->ptr, newsize ); - XP_ASSERT( !!entry->ptr ); + if ( !entry ) { + XP_LOGF( "findEntryFor failed; called from %s, line %d", + file, lineNo ); + } else { + entry->ptr = XP_PLATREALLOC( entry->ptr, newsize ); + XP_ASSERT( !!entry->ptr ); + entry->fileName = file; + entry->lineNo = lineNo; + } return entry->ptr; } /* mpool_realloc */ void -mpool_free( MemPoolCtx* mpool, void* ptr ) +mpool_free( MemPoolCtx* mpool, void* ptr, const char* file, XP_U32 lineNo ) { MemPoolEntry* entry; MemPoolEntry* prev; entry = findEntryFor( mpool, ptr, &prev ); - if ( !!entry ) { + if ( !entry ) { + XP_LOGF( "findEntryFor failed; called from %s, line %d", + file, lineNo ); + } else { if ( !!prev ) { prev->next = entry->next; diff --git a/xwords4/common/mempool.h b/xwords4/common/mempool.h index d34c39771..c162f38e7 100644 --- a/xwords4/common/mempool.h +++ b/xwords4/common/mempool.h @@ -35,8 +35,9 @@ void mpool_destroy( MemPoolCtx* mpool ); void* mpool_alloc( MemPoolCtx* mpool, XP_U32 size, const char* file, XP_U32 lineNo ); -void* mpool_realloc( MemPoolCtx* mpool, void* ptr, XP_U32 newsize ); -void mpool_free( MemPoolCtx* mpool, void* ptr ); +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 ); void mpool_stats( MemPoolCtx* mpool, XWStreamCtxt* stream ); XP_U16 mpool_getNUsed( MemPoolCtx* mpool );