From d772d8db4525be892043767c5bb5e95de45ce18b Mon Sep 17 00:00:00 2001 From: Jeff Squyres Date: Tue, 10 Feb 2004 18:58:55 +0000 Subject: [PATCH] Fix for bug 681: add debug version of realloc() This commit was SVN r689. --- src/include/lam_config_bottom.h | 1 + src/lam/mem/malloc.c | 62 ++++++++++++++++++++------------- src/lam/mem/malloc.h | 16 +++++++++ 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/src/include/lam_config_bottom.h b/src/include/lam_config_bottom.h index 90ba1e925c..22ca3555c3 100644 --- a/src/include/lam_config_bottom.h +++ b/src/include/lam_config_bottom.h @@ -57,5 +57,6 @@ typedef enum { false, true } bool; have Cascading Includes Of Death. */ #include "lam/mem/malloc.h" #define malloc(size) lam_malloc((size), __FILE__, __LINE__) +#define realloc(ptr, size) lam_realloc((ptr), (size), __FILE__, __LINE__) #define free(ptr) lam_free((ptr), __FILE__, __LINE__) #endif diff --git a/src/lam/mem/malloc.c b/src/lam/mem/malloc.c index 9e1c213ec1..1b229798b4 100644 --- a/src/lam/mem/malloc.c +++ b/src/lam/mem/malloc.c @@ -68,18 +68,9 @@ void lam_malloc_finalize(void) } } -/** - * \internal - * - * Back-end error-checking malloc function for LAM (you should use the - * LAM_MALLOC() macro instead of this function). - * - * @param size The number of bytes to allocate - * @param file Typically the __FILE__ macro - * @param line Typically the __LINE__ macro - * - * This function is only used when --enable-mem-debug was specified to - * configure (or by default if you're building in a CVS checkout). + +/* + * Debug version of malloc */ void *lam_malloc(size_t size, char *file, int line) { @@ -103,18 +94,40 @@ void *lam_malloc(size_t size, char *file, int line) } -/** - * \internal - * - * Back-end error-checking free function for LAM (you should use - * free() instead of this function). - * - * @param addr Address on the heap to free() - * @param file Typically the __FILE__ macro - * @param line Typically the __LINE__ macro - * - * This function is only used when --enable-mem-debug was specified to - * configure (or by default if you're building in a CVS checkout). +/* + * Debug version of realloc + */ +void *lam_realloc(void *ptr, size_t size, char *file, int line) +{ + void *addr; + + if (lam_malloc_debug_level > 1) { + if (size <= 0) { + if (NULL == ptr) { + lam_output(lam_malloc_output, + "Realloc NULL for %ld bytes (%s, %d)", + (long) size, file, line); + } else { + lam_output(lam_malloc_output, "Realloc %p for %ld bytes (%s, %d)", + ptr, (long) size, file, line); + } + } + } + addr = realloc(ptr, size); + if (lam_malloc_debug_level > 0) { + if (NULL == addr) { + lam_output(lam_malloc_output, + "Realloc %p for %ld bytes failed (%s, %d)", + ptr, (long) size, file, line); + } + } + + return addr; +} + + +/* + * Debug version of free */ void lam_free(void *addr, char *file, int line) { @@ -125,3 +138,4 @@ void lam_free(void *addr, char *file, int line) } } + diff --git a/src/lam/mem/malloc.h b/src/lam/mem/malloc.h index 6d7dd0eee0..b8c78ab533 100644 --- a/src/lam/mem/malloc.h +++ b/src/lam/mem/malloc.h @@ -70,6 +70,22 @@ extern "C" { */ void *lam_malloc(size_t size, char *file, int line); + /** + * \internal + * + * Back-end error-checking realloc function for LAM (you should use + * the normal realloc() instead of this function). + * + * @param ptr Pointer to reallocate + * @param size The number of bytes to allocate + * @param file Typically the __FILE__ macro + * @param line Typically the __LINE__ macro + * + * This function is only used when --enable-mem-debug was specified to + * configure (or by default if you're building in a CVS checkout). + */ + void *lam_realloc(void *ptr, size_t size, char *file, int line); + /** * \internal *