# HG changeset patch # User Chris Cannam # Date 1488821026 0 # Node ID cc62d7862203fb02da016fc0df556872dc54b190 # Parent d2ecf0acc3e29a7d81430fb728f1af5a9be52d9b Some bits and bobs to do with handling memory pressure diff -r d2ecf0acc3e2 -r cc62d7862203 data/fileio/CodedAudioFileReader.cpp --- 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; } diff -r d2ecf0acc3e2 -r cc62d7862203 data/fileio/MP3FileReader.cpp --- 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); diff -r d2ecf0acc3e2 -r cc62d7862203 system/System.cpp --- 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