changeset 93:27d726916ab3

* Remove mmap-based code again. We can be more efficient with read().
author Chris Cannam
date Wed, 03 May 2006 16:47:04 +0000
parents 4988de098b25
children 5b8392e80ed6
files base/MatrixFileCache.cpp base/MatrixFileCache.h
diffstat 2 files changed, 5 insertions(+), 82 deletions(-) [+]
line wrap: on
line diff
--- a/base/MatrixFileCache.cpp	Wed May 03 16:09:16 2006 +0000
+++ b/base/MatrixFileCache.cpp	Wed May 03 16:47:04 2006 +0000
@@ -30,12 +30,6 @@
 #include <QFile>
 #include <QDir>
 
-//#define HAVE_MMAP 1
-
-#ifdef HAVE_MMAP
-#include <sys/mman.h>
-#endif
-
 std::map<QString, int> MatrixFileCache::m_refcount;
 QMutex MatrixFileCache::m_refcountMutex;
 
@@ -54,11 +48,7 @@
     m_rx(0),
     m_rw(0),
     m_userRegion(false),
-    m_region(0),
-    m_mmapped(false),
-    m_mmapSize(0),
-    m_mmapOff(0),
-    m_preferMmap(false)
+    m_region(0)
 {
     // Ensure header size is a multiple of the size of our data (for
     // alignment purposes)
@@ -121,13 +111,7 @@
 MatrixFileCache::~MatrixFileCache()
 {
     if (m_rw > 0) {
-        if (m_mmapped) {
-#ifdef HAVE_MMAP
-            ::munmap(m_region, m_mmapSize);
-#endif
-        } else {
-            delete[] m_region;
-        }
+        delete[] m_region;
     }
 
     if (m_fd >= 0) {
@@ -173,7 +157,7 @@
 
     if (w * h > m_width * m_height) {
 
-#ifdef HAVE_MMAP
+/*!!!
         // If we're going to mmap the file, we need to ensure it's long
         // enough beforehand
         
@@ -193,8 +177,7 @@
                 ::perror("WARNING: MatrixFileCache::resize: write failed");
             }
         }
-#endif
-
+*/
     } else {
         
         if (::ftruncate(m_fd, off) < 0) {
@@ -358,12 +341,6 @@
 
     float *region = m_region;
 
-    if (m_mmapOff > 0) {
-        char *cr = (char *)m_region;
-        cr += m_mmapOff;
-        region = (float *)cr;
-    }
-
     float *ptr = &(region[(x - m_rx) * m_height + y]);
     
 //    std::cerr << "getRegionPtr(" << x << "," << y << "): region is " << m_region << ", returning " << ptr << std::endl;
@@ -390,18 +367,8 @@
     if (m_rw > 0 && x >= m_rx && x + width <= m_rx + m_rw) return true;
 
     if (m_rw > 0) {
-        if (m_mmapped) {
-#ifdef HAVE_MMAP
-            ::munmap(m_region, m_mmapSize);
-            std::cerr << "unmapped " << m_mmapSize << " at " << m_region << std::endl;
-#endif
-        } else {
-            delete[] m_region;
-        }
+        delete[] m_region;
         m_region = 0;
-        m_mmapped = false;
-        m_mmapSize = 0;
-        m_mmapOff = 0;
         m_rw = 0;
     }
 
@@ -409,46 +376,6 @@
         return true;
     }
 
-#ifdef HAVE_MMAP
-
-    if (m_preferMmap) {
-
-        size_t mmapSize = m_height * width * sizeof(float);
-        off_t offset = m_headerSize + (x * m_height) * sizeof(float);
-        int pagesize = getpagesize();
-        off_t aligned = (offset / pagesize) * pagesize;
-        size_t mmapOff = offset - aligned;
-        mmapSize += mmapOff;
-        
-        m_region = (float *)
-            ::mmap(0, mmapSize, PROT_READ, MAP_PRIVATE, m_fd, aligned);
-        
-        if (m_region == MAP_FAILED) {
-            
-            ::perror("Mmap failed");
-            std::cerr << "ERROR: MatrixFileCache::setRegion(" << x << ", "
-                      << width << "): Mmap(0, " << mmapSize
-                      << ", " << PROT_READ << ", " << MAP_SHARED << ", " << m_fd 
-                      << ", " << aligned << ") failed, falling back to "
-                      << "non-mmapping code for this cache" << std::endl;
-            m_preferMmap = false;
-            
-        } else {
-
-            std::cerr << "mmap succeeded (offset " << aligned << ", size " << mmapSize << ", m_mmapOff " << mmapOff << ") = " << m_region << std::endl;
-            
-            m_mmapped = true;
-            m_mmapSize = mmapSize;
-            m_mmapOff = mmapOff;
-            m_rx = x;
-            m_rw = width;
-            if (user) m_userRegion = true;
-            MUNLOCK(m_region, m_mmapSize);
-            return true;
-        }
-    }
-#endif
-
     if (!seekTo(x, 0)) return false;
 
     m_region = new float[width * m_height];
--- a/base/MatrixFileCache.h	Wed May 03 16:09:16 2006 +0000
+++ b/base/MatrixFileCache.h	Wed May 03 16:47:04 2006 +0000
@@ -62,10 +62,6 @@
     mutable size_t  m_rw;
     mutable bool    m_userRegion;
     mutable float  *m_region;
-    mutable bool    m_mmapped;
-    mutable size_t  m_mmapSize;
-    mutable size_t  m_mmapOff;
-    mutable bool    m_preferMmap;
     float *getRegionPtr(size_t x, size_t y) const;
 
     bool autoSetRegion(size_t x) const;