comparison base/MatrixFile.cpp @ 97:22494cc28c9f

* Reduce number of allocations and deallocations by keeping a spare buffer around (we were generally deallocating and then immediately allocating again, so it's much better not to have to bother as very large allocations can tie up the system)
author Chris Cannam
date Thu, 04 May 2006 20:17:28 +0000
parents 1aebdc68ec6d
children
comparison
equal deleted inserted replaced
96:1aebdc68ec6d 97:22494cc28c9f
36 m_fd(-1), 36 m_fd(-1),
37 m_mode(mode), 37 m_mode(mode),
38 m_width(0), 38 m_width(0),
39 m_height(0), 39 m_height(0),
40 m_headerSize(2 * sizeof(size_t)), 40 m_headerSize(2 * sizeof(size_t)),
41 m_defaultCacheWidth(256), 41 m_defaultCacheWidth(512),
42 m_prevX(0), 42 m_prevX(0),
43 m_requestToken(-1) 43 m_requestToken(-1),
44 m_spareData(0)
44 { 45 {
45 m_cache.data = 0; 46 m_cache.data = 0;
46 47
47 QDir tempDir(TempDirectory::instance()->getPath()); 48 QDir tempDir(TempDirectory::instance()->getPath());
48 QString fileName(tempDir.filePath(QString("%1.mfc").arg(fileBase))); 49 QString fileName(tempDir.filePath(QString("%1.mfc").arg(fileBase)));
88 m_height = header[1]; 89 m_height = header[1];
89 seekTo(0, 0); 90 seekTo(0, 0);
90 } 91 }
91 92
92 m_fileName = fileName; 93 m_fileName = fileName;
93
94 //!!! why isn't this signal being delivered?
95 connect(&m_readThread, SIGNAL(cancelled(int)),
96 this, SLOT(requestCancelled(int)));
97 94
98 m_readThread.start(); 95 m_readThread.start();
99 96
100 QMutexLocker locker(&m_refcountMutex); 97 QMutexLocker locker(&m_refcountMutex);
101 ++m_refcount[fileName]; 98 ++m_refcount[fileName];
116 } 113 }
117 114
118 m_readThread.finish(); 115 m_readThread.finish();
119 m_readThread.wait(); 116 m_readThread.wait();
120 117
121 if (requestData) delete[] requestData; 118 if (requestData) free(requestData);
122 if (m_cache.data) delete[] m_cache.data; 119 if (m_cache.data) free(m_cache.data);
120 if (m_spareData) free(m_spareData);
123 121
124 if (m_fd >= 0) { 122 if (m_fd >= 0) {
125 if (::close(m_fd) < 0) { 123 if (::close(m_fd) < 0) {
126 ::perror("MatrixFile::~MatrixFile: close failed"); 124 ::perror("MatrixFile::~MatrixFile: close failed");
127 } 125 }
406 m_cache.x = (request.start - m_headerSize) / (m_height * sizeof(float)); 404 m_cache.x = (request.start - m_headerSize) / (m_height * sizeof(float));
407 m_cache.width = request.size / (m_height * sizeof(float)); 405 m_cache.width = request.size / (m_height * sizeof(float));
408 406
409 std::cerr << "actual: " << m_cache.x << ", " << m_cache.width << std::endl; 407 std::cerr << "actual: " << m_cache.x << ", " << m_cache.width << std::endl;
410 408
411 if (m_cache.data) delete[] m_cache.data; 409 if (m_cache.data) {
410 if (m_spareData) free(m_spareData);
411 m_spareData = (char *)m_cache.data;
412 }
412 m_cache.data = (float *)request.data; 413 m_cache.data = (float *)request.data;
413 414
414 m_readThread.done(m_requestToken); 415 m_readThread.done(m_requestToken);
415 m_requestToken = -1; 416 m_requestToken = -1;
416 } 417 }
425 // crude way to avoid leaking the data 426 // crude way to avoid leaking the data
426 while (!m_readThread.isCancelled(m_requestToken)) { 427 while (!m_readThread.isCancelled(m_requestToken)) {
427 usleep(10000); 428 usleep(10000);
428 } 429 }
429 430
430 delete[] ((float *)request.data); 431 if (m_spareData) free(m_spareData);
432 m_spareData = request.data;
431 m_readThread.done(m_requestToken); 433 m_readThread.done(m_requestToken);
432 434
433 m_requestToken = -1; 435 m_requestToken = -1;
434 } 436 }
435 437
436 request.fd = m_fd; 438 request.fd = m_fd;
437 request.mutex = &m_fdMutex; 439 request.mutex = &m_fdMutex;
438 request.start = m_headerSize + rx * m_height * sizeof(float); 440 request.start = m_headerSize + rx * m_height * sizeof(float);
439 request.size = rw * m_height * sizeof(float); 441 request.size = rw * m_height * sizeof(float);
440 request.data = (char *)(new float[rw * m_height]); 442 request.data = (char *)realloc(m_spareData, rw * m_height * sizeof(float));
441 MUNLOCK(request.data, rw * m_height * sizeof(float)); 443 MUNLOCK(request.data, rw * m_height * sizeof(float));
444 m_spareData = 0;
442 445
443 m_requestingX = rx; 446 m_requestingX = rx;
444 m_requestingWidth = rw; 447 m_requestingWidth = rw;
445 448
446 int token = m_readThread.request(request); 449 int token = m_readThread.request(request);
447 std::cerr << "MatrixFile::primeCache: request token is " 450 std::cerr << "MatrixFile::primeCache: request token is "
448 << token << " (x = " << rx << ", w = " << rw << ", left = " << goingLeft << ")" << std::endl; 451 << token << " (x = " << rx << ", w = " << rw << ", left = " << goingLeft << ")" << std::endl;
449 452
450 m_requestToken = token; 453 m_requestToken = token;
451 }
452
453 void
454 MatrixFile::requestCancelled(int token)
455 {
456 std::cerr << "MatrixFile::requestCancelled(" << token << ")" << std::endl;
457
458 FileReadThread::Request request;
459 if (m_readThread.getRequest(token, request)) {
460 delete[] ((float *)request.data);
461 m_readThread.done(token);
462 }
463 } 454 }
464 455
465 bool 456 bool
466 MatrixFile::seekTo(size_t x, size_t y) 457 MatrixFile::seekTo(size_t x, size_t y)
467 { 458 {