bes  Updated for version 3.20.6
BESMemoryManager.cc
1 // BESMemoryManager.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <iostream>
34 
35 #if 0
36 using std::endl;
37 using std::set_new_handler;
38 #endif
39 
40 
41 #include "BESMemoryManager.h"
42 
43 #include "BESLog.h"
44 #include "BESDebug.h"
45 #include "BESMemoryGlobalArea.h"
46 #include "BESError.h"
47 
48 using namespace std;
49 
50 BESMemoryGlobalArea* BESMemoryManager::_memory;
51 bool BESMemoryManager::_storage_used(false);
52 new_handler BESMemoryManager::_global_handler;
53 
55 BESMemoryManager::initialize_memory_pool()
56 {
57  static BESMemoryGlobalArea mem ;
58  _memory = &mem ;
59  return _memory ;
60 }
61 
62 void
63 BESMemoryManager::register_global_pool()
64 {
65  _global_handler = set_new_handler( BESMemoryManager::swap_memory ) ;
66 }
67 
68 void
69 BESMemoryManager::swap_memory()
70 {
71  *(BESLog::TheLog()) << "BESMemoryManager::This is just a simulation, here we tell BES to go to persistence state" << endl;
72  set_new_handler( BESMemoryManager::release_global_pool ) ;
73 }
74 
75 bool
76 BESMemoryManager::unregister_global_pool()
77 {
78  if( check_memory_pool() )
79  {
80  set_new_handler( _global_handler ) ;
81  return true ;
82  } else {
83  return false ;
84  }
85 }
86 
87 bool
88 BESMemoryManager::check_memory_pool()
89 {
90  if( _storage_used )
91  {
92  BESDEBUG( "bes", "BES: global pool is used, trying to get it back..." << endl ) ;
93  //Try to regain the memory...
94  if( _memory->reclaim_memory() )
95  {
96  _storage_used = false ;
97  BESDEBUG( "bes", "OK" << endl ) ;
98  return true ;
99  }
100  else
101  {
102  BESDEBUG( "bes", "FAILED" << endl ) ;
103  return false ;
104  }
105  }
106  return true ;
107 }
108 
109 void
110 BESMemoryManager::release_global_pool() throw (bad_alloc)
111 {
112  try {
113  // This is really the final resource for BES since therefore
114  // this method must be second level handler.
115  // It releases enough memory for an exception sequence to be carried.
116  // Without this pool of memory for emergencies we will get really
117  // unexpected behavior from the program.
118  BESDEBUG("bes", "BES Warning: low in memory, " << "releasing global memory pool!" << endl);
119 
120  *(BESLog::TheLog()) << "BES Warning: low in memory, " << "releasing global memory pool!" << endl;
121  }
122  catch (BESError &e) {
123  // At this point, exceptions are pretty moot.
124  cerr << "Exception while trying to release the global memory pool. (" << e.get_verbose_message() << ").";
125  }
126  catch (...) {
127  cerr << "Exception while trying to release the global memory pool.";
128  }
129 
130  _storage_used = true ;
131  _memory->release_memory() ;
132 
133  // Do not let the caller of this memory consume the global pool for
134  // normal stuff this is an emergency.
135  set_new_handler( 0 ) ;
136  throw bad_alloc() ;
137 }
138 
BESMemoryGlobalArea
Definition: BESMemoryGlobalArea.h:44
BESError
Abstract exception class for the BES with basic string message.
Definition: BESError.h:58