# HG changeset patch # User Chris Cannam # Date 1168630375 0 # Node ID e0e7f6c5fda9dfb52d48b6d2e838eecc09cedb37 # Parent fb8ddd00f44010430c724c9042313cdf3d7c71d9 * Make FFT data server more resilient when running out of memory diff -r fb8ddd00f440 -r e0e7f6c5fda9 data/fft/FFTDataServer.cpp --- a/data/fft/FFTDataServer.cpp Fri Jan 12 14:49:18 2007 +0000 +++ b/data/fft/FFTDataServer.cpp Fri Jan 12 19:32:55 2007 +0000 @@ -570,6 +570,11 @@ FFTCache *cache = 0; + size_t width = m_cacheWidth; + if (c * m_cacheWidth + width > m_width) { + width = m_width - c * m_cacheWidth; + } + try { if (m_memoryCache) { @@ -588,37 +593,59 @@ FFTFileCache::Rectangular); } - size_t width = m_cacheWidth; - if (c * m_cacheWidth + width > m_width) { - width = m_width - c * m_cacheWidth; - } - cache->resize(width, m_height); cache->reset(); - StorageAdviser::notifyDoneAllocation - (m_memoryCache ? StorageAdviser::MemoryAllocation : - StorageAdviser::DiscAllocation, - width * m_height * - (m_compactCache ? sizeof(uint16_t) : sizeof(float)) / 1024 + 1); + } catch (std::bad_alloc) { - } catch (std::bad_alloc) { - std::cerr << "ERROR: Memory allocation failed in FFTFileCache::resize:" - << " abandoning this cache" << std::endl; + delete cache; + cache = 0; + + if (m_memoryCache) { + + std::cerr << "WARNING: Memory allocation failed when resizing" + << " FFT memory cache no. " << c << " to " << width + << "x" << m_height << " (of total width " << m_width + << "): falling back to disc cache" << std::endl; + + try { + + cache = new FFTFileCache(name, MatrixFile::ReadWrite, + FFTFileCache::Compact); + + cache->resize(width, m_height); + cache->reset(); + + } catch (std::bad_alloc) { + + delete cache; + cache = 0; + } + } + + if (cache) { + std::cerr << "ERROR: Memory allocation failed when resizing" + << " FFT file cache no. " << c << " to " << width + << "x" << m_height << " (of total width " << m_width + << "): abandoning this cache" << std::endl; + } + //!!! Shouldn't be using QtGui here. Need a better way to report this. QMessageBox::critical (0, QApplication::tr("FFT cache resize failed"), QApplication::tr ("Failed to create or resize an FFT model slice.\n" "There may be insufficient memory or disc space to continue.")); - delete cache; - m_caches[c] = 0; - return 0; } + StorageAdviser::notifyDoneAllocation + (m_memoryCache ? StorageAdviser::MemoryAllocation : + StorageAdviser::DiscAllocation, + width * m_height * + (m_compactCache ? sizeof(uint16_t) : sizeof(float)) / 1024 + 1); + m_caches[c] = cache; m_lastUsedCache = c; - return cache; }