More information in debug output.

This commit is contained in:
Hisham Muhammad 2006-06-06 20:40:29 +00:00
parent f6e0b7d0c0
commit b95993fa22
1 changed files with 50 additions and 16 deletions

View File

@ -1,25 +1,25 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <assert.h> #include <assert.h>
#include "DebugMemory.h"
#undef strdup #undef strdup
#undef malloc #undef malloc
#undef realloc #undef realloc
#undef calloc #undef calloc
#undef free #undef free
#include "DebugMemory.h"
/*{ /*{
typedef struct DebugMemoryItem_ DebugMemoryItem; typedef struct DebugMemoryItem_ DebugMemoryItem;
struct DebugMemoryItem_ { struct DebugMemoryItem_ {
int magic;
void* data; void* data;
char* file; char* file;
int line; int line;
@ -31,12 +31,15 @@ typedef struct DebugMemory_ {
int allocations; int allocations;
int deallocations; int deallocations;
int size; int size;
bool totals;
FILE* file; FILE* file;
} DebugMemory; } DebugMemory;
}*/ }*/
/* private property */ #if defined(DEBUG)
DebugMemory* singleton = NULL;
static DebugMemory* singleton = NULL;
void DebugMemory_new() { void DebugMemory_new() {
if (singleton) if (singleton)
@ -47,40 +50,60 @@ void DebugMemory_new() {
singleton->deallocations = 0; singleton->deallocations = 0;
singleton->size = 0; singleton->size = 0;
singleton->file = fopen("/tmp/htop-debug-alloc.txt", "w"); singleton->file = fopen("/tmp/htop-debug-alloc.txt", "w");
singleton->totals = true;
//singleton->file = NULL;
} }
void* DebugMemory_malloc(int size, char* file, int line) { void* DebugMemory_malloc(int size, char* file, int line, char* str) {
void* data = malloc(size); void* data = malloc(size);
DebugMemory_registerAllocation(data, file, line); DebugMemory_registerAllocation(data, file, line);
fprintf(singleton->file, "%d\t%s:%d\n", size, file, line); if (singleton->file) {
if (singleton->totals) fprintf(singleton->file, "%d\t", singleton->size);
fprintf(singleton->file, "%d\t%s:%d (%s)\n", size, file, line, str);
}
return data; return data;
} }
void* DebugMemory_calloc(int a, int b, char* file, int line) { void* DebugMemory_calloc(int a, int b, char* file, int line) {
void* data = calloc(a, b); void* data = calloc(a, b);
DebugMemory_registerAllocation(data, file, line); DebugMemory_registerAllocation(data, file, line);
fprintf(singleton->file, "%d\t%s:%d\n", a*b, file, line); if (singleton->file) {
if (singleton->totals) fprintf(singleton->file, "%d\t", singleton->size);
fprintf(singleton->file, "%d\t%s:%d\n", a*b, file, line);
}
return data; return data;
} }
void* DebugMemory_realloc(void* ptr, int size, char* file, int line) { void* DebugMemory_realloc(void* ptr, int size, char* file, int line, char* str) {
if (ptr != NULL) if (ptr != NULL)
DebugMemory_registerDeallocation(ptr, file, line); DebugMemory_registerDeallocation(ptr, file, line);
void* data = realloc(ptr, size); void* data = realloc(ptr, size);
DebugMemory_registerAllocation(data, file, line); DebugMemory_registerAllocation(data, file, line);
fprintf(singleton->file, "%d\t%s:%d\n", size, file, line); if (singleton->file) {
if (singleton->totals) fprintf(singleton->file, "%d\t", singleton->size);
fprintf(singleton->file, "%d\t%s:%d (%s)\n", size, file, line, str);
}
return data; return data;
} }
void* DebugMemory_strdup(char* str, char* file, int line) { void* DebugMemory_strdup(char* str, char* file, int line) {
assert(str);
char* data = strdup(str); char* data = strdup(str);
DebugMemory_registerAllocation(data, file, line); DebugMemory_registerAllocation(data, file, line);
fprintf(singleton->file, "%d\t%s:%d\n", (int) strlen(str), file, line); if (singleton->file) {
if (singleton->totals) fprintf(singleton->file, "%d\t", singleton->size);
fprintf(singleton->file, "%d\t%s:%d\n", (int) strlen(str), file, line);
}
return data; return data;
} }
void DebugMemory_free(void* data, char* file, int line) { void DebugMemory_free(void* data, char* file, int line) {
assert(data);
DebugMemory_registerDeallocation(data, file, line); DebugMemory_registerDeallocation(data, file, line);
if (singleton->file) {
if (singleton->totals) fprintf(singleton->file, "%d\t", singleton->size);
fprintf(singleton->file, "free\t%s:%d\n", file, line);
}
free(data); free(data);
} }
@ -91,6 +114,7 @@ void DebugMemory_assertSize() {
DebugMemoryItem* walk = singleton->first; DebugMemoryItem* walk = singleton->first;
int i = 0; int i = 0;
while (walk != NULL) { while (walk != NULL) {
assert(walk->magic == 11061980);
i++; i++;
walk = walk->next; walk = walk->next;
} }
@ -104,6 +128,7 @@ int DebugMemory_getBlockCount() {
DebugMemoryItem* walk = singleton->first; DebugMemoryItem* walk = singleton->first;
int i = 0; int i = 0;
while (walk != NULL) { while (walk != NULL) {
assert(walk->magic == 11061980);
i++; i++;
walk = walk->next; walk = walk->next;
} }
@ -115,6 +140,7 @@ void DebugMemory_registerAllocation(void* data, char* file, int line) {
DebugMemory_new(); DebugMemory_new();
DebugMemory_assertSize(); DebugMemory_assertSize();
DebugMemoryItem* item = (DebugMemoryItem*) malloc(sizeof(DebugMemoryItem)); DebugMemoryItem* item = (DebugMemoryItem*) malloc(sizeof(DebugMemoryItem));
item->magic = 11061980;
item->data = data; item->data = data;
item->file = file; item->file = file;
item->line = line; item->line = line;
@ -130,6 +156,7 @@ void DebugMemory_registerAllocation(void* data, char* file, int line) {
walk->next = item; walk->next = item;
break; break;
} }
assert(walk->magic == 11061980);
walk = walk->next; walk = walk->next;
} }
} }
@ -141,14 +168,13 @@ void DebugMemory_registerAllocation(void* data, char* file, int line) {
} }
void DebugMemory_registerDeallocation(void* data, char* file, int line) { void DebugMemory_registerDeallocation(void* data, char* file, int line) {
if (!data)
return;
assert(singleton); assert(singleton);
assert(singleton->first); assert(singleton->first);
DebugMemoryItem* walk = singleton->first; DebugMemoryItem* walk = singleton->first;
DebugMemoryItem* prev = NULL; DebugMemoryItem* prev = NULL;
int val = DebugMemory_getBlockCount(); int val = DebugMemory_getBlockCount();
while (walk != NULL) { while (walk != NULL) {
assert(walk->magic == 11061980);
if (walk->data == data) { if (walk->data == data) {
if (prev == NULL) { if (prev == NULL) {
singleton->first = walk->next; singleton->first = walk->next;
@ -176,6 +202,7 @@ void DebugMemory_report() {
DebugMemoryItem* walk = singleton->first; DebugMemoryItem* walk = singleton->first;
int i = 0; int i = 0;
while (walk != NULL) { while (walk != NULL) {
assert(walk->magic == 11061980);
i++; i++;
fprintf(stderr, "%p %s:%d\n", walk->data, walk->file, walk->line); fprintf(stderr, "%p %s:%d\n", walk->data, walk->file, walk->line);
walk = walk->next; walk = walk->next;
@ -185,5 +212,12 @@ void DebugMemory_report() {
fprintf(stderr, "%d deallocations\n", singleton->deallocations); fprintf(stderr, "%d deallocations\n", singleton->deallocations);
fprintf(stderr, "%d size\n", singleton->size); fprintf(stderr, "%d size\n", singleton->size);
fprintf(stderr, "%d non-freed blocks\n", i); fprintf(stderr, "%d non-freed blocks\n", i);
fclose(singleton->file); if (singleton->file)
fclose(singleton->file);
} }
#elif defined(DEBUGLITE)
//#include "efence.h"
#endif