BESMemoryManager.cc

Go to the documentation of this file.
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,2005 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 <unistd.h>
00034 #include <iostream>
00035 
00036 using std::endl ;
00037 using std::set_new_handler ;
00038 
00039 #include "BESMemoryManager.h"
00040 
00041 #include "BESLog.h"
00042 #include "BESDebug.h"
00043 #include "BESMemoryGlobalArea.h"
00044 
00045 BESMemoryGlobalArea* BESMemoryManager::_memory;
00046 bool BESMemoryManager::_storage_used(false);
00047 new_handler BESMemoryManager::_global_handler;
00048 
00049 BESMemoryGlobalArea *
00050 BESMemoryManager::initialize_memory_pool()
00051 {
00052     static BESMemoryGlobalArea mem ;
00053     _memory = &mem ;
00054     return _memory ;
00055 }
00056 
00057 void
00058 BESMemoryManager::register_global_pool() 
00059 {
00060     _global_handler = set_new_handler( BESMemoryManager::swap_memory ) ;
00061 }
00062 
00063 void
00064 BESMemoryManager::swap_memory()
00065 {
00066     *(BESLog::TheLog()) << "BESMemoryManager::This is just a simulation, here we tell BES to go to persistence state" << endl;
00067     set_new_handler( BESMemoryManager::release_global_pool ) ;
00068 }
00069 
00070 bool
00071 BESMemoryManager::unregister_global_pool() 
00072 {
00073     if( check_memory_pool() )
00074     {
00075         set_new_handler( _global_handler ) ;
00076         return true ;
00077     } else {
00078         return false ;
00079     }
00080 }
00081 
00082 bool
00083 BESMemoryManager::check_memory_pool()
00084 { 
00085     if( _storage_used )
00086     {
00087         BESDEBUG( "bes", "BES: global pool is used, trying to get it back..." )
00088         //Try to regain the memory...
00089         if( _memory->reclaim_memory() )
00090         {
00091             _storage_used = false ;
00092             BESDEBUG( "bes", "OK" << endl )
00093             return true ;
00094         }
00095         else
00096         {
00097             BESDEBUG( "bes", "FAILED" << endl )
00098             return false ;
00099         }
00100     }
00101     return true ;
00102 }
00103     
00104 void
00105 BESMemoryManager::release_global_pool() throw (bad_alloc)
00106 {
00107     // This is really the final resource for BES since therefore 
00108     // this method must be second level handler.
00109     // It releases enough memory for an exception sequence to be carried.
00110     // Without this pool of memory for emergencies we will get really
00111     // unexpected behavior from the program.
00112     BESDEBUG( "bes", "BES Warning: low in memory, "
00113               << "releasing global memory pool!" << endl )
00114     *(BESLog::TheLog()) << "BES Warning: low in memory, "
00115                         << "releasing global memory pool!"
00116                         << endl;
00117     _storage_used = true ;
00118     _memory->release_memory() ;
00119 
00120     // Do not let the caller of this memory consume the global pool for
00121     // normal stuff this is an emergency.
00122     set_new_handler( 0 ) ;
00123     throw bad_alloc() ;
00124 } 
00125 

Generated on Wed Jan 2 06:00:39 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.4