From 7ff996b8f5a886f3d92bf8ae33202f85e93c276e Mon Sep 17 00:00:00 2001 From: eap Date: Tue, 11 Sep 2007 06:12:14 +0000 Subject: [PATCH] PAL16774,PAL16631(SALOME crash after a mesh computation that failed because of lack of memory) - bool CheckMemory() + int CheckMemory() - return size of free memory --- src/SMDS/SMDS_Mesh.cxx | 27 +++++++++++++++++---------- src/SMDS/SMDS_Mesh.hxx | 6 +++--- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/SMDS/SMDS_Mesh.cxx b/src/SMDS/SMDS_Mesh.cxx index 9c4838274..0de7c4af5 100644 --- a/src/SMDS/SMDS_Mesh.cxx +++ b/src/SMDS/SMDS_Mesh.cxx @@ -46,27 +46,34 @@ using namespace std; //================================================================================ /*! * \brief Raise an exception if free memory (ram+swap) too low - * \param doNotRaise - if true, suppres exception, just return bool - * \retval bool - true if there is enough memory + * \param doNotRaise - if true, suppres exception, just return free memory size + * \retval int - amount of available memory in MB or negative number in failure case */ //================================================================================ -bool SMDS_Mesh::CheckMemory(const bool doNotRaise) throw (std::bad_alloc) +int SMDS_Mesh::CheckMemory(const bool doNotRaise) throw (std::bad_alloc) { #ifndef WIN32 struct sysinfo si; int err = sysinfo( &si ); if ( err ) - return true; - - int freeMbyte = ( si.freeram + si.freeswap ) * si.mem_unit / 1024 / 1024; - if ( freeMbyte > 4 ) - return true; + return -1; + + const unsigned long Mbyte = 1024 * 1024; + // compute separately to avoid overflow + int freeMb = + ( si.freeram * si.mem_unit ) / Mbyte + + ( si.freeswap * si.mem_unit ) / Mbyte; + + if ( freeMb > 4 ) + return freeMb; + if ( doNotRaise ) - return false; + return 0; + cout<<"SMDS_Mesh::CheckMemory() throws std::bad_alloc() as freeMb="<