Salome HOME
Modification of the getHeapMemorySize computation.
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingRefCountObject.cxx
index 443ae2cd8ff584b590f3cd78cd9cea3fec935eb7..49d954f8e217cd25a4692976e8409cdd75b27bd0 100644 (file)
@@ -21,6 +21,9 @@
 #include "MEDCouplingRefCountObject.hxx"
 #include "MED_version.h"
 
+#include <sstream>
+#include <set>
+
 using namespace ParaMEDMEM;
 
 const char *ParaMEDMEM::MEDCouplingVersionStr()
@@ -46,6 +49,28 @@ int ParaMEDMEM::MEDCouplingSizeOfVoidStar()
   return 8*sizeof(std::size_t);
 }
 
+/*!
+ * If true is returned it is a LittleEndian machine.
+ * If false it is a BigEndian machine.
+ * \return the coding mode of integers of the machine.
+ */
+bool ParaMEDMEM::MEDCouplingByteOrder()
+{
+  unsigned int x(1);
+  unsigned char *xc(reinterpret_cast<unsigned char *>(&x));
+  return xc[0]==1;
+}
+
+const char *ParaMEDMEM::MEDCouplingByteOrderStr()
+{
+  static const char LITTLEENDIAN_STR[]="LittleEndian";
+  static const char BIGENDIAN_STR[]="BigEndian";
+  if(MEDCouplingByteOrder())
+    return LITTLEENDIAN_STR;
+  else
+    return BIGENDIAN_STR;
+}
+
 RefCountObject::RefCountObject():_cnt(1)
 {
 }
@@ -63,6 +88,71 @@ RefCountObject& RefCountObject::operator=(const RefCountObject& other)
   return *this;
 }
 
+std::size_t RefCountObject::getHeapMemorySize() const
+{
+  std::size_t ret(getHeapMemorySizeWithoutChildren());
+  std::vector<RefCountObject *> v(getDirectChildren());
+  std::set<RefCountObject *> s1,s2(v.begin(),v.end());
+  while(!s2.empty())
+    {
+      std::set<RefCountObject *> s3;
+      for(std::set<RefCountObject *>::const_iterator it=s2.begin();it!=s2.end();it++)
+        {
+          if(s1.find(*it)==s1.end())
+            {
+              ret+=(*it)->getHeapMemorySizeWithoutChildren();
+              s1.insert(*it);
+              std::vector<RefCountObject *> v2((*it)->getDirectChildren());
+              for(std::vector<RefCountObject *>::const_iterator it2=v2.begin();it2!=v2.end();it2++)
+                if(s1.find(*it2)==s1.end())
+                  s3.insert(*it2);
+            }
+        }
+      s2=s3;
+    }
+  return ret;
+}
+
+std::string RefCountObject::getHeapMemorySizeStr() const
+{
+  static const char *UNITS[4]={"B","kB","MB","GB"};
+  std::size_t m(getHeapMemorySize());
+  std::ostringstream oss; oss.precision(3);
+  std::size_t remain(0);
+  int i(0);
+  for(;i<4;i++)
+    {
+      if(m<1024)
+        {
+          oss << m;
+          if(remain!=0)
+            {
+              std::ostringstream oss2; oss2 << std::fixed << ((double)remain)/1024.;
+              std::string s(oss2.str());
+              s=s.substr(1,4);
+              std::size_t pos(s.find_last_not_of('0'));
+              if(pos==4)
+                oss << s;
+              else
+                oss << s.substr(0,pos+1);
+            }
+          oss << " " << UNITS[i];
+          break;
+        }
+      else
+        {
+          if(i!=3)
+            {
+              remain=(m%1024);
+              m/=1024;
+            }
+        }
+    }
+  if(i==4)
+    oss << m << " " << UNITS[3];
+  return oss.str();
+}
+
 bool RefCountObject::decrRef() const
 {
   bool ret=((--_cnt)==0);