[Kst] branches/work/kst/portto4/kst/src/datasources/ascii

Peter Kümmel syntheticpp at gmx.net
Sat Oct 13 12:10:41 UTC 2012


SVN commit 1320298 by kuemmel:

FileBuffer: make the array private

also remove the KST_PREALLOC macro.

 M  +11 -15    asciisource.cpp  
 M  +23 -13    asciisource.h  


--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.cpp #1320297:1320298
@@ -382,17 +382,16 @@
   _byteLength = file.size();
 
   FileBuffer buf;
-  buf._array->resize(buf._array->capacity());
   buf._bufferedS = _rowIndex[_numFrames];
   buf._bufferedN = 0;
   do {
     // Read the tmpbuffer, starting at row_index[_numFrames]
-    buf._array->clear();
-    buf._array->resize(buf._array->capacity());
+    buf.clear();
+    buf.resize(buf.capacity());
 
     //bufstart += bufread;
     buf._bufferedS = _rowIndex[_numFrames]; // always read from the start of a line
-    buf._bufferedN = readFromFile(file, buf, buf._bufferedS, _byteLength - buf._bufferedS, KST_PREALLOC - 1);
+    buf._bufferedN = readFromFile(file, buf, buf._bufferedS, _byteLength - buf._bufferedS, FileBuffer::Prealloc - 1);
 #ifdef KST_DONT_CHECK_INDEX_IN_DEBUG
     const char* bufferData = buf.constData();
     const char* buffer = bufferData;
@@ -424,7 +423,7 @@
       }
     }
 
-  } while (buf._bufferedN == KST_PREALLOC - 1  && read_completely);
+  } while (buf._bufferedN == FileBuffer::Prealloc - 1  && read_completely);
 
   _rowIndex.resize(_numFrames+1);
 
@@ -450,7 +449,7 @@
         _rowIndex[_numFrames] = row_start;
         ++_numFrames;
         if (_numFrames >= _rowIndex.size()) {
-          _rowIndex.resize(_rowIndex.size() + KST_PREALLOC - 1);
+          _rowIndex.resize(_rowIndex.size() + FileBuffer::Prealloc - 1);
         }
         new_data = true;
         row_start = row_offset+i;
@@ -490,14 +489,11 @@
 //-------------------------------------------------------------------------------------------
 void AsciiSource::clearFileBuffer(bool forceDelete)
 {
-  // force deletion of internal allocated memory if any
-  const int memoryOnStack = sizeof(FileBuffer::Array) - sizeof(QVarLengthArray<char, 0>);
-  if (forceDelete || _fileBuffer->_array->capacity() > memoryOnStack) {
-    delete _fileBuffer->_array;
-    _fileBuffer->_array = new FileBuffer::Array;
+  // force deletion of heap allocated memory if any
+  if (forceDelete || _fileBuffer->capacity() > FileBuffer::Prealloc) {
+    delete _fileBuffer;
+    _fileBuffer = new FileBuffer;
   }
-  _fileBuffer->_bufferedS = -10;
-  _fileBuffer->_bufferedN = -10;
 }
 
 //-------------------------------------------------------------------------------------------
@@ -589,9 +585,9 @@
   }
 
 #ifdef KST_DONT_CHECK_INDEX_IN_DEBUG
-  const char* buffer = _fileBuffer->_array->constData();
+  const char* buffer = _fileBuffer->constData();
 #else
-  const QVarLengthArray<char, KST_PREALLOC>& buffer = *_fileBuffer->_array;
+  const FileBuffer::Array buffer = *_fileBuffer->_array;
 #endif
 
   if (_config._columnType == AsciiSourceConfig::Fixed) {
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.h #1320297:1320298
@@ -78,35 +78,45 @@
     Kst::ObjectList<Kst::Object> autoCurves(Kst::ObjectStore& objectStore);
 
   private:
-    // Question: Is this too big or should we use even more: 1MB on the stack?
-#if defined(__ANDROID__) || defined(__QNX__)
-    // Answer: Depends on the system. Some mobile systems, for example, really do not like you allocating 1MB on the stack.
-#define KST_PREALLOC 1 * 1024
+
+    class FileBuffer
+    {
+    public:
+
+      enum SizeOnStack
+      {
+        Prealloc =
+#if defined(__ANDROID__) || defined(__QNX__) // Some mobile systems really do not like you allocating 1MB on the stack.
+        1 * 1024
 #else
-#define KST_PREALLOC 1 * 1024 * 1024
+        1 * 1024 * 1024
 #endif
+      };
 
-    struct FileBuffer
-    {
-      typedef QVarLengthArray<char, KST_PREALLOC> Array;
+      typedef QVarLengthArray<char, Prealloc> Array;
 
-      FileBuffer() : _array(new Array), _bufferedS(-10), _bufferedN(-10) {}
-      ~FileBuffer() { delete _array; }
+      inline FileBuffer() : _array(new Array), _bufferedS(-10), _bufferedN(-10) {}
+      inline ~FileBuffer() { delete _array; }
 
-      Array* _array;
       int _bufferedS;
       int _bufferedN;
 
+      inline void clear() { _array->clear(); }
       inline int size() const { return _array->size(); }
       inline void resize(int size) { _array->resize(size); }
+      inline int  capacity() const { return _array->capacity(); }
       inline char* data() { return _array->data(); }
       inline const char* constData() const { return _array->data(); }
+
       void clearFileBuffer(bool forceDelete = false);
+
+    private:
+      Array* _array;
     };
 
     FileBuffer* _fileBuffer;
 
-    QVarLengthArray<int, KST_PREALLOC> _rowIndex;
+    QVarLengthArray<int, FileBuffer::Prealloc> _rowIndex;
 
     void clearFileBuffer(bool forceDelete = false);
 
@@ -293,7 +303,7 @@
       return 0;
   } else {
     bytesToRead = qMin(bytesToRead, maximalBytes);
-    if (buffer._array->size() <= bytesToRead) {
+    if (buffer.size() <= bytesToRead) {
       if (!resizeBuffer(buffer, bytesToRead + 1))
         return 0;
     }


More information about the Kst mailing list