1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D
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, or (at your option) any later version.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "MEDCouplingRefCountObject.hxx"
22 #include "MED_version.h"
27 using namespace ParaMEDMEM;
29 const char *ParaMEDMEM::MEDCouplingVersionStr()
31 return SALOMEMED_VERSION_STR;
34 int ParaMEDMEM::MEDCouplingVersion()
36 return SALOMEMED_VERSION;
39 void ParaMEDMEM::MEDCouplingVersionMajMinRel(int& maj, int& minor, int& releas)
41 int ver=SALOMEMED_VERSION;
42 maj=(ver & 0xFF0000) >> 16;
43 minor=(ver & 0xFF00) >> 8;
47 int ParaMEDMEM::MEDCouplingSizeOfVoidStar()
49 return 8*sizeof(std::size_t);
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.
57 bool ParaMEDMEM::MEDCouplingByteOrder()
60 unsigned char *xc(reinterpret_cast<unsigned char *>(&x));
64 const char *ParaMEDMEM::MEDCouplingByteOrderStr()
66 static const char LITTLEENDIAN_STR[]="LittleEndian";
67 static const char BIGENDIAN_STR[]="BigEndian";
68 if(MEDCouplingByteOrder())
69 return LITTLEENDIAN_STR;
76 std::size_t BigMemoryObject::getHeapMemorySize() const
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 return ret+GetHeapMemoryOfSet(s1,s2);
85 * This method returns all the progeny of \a this (this is \b not included in returned vector).
86 * All the progeny means all the subobjects (children), subsubobjects (little children), ... of \a this.
87 * The elements in returned array are reported only once even if they appear several times in the progeny of \a this.
89 std::vector<const BigMemoryObject *> BigMemoryObject::getAllTheProgeny() const
91 std::vector<const BigMemoryObject *> s1(getDirectChildren());
92 std::vector<const BigMemoryObject *> ret;
95 ret.insert(ret.end(),s1.begin(),s1.end());
96 std::vector<const BigMemoryObject *> s3;
97 for(std::vector<const BigMemoryObject *>::const_iterator it0=s1.begin();it0!=s1.end();it0++)
99 std::vector<const BigMemoryObject *> s2;
101 s2=(*it0)->getDirectChildren();
102 for(std::vector<const BigMemoryObject *>::const_iterator it1=s2.begin();it1!=s2.end();it1++)
105 if(std::find(ret.begin(),ret.end(),*it1)==ret.end())
115 * This method scan all the progeny of \a this (\a this excluded) to see if \a obj is part of it.
116 * If obj is NULL false is returned.
117 * \sa BigMemoryObject::getAllTheProgeny
119 bool BigMemoryObject::isObjectInTheProgeny(const BigMemoryObject *obj) const
123 std::vector<const BigMemoryObject *> objs(getAllTheProgeny());
124 return std::find(objs.begin(),objs.end(),obj)!=objs.end();
127 std::size_t BigMemoryObject::GetHeapMemorySizeOfObjs(const std::vector<const BigMemoryObject *>& objs)
130 std::set<const BigMemoryObject *> s1,s2;
131 for(std::vector<const BigMemoryObject *>::const_iterator it0=objs.begin();it0!=objs.end();it0++)
134 if(s1.find(*it0)==s1.end())
136 std::vector<const BigMemoryObject *> vTmp((*it0)->getDirectChildren());
137 s2.insert(vTmp.begin(),vTmp.end());
138 ret+=(*it0)->getHeapMemorySizeWithoutChildren();
142 return ret+GetHeapMemoryOfSet(s1,s2);
145 std::size_t BigMemoryObject::GetHeapMemoryOfSet(std::set<const BigMemoryObject *>& s1, std::set<const BigMemoryObject *>& s2)
150 std::set<const BigMemoryObject *> s3;
151 for(std::set<const BigMemoryObject *>::const_iterator it=s2.begin();it!=s2.end();it++)
153 if(s1.find(*it)==s1.end())
155 ret+=(*it)->getHeapMemorySizeWithoutChildren();
157 std::vector<const BigMemoryObject *> v2((*it)->getDirectChildren());
158 for(std::vector<const BigMemoryObject *>::const_iterator it2=v2.begin();it2!=v2.end();it2++)
159 if(s1.find(*it2)==s1.end())
168 std::string BigMemoryObject::getHeapMemorySizeStr() const
170 static const char *UNITS[4]={"B","kB","MB","GB"};
171 std::size_t m(getHeapMemorySize());
172 std::ostringstream oss; oss.precision(3);
173 std::size_t remain(0);
182 std::ostringstream oss2; oss2 << std::fixed << ((double)remain)/1024.;
183 std::string s(oss2.str());
185 std::size_t pos(s.find_last_not_of('0'));
189 oss << s.substr(0,pos+1);
191 oss << " " << UNITS[i];
204 oss << m << " " << UNITS[3];
208 std::vector<const BigMemoryObject *> BigMemoryObject::getDirectChildren() const
210 std::vector<const BigMemoryObject *> ret;
211 std::vector<const BigMemoryObject *> retWithNull(getDirectChildrenWithNull());
212 for(std::vector<const BigMemoryObject *>::const_iterator it=retWithNull.begin();it!=retWithNull.end();it++)
218 BigMemoryObject::~BigMemoryObject()
224 RefCountObjectOnly::RefCountObjectOnly():_cnt(1)
228 RefCountObjectOnly::RefCountObjectOnly(const RefCountObjectOnly& other):_cnt(1)
232 bool RefCountObjectOnly::decrRef() const
234 bool ret=((--_cnt)==0);
240 void RefCountObjectOnly::incrRef() const
245 int RefCountObjectOnly::getRCValue() const
250 RefCountObjectOnly::~RefCountObjectOnly()
255 * Do nothing here ! It is not a bug ( I hope :) ) because all subclasses that
256 * copies using operator= should not copy the ref counter of \a other !
258 RefCountObjectOnly& RefCountObjectOnly::operator=(const RefCountObjectOnly& other)
265 RefCountObject::RefCountObject()
269 RefCountObject::RefCountObject(const RefCountObject& other):RefCountObjectOnly(other)
273 RefCountObject::~RefCountObject()