Memory diagnostics. Huge memory leak cleanup related to templates

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@4639 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2003-03-27 23:17:27 +00:00
commit 9f47936dd0
7 changed files with 167 additions and 50 deletions

View file

@ -107,6 +107,7 @@
#define DohObjMalloc DOH_NAMESPACE(ObjMalloc)
#define DohObjFree DOH_NAMESPACE(ObjFree)
#define DohMemoryDebug DOH_NAMESPACE(MemoryDebug)
#define DohStringType DOH_NAMESPACE(StringType)
#define DohListType DOH_NAMESPACE(ListType)
#define DohHashType DOH_NAMESPACE(HashType)
@ -297,6 +298,8 @@ extern DOHVoid *DohNewVoid(void *ptr, void (*del)(void *));
extern DOHList *DohSplit(DOHFile *input, char ch, int nsplits);
extern DOH *DohNone;
extern void DohMemoryDebug(void);
#ifndef DOH_LONG_NAMES
/* Macros to invoke the above functions. Includes the location of
the caller to simplify debugging if something goes wrong */

View file

@ -45,6 +45,7 @@ CreatePool() {
assert(p);
p->ptr = (DohBase *) DohMalloc(sizeof(DohBase)*PoolSize);
assert(p->ptr);
memset(p->ptr,0,sizeof(DohBase)*PoolSize);
p->len = PoolSize;
p->blen = PoolSize*sizeof(DohBase);
p->current = 0;
@ -139,6 +140,7 @@ DohObjFree(DOH *ptr) {
b = (DohBase *) ptr;
if (b->flag_intern) return;
b->data = (void *) FreeList;
b->refcount = 0;
if (b->meta) {
Delete(b->meta);
b->meta = 0;
@ -146,3 +148,71 @@ DohObjFree(DOH *ptr) {
b->type = 0;
FreeList = b;
}
/* ----------------------------------------------------------------------
* DohMemoryDebug()
*
* Display memory usage statistics
* ---------------------------------------------------------------------- */
void
DohMemoryDebug(void) {
extern DohObjInfo DohStringType;
extern DohObjInfo DohListType;
extern DohObjInfo DohHashType;
Pool *p;
int totsize = 0;
int totused = 0;
int totfree = 0;
int numstring = 0;
int numlist = 0;
int numhash = 0;
printf("Memory statistics:\n\n");
printf("Pools:\n");
p = Pools;
while(p) {
/* Calculate number of used, free items */
int i;
int nused = 0, nfree = 0;
for (i = 0; i < p->len; i++) {
if (p->ptr[i].refcount <= 0) nfree++;
else {
nused++;
if (p->ptr[i].type == &DohStringType) numstring++;
else if (p->ptr[i].type == &DohListType) numlist++;
else if (p->ptr[i].type == &DohHashType) numhash++;
}
}
printf(" Pool %8x: size = %10d. used = %10d. free = %10d\n", p, p->len, nused, nfree);
totsize += p->len;
totused+= nused;
totfree+= nfree;
p = p->next;
}
printf("\n Total: size = %d, used = %d, free = %d\n", totsize, totused, totfree);
printf("\nObject types\n");
printf(" Strings : %d\n", numstring);
printf(" Lists : %d\n", numlist);
printf(" Hashes : %d\n", numhash);
#if 0
p = Pools;
while(p) {
int i;
for (i = 0; i < p->len; i++) {
if (p->ptr[i].refcount > 0) {
if (p->ptr[i].type == &DohStringType) {
Printf(stdout,"%s\n", p->ptr+i);
}
}
}
p = p->next;
}
#endif
}

View file

@ -88,8 +88,8 @@ static void
DelString(DOH *so) {
String *s = (String *) ObjData(so);
s->hashkey = -1;
s->str = 0;
DohFree(s->str);
s->str = 0;
DohFree(s);
}