bes  Updated for version 3.20.10
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 "config.h"
34 
35 #include <iostream>
36 
37 #if 0
38 using std::endl;
39 using std::set_new_handler;
40 #endif
41 
42 
43 #include "BESMemoryManager.h"
44 
45 #include "BESLog.h"
46 #include "BESDebug.h"
47 #include "BESMemoryGlobalArea.h"
48 #include "BESError.h"
49 
50 using namespace std;
51 
52 BESMemoryGlobalArea* BESMemoryManager::_memory;
53 bool BESMemoryManager::_storage_used(false);
54 new_handler BESMemoryManager::_global_handler;
55 
57 BESMemoryManager::initialize_memory_pool()
58 {
59  static BESMemoryGlobalArea mem ;
60  _memory = &mem ;
61  return _memory ;
62 }
63 
64 void
65 BESMemoryManager::register_global_pool()
66 {
67  _global_handler = set_new_handler( BESMemoryManager::swap_memory ) ;
68 }
69 
70 void
71 BESMemoryManager::swap_memory()
72 {
73  INFO_LOG("BESMemoryManager::This is just a simulation, here we tell BES to go to persistence state" << endl);
74  set_new_handler( BESMemoryManager::release_global_pool ) ;
75 }
76 
77 bool
78 BESMemoryManager::unregister_global_pool()
79 {
80  if( check_memory_pool() )
81  {
82  set_new_handler( _global_handler ) ;
83  return true ;
84  } else {
85  return false ;
86  }
87 }
88 
89 bool
90 BESMemoryManager::check_memory_pool()
91 {
92  if( _storage_used )
93  {
94  BESDEBUG( "bes", "BES: global pool is used, trying to get it back..." << endl ) ;
95  //Try to regain the memory...
96  if( _memory->reclaim_memory() )
97  {
98  _storage_used = false ;
99  BESDEBUG( "bes", "OK" << endl ) ;
100  return true ;
101  }
102  else
103  {
104  BESDEBUG( "bes", "FAILED" << endl ) ;
105  return false ;
106  }
107  }
108  return true ;
109 }
110 
111 void
112 BESMemoryManager::release_global_pool() throw (bad_alloc)
113 {
114  try {
115  // This is really the final resource for BES since therefore
116  // this method must be second level handler.
117  // It releases enough memory for an exception sequence to be carried.
118  // Without this pool of memory for emergencies we will get really
119  // unexpected behavior from the program.
120  BESDEBUG("bes", "BES Warning: low in memory, " << "releasing global memory pool!" << endl);
121 
122  INFO_LOG("BES Warning: low in memory, " << "releasing global memory pool!" << endl);
123  }
124  catch (BESError &e) {
125  // At this point, exceptions are pretty moot.
126  cerr << "Exception while trying to release the global memory pool. (" << e.get_verbose_message() << ").";
127  }
128  catch (...) {
129  cerr << "Exception while trying to release the global memory pool.";
130  }
131 
132  _storage_used = true ;
133  _memory->release_memory() ;
134 
135  // Do not let the caller of this memory consume the global pool for
136  // normal stuff this is an emergency.
137  set_new_handler( 0 ) ;
138  throw bad_alloc() ;
139 }
140 
Abstract exception class for the BES with basic string message.
Definition: BESError.h:58