annotate data/fileio/MatrixFile.h @ 1192:9884efa1f88a spectrogram-minor-refactor

Fix potential crash in Dense3dModelPeakCache owing to combination of wrong width calculation and wrong handling of width overrun
author Chris Cannam
date Thu, 14 Jul 2016 14:49:04 +0100
parents aa588c391d1a
children
rev   line source
Chris@148 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@148 2
Chris@148 3 /*
Chris@148 4 Sonic Visualiser
Chris@148 5 An audio file viewer and annotation editor.
Chris@148 6 Centre for Digital Music, Queen Mary, University of London.
Chris@537 7 This file copyright 2006-2009 Chris Cannam and QMUL.
Chris@148 8
Chris@148 9 This program is free software; you can redistribute it and/or
Chris@148 10 modify it under the terms of the GNU General Public License as
Chris@148 11 published by the Free Software Foundation; either version 2 of the
Chris@148 12 License, or (at your option) any later version. See the file
Chris@148 13 COPYING included with this distribution for more information.
Chris@148 14 */
Chris@148 15
Chris@1154 16 #ifndef MATRIX_FILE_H
Chris@1154 17 #define MATRIX_FILE_H
Chris@148 18
Chris@148 19 #include "FileReadThread.h"
Chris@148 20
Chris@148 21 #include <sys/types.h>
Chris@148 22 #include <QString>
Chris@148 23 #include <QMutex>
Chris@148 24 #include <map>
Chris@148 25
Chris@148 26 class MatrixFile : public QObject
Chris@148 27 {
Chris@148 28 Q_OBJECT
Chris@148 29
Chris@148 30 public:
Chris@537 31 enum Mode { ReadOnly, WriteOnly };
Chris@148 32
Chris@148 33 /**
Chris@148 34 * Construct a MatrixFile object reading from and/or writing to
Chris@148 35 * the matrix file with the given base name in the application's
Chris@148 36 * temporary directory.
Chris@148 37 *
Chris@148 38 * If mode is ReadOnly, the file must exist and be readable.
Chris@148 39 *
Chris@537 40 * If mode is WriteOnly, the file must not exist.
Chris@148 41 *
Chris@148 42 * cellSize specifies the size in bytes of the object type stored
Chris@148 43 * in the matrix. For example, use cellSize = sizeof(float) for a
Chris@148 44 * matrix of floats. The MatrixFile object doesn't care about the
Chris@148 45 * objects themselves, it just deals with raw data of a given size.
Chris@148 46 *
Chris@537 47 * width and height specify the dimensions of the file. These
Chris@537 48 * cannot be changed after construction.
Chris@537 49 *
Chris@537 50 * MatrixFiles are reference counted by name. When the last
Chris@537 51 * MatrixFile with a given name is destroyed, the file is removed.
Chris@537 52 * These are temporary files; the normal usage is to have one
Chris@537 53 * MatrixFile of WriteOnly type creating the file and then
Chris@537 54 * persisting until all readers are complete.
Chris@537 55 *
Chris@537 56 * MatrixFile has no built-in cache and is not thread-safe. Use a
Chris@537 57 * separate MatrixFile in each thread.
Chris@148 58 */
Chris@929 59 MatrixFile(QString fileBase, Mode mode, int cellSize,
Chris@929 60 int width, int height);
Chris@148 61 virtual ~MatrixFile();
Chris@148 62
Chris@148 63 Mode getMode() const { return m_mode; }
Chris@148 64
Chris@929 65 int getWidth() const { return m_width; }
Chris@929 66 int getHeight() const { return m_height; }
Chris@929 67 int getCellSize() const { return m_cellSize; }
Chris@550 68
Chris@550 69 /**
Chris@550 70 * If this is set true on a write-mode MatrixFile, then the file
Chris@550 71 * will close() itself when all columns have been written.
Chris@550 72 */
Chris@550 73 void setAutoClose(bool a) { m_autoClose = a; }
Chris@550 74
Chris@537 75 void close(); // does not decrement ref count; that happens in dtor
Chris@148 76
Chris@929 77 bool haveSetColumnAt(int x) const;
Chris@929 78 void getColumnAt(int x, void *data); // may throw FileReadFailed
Chris@929 79 void setColumnAt(int x, const void *data);
Chris@148 80
Chris@148 81 protected:
Chris@148 82 int m_fd;
Chris@148 83 Mode m_mode;
Chris@148 84 int m_flags;
Chris@148 85 mode_t m_fmode;
Chris@929 86 int m_cellSize;
Chris@929 87 int m_width;
Chris@929 88 int m_height;
Chris@929 89 int m_headerSize;
Chris@148 90 QString m_fileName;
Chris@148 91
Chris@1154 92 std::vector<bool> m_setColumns; // only populated in writer
Chris@550 93 bool m_autoClose;
Chris@550 94
Chris@554 95 // In reader: if this is >= 0, we can read that column directly
Chris@554 96 // without seeking (and we know that the column exists)
Chris@554 97 mutable int m_readyToReadColumn;
Chris@554 98
Chris@148 99 static std::map<QString, int> m_refcount;
Chris@537 100 static QMutex m_createMutex;
Chris@148 101
Chris@537 102 void initialise();
Chris@929 103 bool seekTo(int col) const;
Chris@148 104 };
Chris@148 105
Chris@148 106 #endif
Chris@148 107