Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

dmalloc.c

Go to the documentation of this file.
00001 /*! \file dmalloc.c
00002     This file implements the debug tools for malloc.
00003  */
00004 
00005 
00006 #include "dmalloc.h"
00007 
00008 #define DMALLOC_ADDRESS_SIZE 100
00009 
00010 /*! \brief Allocate memory (using malloc), and log a message into a log file.
00011     \param size Number of bytes to allocate.
00012     \param message Pointer to a zero terminated string of characters that contains the message to log.
00013     \param file Name of the log file.
00014     \return This function returns the pointer allocated (via malloc), or NULL if the system is running
00015             out of memory.
00016  */
00017 
00018 void* dmalloc(size_t size, char *message, char *file)
00019 {
00020   void *p;
00021   char *iobuff, address[DMALLOC_ADDRESS_SIZE];
00022   int  buff_size;
00023 
00024   p = malloc (size);
00025   if (p == NULL) { return NULL; }
00026 
00027   snprintf (address, DMALLOC_ADDRESS_SIZE-1, "\n%X", (unsigned int)p);
00028 
00029   buff_size = strlen(address)+strlen(": ")+strlen(message)+1;
00030 
00031   iobuff = (char*)malloc(buff_size);
00032   if (iobuff == NULL) { return NULL; }
00033 
00034   my_logger (
00035               file,
00036               iobuff,
00037               buff_size,
00038               "sss",
00039               address,
00040               ": ",
00041               message
00042             );
00043 
00044   free (iobuff);
00045   return p;
00046 }
00047 
00048 /*! \example test_dmalloc.c
00049     This file shows how to use the functions of the dmalloc library.
00050  */
00051 
00052 /*! \brief Free allocated memory (using free), and log a message into a log file.
00053     \param p Pointer to the start of the memory location to free.
00054     \param message Pointer to a zero terminated string of characters that contains the message to log.
00055     \param file Name of the log file.
00056     \return The function returns 0 if success, otherwise the function returns 1 (this means that the system
00057             is running out of memory).
00058  */
00059 
00060 int dfree(void* p, char *message, char *file)
00061 {
00062   char *iobuff, address[DMALLOC_ADDRESS_SIZE];
00063   int  buff_size;
00064 
00065   snprintf (address, DMALLOC_ADDRESS_SIZE-1, "\n%X", (unsigned int)p);
00066   free (p);
00067   buff_size = strlen(address)+strlen(": ")+strlen(message)+1;
00068 
00069   iobuff = (char*)malloc(buff_size);
00070   if (iobuff == NULL) { return 1; }
00071 
00072   my_logger (
00073               file,
00074               iobuff,
00075               buff_size,
00076               "sss",
00077               address,
00078               ": ",
00079               message
00080             );
00081 
00082   free (iobuff);
00083 
00084   return 0;
00085 }
00086 
00087 /*! \example test_dmalloc.c
00088     This file shows how to use the functions of the dmalloc library.
00089  */

Generated on Thu Apr 3 16:23:43 2003 for Common_C_libraries by doxygen1.3-rc1