BESMemoryManager.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
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
00107
00108
00109
00110
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
00120
00121 set_new_handler( 0 ) ;
00122 throw bad_alloc() ;
00123 }
00124