Skip to content

Commit

Permalink
Various Changes to the memory accounting code, myrealloc() added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnar Mar Hrafnkelsson committed Oct 22, 2001
1 parent bf94b65 commit fe6163b
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 70 deletions.
23 changes: 13 additions & 10 deletions imio.h
Expand Up @@ -15,23 +15,23 @@
#ifdef IMAGER_DEBUG_MALLOC

#define mymalloc(x) (mymalloc_file_line((x), __FILE__, __LINE__))
#define myrealloc(x,y) (myrealloc_file_line((x),(y), __FILE__, __LINE__))
#define myfree(x) (myfree_file_line((x), __FILE__, __LINE__))

void malloc_state();
void* mymalloc_file_line(int size,char* file,int line);
void* mymalloc_comm(int size,char *comm);

void myfree_file_line(void *p, char*file, int line);

void bndcheck_all(void);
void malloc_state ();
void* mymalloc_file_line (size_t size, char* file, int line);
void myfree_file_line (void *p, char*file, int line);
void* myrealloc_file_line(void *p, size_t newsize, char* file,int line);
void* mymalloc_comm (int size, char *comm);
void bndcheck_all (void);

#else

#define malloc_comm(a,b) (mymalloc(a))
void malloc_state(void);
void malloc_state(void);
void* mymalloc(int size);
void myfree(void *p);
void *myrealloc(void *p, size_t newsize);
void myfree(void *p);
void* myrealloc(void *p, size_t newsize);

#endif /* IMAGER_MALLOC_DEBUG */

Expand All @@ -40,6 +40,9 @@ void *myrealloc(void *p, size_t newsize);
#undef min
#undef max
#endif

/* XXX Shouldn't all of these go away */

int min(int a,int b);
int max(int x,int y);
int myread(int fd,void *buf,int len);
Expand Down
167 changes: 107 additions & 60 deletions io.c
Expand Up @@ -22,110 +22,157 @@
static int malloc_need_init = 1;

typedef struct {
void* point;
void* ptr;
size_t size;
char comm[MAXDESC];
char comm[MAXDESC];
} malloc_entry;

malloc_entry malloc_pointers[MAXMAL];

/*
#define mymalloc(x) (mymalloc_file_line(x,__FILE__,__LINE__))
*/



/* Utility functions */


static
void
malloc_init() {
int i;
for(i=0; i<MAXMAL; i++) malloc_pointers[i].ptr = NULL;
malloc_need_init = 0;
atexit(malloc_state);
}


static
int
find_ptr(void *p) {
int i;
for(i=0;i<MAXMAL;i++)
if (malloc_pointers[i].ptr == p)
return i;
return -1;
}


/* Takes a pointer to real start of array,
* sets the entries in the table, returns
* the offset corrected pointer */

static
void *
set_entry(int i, char *buf, size_t size, char *file, int line) {
memset( buf, PADBYTE, UNDRRNVAL );
memset( &buf[UNDRRNVAL+size], PADBYTE, OVERRNVAL );
buf += UNDRRNVAL;
malloc_pointers[i].ptr = buf;
malloc_pointers[i].size = size;
sprintf(malloc_pointers[i].comm,"%s (%d)", file, line);
return buf;
}




void
malloc_state() {
int i, total;
total=0;
int i, total = 0;

mm_log((0,"malloc_state()\n"));
bndcheck_all();
for(i=0;i<MAXMAL;i++) if (malloc_pointers[i].point!=NULL) {
mm_log((0,"%d: %d (0x%x) : %s\n",i,malloc_pointers[i].size,malloc_pointers[i].point,malloc_pointers[i].comm));
total+=malloc_pointers[i].size;
for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr != NULL) {
mm_log((0,"%d: %d (0x%x) : %s\n", i, malloc_pointers[i].size, malloc_pointers[i].ptr, malloc_pointers[i].comm));
total += malloc_pointers[i].size;
}
if (total==0 ) mm_log((0,"No memory currently used!\n"))
else mm_log((0,"total: %d\n",total));
if (total == 0) mm_log((0,"No memory currently used!\n"))
else mm_log((0,"total: %d\n",total));
}



