Commit 3f345674 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

dmemory: added env. MEMORY_INFO + cleanup

parent 02b06e8f
2015-05-28 Uwe Schulzweida
* dmemory: added env. MEMORY_INFO
2015-04-28 Uwe Schulzweida 2015-04-28 Uwe Schulzweida
* Version 1.6.9 released * Version 1.6.9 released
......
...@@ -9,8 +9,6 @@ ...@@ -9,8 +9,6 @@
#include <stdarg.h> #include <stdarg.h>
#include <errno.h> #include <errno.h>
#include "error.h"
#if !defined(HAVE_CONFIG_H) && !defined(HAVE_MALLOC_H) && defined(SX) #if !defined(HAVE_CONFIG_H) && !defined(HAVE_MALLOC_H) && defined(SX)
# define HAVE_MALLOC_H # define HAVE_MALLOC_H
#endif #endif
...@@ -24,21 +22,18 @@ ...@@ -24,21 +22,18 @@
#include "dmemory.h" #include "dmemory.h"
//However, we need to avoid clobbering our own `malloc()` calls, so we ensure that our own malloc calls cannot be interpreted as macro calls. //However, we need to avoid clobbering our own `malloc()` calls, so we ensure that our own malloc calls cannot be interpreted as macro calls.
#define protected_realloc (realloc)
#define protected_calloc (calloc)
#define protected_malloc (malloc) #define protected_malloc (malloc)
#define protected_calloc (calloc)
#define protected_realloc (realloc)
#define protected_free (free) #define protected_free (free)
enum {MALLOC_FUNC=0, CALLOC_FUNC, REALLOC_FUNC, FREE_FUNC};
static const char *memfunc[] = {"Malloc", "Calloc", "Realloc", "Free"};
#define MALLOC_FUNC 0 #undef MEM_UNDEFID
#define CALLOC_FUNC 1 #define MEM_UNDEFID -1
#define REALLOC_FUNC 2
#define FREE_FUNC 3
#undef UNDEFID
#define UNDEFID -1
#define MAXNAME 32 /* Min = 8, for "unknown" ! */ #define MEM_MAXNAME 32 /* Min = 8, for "unknown" ! */
int dmemory_ExitOnError = 0; int dmemory_ExitOnError = 0;
...@@ -50,8 +45,8 @@ typedef struct ...@@ -50,8 +45,8 @@ typedef struct
size_t nobj; size_t nobj;
int mtype; int mtype;
int line; int line;
char file[MAXNAME]; char file[MEM_MAXNAME];
char caller[MAXNAME]; char caller[MEM_MAXNAME];
} }
MemTable_t; MemTable_t;
...@@ -65,6 +60,7 @@ static size_t MemUsed = 0; ...@@ -65,6 +60,7 @@ static size_t MemUsed = 0;
static size_t MaxMemUsed = 0; static size_t MaxMemUsed = 0;
static int MEM_Debug = 0; /* If set to 1, debugging */ static int MEM_Debug = 0; /* If set to 1, debugging */
static int MEM_Info = 0; /* If set to 1, print mem table at exit */
void memDebug(int debug) void memDebug(int debug)
{ {
...@@ -104,62 +100,34 @@ static ...@@ -104,62 +100,34 @@ static
void memListPrintEntry(int mtype, int item, size_t size, void *ptr, void memListPrintEntry(int mtype, int item, size_t size, void *ptr,
const char *caller, const char *file, int line) const char *caller, const char *file, int line)
{ {
switch (mtype) fprintf(stderr, "[%-7s ", memfunc[mtype]);
fprintf(stderr, "memory item %3d ", item);
fprintf(stderr, "(%6zu byte) ", size);
fprintf(stderr, "at %p", ptr);
if ( file != NULL )
{ {
case MALLOC_FUNC: fprintf(stderr, " line %4d", line);
fprintf(stderr, "[%-7s ", "Malloc"); fprintf(stderr, " file %s", file);
break;
case CALLOC_FUNC:
fprintf(stderr, "[%-7s ", "Calloc");
break;
case REALLOC_FUNC:
fprintf(stderr, "[%-7s ", "Realloc");
break;
case FREE_FUNC:
fprintf(stderr, "[%-7s ", "Free");
break;
} }
if ( caller != NULL )
fprintf(stderr, "memory item %3d ", item); fprintf(stderr, " (%s)", caller);
fprintf(stderr, "(%6zu byte) ", size); fprintf(stderr, "]\n");
fprintf(stderr, "at %p", ptr);
if ( file != NULL )
{
fprintf(stderr, " line %4d", line);
fprintf(stderr, " file %s", file);
}
if ( caller != NULL )
fprintf(stderr, " (%s)", caller);
fprintf(stderr, "]\n");
} }
static static
void memListPrintTable(void) void memListPrintTable(void)
{ {
int item, item1, item2 = 0;
if ( MemObjs ) fprintf(stderr, "\nMemory table:\n"); if ( MemObjs ) fprintf(stderr, "\nMemory table:\n");
/* find maximum item */ for ( size_t memID = 0; memID < memTableSize; memID++ )
for (size_t memID = 0; memID < memTableSize; memID++) {
if ( memTable[memID].item != UNDEFID ) if ( memTable[memID].item != MEM_UNDEFID )
if ( memTable[memID].item > item2 ) item2 = memTable[memID].item; memListPrintEntry(memTable[memID].mtype, memTable[memID].item,
memTable[memID].size*memTable[memID].nobj,
/* find minimum item */ memTable[memID].ptr, memTable[memID].caller,
item1 = item2; memTable[memID].file, memTable[memID].line);
for (size_t memID = 0; memID < memTableSize; memID++) }
if ( memTable[memID].item != UNDEFID )
if ( memTable[memID].item < item1 ) item1 = memTable[memID].item;
for ( item = item1; item <= item2; item++ )
for (size_t memID = 0; memID < memTableSize; memID++)
{
if ( memTable[memID].item == item )
memListPrintEntry(memTable[memID].mtype, memTable[memID].item,
memTable[memID].size*memTable[memID].nobj,
memTable[memID].ptr, memTable[memID].caller,
memTable[memID].file, memTable[memID].line);
}
if ( MemObjs ) if ( MemObjs )
{ {
...@@ -195,18 +163,18 @@ void memListPrintTable(void) ...@@ -195,18 +163,18 @@ void memListPrintTable(void)
static static
void memGetDebugLevel(void) void memGetDebugLevel(void)
{ {
char *debugLevel; const char *envstr;
debugLevel = getenv("MEMORY_DEBUG"); envstr = getenv("MEMORY_INFO");
if ( envstr && isdigit((int) envstr[0]) ) MEM_Info = atoi(envstr);
if ( debugLevel ) envstr = getenv("MEMORY_DEBUG");
{
if ( isdigit((int) debugLevel[0]) )
MEM_Debug = atoi(debugLevel);
if ( MEM_Debug ) if ( envstr && isdigit((int) envstr[0]) ) MEM_Debug = atoi(envstr);
atexit(memListPrintTable);
} if ( MEM_Debug && !MEM_Info ) MEM_Info = 1;
if ( MEM_Info ) atexit(memListPrintTable);
} }
static static
...@@ -224,12 +192,12 @@ void memInit(void) ...@@ -224,12 +192,12 @@ void memInit(void)
static static
int memListDeleteEntry(void *ptr, size_t *size) int memListDeleteEntry(void *ptr, size_t *size)
{ {
int item = UNDEFID; int item = MEM_UNDEFID;
size_t memID; size_t memID;
for (memID = 0; memID < memTableSize; memID++ ) for ( memID = 0; memID < memTableSize; memID++ )
{ {
if ( memTable[memID].item == UNDEFID ) continue; if ( memTable[memID].item == MEM_UNDEFID ) continue;
if ( memTable[memID].ptr == ptr ) break; if ( memTable[memID].ptr == ptr ) break;
} }
...@@ -238,11 +206,11 @@ int memListDeleteEntry(void *ptr, size_t *size) ...@@ -238,11 +206,11 @@ int memListDeleteEntry(void *ptr, size_t *size)
MemObjs--; MemObjs--;
MemUsed -= memTable[memID].size * memTable[memID].nobj; MemUsed -= memTable[memID].size * memTable[memID].nobj;
*size = memTable[memID].size * memTable[memID].nobj; *size = memTable[memID].size * memTable[memID].nobj;
item = memTable[memID].item; item = memTable[memID].item;
memTable[memID].item = UNDEFID; memTable[memID].item = MEM_UNDEFID;
} }
return (item); return item;
} }
static static
...@@ -252,11 +220,11 @@ void memTableInitEntry(size_t memID) ...@@ -252,11 +220,11 @@ void memTableInitEntry(size_t memID)
memInternalProblem(__func__, "memID %d undefined!", memID); memInternalProblem(__func__, "memID %d undefined!", memID);
memTable[memID].ptr = NULL; memTable[memID].ptr = NULL;
memTable[memID].item = UNDEFID; memTable[memID].item = MEM_UNDEFID;
memTable[memID].size = 0; memTable[memID].size = 0;
memTable[memID].nobj = 0; memTable[memID].nobj = 0;
memTable[memID].mtype = UNDEFID; memTable[memID].mtype = MEM_UNDEFID;
memTable[memID].line = UNDEFID; memTable[memID].line = MEM_UNDEFID;
} }
static static
...@@ -278,14 +246,14 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj, ...@@ -278,14 +246,14 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj,
memTable = (MemTable_t *) protected_malloc(memSize); memTable = (MemTable_t *) protected_malloc(memSize);
if( memTable == NULL ) memError(__func__, __FILE__, __LINE__, memSize); if( memTable == NULL ) memError(__func__, __FILE__, __LINE__, memSize);
for(size_t i = 0; i < memTableSize; i++) for ( size_t i = 0; i < memTableSize; i++ )
memTableInitEntry(i); memTableInitEntry(i);
} }
else else
{ {
while( memID < memTableSize ) while ( memID < memTableSize )
{ {
if ( memTable[memID].item == UNDEFID ) break; if ( memTable[memID].item == MEM_UNDEFID ) break;
memID++; memID++;
} }
} }
...@@ -297,9 +265,9 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj, ...@@ -297,9 +265,9 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj,
memTableSize = 2*memTableSize; memTableSize = 2*memTableSize;
memSize = memTableSize*sizeof(MemTable_t); memSize = memTableSize*sizeof(MemTable_t);
memTable = (MemTable_t*) protected_realloc(memTable, memSize); memTable = (MemTable_t*) protected_realloc(memTable, memSize);
if( memTable == NULL ) memError(__func__, __FILE__, __LINE__, memSize); if ( memTable == NULL ) memError(__func__, __FILE__, __LINE__, memSize);
for (size_t i = memID; i < memTableSize; i++) for ( size_t i = memID; i < memTableSize; i++ )
memTableInitEntry(i); memTableInitEntry(i);
} }
...@@ -313,7 +281,7 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj, ...@@ -313,7 +281,7 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj,
if ( file ) if ( file )
{ {
size_t len = strlen(file); size_t len = strlen(file);
if ( len > MAXNAME-1 ) len = MAXNAME-1; if ( len > MEM_MAXNAME-1 ) len = MEM_MAXNAME-1;
(void) memcpy(memTable[memID].file, file, len); (void) memcpy(memTable[memID].file, file, len);
memTable[memID].file[len] = '\0'; memTable[memID].file[len] = '\0';
...@@ -326,7 +294,7 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj, ...@@ -326,7 +294,7 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj,
if ( caller ) if ( caller )
{ {
size_t len = strlen(caller); size_t len = strlen(caller);
if ( len > MAXNAME-1 ) len = MAXNAME-1; if ( len > MEM_MAXNAME-1 ) len = MEM_MAXNAME-1;
(void) memcpy(memTable[memID].caller, caller, len); (void) memcpy(memTable[memID].caller, caller, len);
memTable[memID].caller[len] = '\0'; memTable[memID].caller[len] = '\0';
...@@ -341,20 +309,20 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj, ...@@ -341,20 +309,20 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj,
MemUsed += size*nobj; MemUsed += size*nobj;
if ( MemUsed > MaxMemUsed ) MaxMemUsed = MemUsed; if ( MemUsed > MaxMemUsed ) MaxMemUsed = MemUsed;
return (item++); return item++;
} }
static static
int memListChangeEntry(void *ptrold, void *ptr, size_t size, int memListChangeEntry(void *ptrold, void *ptr, size_t size,
const char *caller, const char *file, int line) const char *caller, const char *file, int line)
{ {
int item = UNDEFID; int item = MEM_UNDEFID;
size_t memID = 0; size_t memID = 0;
size_t sizeold; size_t sizeold;
while( memID < memTableSize ) while( memID < memTableSize )
{ {
if ( memTable[memID].item != UNDEFID ) if ( memTable[memID].item != MEM_UNDEFID )
if ( memTable[memID].ptr == ptrold ) break; if ( memTable[memID].ptr == ptrold ) break;
memID++; memID++;
} }
...@@ -379,7 +347,7 @@ int memListChangeEntry(void *ptrold, void *ptr, size_t size, ...@@ -379,7 +347,7 @@ int memListChangeEntry(void *ptrold, void *ptr, size_t size,
if ( file ) if ( file )
{ {
size_t len = strlen(file); size_t len = strlen(file);
if ( len > MAXNAME-1 ) len = MAXNAME-1; if ( len > MEM_MAXNAME-1 ) len = MEM_MAXNAME-1;
(void) memcpy(memTable[memID].file, file, len); (void) memcpy(memTable[memID].file, file, len);
memTable[memID].file[len] = '\0'; memTable[memID].file[len] = '\0';
...@@ -392,7 +360,7 @@ int memListChangeEntry(void *ptrold, void *ptr, size_t size, ...@@ -392,7 +360,7 @@ int memListChangeEntry(void *ptrold, void *ptr, size_t size,
if ( caller ) if ( caller )
{ {
size_t len = strlen(caller); size_t len = strlen(caller);
if ( len > MAXNAME-1 ) len = MAXNAME-1; if ( len > MEM_MAXNAME-1 ) len = MEM_MAXNAME-1;
(void) memcpy(memTable[memID].caller, caller, len); (void) memcpy(memTable[memID].caller, caller, len);
memTable[memID].caller[len] = '\0'; memTable[memID].caller[len] = '\0';
...@@ -407,14 +375,14 @@ int memListChangeEntry(void *ptrold, void *ptr, size_t size, ...@@ -407,14 +375,14 @@ int memListChangeEntry(void *ptrold, void *ptr, size_t size,
if ( MemUsed > MaxMemUsed ) MaxMemUsed = MemUsed; if ( MemUsed > MaxMemUsed ) MaxMemUsed = MemUsed;
} }
return (item); return item;
} }
void *Calloc(const char *caller, const char *file, int line, size_t nobjs, size_t size) void *Calloc(const char *caller, const char *file, int line, size_t nobjs, size_t size)
{ {
void *ptr = NULL; void *ptr = NULL;
int item = UNDEFID; int item = MEM_UNDEFID;
memInit(); memInit();
...@@ -422,13 +390,13 @@ void *Calloc(const char *caller, const char *file, int line, size_t nobjs, size_ ...@@ -422,13 +390,13 @@ void *Calloc(const char *caller, const char *file, int line, size_t nobjs, size_
{ {
ptr = protected_calloc(nobjs, size); ptr = protected_calloc(nobjs, size);
if ( MEM_Debug ) if ( MEM_Info )
{ {
memAccess++; memAccess++;
if ( ptr ) item = memListNewEntry(CALLOC_FUNC, ptr, size, nobjs, caller, file, line); if ( ptr ) item = memListNewEntry(CALLOC_FUNC, ptr, size, nobjs, caller, file, line);
memListPrintEntry(CALLOC_FUNC, item, size*nobjs, ptr, caller, file, line); if ( MEM_Debug ) memListPrintEntry(CALLOC_FUNC, item, size*nobjs, ptr, caller, file, line);
} }
if ( ptr == NULL && dmemory_ExitOnError ) if ( ptr == NULL && dmemory_ExitOnError )
...@@ -444,7 +412,7 @@ void *Calloc(const char *caller, const char *file, int line, size_t nobjs, size_ ...@@ -444,7 +412,7 @@ void *Calloc(const char *caller, const char *file, int line, size_t nobjs, size_
void *Malloc(const char *caller, const char *file, int line, size_t size) void *Malloc(const char *caller, const char *file, int line, size_t size)
{ {
void *ptr = NULL; void *ptr = NULL;
int item = UNDEFID; int item = MEM_UNDEFID;
memInit(); memInit();
...@@ -452,13 +420,13 @@ void *Malloc(const char *caller, const char *file, int line, size_t size) ...@@ -452,13 +420,13 @@ void *Malloc(const char *caller, const char *file, int line, size_t size)
{ {
ptr = protected_malloc(size); ptr = protected_malloc(size);
if ( MEM_Debug ) if ( MEM_Info )
{ {
memAccess++; memAccess++;
if ( ptr ) item = memListNewEntry(MALLOC_FUNC, ptr, size, 1, caller, file, line); if ( ptr ) item = memListNewEntry(MALLOC_FUNC, ptr, size, 1, caller, file, line);
memListPrintEntry(MALLOC_FUNC, item, size, ptr, caller, file, line); if ( MEM_Debug ) memListPrintEntry(MALLOC_FUNC, item, size, ptr, caller, file, line);
} }
if ( ptr == NULL && dmemory_ExitOnError ) if ( ptr == NULL && dmemory_ExitOnError )
...@@ -467,14 +435,14 @@ void *Malloc(const char *caller, const char *file, int line, size_t size) ...@@ -467,14 +435,14 @@ void *Malloc(const char *caller, const char *file, int line, size_t size)
else else
fprintf(stderr, "Warning (%s) : Allocation of 0 bytes! [ line %d file %s ]\n", caller, line, file); fprintf(stderr, "Warning (%s) : Allocation of 0 bytes! [ line %d file %s ]\n", caller, line, file);
return (ptr); return ptr;
} }
void *Realloc(const char *caller, const char *file, int line, void *ptrold, size_t size) void *Realloc(const char *caller, const char *file, int line, void *ptrold, size_t size)
{ {
void *ptr = NULL; void *ptr = NULL;
int item = UNDEFID; int item = MEM_UNDEFID;
memInit(); memInit();
...@@ -482,7 +450,7 @@ void *Realloc(const char *caller, const char *file, int line, void *ptrold, size ...@@ -482,7 +450,7 @@ void *Realloc(const char *caller, const char *file, int line, void *ptrold, size
{ {
ptr = protected_realloc(ptrold, size); ptr = protected_realloc(ptrold, size);
if ( MEM_Debug ) if ( MEM_Info )
{ {
memAccess++; memAccess++;
...@@ -490,10 +458,10 @@ void *Realloc(const char *caller, const char *file, int line, void *ptrold, size ...@@ -490,10 +458,10 @@ void *Realloc(const char *caller, const char *file, int line, void *ptrold, size
{ {
item = memListChangeEntry(ptrold, ptr, size, caller, file, line); item = memListChangeEntry(ptrold, ptr, size, caller, file, line);
if ( item == UNDEFID ) item = memListNewEntry(REALLOC_FUNC, ptr, size, 1, caller, file, line); if ( item == MEM_UNDEFID ) item = memListNewEntry(REALLOC_FUNC, ptr, size, 1, caller, file, line);
} }
memListPrintEntry(REALLOC_FUNC, item, size, ptr, caller, file, line); if ( MEM_Debug ) memListPrintEntry(REALLOC_FUNC, item, size, ptr, caller, file, line);
} }
if ( ptr == NULL && dmemory_ExitOnError ) if ( ptr == NULL && dmemory_ExitOnError )
...@@ -513,15 +481,15 @@ void Free(const char *caller, const char *file, int line, void *ptr) ...@@ -513,15 +481,15 @@ void Free(const char *caller, const char *file, int line, void *ptr)
memInit(); memInit();
if ( MEM_Debug ) if ( MEM_Info )
{ {
if ( (item = memListDeleteEntry(ptr, &size)) >= 0 ) if ( (item = memListDeleteEntry(ptr, &size)) >= 0 )
{ {
memListPrintEntry(FREE_FUNC, item, size, ptr, caller, file, line); if ( MEM_Debug ) memListPrintEntry(FREE_FUNC, item, size, ptr, caller, file, line);
} }
else else
{ {
if ( ptr ) if ( ptr && MEM_Debug )
fprintf(stderr, "%s info: memory entry at %p not found. [line %4d file %s (%s)]\n", fprintf(stderr, "%s info: memory entry at %p not found. [line %4d file %s (%s)]\n",
__func__, ptr, line, file, caller); __func__, ptr, line, file, caller);
} }
...@@ -530,6 +498,8 @@ void Free(const char *caller, const char *file, int line, void *ptr) ...@@ -530,6 +498,8 @@ void Free(const char *caller, const char *file, int line, void *ptr)
protected_free(ptr); protected_free(ptr);
} }
#include "error.h" // cdiAbort()
void *cdiXmalloc(size_t size, const char *filename, const char *functionname, void *cdiXmalloc(size_t size, const char *filename, const char *functionname,
int line) int line)
{ {
...@@ -544,7 +514,7 @@ void *cdiXcalloc(size_t nmemb, size_t size, const char *filename, ...@@ -544,7 +514,7 @@ void *cdiXcalloc(size_t nmemb, size_t size, const char *filename,
{ {
void *value = protected_calloc(nmemb, size); void *value = protected_calloc(nmemb, size);
if (size == 0 || value != NULL) ; else if (size == 0 || value != NULL) ; else
cdiAbort(filename, functionname, line, "calloc failed: %s", strerror(errno) ); cdiAbort(filename, functionname, line, "calloc failed: %s", strerror(errno));
return value; return value;
} }
...@@ -581,7 +551,7 @@ size_t memTotal(void) ...@@ -581,7 +551,7 @@ size_t memTotal(void)
memtotal = (size_t)meminfo.arena; memtotal = (size_t)meminfo.arena;
#endif #endif
return (memtotal); return memtotal;
} }
......