Commit 36d7d808 authored by anilsaharan's avatar anilsaharan

Added for Memory Debugging


git-svn-id: http://svn.code.sf.net/p/cunit/code/trunk@26 f00e6729-6848-4c17-8bfb-678c97c00071
parent f913366c
......@@ -76,7 +76,7 @@ DIST_COMMON = Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
all: all-redirect
.SUFFIXES:
......@@ -140,7 +140,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
/*
* CUnit - A Unit testing framework library for C.
* Copyright (C) 2001 Anil Kumar
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Contains Memory Related Defines to use internal routines to detect Memory Leak
* in Debug Versions
*
* Created By : Anil Kumar on ...(18 June 2002)
* Last Modified : 18/Jun/2002
* Comment : Memory Debug Funstions
* EMail : aksaharan@yahoo.com
*/
#ifndef _CUNIT_MYMEM_H
#define _CUNIT_MYMEM_H 1
#ifdef __cplusplus
extern "C" {
#endif
#ifdef MEMTRACE
void* my_calloc(size_t nmemb, size_t size, unsigned int uiLine, const char* szFileName);
void* my_malloc(size_t size, unsigned int uiLine, const char* szFileName);
void my_free(void *ptr, unsigned int uiLine, const char* szFileName);
void* my_realloc(void *ptr, size_t size, unsigned int uiLine, const char* szFileName);
void dump_memory_usage(void);
#define MR_CALLOC(x, y) my_calloc((x), (y), __LINE__, __FILE__)
#define MY_MALLOC(x) my_malloc((x), __LINE__, __FILE__)
#define MY_FREE(x) my_free((x), __LINE__, __FILE__)
#define MR_REALLOC(x, y) my_realloc((x), (y), __LINE__, __FILE__)
#define DUMP_MEMORY_USAGE() dump_memory_usage()
#else
#define MR_CALLOC(x, y) calloc((x), (y))
#define MY_MALLOC(x) malloc((x))
#define MY_FREE(x) free((x))
#define MR_REALLOC(x, y) realloc((x), (y))
#define DUMP_MEMORY_USAGE()
#endif
#ifdef __cplusplus
}
#endif
#endif /* _CUNIT_MYMEM_H */
......@@ -72,7 +72,7 @@ DIST_COMMON = Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
all: all-redirect
.SUFFIXES:
......@@ -181,7 +181,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
......@@ -89,7 +89,7 @@ DIST_COMMON = Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
SOURCES = $(libcunitautomated_a_SOURCES)
OBJECTS = $(libcunitautomated_a_OBJECTS)
......@@ -175,7 +175,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
......@@ -89,7 +89,7 @@ DIST_COMMON = Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
SOURCES = $(libcunitconsole_a_SOURCES)
OBJECTS = $(libcunitconsole_a_OBJECTS)
......@@ -175,7 +175,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
......@@ -89,7 +89,7 @@ DIST_COMMON = Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
SOURCES = $(libcunitcurses_a_SOURCES)
OBJECTS = $(libcunitcurses_a_OBJECTS)
......@@ -175,7 +175,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
......@@ -2,6 +2,7 @@ noinst_LIBRARIES = libcunitfmk.a
libcunitfmk_a_SOURCES = \
AssertImpl.c \
MyMem.c \
TestDB.c \
TestRun.c \
Util.c
......@@ -66,7 +66,7 @@ VERSION = @VERSION@
noinst_LIBRARIES = libcunitfmk.a
libcunitfmk_a_SOURCES = AssertImpl.c TestDB.c TestRun.c Util.c
libcunitfmk_a_SOURCES = AssertImpl.c MyMem.c TestDB.c TestRun.c Util.c
mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
CONFIG_CLEAN_FILES =
......@@ -78,7 +78,7 @@ CPPFLAGS = @CPPFLAGS@
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@
libcunitfmk_a_LIBADD =
libcunitfmk_a_OBJECTS = AssertImpl.o TestDB.o TestRun.o Util.o
libcunitfmk_a_OBJECTS = AssertImpl.o MyMem.o TestDB.o TestRun.o Util.o
AR = ar
CFLAGS = @CFLAGS@
COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
......@@ -89,7 +89,7 @@ DIST_COMMON = Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
SOURCES = $(libcunitfmk_a_SOURCES)
OBJECTS = $(libcunitfmk_a_OBJECTS)
......@@ -175,7 +175,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
/*
* CUnit - A Unit testing framework library for C.
* Copyright (C) 2001 Anil Kumar
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Contains some generic functions used across CUnit project files.
*
* Created By : Anil Kumar on 13/Oct/2001
* Last Modified : 13/Oct/2001
* Comment : Moved some of the generic functions definitions
* from other files to this one so as to use the
* functions consitently. This file is not included
* in the distribution headers because it is used
* internally by CUnit.
* EMail : aksaharan@yahoo.com
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#ifdef MEMTRACE
#define MAX_FILE_NAME_LENGTH 256
const unsigned int NOT_DELETED = 0;
const char* szDumpFileName = "CUnit-Memory-Dump.lst";
typedef enum {
ALLOCATED = 1,
DEALLOCATED,
REALLOCATED
} STATUS;
typedef struct mem_node {
int nSize;
void* pLocation;
unsigned int uiAllocationLine;
char szAllocationFileName[MAX_FILE_NAME_LENGTH];
unsigned int uiDeletionLine;
char szDeletionFileName[MAX_FILE_NAME_LENGTH];
STATUS ChangeStatus;
struct mem_node* pNext;
} MEMORY_NODE;
typedef MEMORY_NODE* PMEMORY_NODE;
PMEMORY_NODE pMemoryTrackerHead = NULL, pMemoryTrackerTail = NULL;
PMEMORY_NODE allocate_memory(int nSize, void* pLocation, unsigned int uiAllocationLine, const char* szAllocationFile)
{
PMEMORY_NODE pMemory = malloc(sizeof(MEMORY_NODE));
assert(pMemory && "Memory Allocation failed in memory debug routine.");
pMemory->nSize = nSize;
pMemory->pLocation = pLocation;
pMemory->uiAllocationLine = uiAllocationLine;
strcpy(pMemory->szAllocationFileName, szAllocationFile);
pMemory->uiDeletionLine = NOT_DELETED;
pMemory->pNext = NULL;
if (!pMemoryTrackerHead) {
pMemoryTrackerHead = pMemoryTrackerTail = pMemory;
} else {
pMemoryTrackerTail->pNext = pMemory;
pMemoryTrackerTail = pMemory;
}
return NULL;
}
void deallocate_memory(void* pLocation, unsigned int uiDeletionLine, const char* szDeletionFileName)
{
PMEMORY_NODE pTemp = NULL;
for (pTemp = pMemoryTrackerHead; pTemp; pTemp = pTemp->pNext) {
if (pTemp->pLocation == pLocation && pTemp->uiDeletionLine == NOT_DELETED) {
pTemp->uiDeletionLine = uiDeletionLine;
strcpy(pTemp->szDeletionFileName, szDeletionFileName);
return ;
}
}
assert("Unable to find the allocated node in the memory allocation list.");
}
void* my_calloc(size_t nmemb, size_t size, unsigned int uiLine, const char* szFileName)
{
void* pVoid = calloc(nmemb, size);
if (!pVoid) {
allocate_memory(nmemb * size, pVoid, uiLine, szFileName);
}
return pVoid;
}
void* my_malloc(size_t size, unsigned int uiLine, const char* szFileName)
{
void* pVoid = malloc(size);
if (pVoid) {
allocate_memory(size, pVoid, uiLine, szFileName);
}
return pVoid;
}
void my_free(void *ptr, unsigned int uiLine, const char* szFileName)
{
deallocate_memory(ptr, uiLine, szFileName);
free(ptr);
}
void* my_realloc(void *ptr, size_t size, unsigned int uiLine, const char* szFileName)
{
void* pVoid = NULL;
deallocate_memory(ptr, uiLine, szFileName);
pVoid = realloc(ptr, size);
if (!pVoid) {
allocate_memory(size, pVoid, uiLine, szFileName);
}
return pVoid;
}
void dump_memory_usage(void)
{
int nSerial = 0;
PMEMORY_NODE pTemp = NULL;
FILE* pFile = fopen(szDumpFileName, "a");
time_t tTime = 0;
if (!pFile) {
fprintf(stderr, "Failed to open file \"%s\" : %s", szDumpFileName, strerror(errno));
return;
}
setvbuf(pFile, NULL, _IONBF, 0);
time(&tTime);
fprintf(pFile, "---------- Memory Trace for CUnit Run at %s -----------\n", ctime(&tTime));
fprintf(pFile, "---Size Pointer Allocation File:Line Deletion File:Line\n");
for (pTemp = pMemoryTrackerHead, nSerial = 0; pTemp != NULL; pTemp = pTemp->pNext) {
fprintf(pFile, "%d\t%d\t%p\t%s:%d\t%s:%d\n", ++nSerial, pTemp->nSize, pTemp->pLocation,
pTemp->szAllocationFileName, pTemp->uiAllocationLine,
pTemp->szDeletionFileName, pTemp->uiDeletionLine);
}
fprintf(pFile, "--- Total Number of Records : %d", nSerial);
fclose(pFile);
}
#endif
This diff is collapsed.
This diff is collapsed.
......@@ -9,6 +9,7 @@ libcunit_a_LIBADD = \
Console/Console.o \
Curses/Curses.o \
Framework/AssertImpl.o \
Framework/MyMem.o \
Framework/TestDB.o \
Framework/TestRun.o \
Framework/Util.o
......@@ -70,7 +70,7 @@ lib_LIBRARIES = libcunit.a
libcunit_a_SOURCES =
libcunit_a_LIBADD = Automated/Automated.o Console/Console.o Curses/Curses.o Framework/AssertImpl.o Framework/TestDB.o Framework/TestRun.o Framework/Util.o
libcunit_a_LIBADD = Automated/Automated.o Console/Console.o Curses/Curses.o Framework/AssertImpl.o Framework/MyMem.o Framework/TestDB.o Framework/TestRun.o Framework/Util.o
mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
CONFIG_CLEAN_FILES =
......@@ -82,8 +82,8 @@ CPPFLAGS = @CPPFLAGS@
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@
libcunit_a_DEPENDENCIES = Automated/Automated.o Console/Console.o \
Curses/Curses.o Framework/AssertImpl.o Framework/TestDB.o \
Framework/TestRun.o Framework/Util.o
Curses/Curses.o Framework/AssertImpl.o Framework/MyMem.o \
Framework/TestDB.o Framework/TestRun.o Framework/Util.o
libcunit_a_OBJECTS =
AR = ar
DIST_COMMON = Makefile.am Makefile.in
......@@ -91,7 +91,7 @@ DIST_COMMON = Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
SOURCES = $(libcunit_a_SOURCES)
OBJECTS = $(libcunit_a_OBJECTS)
......@@ -260,7 +260,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
......@@ -96,7 +96,7 @@ DIST_COMMON = README Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
SOURCES = AutomatedTest.c
OBJECTS = AutomatedTest.o
......@@ -216,7 +216,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
......@@ -96,7 +96,7 @@ DIST_COMMON = README Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
SOURCES = ConsoleTest.c
OBJECTS = ConsoleTest.o
......@@ -216,7 +216,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
......@@ -96,7 +96,7 @@ DIST_COMMON = README Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
SOURCES = CursesTest.c
OBJECTS = CursesTest.o
......@@ -216,7 +216,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
......@@ -72,7 +72,7 @@ DIST_COMMON = Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
all: all-redirect
.SUFFIXES:
......@@ -181,7 +181,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
......@@ -73,7 +73,7 @@ DIST_COMMON = Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
all: all-redirect
.SUFFIXES:
......@@ -182,7 +182,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
......@@ -77,7 +77,7 @@ DIST_COMMON = Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
all: all-redirect
.SUFFIXES:
......@@ -139,7 +139,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
......@@ -77,7 +77,7 @@ DIST_COMMON = Makefile.am Makefile.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = gtar
TAR = tar
GZIP_ENV = --best
all: all-redirect
.SUFFIXES:
......@@ -139,7 +139,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment