Skip to content

Commit

Permalink
Fix delete twice when read buffer is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mhtvsSFrpHdE committed Dec 2, 2022
1 parent 432442d commit 9bee4a6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
4 changes: 2 additions & 2 deletions qpp/prefetch/Source/Core/ReadFile/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool ReadFile::run_runThreadPool(int rescanInterval)
threadPoolTimer.start();

// Allocate RAM
ReadThread::newSharedReadBuffer();
(*ReadThread::newSharedReadBuffer_action)();

// Consume thread queue
for (int i = 0; i < readThreadQueue.size(); ++i)
Expand All @@ -45,7 +45,7 @@ bool ReadFile::run_runThreadPool(int rescanInterval)
readThreadPool->waitForDone();

// Release RAM
ReadThread::deleteSharedReadBuffer();
(*ReadThread::deleteSharedReadBuffer_action)();

// Get code execute time (only measure read, without other action)
auto threadPoolTimeConsumed_miliseconds = threadPoolTimer.elapsed();
Expand Down
10 changes: 9 additions & 1 deletion qpp/prefetch/Source/Core/Thread/Read/init_crtrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@ void ReadThread::init()
sharedReadBufferSize = getReadBufferSize.result;

// Confirm useBuffer
useBuffer = sharedReadBufferSize > 0;
bool useBuffer = sharedReadBufferSize > 0;
if (useBuffer)
{
// Convert read buffer size
sharedReadBufferSize = getReadBufferSize.result * MegabyteMultiplier;

// Read action function pointer
run_read_action = &run_read_WithBuffer;

// Shared buffer action function pointer
newSharedReadBuffer_action = &newSharedReadBuffer_WithBuffer;
deleteSharedReadBuffer_action = &deleteSharedReadBuffer_WithBuffer;
}
// Buffer not enabled
else
{
// Read action function pointer
run_read_action = &run_read_Directly;

// Shared buffer action function pointer
newSharedReadBuffer_action = &newSharedReadBuffer_Directly;
deleteSharedReadBuffer_action = &deleteSharedReadBuffer_Directly;
}
}
23 changes: 16 additions & 7 deletions qpp/prefetch/Source/Core/Thread/Read/read_thread_crtrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@ QMutex ReadThread::pendingDeleteThreadMutex(QMutex::NonRecursive);
char *ReadThread::sharedReadBuffer;
int ReadThread::sharedReadBufferSize;
void (*ReadThread::run_read_action)(QFile *);
bool ReadThread::useBuffer;
void (*ReadThread::newSharedReadBuffer_action)();
void (*ReadThread::deleteSharedReadBuffer_action)();

void ReadThread::newSharedReadBuffer()
void ReadThread::newSharedReadBuffer_WithBuffer()
{
if (useBuffer)
{
sharedReadBuffer = new char[sharedReadBufferSize];
}
sharedReadBuffer = new char[sharedReadBufferSize];
}
void ReadThread::deleteSharedReadBuffer()

void ReadThread::newSharedReadBuffer_Directly()
{
// Do nothing
}

void ReadThread::deleteSharedReadBuffer_WithBuffer()
{
delete sharedReadBuffer;
}

void ReadThread::deleteSharedReadBuffer_Directly()
{
// Do nothing
}

void ReadThread::lockPendingDeleteThread()
{
// Too much of them only turn on if serious problem occur
Expand Down
21 changes: 12 additions & 9 deletions qpp/prefetch/Source/Core/Thread/Read/read_thread_crtrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,9 @@ class ReadThread : public QRunnable
// Any init code
static void init();

// If buffer is enabled, allocate RAM
// Note: New twice = Memory leak
static void newSharedReadBuffer();

// Note: Delete twice is undefined behavior
static void deleteSharedReadBuffer();

// Cache use buffer result
static bool useBuffer;
// Redirect buffer function at runtime
static void (*newSharedReadBuffer_action)();
static void (*deleteSharedReadBuffer_action)();

private:
// Confirm file path is included or not
Expand Down Expand Up @@ -85,4 +79,13 @@ class ReadThread : public QRunnable
// Size of sharedReadBuffer
// ReadBufferSize * 1024 * 1024
static int sharedReadBufferSize;

// If buffer is enabled, allocate RAM
// Note: New twice = Memory leak
static void newSharedReadBuffer_WithBuffer();
static void newSharedReadBuffer_Directly();

// Note: Delete twice is undefined behavior
static void deleteSharedReadBuffer_WithBuffer();
static void deleteSharedReadBuffer_Directly();
};

0 comments on commit 9bee4a6

Please sign in to comment.