AllocTracker.h

Go to the documentation of this file.
00001 // AllocTracker.h,v 1.5 2006/03/14 06:14:34 jtc Exp
00002 #ifndef ALLOC_TRACKER_H
00003 #define ALLOC_TRACKER_H
00004 #include /**/"ace/pre.h"
00005 
00006 #include "orbsvcs/Notify/notify_export.h"
00007 
00008 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00009 # pragma once
00010 #endif /* ACE_LACKS_PRAGMA_ONCE */
00011 
00012 //#define DEBUG_MEMORY_USE
00013 #if defined(_MSC_VER) && defined (_DEBUG) && defined (DEBUG_MEMORY_USE)
00014 namespace CRT{
00015 
00016 /// \brief dump heap allocation stats
00017 ///
00018 /// This works only on microsoft/windows compilers
00019 /// but it's handy to diagnose memory allocation problems.
00020 /// To use, add the following macro in the scope where you
00021 /// want to check memory usage.
00022 ///   ACE_WIN32_HEAP_MONITOR(name);
00023 ///     where name is a quoted string to label the stats.
00024 /// When the object created by this macro goes out of scope
00025 /// it will write a log message like:
00026 /// (10056|6396) name: New heap blocks: 39; bytes: 19550
00027 ///
00028 /// For non-debug, or non-vc builds, the macro expands to nothing
00029 class CrtHeapDumper
00030 {
00031 public:
00032   CrtHeapDumper(const char * name, bool verbose = false)
00033     : name_ (name)
00034     , verbose_ (verbose)
00035   {
00036     _CrtMemCheckpoint (&before_);
00037   }
00038 
00039   ~CrtHeapDumper()
00040   {
00041     dump();
00042   }
00043   void dump()
00044   {
00045     _CrtMemState after;
00046     _CrtMemCheckpoint (&after);
00047     _CrtMemState diff;
00048     _CrtMemDifference (&diff, &before_, &after);
00049 
00050     ACE_DEBUG ((LM_DEBUG,
00051       ACE_TEXT ("(%P|%t) %s: New heap blocks: %d; bytes: %d\n"),
00052       name_.c_str (),
00053       static_cast<int> (diff.lCounts[_NORMAL_BLOCK]),
00054       static_cast<int> (diff.lSizes[_NORMAL_BLOCK])
00055       ));
00056     if (this->verbose_)
00057     {
00058      _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
00059      _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
00060      _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
00061      _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
00062      _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
00063      _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );
00064      _CrtMemDumpAllObjectsSince (&this->before_);
00065     }
00066   }
00067 
00068 private:
00069   ACE_CString name_;
00070   bool verbose_;
00071   _CrtMemState before_;
00072 };
00073 } //namespace
00074 #define ACE_WIN32_HEAP_MONITOR(name) \
00075   CRT::CrtHeapDumper heap_check___(name); \
00076   ACE_UNUSED_ARG (heap_check___)
00077 #else // _MSC_VER etc
00078  #define ACE_WIN32_HEAP_MONITOR(name)
00079 #endif // _MSC_VER etc
00080 #include /**/"ace/post.h"
00081 #endif // ALLOC_TRACKER_H

Generated on Thu Nov 9 13:24:08 2006 for TAO_CosNotification by doxygen 1.3.6