changeset 1401:cc62d7862203

Some bits and bobs to do with handling memory pressure
author Chris Cannam
date Mon, 06 Mar 2017 17:23:46 +0000
parents d2ecf0acc3e2
children aadfb395e933
files data/fileio/CodedAudioFileReader.cpp data/fileio/MP3FileReader.cpp system/System.cpp
diffstat 3 files changed, 26 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/data/fileio/CodedAudioFileReader.cpp	Mon Mar 06 09:35:03 2017 +0000
+++ b/data/fileio/CodedAudioFileReader.cpp	Mon Mar 06 17:23:46 2017 +0000
@@ -478,7 +478,13 @@
 
     case CacheInMemory:
         m_dataLock.lock();
-        m_data.insert(m_data.end(), buffer, buffer + count);
+        try {
+            m_data.insert(m_data.end(), buffer, buffer + count);
+        } catch (const std::bad_alloc &e) {
+            m_data.clear();
+            m_dataLock.unlock();
+            throw e;
+        }
         m_dataLock.unlock();
         break;
     }
--- a/data/fileio/MP3FileReader.cpp	Mon Mar 06 09:35:03 2017 +0000
+++ b/data/fileio/MP3FileReader.cpp	Mon Mar 06 17:23:46 2017 +0000
@@ -101,6 +101,7 @@
         // We need a mysterious MAD_BUFFER_GUARD (== 8) zero bytes at
         // end of input, to ensure libmad decodes the last frame
         // correctly. Otherwise the decoded audio is truncated.
+        SVDEBUG << "file size = " << m_fileSize << ", buffer guard = " << MAD_BUFFER_GUARD << endl;
         m_fileBufferSize = m_fileSize + MAD_BUFFER_GUARD;
         m_fileBuffer = new unsigned char[m_fileBufferSize];
         memset(m_fileBuffer + m_fileSize, 0, MAD_BUFFER_GUARD);
--- a/system/System.cpp	Mon Mar 06 09:35:03 2017 +0000
+++ b/system/System.cpp	Mon Mar 06 17:23:46 2017 +0000
@@ -181,6 +181,24 @@
     if (size > INT_MAX) size = INT_MAX;
     total = ssize_t(size);
 
+    // In 32-bit addressing mode we can't address more than 4Gb.
+    // If the total memory is reported as more than 4Gb, we should
+    // reduce the available amount by the difference between 4Gb
+    // and the total. This won't give us an accurate idea of the
+    // amount of memory available any more, but it should be enough
+    // to prevent us from trying to allocate more for our own use
+    // than can be addressed at all!
+    if (sizeof(void *) < 8) {
+        if (total > 4096) {
+            ssize_t excess = total - 4096;
+            if (available > excess) {
+                available -= excess;
+            } else {
+                available = 0;
+            }
+        }
+    }
+
     return;
 
 #else