00001 // BESMemoryManager.cc 00002 00003 // This file is part of bes, A C++ back-end server implementation framework 00004 // for the OPeNDAP Data Access Protocol. 00005 00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research 00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu> 00008 // 00009 // This library is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Lesser General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2.1 of the License, or (at your option) any later version. 00013 // 00014 // This library is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 // Lesser General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU Lesser General Public 00020 // License along with this library; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 // 00023 // You can contact University Corporation for Atmospheric Research at 00024 // 3080 Center Green Drive, Boulder, CO 80301 00025 00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005 00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR. 00028 // 00029 // Authors: 00030 // pwest Patrick West <pwest@ucar.edu> 00031 // jgarcia Jose Garcia <jgarcia@ucar.edu> 00032 00033 #include <iostream> 00034 00035 using std::endl ; 00036 using std::set_new_handler ; 00037 00038 #include "BESMemoryManager.h" 00039 00040 #include "BESLog.h" 00041 #include "BESDebug.h" 00042 #include "BESMemoryGlobalArea.h" 00043 00044 BESMemoryGlobalArea* BESMemoryManager::_memory; 00045 bool BESMemoryManager::_storage_used(false); 00046 new_handler BESMemoryManager::_global_handler; 00047 00048 BESMemoryGlobalArea * 00049 BESMemoryManager::initialize_memory_pool() 00050 { 00051 static BESMemoryGlobalArea mem ; 00052 _memory = &mem ; 00053 return _memory ; 00054 } 00055 00056 void 00057 BESMemoryManager::register_global_pool() 00058 { 00059 _global_handler = set_new_handler( BESMemoryManager::swap_memory ) ; 00060 } 00061 00062 void 00063 BESMemoryManager::swap_memory() 00064 { 00065 *(BESLog::TheLog()) << "BESMemoryManager::This is just a simulation, here we tell BES to go to persistence state" << endl; 00066 set_new_handler( BESMemoryManager::release_global_pool ) ; 00067 } 00068 00069 bool 00070 BESMemoryManager::unregister_global_pool() 00071 { 00072 if( check_memory_pool() ) 00073 { 00074 set_new_handler( _global_handler ) ; 00075 return true ; 00076 } else { 00077 return false ; 00078 } 00079 } 00080 00081 bool 00082 BESMemoryManager::check_memory_pool() 00083 { 00084 if( _storage_used ) 00085 { 00086 BESDEBUG( "bes", "BES: global pool is used, trying to get it back..." << endl ) 00087 //Try to regain the memory... 00088 if( _memory->reclaim_memory() ) 00089 { 00090 _storage_used = false ; 00091 BESDEBUG( "bes", "OK" << endl ) 00092 return true ; 00093 } 00094 else 00095 { 00096 BESDEBUG( "bes", "FAILED" << endl ) 00097 return false ; 00098 } 00099 } 00100 return true ; 00101 } 00102 00103 void 00104 BESMemoryManager::release_global_pool() throw (bad_alloc) 00105 { 00106 // This is really the final resource for BES since therefore 00107 // this method must be second level handler. 00108 // It releases enough memory for an exception sequence to be carried. 00109 // Without this pool of memory for emergencies we will get really 00110 // unexpected behavior from the program. 00111 BESDEBUG( "bes", "BES Warning: low in memory, " 00112 << "releasing global memory pool!" << endl ) 00113 *(BESLog::TheLog()) << "BES Warning: low in memory, " 00114 << "releasing global memory pool!" 00115 << endl; 00116 _storage_used = true ; 00117 _memory->release_memory() ; 00118 00119 // Do not let the caller of this memory consume the global pool for 00120 // normal stuff this is an emergency. 00121 set_new_handler( 0 ) ; 00122 throw bad_alloc() ; 00123 } 00124