24#ifndef _GEMA_MEMORY_DUMP_H_
25#define _GEMA_MEMORY_DUMP_H_
46#define GM_MEMORY_DUMP_ITEM_NAME_SIZE 80
49#define GM_MEMORY_DUMP_ITEM_DATA_SIZE 16
52#define GM_MEMORY_DUMP_MIN_COMPRESSION (4 * 1024)
63 void addBuffer(
char* buffer, qint64 size);
66 qint64
size()
const {
return _totalSize; }
74 bool isDone()
const {
return _bufIndex == _bufferList.size() && _bufOffset == 0; }
76 bool writeDataToFile (
QFile* file, qint64 size);
77 bool readDataFromFile(
QFile* file, qint64 size);
78 void writeDataToMem (
char* ptr, qint64 size);
79 void readDataFromMem (
char* ptr, qint64 size);
132 virtual bool opStart(
int op) { Q_UNUSED(op);
return true; }
141 virtual bool opEnd(
int op) { Q_UNUSED(op);
return true; }
144 virtual qint64
size()
const = 0;
226 double dumpSizeA = 1.0,
unsigned dumpSizeB = 0);
240 assert(itemId >= 0 && itemId < _dumpItems.size());
241 return _dumpItems[itemId]->_item;
244 int findItem(
QString name,
int typeMask = 0,
int typeValue = 0,
int groupId = -1,
int start = 0);
249 assert(itemId >= 0 && itemId < _dumpItems.size());
250 assert(_dumpItems[itemId]->_data);
251 return _dumpItems[itemId]->_data->_groupId;
257 void setItemActive (
int itemId,
bool active);
258 void setGroupActive(
int groupId,
bool active);
263 assert(itemId >= 0 && itemId < _dumpItems.size());
264 return _dumpItems[itemId]->_active;
267 bool init(
QString dumpFile,
bool overwrite,
bool keep);
273 bool saveItem(
int itemId, qint64 size = 0, qint64 offset = 0);
274 bool loadItem(
int itemId, qint64 size = 0, qint64 offset = 0);
276 bool saveActiveItems();
277 bool loadActiveItems();
283 int adjustGroupId(
int id)
const { assert(
id >= -2);
return id == -1 ? _nextGroupId : (
id == -2 ? _currentGroupId : id); }
286 static int strToMode(
const QString& str);
338 static_assert(
sizeof(
FileItemHeader) % 16 == 0,
"Unexpected structure size");
381 bool fileInit(
QString dumpFile,
bool overwrite,
bool keep);
383 bool fileInitExisting();
388 bool fileSaveItem (
DumpItem* item,
bool update, qint64 size = 0, qint64 offset = 0);
389 bool memorySaveItem(
DumpItem* item, qint64 size = 0, qint64 offset = 0);
391 bool fileLoadItem (
DumpItem* item, qint64 size = 0, qint64 offset = 0);
392 bool memoryLoadItem(
DumpItem* item, qint64 size = 0, qint64 offset = 0);
394 bool writeOrCheckControlHeader(
bool write, qint64* infoBlockPos);
395 bool saveDataBlock (qint32 blockId, qint64 blockSize,
const char* buffer, qint64 size);
402 bool loadChunkData (
char* buffer, qint64 offset, qint64 size);
404 bool updateFileInfoHeader();
407 bool readData (
char* buffer, qint64 size);
409 bool writeData(
const char* buffer, qint64 size);
412 qint64 blockSize(qint64 dataSize,
bool grow);
414#if defined ENABLE_TESTS
416 friend Q_DECL_EXPORT
void TEST_GmMemoryDumpCheck(
GmMemoryDump*,
bool);
417 void checkAndPrintFileStructure(
bool print,
bool saved);
Class representing a category with multiple logging levels.
Definition gmLog.h:58
A helper class that can store a list of buffers and their sizes, allowing for GmMemoryDumpItem instan...
Definition gmMemoryDump.h:59
qint64 _bufOffset
The current buffer offset for the next operation.
Definition gmMemoryDump.h:84
bool isDone() const
Check that all data data on the buffer has been traversed by read or write functions.
Definition gmMemoryDump.h:74
QVarLengthArray< QPair< char *, qint64 >, 5 > _bufferList
A list with buffers and their sizes.
Definition gmMemoryDump.h:81
int _bufIndex
The current buffer for the next operation.
Definition gmMemoryDump.h:83
qint64 size() const
Returns the sum of the sizes of the stored buffers.
Definition gmMemoryDump.h:66
qint64 _totalSize
The sum of the buffers sizes on _bufferList.
Definition gmMemoryDump.h:82
Class storing a memory dump containing several dump items.
Definition gmMemoryDump.h:222
int adjustGroupId(int id) const
Returns the adjusted group id, translating the -1 (next group) or -2 (current group) options for addD...
Definition gmMemoryDump.h:283
bool _keepFile
Should we keep dump files after the object is destroyed?
Definition gmMemoryDump.h:426
QVector< DumpItem * > _dumpItems
A list with the set of dump items + their controll info.
Definition gmMemoryDump.h:434
int _minCompressionSize
The minumum size a buffer must have to be compressed.
Definition gmMemoryDump.h:422
bool _printStructure
Should we print the configured item structure?
Definition gmMemoryDump.h:427
bool isItemActive(int itemId)
Returns true if the given item is active.
Definition gmMemoryDump.h:261
qint64 _firstChunkPos
The position in the dump file for the first dump item chunk.
Definition gmMemoryDump.h:432
QFile * _file
The dump file (if the current mode so requires)
Definition gmMemoryDump.h:429
double _dumpSizeA
Multiplier for extra grow size added to a data item size.
Definition gmMemoryDump.h:423
int _currentGroupId
The id of the last added group id.
Definition gmMemoryDump.h:435
qint32 _lastChunkBlockId
The block Id for the last chunk stored in the file.
Definition gmMemoryDump.h:433
unsigned _dumpSizeB
Offset for extra grow size added to a data item size.
Definition gmMemoryDump.h:424
const GmLogCategory & _logger
Basic logger object for dump messages.
Definition gmMemoryDump.h:438
GmMemoryDumpItem * item(int itemId) const
Returns the dump item, given the id returned by addDumpItem()
Definition gmMemoryDump.h:238
QString _fileName
Dump file name.
Definition gmMemoryDump.h:425
int numItems() const
Returns the number of added dump items.
Definition gmMemoryDump.h:255
void setPrintStructure(bool mode)
Enables or disables printing the dump item structure, both when addDumpItem() is called and when a fi...
Definition gmMemoryDump.h:233
qint64 _fileInfoPos
The position on the file for the Info block header.
Definition gmMemoryDump.h:431
int _compression
Compression level for dumped data. From -1 to 9. 0 = no, 9 = max, -1 = zlib default.
Definition gmMemoryDump.h:421
int itemGroup(int itemId) const
Returns the group id of the given item.
Definition gmMemoryDump.h:247
GmMemoryDumpMode _mode
Dump mode.
Definition gmMemoryDump.h:420
int _nextGroupId
The id of the next item group id (can be different from _currentGroupId+1 if the user explicitly gave...
Definition gmMemoryDump.h:436
bool _init
Check if init was called successfully.
Definition gmMemoryDump.h:430
Interface for an object that can be dumped and undumped to either file or memory by GmMemoryDump.
Definition gmMemoryDump.h:100
virtual void getDataBuffer(GmMemoryBufferList &bufferList, qint64 size=0, qint64 offset=0)=0
Fills the provided bufferList object with the set of buffers that together define the dump item data.
virtual QString itemName() const =0
Returns the item name. Should be a string with at most GM_MEMORY_DUMP_ITEM_NAME_SIZE characters.
virtual bool fixedSize() const =0
Is this dump item a fixed size item that probably won't grow in the future? If so,...
virtual bool fillDataHeader(char *header)=0
Function called together with getDataBuffer() to provide a "header information" with fixed size (equa...
virtual int itemType() const =0
Returns the item type.
virtual bool partialOp() const =0
Does this item supports partial load / save operations?
virtual bool opStart(int op)
Function called by GmMemoryDump::load(), GmMemoryDump::save(), GmMemoryDump::loadItem() and GmMemoryD...
Definition gmMemoryDump.h:132
virtual void bufferReleased(int op, bool ok)
Function called once after a call to getDataBuffer() / setDataBuffer() to inform the item data that t...
Definition gmMemoryDump.h:217
virtual bool canCompress() const =0
Should this item be compressed if the MemoryDump object is configured with compression?
virtual bool opEnd(int op)
Function called by GmMemoryDump::load(), GmMemoryDump::save(), GmMemoryDump::loadItem() and GmMemoryD...
Definition gmMemoryDump.h:141
virtual qint64 size() const =0
Returns the current item size (in bytes)
virtual bool setDataBuffer(GmMemoryBufferList &bufferList, const char *header, qint64 size, qint64 offset=0)=0
Fills the provided bufferList object with the set of buffers that should be used to update the dump i...
virtual ~GmMemoryDumpItem()
Virtual destructor.
Definition gmMemoryDump.h:103
Declaration of useful configuration definitions for the Core library.
#define GMC_API_EXPORT
Macro for controlling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_L...
Definition gmCoreConfig.h:35
Declaration of support functions and macros for information logging.
#define GM_MEMORY_DUMP_ITEM_NAME_SIZE
The maximum size for a dump item name.
Definition gmMemoryDump.h:46
#define GM_MEMORY_DUMP_ITEM_DATA_SIZE
The size of the per item fixed item data buffer.
Definition gmMemoryDump.h:49
GmMemoryDumpMode
Supported memory dump modes.
Definition gmMemoryDump.h:38
@ GM_DUMP_RESTORE
Dumped file is opend for restore only. No save operations are allowed.
Definition gmMemoryDump.h:42
@ GM_DUMP_AUTO
Auto mode. Dumped memory is saved either on file or on memory depending on memory usage.
Definition gmMemoryDump.h:39
@ GM_DUMP_MEMORY
Dumped memory is saved on memory.
Definition gmMemoryDump.h:40
@ GM_DUMP_FILE
Dumped memory is saved to a file.
Definition gmMemoryDump.h:41
#define GM_MEMORY_DUMP_MIN_COMPRESSION
The default minimum buffer size in bytes for compression to be worthy (must be > 0)
Definition gmMemoryDump.h:52
The structure adding controll information to a GmMemoryDumpItem.
Definition gmMemoryDump.h:369
~DumpItem()
Dump item destructor. Deletes the stored item data object along with other control data.
Definition gmMemoryDump.h:374
GmMemoryDumpItem * _item
The dump item.
Definition gmMemoryDump.h:376
DumpItem(GmMemoryDumpItem *item, qint32 id, qint16 groupId)
Dump item constructor.
Definition gmMemoryDump.h:371
ItemHeader * _data
The item control header.
Definition gmMemoryDump.h:377
bool _active
Is this item active or not? Used by saveActiveItems() / loadActiveItems()
Definition gmMemoryDump.h:378