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

Peter Kümmel syntheticpp at gmx.net
Tue Oct 16 14:47:45 UTC 2012


SVN commit 1320703 by kuemmel:

improve OOM debugging

 M  +2 -0      asciidatareader.h  
 M  +100 -0    asciifilebuffer.cpp  
 M  +6 -5      asciifilebuffer.h  
 M  +0 -8      asciisource.cpp  


--- branches/work/kst/portto4/kst/src/datasources/ascii/asciidatareader.h #1320702:1320703
@@ -16,6 +16,8 @@
 #include "asciifilebuffer.h"
 #include "asciicharactertraits.h"
 
+#include <QVarLengthArray>
+
 class QFile;
 struct LexicalCast;
 class AsciiSourceConfig;
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciifilebuffer.cpp #1320702:1320703
@@ -10,12 +10,112 @@
  *                                                                         *
  ***************************************************************************/
 
+// needed to track memeory usage
+#include "qplatformdefs.h"
+#include <stdlib.h>
+void* fileBufferMalloc(size_t bytes);
+void fileBufferFree(void* ptr);
+#define malloc fileBufferMalloc
+#define qMalloc fileBufferMalloc
+#define free fileBufferFree
+#define qFree fileBufferFree
+#include <QVarLengthArray>
+#undef malloc
+#undef qMalloc
+#undef free
+#undef qFree
+
 #include "asciifilebuffer.h"
 
+#include "debug.h"
+
 #include <QFile>
+#include <QDebug>
+#include <QMap>
 
+static int MB = 1024*1024;
 
+// Simulate out of memory scenario
+//#define KST_TEST_OOM
+#ifdef KST_TEST_OOM
+static const size_t maxMB = 50;
+#else
+static const size_t maxMB = 0;
+#endif
+
+#define KST_MEMORY_DEBUG if(1)
+
 //-------------------------------------------------------------------------------------------
+static QMap<void*, size_t> allocatedMBs;
+
+//-------------------------------------------------------------------------------------------
+static void logMemoryUsed()
+{
+  size_t sum = 0;
+  QMapIterator<void*, size_t> it(allocatedMBs);
+  while (it.hasNext()) {
+    it.next();
+    sum +=  it.value();
+  }
+  Kst::Debug::self()->log(QString("AsciiFileBuffer: %1 MB used").arg(sum/MB), Kst::Debug::Warning);
+  KST_MEMORY_DEBUG qDebug() << "AsciiFileBuffer: " << sum/MB<< "MB used";
+}
+
+//-------------------------------------------------------------------------------------------
+void* fileBufferMalloc(size_t bytes)
+{
+  void* ptr = 0;
+  if (maxMB == 0 || bytes < maxMB*MB) {
+    ptr = malloc(bytes);
+  }
+  if (ptr)  {
+    allocatedMBs[ptr] = bytes;
+    KST_MEMORY_DEBUG qDebug() << "AsciiFileBuffer: " << bytes/MB << "MB allocated";
+    KST_MEMORY_DEBUG logMemoryUsed();
+  } else {
+    Kst::Debug::self()->log(QString("AsciiFileBuffer: failed to allocate %1 MBs").arg(bytes/MB), Kst::Debug::Warning);
+    logMemoryUsed();
+    KST_MEMORY_DEBUG qDebug() << "AsciiFileBuffer: error when allocating " << bytes/MB << "MB";
+  }
+  return ptr;
+}
+
+//-------------------------------------------------------------------------------------------
+void fileBufferFree(void* ptr)
+{
+  if (allocatedMBs.contains(ptr)) {
+    KST_MEMORY_DEBUG qDebug() << "AsciiFileBuffer: " << allocatedMBs[ptr]/MB << "MB freed";
+    allocatedMBs.remove(ptr);
+  }
+  KST_MEMORY_DEBUG logMemoryUsed();
+  free(ptr);
+}
+
+//-------------------------------------------------------------------------------------------
+AsciiFileBuffer::AsciiFileBuffer() : _array(new Array), _begin(-1), _bytesRead(0)
+{
+}
+
+//-------------------------------------------------------------------------------------------
+AsciiFileBuffer::~AsciiFileBuffer()
+{
+  delete _array;
+}
+
+
+//-------------------------------------------------------------------------------------------
+char* AsciiFileBuffer::data()
+{
+  return _array->data();
+}
+
+//-------------------------------------------------------------------------------------------
+const char* const AsciiFileBuffer::constPointer() const
+{
+  return _array->data();
+}
+
+//-------------------------------------------------------------------------------------------
 bool AsciiFileBuffer::resize(int bytes)
 { 
   try {
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciifilebuffer.h #1320702:1320703
@@ -13,7 +13,8 @@
 #ifndef ASCII_FILE_BUFFER_H
 #define ASCII_FILE_BUFFER_H
 
-#include <QVarLengthArray>
+template<class T, int Prealloc>
+class QVarLengthArray;
 
 class QFile;
 
@@ -33,17 +34,17 @@
 
   typedef QVarLengthArray<char, Prealloc> Array;
   
-  inline AsciiFileBuffer() : _array(new Array), _begin(-1), _bytesRead(0) {}
-  inline ~AsciiFileBuffer() { delete _array; }
+  AsciiFileBuffer();
+  ~AsciiFileBuffer();
 
   inline int begin() const { return _begin; }
   inline int bytesRead() const { return _bytesRead; }
 
   void read(QFile&, int start, int numberOfBytes, int maximalBytes = -1);
 
-  inline char* data() { return _array->data(); }
+  char* data();
 
-  inline const char* const constPointer() const { return _array->data(); }
+  const char* const constPointer() const;
   inline const Array& constArray() const{ return *_array; }
 
   bool resize(int size);
--- branches/work/kst/portto4/kst/src/datasources/ascii/asciisource.cpp #1320702:1320703
@@ -29,9 +29,6 @@
 #include <stdlib.h>
 
 
-// simulate out of memory scenario
-//#define KST_TEST_OOM
-
 using namespace Kst;
 
 
@@ -54,11 +51,6 @@
   is(new DataInterfaceAsciiString(*this)),
   iv(new DataInterfaceAsciiVector(*this))
 {
-#ifdef KST_TEST_OOM
-  // eat the giga bytes
-  while (qMalloc(1024*1024*1024) != 0) {}
-#endif
-  
   setInterface(is);
   setInterface(iv);
   


More information about the Kst mailing list