Go Back   EQEmulator Home > EQEmulator Forums > Archives > OpenEQ > OpenEQ::Development

OpenEQ::Development Development discussion for OpenEQ. Do not post for support.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 06:24 PM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3