From 2da252539ac38df613cd0dadcfe5b81d6eb75b56 Mon Sep 17 00:00:00 2001 From: Xiao YiFang Date: Thu, 16 Jun 2022 20:34:32 +0800 Subject: [PATCH] auto file unmap --- chunkedstorage.cc | 9 ++++++--- ftshelpers.cc | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/chunkedstorage.cc b/chunkedstorage.cc index 3133b651..33de4099 100644 --- a/chunkedstorage.cc +++ b/chunkedstorage.cc @@ -5,6 +5,7 @@ #include #include #include +#include namespace ChunkedStorage { @@ -144,7 +145,6 @@ char * Reader::getBlock( uint32_t address, vector< char > & chunk ) uint32_t uncompressedSize; uint32_t compressedSize; - // = file.read< uint32_t >(); in >> uncompressedSize >> compressedSize; @@ -155,17 +155,20 @@ char * Reader::getBlock( uint32_t address, vector< char > & chunk ) auto chunkDataBytes = file.map( offsets[ chunkIdx ] + 8, compressedSize ); // file.read( &compressedData.front(), compressedData.size() ); + auto autoUnmap = qScopeGuard( + [ & ] { + file.unmap( chunkDataBytes ); + } ); + Q_UNUSED( autoUnmap ) unsigned long decompressedLength = chunk.size(); if( uncompress( (unsigned char *)&chunk.front(), &decompressedLength, chunkDataBytes, compressedSize ) != Z_OK || decompressedLength != chunk.size() ) { - file.unmap( chunkDataBytes ); throw exFailedToDecompressChunk(); } - file.unmap( chunkDataBytes ); } size_t offsetInChunk = address & 0xffFF; diff --git a/ftshelpers.cc b/ftshelpers.cc index 497fede3..85f0abf4 100644 --- a/ftshelpers.cc +++ b/ftshelpers.cc @@ -19,6 +19,8 @@ #include "wildcard.hh" #include #include "base/globalregex.hh" +#include +#include using std::vector; using std::string;