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