Salome HOME
getHeapMemorySize rearch
[modules/med.git] / src / MEDCoupling / MEDCouplingRefCountObject.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDCouplingRefCountObject.hxx"
22 #include "MED_version.h"
23
24 #include <sstream>
25 #include <set>
26
27 using namespace ParaMEDMEM;
28
29 const char *ParaMEDMEM::MEDCouplingVersionStr()
30 {
31   return SALOMEMED_VERSION_STR;
32 }
33
34 int ParaMEDMEM::MEDCouplingVersion()
35 {
36   return SALOMEMED_VERSION;
37 }
38
39 void ParaMEDMEM::MEDCouplingVersionMajMinRel(int& maj, int& minor, int& releas)
40 {
41   int ver=SALOMEMED_VERSION;
42   maj=(ver & 0xFF0000) >> 16;
43   minor=(ver & 0xFF00) >> 8;
44   releas=(ver & 0xFF);
45 }
46
47 int ParaMEDMEM::MEDCouplingSizeOfVoidStar()
48 {
49   return 8*sizeof(std::size_t);
50 }
51
52 /*!
53  * If true is returned it is a LittleEndian machine.
54  * If false it is a BigEndian machine.
55  * \return the coding mode of integers of the machine.
56  */
57 bool ParaMEDMEM::MEDCouplingByteOrder()
58 {
59   unsigned int x(1);
60   unsigned char *xc(reinterpret_cast<unsigned char *>(&x));
61   return xc[0]==1;
62 }
63
64 const char *ParaMEDMEM::MEDCouplingByteOrderStr()
65 {
66   static const char LITTLEENDIAN_STR[]="LittleEndian";
67   static const char BIGENDIAN_STR[]="BigEndian";
68   if(MEDCouplingByteOrder())
69     return LITTLEENDIAN_STR;
70   else
71     return BIGENDIAN_STR;
72 }
73
74 //=
75
76 std::size_t BigMemoryObject::getHeapMemorySize() const
77 {
78   std::size_t ret(getHeapMemorySizeWithoutChildren());
79   std::vector<const BigMemoryObject *> v(getDirectChildren());
80   std::set<const BigMemoryObject *> s1,s2(v.begin(),v.end());
81   while(!s2.empty())
82     {
83       std::set<const BigMemoryObject *> s3;
84       for(std::set<const BigMemoryObject *>::const_iterator it=s2.begin();it!=s2.end();it++)
85         {
86           if(s1.find(*it)==s1.end())
87             {
88               ret+=(*it)->getHeapMemorySizeWithoutChildren();
89               s1.insert(*it);
90               std::vector<const BigMemoryObject *> v2((*it)->getDirectChildren());
91               for(std::vector<const BigMemoryObject *>::const_iterator it2=v2.begin();it2!=v2.end();it2++)
92                 if(s1.find(*it2)==s1.end())
93                   s3.insert(*it2);
94             }
95         }
96       s2=s3;
97     }
98   return ret;
99 }
100
101 std::string BigMemoryObject::getHeapMemorySizeStr() const
102 {
103   static const char *UNITS[4]={"B","kB","MB","GB"};
104   std::size_t m(getHeapMemorySize());
105   std::ostringstream oss; oss.precision(3);
106   std::size_t remain(0);
107   int i(0);
108   for(;i<4;i++)
109     {
110       if(m<1024)
111         {
112           oss << m;
113           if(remain!=0)
114             {
115               std::ostringstream oss2; oss2 << std::fixed << ((double)remain)/1024.;
116               std::string s(oss2.str());
117               s=s.substr(1,4);
118               std::size_t pos(s.find_last_not_of('0'));
119               if(pos==4)
120                 oss << s;
121               else
122                 oss << s.substr(0,pos+1);
123             }
124           oss << " " << UNITS[i];
125           break;
126         }
127       else
128         {
129           if(i!=3)
130             {
131               remain=(m%1024);
132               m/=1024;
133             }
134         }
135     }
136   if(i==4)
137     oss << m << " " << UNITS[3];
138   return oss.str();
139 }
140
141 BigMemoryObject::~BigMemoryObject()
142 {
143 }
144
145 //=
146
147 RefCountObjectOnly::RefCountObjectOnly():_cnt(1)
148 {
149 }
150
151 RefCountObjectOnly::RefCountObjectOnly(const RefCountObjectOnly& other):_cnt(1)
152 {
153 }
154
155 bool RefCountObjectOnly::decrRef() const
156 {
157   bool ret=((--_cnt)==0);
158   if(ret)
159     delete this;
160   return ret;
161 }
162
163 void RefCountObjectOnly::incrRef() const
164 {
165   _cnt++;
166 }
167
168 int RefCountObjectOnly::getRCValue() const
169 {
170   return _cnt;
171 }
172
173 RefCountObjectOnly::~RefCountObjectOnly()
174 {
175 }
176
177 /*!
178  * Do nothing here ! It is not a bug ( I hope :) ) because all subclasses that
179  * copies using operator= should not copy the ref counter of \a other !
180  */
181 RefCountObjectOnly& RefCountObjectOnly::operator=(const RefCountObjectOnly& other)
182 {
183   return *this;
184 }
185
186 //=
187
188 RefCountObject::RefCountObject()
189 {
190 }
191
192 RefCountObject::RefCountObject(const RefCountObject& other):RefCountObjectOnly(other)
193 {
194 }
195
196 RefCountObject::~RefCountObject()
197 {
198 }