void*
mymalloc_file_line(int size, char* file, int line) {
mymalloc_file_line(size_t size, char* file, int line) {
char *buf;
int i;
if (malloc_need_init) {
for(i=0; i<MAXMAL; i++) malloc_pointers[i].point = NULL;
malloc_need_init = 0;
atexit(malloc_state);
if (malloc_need_init) malloc_init();

/* bndcheck_all(); Uncomment for LOTS OF THRASHING */

if ( (i = find_ptr(NULL)) < 0 ) {
mm_log((0,"more than %d segments allocated at %s (%d)\n", MAXMAL, file, line));
exit(3);
}

/* bndcheck_all(); ACTIVATE FOR LOTS OF THRASHING */

if ( (buf = malloc(size+UNDRRNVAL+OVERRNVAL)) == NULL ) { mm_log((1,"Unable to allocate %i for %s (%i)\n", size, file, line)); exit(3); }
memset( buf, PADBYTE, UNDRRNVAL );
memset( &buf[UNDRRNVAL+size], PADBYTE, OVERRNVAL );

mm_log((1,"mymalloc_file_line: real address %p\n", buf));
buf += UNDRRNVAL; /* Do this now so we will see the returned address in the logs. */

for(i=0;i<MAXMAL;i++) if (malloc_pointers[i].point == NULL) {
malloc_pointers[i].size = size;
sprintf(malloc_pointers[i].comm,"%s (%d)", file, line);
malloc_pointers[i].point = buf;
mm_log((1,"mymalloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, size, buf, file, line));
return buf;
if ( (buf = malloc(size+UNDRRNVAL+OVERRNVAL)) == NULL ) {
mm_log((1,"Unable to allocate %i for %s (%i)\n", size, file, line));
exit(3);
}

mm_log((0,"more than %d segments allocated at %s (%d)\n", MAXMAL, file, line));
exit(255);
return NULL;
buf = set_entry(i, buf, size, file, line);
mm_log((1,"mymalloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, size, buf, file, line));
return buf;
}




/* This function not maintained for now */

/*


void*
mymalloc_comm(int size,char *comm) {
void *buf;
myrealloc_file_line(void *ptr, size_t newsize, char* file, int line) {
char *buf;
int i;
if (malloc_need_init) {
for(i=0;i<MAXMAL;i++) malloc_pointers[i].point=NULL;
malloc_need_init=0;

if (malloc_need_init) malloc_init();
/* bndcheck_all(); ACTIVATE FOR LOTS OF THRASHING */

if (!ptr) {
mm_log((1, "realloc called with ptr = NULL, sending request to malloc\n"));
return mymalloc_file_line(newsize, file, line);
}

if ((buf=malloc(size))==NULL) { mm_log((1,"Unable to malloc.\n")); exit(3); }
if (!newsize) {
mm_log((1, "newsize = 0, sending request to free\n"));
myfree_file_line(ptr, file, line);
return NULL;
}

for(i=0;i<MAXMAL;i++) if (malloc_pointers[i].point==NULL) {
malloc_pointers[i].point=buf;
malloc_pointers[i].size=size;
strncpy(malloc_pointers[i].comm,comm,MAXDESC-1);
return buf;
if ( (i = find_ptr(ptr)) == -1) {
mm_log((0, "Unable to find %p in realloc for %s (%i)\n", ptr, file, line));
exit(3);
}
mm_log((0,"more than %d segments malloced\n",MAXMAL));
exit(255);
return NULL;

if ( (buf = realloc(ptr-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize) == NULL) ) {
mm_log((1,"Unable to reallocate %i bytes at %p for %s (%i)\n", newsize, ptr, file, line));
exit(3);
}

buf = set_entry(i, buf, newsize, file, line);
mm_log((1,"realloc_file_line: slot <%d> %d bytes allocated at %p for %s (%d)\n", i, newsize, buf, file, line));
return buf;
}
*/




static
void
bndcheck(int idx) {
int i;
size_t s = malloc_pointers[idx].size;
unsigned char *pp = malloc_pointers[idx].point;
unsigned char *pp = malloc_pointers[idx].ptr;
if (!pp) {
mm_log((1, "bndcheck: No pointer in slot %d\n", idx));
return;
}

for(i=0;i<UNDRRNVAL;i++)
if (pp[-(1+i)] != PADBYTE)
mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
if (pp[-(1+i)] != PADBYTE)
mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));

for(i=0;i<OVERRNVAL;i++)
for(i=0;i<OVERRNVAL;i++)
if (pp[s+i] != PADBYTE)
mm_log((1,"bndcheck: OVERRUN OF %d bytes detected: slot = %d, point = %p, size = %d\n", i+1, idx, pp, s ));
}
Expand All @@ -134,8 +181,8 @@ void
bndcheck_all() {
int idx;
mm_log((1, "bndcheck_all()\n"));
for(idx=0; idx<MAXMAL; idx++)
if (malloc_pointers[idx].point)
for(idx=0; idx<MAXMAL; idx++)
if (malloc_pointers[idx].ptr)
bndcheck(idx);
}

Expand All @@ -149,10 +196,10 @@ myfree_file_line(void *p, char *file, int line) {
int match = 0;
int i;

for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].point == p) {
for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr == p) {
mm_log((1,"myfree_file_line: pointer %i (%s) freed at %s (%i)\n", i, malloc_pointers[i].comm, file, line));
bndcheck(i);
malloc_pointers[i].point = NULL;
malloc_pointers[i].ptr = NULL;
match++;
}

Expand Down

0 comments on commit fe6163b

Please sign in to comment.