00001 //<<<<<< INCLUDES >>>>>> 00002 00003 #include "Iguana/Framework/interface/IgOnLowMemoryService.h" 00004 #include "classlib/utils/LowMemoryPool.h" 00005 #include <iostream> 00006 00007 //<<<<<< PRIVATE DEFINES >>>>>> 00008 //<<<<<< PRIVATE CONSTANTS >>>>>> 00009 //<<<<<< PRIVATE TYPES >>>>>> 00010 //<<<<<< PRIVATE VARIABLE DEFINITIONS >>>>>> 00011 //<<<<<< PUBLIC VARIABLE DEFINITIONS >>>>>> 00012 //<<<<<< CLASS STRUCTURE INITIALIZATION >>>>>> 00013 00014 bool IgOnLowMemoryService::s_allocated = false; 00015 00016 //<<<<<< PRIVATE FUNCTION DEFINITIONS >>>>>> 00017 //<<<<<< PUBLIC FUNCTION DEFINITIONS >>>>>> 00018 //<<<<<< MEMBER FUNCTION DEFINITIONS >>>>>> 00019 00020 bool 00021 IgOnLowMemoryService::init (const char *appname) 00022 { 00023 // initialise exception handling 00024 try { throw 1; } catch (...) {} 00025 00026 s_allocated = false; 00027 lat::LowMemoryPool::reset (); 00028 try 00029 { 00030 lat::LowMemoryPool::add (10, lat::CreateCallback (fatal), true); 00031 lat::LowMemoryPool::add (100, lat::CreateCallback (quit)); 00032 lat::LowMemoryPool::add (200, lat::CreateCallback (danger)); 00033 } 00034 catch (std::bad_alloc &) 00035 { 00036 lat::LowMemoryPool::reset (); 00037 std::cerr << appname << ": out of memory -- " 00038 "cannot allocate enough memory\n"; 00039 return false; 00040 } 00041 00042 s_allocated = true; 00043 return true; 00044 } 00045 00046 void 00047 IgOnLowMemoryService::fatal (void) 00048 { 00049 if (! s_allocated) return; 00050 00051 static const char *message = 00052 "*** OUT OF MEMORY. I will try to shut myself down as gracefully\n" 00053 " as the remaining memory permits. Brace yourself.\n"; 00054 00055 std::cerr << message; 00056 std::cerr.flush (); 00057 } 00058 00059 void 00060 IgOnLowMemoryService::quit (void) 00061 { 00062 if (! s_allocated) return; 00063 00064 static const char *message = 00065 "*** WARNING: Running very low on memory. Please try to close or\n" 00066 " disable any unnecessary application functions, and please save\n" 00067 " your work now. To be safe, you should probably restart the program.\n"; 00068 00069 std::cerr << message; 00070 std::cerr.flush (); 00071 } 00072 00073 void 00074 IgOnLowMemoryService::danger (void) 00075 { 00076 if (! s_allocated) return; 00077 00078 static const char *message = 00079 "*** WARNING: Memory seems to be getting low. Please try to disable\n" 00080 " application features you do not need right now. To be safe,\n" 00081 " please save your work now.\n"; 00082 00083 std::cerr << message; 00084 std::cerr.flush (); 00085 } 00086 00087