diff --git a/qpp/prefetch/Source/Core/ReadFile/run.cpp b/qpp/prefetch/Source/Core/ReadFile/run.cpp index 53cf503..facc5c9 100644 --- a/qpp/prefetch/Source/Core/ReadFile/run.cpp +++ b/qpp/prefetch/Source/Core/ReadFile/run.cpp @@ -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) @@ -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(); diff --git a/qpp/prefetch/Source/Core/Thread/Read/init_crtrt.cpp b/qpp/prefetch/Source/Core/Thread/Read/init_crtrt.cpp index 1cd3dc8..9492252 100644 --- a/qpp/prefetch/Source/Core/Thread/Read/init_crtrt.cpp +++ b/qpp/prefetch/Source/Core/Thread/Read/init_crtrt.cpp @@ -14,7 +14,7 @@ void ReadThread::init() sharedReadBufferSize = getReadBufferSize.result; // Confirm useBuffer - useBuffer = sharedReadBufferSize > 0; + bool useBuffer = sharedReadBufferSize > 0; if (useBuffer) { // Convert read buffer size @@ -22,11 +22,19 @@ void ReadThread::init() // 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; } } diff --git a/qpp/prefetch/Source/Core/Thread/Read/read_thread_crtrt.cpp b/qpp/prefetch/Source/Core/Thread/Read/read_thread_crtrt.cpp index 725edb1..d2a4efc 100644 --- a/qpp/prefetch/Source/Core/Thread/Read/read_thread_crtrt.cpp +++ b/qpp/prefetch/Source/Core/Thread/Read/read_thread_crtrt.cpp @@ -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 diff --git a/qpp/prefetch/Source/Core/Thread/Read/read_thread_crtrt.h b/qpp/prefetch/Source/Core/Thread/Read/read_thread_crtrt.h index de060d2..8befccf 100644 --- a/qpp/prefetch/Source/Core/Thread/Read/read_thread_crtrt.h +++ b/qpp/prefetch/Source/Core/Thread/Read/read_thread_crtrt.h @@ -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 @@ -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(); };