[Kst] branches/work/kst/portto4/kst/src/libkst

Peter Kümmel syntheticpp at gmx.net
Mon Oct 8 17:40:19 UTC 2012


SVN commit 1319743 by kuemmel:

when resize fails old pointer remains valid

 M  +0 -7      datacollection.cpp  
 M  +12 -2     datacollection.h  
 M  +3 -1      datamatrix.cpp  
 M  +0 -18     datasource.cpp  
 M  +0 -5      datasource.h  
 M  +8 -6      datavector.cpp  
 M  +7 -9      matrix.cpp  
 M  +2 -2      vector.cpp  


--- branches/work/kst/portto4/kst/src/libkst/datacollection.cpp #1319742:1319743
@@ -33,14 +33,7 @@
 
 static QMutex bigLock;
 
-void *realloc(void *ptr, size_t size) {
-  return ::realloc(ptr, size);
-}
 
-void *malloc(size_t size) {
-  return ::malloc(size);
-}
-
 double Data::AvailableMemory() {
   double one_GB = 1024.0*1024.0*1024.0;
   double available_memory = 0;
--- branches/work/kst/portto4/kst/src/libkst/datacollection.h #1319742:1319743
@@ -61,9 +61,19 @@
     static double AvailableMemory();
 };
 
+
 /** Bad choice for location - maybe move it later */
-KSTCORE_EXPORT void *malloc(size_t size);
-KSTCORE_EXPORT void *realloc(void *ptr, size_t size);
+template<class T>
+bool kstrealloc(T* &ptr, size_t size)
+{
+  // don't overwrite old pointer when resize fails
+  // it doesn't free the old pointer
+  void* newptr = qRealloc(static_cast<void*>(ptr), size);
+  if (!newptr)
+    return false;
+  ptr = static_cast<T*>(newptr);
+  return true;
+}
 
 }
 
--- branches/work/kst/portto4/kst/src/libkst/datamatrix.cpp #1319742:1319743
@@ -248,8 +248,10 @@
     // boxcar filtering is not supported by datasources currently; need to manually average
     if (_aveReadBufferSize < _samplesPerFrameCache*_skip*_samplesPerFrameCache*_skip) {
       _aveReadBufferSize = _samplesPerFrameCache*_skip*_samplesPerFrameCache*_skip;
-      _aveReadBuffer = static_cast<double*>(realloc(_aveReadBuffer, _aveReadBufferSize*sizeof(double)));
+      if (!kstrealloc(_aveReadBuffer, _aveReadBufferSize*sizeof(double))) {
+        qCritical() << "Matrix resize failed";
     }
+    }
     _NS = 0;
     bool first = true;
     matData.z = _aveReadBuffer;
--- branches/work/kst/portto4/kst/src/libkst/datasource.cpp #1319742:1319743
@@ -309,24 +309,6 @@
 }
 
 
-void *DataSource::bufferMalloc(size_t size) {
-  return malloc(size);
-}
-
-
-void DataSource::bufferFree(void *ptr) {
-  return ::free(ptr);
-}
-
-
-void *DataSource::bufferRealloc(void *ptr, size_t size) {
-  return realloc(ptr, size);
-}
-
-
-
-
-
 bool DataSource::isEmpty() const {
   return true;
 }
--- branches/work/kst/portto4/kst/src/libkst/datasource.h #1319742:1319743
@@ -192,11 +192,6 @@
 
     const QString& sourceName() const { return _source; }
 
-    // These malloc calls do not appear to be used.
-    virtual void *bufferMalloc(size_t size);
-    virtual void bufferFree(void *ptr);
-    virtual void *bufferRealloc(void *ptr, size_t size);
-
     /** Returns true if this file is empty */
     virtual bool isEmpty() const;
 
--- branches/work/kst/portto4/kst/src/libkst/datavector.cpp #1319742:1319743
@@ -476,9 +476,10 @@
   if (DoSkip) {
     // reallocate V if necessary
     if (new_nf / Skip != _size) {
-      bool rc = resize(new_nf/Skip);
-      if (!rc) {
-        // FIXME: handle failed resize
+      if (! resize(new_nf/Skip)) {
+        // TODO: Is aborting all we can do?
+        fatalError("Not enough memory for vector data");
+        return;
       }
     }
     // FIXME maybe.
@@ -518,7 +519,9 @@
         /* enlarge AveReadBuf if necessary */
         if (N_AveReadBuf < Skip*SPF) {
           N_AveReadBuf = Skip*SPF;
-          AveReadBuf = static_cast<double*>(realloc(AveReadBuf, N_AveReadBuf*sizeof(double)));
+          if (!kstrealloc(AveReadBuf, N_AveReadBuf*sizeof(double))) {
+            qCritical() << "Vector resize failed";
+          }
           if (!AveReadBuf) {
             // FIXME: handle failed resize
           }
@@ -542,8 +545,7 @@
   } else {
     // reallocate V if necessary
     if ((new_nf - 1)*SPF + 1 != _size) {
-      bool rc = resize((new_nf - 1)*SPF + 1);
-      if (!rc) {
+      if (!resize((new_nf - 1)*SPF + 1)) {
         // TODO: Is aborting all we can do?
         fatalError("Not enough memory for vector data");
         return;
--- branches/work/kst/portto4/kst/src/libkst/matrix.cpp #1319742:1319743
@@ -462,11 +462,11 @@
 bool Matrix::resizeZ(int sz, bool reinit) {
 //   qDebug() << "resizing to: " << sz << endl;
   if (sz >= 1) {
-    _z = static_cast<double*>(Kst::realloc(_z, sz*sizeof(double)));
-    _vectors["z"]->setV(_z, sz);
-    if (!_z) {
+    if (!kstrealloc(_z, sz*sizeof(double))) {
+      qCritical() << "Matrix resize failed";
       return false;
     }
+    _vectors["z"]->setV(_z, sz);
 #ifdef ZERO_MEMORY
     if (reinit && _zSize < sz) {
 #if ZERO_MEMORY == 2
@@ -522,12 +522,11 @@
   int sz = xSize * ySize;
   if (sz > _zSize) {
     // array is getting bigger, so resize before moving
-    _z = static_cast<double*>(Kst::realloc(_z, sz*sizeof(double)));
-    _vectors["z"]->setV(_z, sz);
-    if (!_z) {
+    if (!kstrealloc(_z, sz*sizeof(double))) {
       qCritical() << "Matrix resize failed";
       return false;
     }
+    _vectors["z"]->setV(_z, sz);
   }
 
   if (valid && ySize != _nY && _nY > 0) {
@@ -547,12 +546,11 @@
 
   if (sz < _zSize) {
     // array is getting smaller, so resize after moving
-    _z = static_cast<double*>(Kst::realloc(_z, sz*sizeof(double)));
-    _vectors["z"]->setV(_z, sz);
-    if (!_z) {
+    if (!kstrealloc(_z, sz*sizeof(double))) {
       qCritical() << "Matrix resize failed";
       return false;
     }
+    _vectors["z"]->setV(_z, sz);
   }
 
   if (reinit && _zSize < sz) {
--- branches/work/kst/portto4/kst/src/libkst/vector.cpp #1319742:1319743
@@ -364,8 +364,8 @@
 
 bool Vector::resize(int sz, bool init) {
   if (sz > 0) {
-    _v = static_cast<double*>(realloc(_v, sz*sizeof(double)));
-    if (!_v) {
+    if (!kstrealloc(_v, sz*sizeof(double))){
+       qCritical() << "Vector resize failed";
       return false;
     }
     if (init && _size < sz) {


More information about the Kst mailing list