View Single Post
  #1  
Old 09-30-2004, 11:53 AM
daeken_bb
Discordant
 
Join Date: Mar 2003
Location: Chambersburg, PA
Posts: 469
Default Debugging malloc/free/realloc implimentation

I wrote this wrapper to allow for easier debugging of allocation/reallocation/freeing issues, as well as ease of tracking down memory leaks.

To use, simply build safe_mem.c into your application and include safe_mem.h from every file. An easy way to do this is to include safe_mem.h from a file that's included from all your source (e.g. internal type declarations). Everything else is done for you automagickly, and no further modifications must be made to your code.

If the precompiler constant SAFE_MEM is declared then the implimentation is enabled and calls to malloc() and realloc() will print an error if a null pointer is generated from either. If the precompiler constant SAFE_MEM_DEBUG is declared (in addition to SAFE_MEM) then every call to malloc, realloc, and free will print debug output in addition to the null pointer error checking.

Source is available for download at http://home.archshadow.com/~daeken/safe_mem.c and http://home.archshadow.com/~daeken/safe_mem.h, as well as below.

safe_mem.c:
Code:
void *srealloc(void *o, int s, int line_number, char *filename) {
  void *p;
#ifdef SAFE_MEM_DEBUG
  printf("SRealloc: Attempting to reallocate %i bytes to %p. (%s:%i)\n", size, o, filename, line_number);
#endif

  p = (void *) realloc(o, size);

  if(!p)
    printf("SRealloc: Could not reallocate %i bytes to %p! (%s:%i)\n", size, o, filename, line_number);
#ifdef SAFE_MEM_DEBUG
  else
    printf("SRealloc: Successfully reallocated %i bytes to pointer %p (Originally %p). (%s:%i)\n", size, p, o, filename, line_number);
#endif
  return p;
}

void *smalloc(int size, int line_number, char *filename) {
  void *p;
#ifdef SAFE_MEM_DEBUG
  printf("SMalloc: Attempting to allocate %i bytes. (%s:%i)\n", size, filename, line_number);
#endif

  p = (void *) malloc(size);

  if(!p)
    printf("SMalloc: Could not allocate %i bytes! (%s:%i)\n", size, filename, line_number);
#ifdef SAFE_MEM_DEBUG
  else
    printf("SMalloc: Successfully allocated %i bytes to pointer %p. (%s:%i)\n", size, p, filename, line_number);
#endif
  return p;
}

void sfree(void *p, int line_number, char *filename) {
#ifdef SAFE_MEM_DEBUG
  printf("SFree: Freeing pointer %p. (%s:%i)\n", p, filename, line_number);
#endif

  free(p);

#ifdef SAFE_MEM_DEBUG
  printf("SFree: Successfully freed pointer %p. (%s:%i)\n", p, filename, line_number);
#endif
}
safe_mem.h:
Code:
#ifndef __EQCLIENT_SAFE_MEM_H_
#define __EQCLIENT_SAFE_MEM_H_

#ifdef SAFE_MEM

void *srealloc(void *p, int s, int line_number, char *filename);
void *smalloc(int size, int line_number, char *filename);
void sfree(void *p, int line_number, char *filename);

#define realloc(p, bytes) (smalloc((p), (bytes), __LINE__, __FILE__))
#define malloc(bytes) (smalloc((bytes), __LINE__, __FILE__))
#define free(p) (sfree((p), __LINE__, __FILE__))

#endif

#endif
Enjoy.

Happy Hacking,
Lord Daeken M. BlackBlade
(Cody Brocious)
__________________
Keep me unemployed and working on OpenEQ, PM me about donating

Check out my deviantART page at http://daeken.deviantart.com/
Reply With Quote