Skip to content

Commit 1393500

Browse files
committed
Improve memory
1 parent 3144954 commit 1393500

File tree

4 files changed

+21
-89
lines changed

4 files changed

+21
-89
lines changed

src/Core/Matlab/matfile.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ void matfile::readtag(matfiledata& md)
742742
if (type >= miEND) throw unknown_type();
743743

744744
md.clear();
745-
md.type(static_cast<mitype>(type));
745+
md.setType(static_cast<mitype>(type));
746746
m_->curptr_.type = static_cast<mitype>(type);
747747
}
748748
catch(...)

src/Core/Matlab/matfiledata.cc

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -61,81 +61,26 @@ using namespace SCIRun::MatlabIO;
6161
matfiledata::matfiledata()
6262
: m_(0), ptr_(0)
6363
{
64-
m_ = new mxdata;
65-
m_->dataptr_ = 0;
66-
m_->owndata_ = false;
67-
m_->bytesize_ = 0;
68-
m_->type_ = miUNKNOWN;
69-
m_->ref_ = 1;
64+
m_.reset(new mxdata);
7065
}
7166

7267
matfiledata::matfiledata(matfiledata::mitype type)
7368
: m_(0) , ptr_(0)
7469
{
75-
m_ = new mxdata;
76-
m_->dataptr_ = 0;
77-
m_->owndata_ = false;
78-
m_->bytesize_ = 0;
70+
m_.reset(new mxdata);
7971
m_->type_ = type;
80-
m_->ref_ = 1;
81-
}
82-
83-
matfiledata::~matfiledata()
84-
{
85-
if (m_ != 0)
86-
{
87-
clearptr();
88-
}
8972
}
9073

9174
void matfiledata::clear()
9275
{
93-
if (m_ == 0)
94-
{
95-
std::cerr << "internal error in clear()\n";
96-
throw internal_error();
97-
}
98-
if ((m_->dataptr_ != 0)&&(m_->owndata_ == true)) delete[] static_cast<char *>(m_->dataptr_);
99-
m_->owndata_ = false;
100-
m_->dataptr_ = 0;
101-
m_->bytesize_ = 0;
102-
m_->type_ = miUNKNOWN;
103-
}
104-
105-
void matfiledata::clearptr()
106-
{
107-
if (m_ == 0) return;
108-
m_->ref_--;
109-
if (m_->ref_ == 0)
110-
{
111-
clear();
112-
delete m_;
113-
}
114-
m_ = 0;
115-
ptr_ = 0;
116-
}
117-
118-
matfiledata::matfiledata(const matfiledata &mfd)
119-
{
120-
m_ = 0;
121-
m_ = mfd.m_;
122-
ptr_ = mfd.ptr_;
123-
m_->ref_++;
124-
}
125-
126-
matfiledata& matfiledata::operator= (const matfiledata &mfd)
127-
{
128-
if (this != &mfd)
76+
if (m_ == 0)
12977
{
130-
clearptr();
131-
m_ = mfd.m_;
132-
ptr_ = mfd.ptr_;
133-
m_->ref_++;
78+
std::cerr << "internal error in clear()\n";
79+
throw internal_error();
13480
}
135-
return *this;
81+
m_.reset(new mxdata);
13682
}
13783

