Memory.h

Go to the documentation of this file.
00001 #ifndef __MEMORY_H__
00002 #define __MEMORY_H__
00003 
00004 /*
00005 
00006 $Header$
00007 
00008 */
00009 
00010 #include <stdlib.h>
00011 
00012 #include <map>
00013 
00014 #ifdef DEBUG_MODE
00015 
00016 #include <sys/time.h>
00017 
00018 #include "Limits.h"
00019 #include "String.h"
00020 
00021 // Messages.
00022 static const char *const MSG_UNKNOWN_MEMORY_CHUNK = "Unknown memory chunk. Address is 0x%X.";
00023 static const char *const MSG_MEMORY_PROC          = "Can not access memory statistics in /proc (%s).";
00024 
00025 #define MEM_CALLOC(nmemb, size) Memory::calloc(nmemb, size, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00026 #define MEM_ALLOC(size)         Memory::alloc(size, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00027 #define MEM_FREE(ptr)           Memory::free(ptr)
00028 #define MEM_REALLOC(ptr, size)  Memory::realloc(ptr, size, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00029 
00030 using namespace std;
00031 
00032 class Memory
00033 {
00034 
00035 public:
00036 
00037   static void *calloc(size_t nmemb, size_t size, const char *file, const char *function, const unsigned int line);
00038   static void *alloc(size_t size, const char *file, const char *function, const unsigned int line);
00039   static void free(void *ptr);
00040   static void *realloc(void *ptr, size_t size, const char *file, const char *function, const unsigned int line);
00041 
00042   static long free();
00043   static int total(long &vsize, long &rss);
00044 
00045   static int list(String &text);
00046 
00047 private:
00048 
00049   typedef struct
00050   {
00051     void *ptr;
00052     size_t size;
00053     struct timeval time;
00054     String file;
00055     String function;
00056     unsigned int line;
00057   } MemoryChunk;
00058 
00059   typedef map<void *, MemoryChunk> MemoryChunkList;
00060 
00061   static void addChunk(void *ptr, const size_t size, const char *file, const char *function, const unsigned int line);
00062   static void removeChunk(void *ptr);
00063 
00064   static MemoryChunkList _chunks;
00065 };
00066 
00067 inline
00068 String MEMORY_COLOR_TITLE(const String &str)
00069 {
00070   return COLOR_STRING(str, LIGHT_CYAN, NONE);
00071 }
00072 
00073 #else
00074 
00075 #define MEM_CALLOC(nmemb, size) calloc(nmemb, size)
00076 #define MEM_ALLOC(size)         malloc(size)
00077 #define MEM_FREE(ptr)           free(ptr)
00078 #define MEM_REALLOC(ptr, size)  realloc(ptr, size)
00079 
00080 #endif
00081 
00082 #endif

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1