Salome HOME
Modification of the getHeapMemorySize computation.
[modules/med.git] / src / MEDCoupling / MEDCouplingRefCountObject.cxx
index 7f9f493f699e17bb262483694325c10affc72de7..b2374b980bddca7ab85176de6c96def3a7bdd007 100644 (file)
@@ -21,6 +21,8 @@
 #include "MEDCouplingRefCountObject.hxx"
 #include "MED_version.h"
 
+#include <set>
+
 using namespace ParaMEDMEM;
 
 const char *ParaMEDMEM::MEDCouplingVersionStr()
@@ -85,6 +87,31 @@ 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;
+}
+
 bool RefCountObject::decrRef() const
 {
   bool ret=((--_cnt)==0);