138-
13984
void matfiledata::newdatabuffer(int bytesize,mitype type)
14085
{
14186
if (m_ == 0)
@@ -340,7 +285,7 @@ int matfiledata::putstringarray(const std::vector<std::string>& vec)
340285

341286

342287
// in case of a void just copy the data (no conversion)
343-
void matfiledata::getdata(void *dataptr,int dbytesize)
288+
void matfiledata::getdata(void *dataptr,int dbytesize) const
344289
{
345290
if (databuffer() == 0) return;
346291
if (dataptr == 0) return;

src/Core/Matlab/matfiledata.h

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ DEALINGS IN THE SOFTWARE.
7979
*/
8080

8181
#include <vector>
82+
#include <boost/shared_ptr.hpp>
8283
#include <Core/Matlab/matfilebase.h>
8384
#include <Core/Matlab/share.h>
8485

@@ -110,30 +111,25 @@ namespace SCIRun
110111
private:
111112
struct mxdata
112113
{
114+
mxdata() : dataptr_(0), owndata_(false), bytesize_(0), type_(miUNKNOWN) {}
115+
~mxdata() { if (owndata_) delete[] dataptr_; }
113116
void *dataptr_; // Store the data to put in the matfile
114117
bool owndata_; // Do we own the data
115118
int bytesize_; // Size of the data in bytes
116119
mitype type_; // The type of the data
117-
int ref_; // reference counter
118120
};
119121

120122
// data objects
121123
private:
122-
mxdata *m_;
124+
boost::shared_ptr<mxdata> m_;
123125
void *ptr_;
124-
void clearptr();
125126

126127
// functions
127128
public:
128129
matfiledata();
129-
~matfiledata();
130+
explicit matfiledata(mitype type);
130131

131-
matfiledata(mitype type);
132-
133-
matfiledata(const matfiledata &m); // copy constructor
134-
matfiledata& operator= (const matfiledata &m); // assignment
135-
136-
// clear() will remove any databuffer and emtpty the object
132+
// clear() will remove any databuffer and empty the object
137133
// After calling this function a new buffer can be created
138134
void clear();
139135

@@ -194,13 +190,13 @@ namespace SCIRun
194190
template<class T> void putandcastvalue(T value,int index);
195191

196192
// string functions
197-
// support functions for reading and writing fieldnames and matrixnames
198-
// A struct arrray, can have multiple fields, hence an array of strings
193+
// support functions for reading and writing field names and matrix names
194+
// A struct array, can have multiple fields, hence an array of strings
199195
// needs to be read or written. Matlab stores string arrays differently
200196
// in comparison to a single string, hence the two different types of access
201197
// functions.
202198

203-
std::string getstring();
199+
std::string getstring() const;
204200
void putstring(const std::string& str);
205201
std::vector<std::string> getstringarray(int strlength);
206202
int putstringarray(const std::vector<std::string>&);
@@ -218,19 +214,13 @@ namespace SCIRun
218214
// This function should be used with care as destroying the object
219215
// will free the databuffer. A similar effect has clearing or
220216
// initiating a new buffer.
221-
void *databuffer();
217+
void *databuffer() const;
222218

223219
void ptrset(void *ptr);
224220
void ptrclear();
225221

226222
};
227-
228-
229-
////////////////////////////////////////
230-
////// TEMPLATE FUNCTIONS///////////////
231-
////////////////////////////////////////
232-
233-
223+
234224
template<class T> void matfiledata::getandcast(T *dataptr,int dsize) const
235225
{
236226
// This function copies and casts the data in the matfilebuffer into
@@ -290,7 +280,6 @@ namespace SCIRun
290280
}
291281
}
292282

293-
294283
template<class T> void matfiledata::getandcast(T **dataptr,int dim1, int dim2) const
295284
{
296285
// This function copies and casts the data in the matfilebuffer into
@@ -365,7 +354,6 @@ namespace SCIRun
365354
}
366355
}
367356

368-
369357
template<class T> void matfiledata::getandcast(T ***dataptr,int dim1, int dim2, int dim3) const
370358
{
371359
// This function copies and casts the data in the matfilebuffer into
@@ -443,7 +431,6 @@ namespace SCIRun
443431
}
444432
}
445433

446-
447434
template<class T> void matfiledata::getandcastvector(std::vector<T> &vec) const
448435
{
449436

src/Core/Matlab/matlabfile.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void matlabfile::exportmatlabarray(matlabarray &matrix)
181181
matfiledata matrixdims;
182182
matfiledata matrixname;
183183

184-
matrixheader.type(miMATRIX);
184+
matrixheader.setType(miMATRIX);
185185
writedat(matrixheader);
186186

187187
std::vector<unsigned int> classinfo(2);
@@ -228,7 +228,7 @@ void matlabfile::exportmatlabarray(matlabarray &matrix)
228228

229229
// Create the different tags and data fields
230230

231-
matrixheader.type(miMATRIX);
231+
matrixheader.setType(miMATRIX);
232232
matrixclass.putandcastvector(classinfo,miUINT32);
233233
std::vector<int> mdims = matrix.getdims();
234234
matrixdims.putandcastvector(mdims,miINT32);

0 commit comments

Comments
 (0)