From e8afb4e33178ef5545640b34d888505f306c19ed Mon Sep 17 00:00:00 2001 From: Andrey Prygunkov Date: Tue, 23 Feb 2016 22:31:27 +0100 Subject: [PATCH] #172: use system objects directly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit instead of dynamic creation, which were used to avoid system includes; that’s become unimportant after using of precompiled headers. --- daemon/util/Thread.cpp | 41 +++++++++++++--------------------- daemon/util/Thread.h | 12 ++++++++-- daemon/util/Util.cpp | 49 +++++++++++++++-------------------------- daemon/util/Util.h | 16 ++++++++------ tests/util/UtilTest.cpp | 10 +++++++++ 5 files changed, 62 insertions(+), 66 deletions(-) diff --git a/daemon/util/Thread.cpp b/daemon/util/Thread.cpp index 55dc2098..266cdc93 100644 --- a/daemon/util/Thread.cpp +++ b/daemon/util/Thread.cpp @@ -34,47 +34,44 @@ std::unique_ptr Thread::m_threadMutex; Mutex::Mutex() { #ifdef WIN32 - m_mutexObj = (CRITICAL_SECTION*)malloc(sizeof(CRITICAL_SECTION)); - InitializeCriticalSection((CRITICAL_SECTION*)m_mutexObj); + InitializeCriticalSection(&m_mutexObj); #else - m_mutexObj = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); - pthread_mutex_init((pthread_mutex_t*)m_mutexObj, nullptr); + pthread_mutex_init(&m_mutexObj, nullptr); #endif } Mutex::~Mutex() { #ifdef WIN32 - DeleteCriticalSection((CRITICAL_SECTION*)m_mutexObj); + DeleteCriticalSection(&m_mutexObj); #else - pthread_mutex_destroy((pthread_mutex_t*)m_mutexObj); + pthread_mutex_destroy(&m_mutexObj); #endif - free(m_mutexObj); } void Mutex::Lock() { #ifdef WIN32 - EnterCriticalSection((CRITICAL_SECTION*)m_mutexObj); + EnterCriticalSection(&m_mutexObj); #ifdef DEBUG // CriticalSections on Windows can be locked many times from the same thread, // but we do not want this and must treat such situations as errors and detect them. - if (((CRITICAL_SECTION*)m_mutexObj)->RecursionCount > 1) + if (m_mutexObj.RecursionCount > 1) { error("Internal program error: inconsistent thread-lock detected"); } #endif #else - pthread_mutex_lock((pthread_mutex_t*)m_mutexObj); + pthread_mutex_lock(&m_mutexObj); #endif } void Mutex::Unlock() { #ifdef WIN32 - LeaveCriticalSection((CRITICAL_SECTION*)m_mutexObj); + LeaveCriticalSection(&m_mutexObj); #else - pthread_mutex_unlock((pthread_mutex_t*)m_mutexObj); + pthread_mutex_unlock(&m_mutexObj); #endif } @@ -90,12 +87,7 @@ Thread::Thread() { debug("Creating Thread"); -#ifdef WIN32 - m_threadObj = nullptr; -#else - m_threadObj = (pthread_t*)malloc(sizeof(pthread_t)); - *((pthread_t*)m_threadObj) = 0; -#endif + m_threadObj = 0; m_running = false; m_stopped = false; m_autoDestroy = false; @@ -104,9 +96,6 @@ Thread::Thread() Thread::~Thread() { debug("Destroying Thread"); -#ifndef WIN32 - free(m_threadObj); -#endif } void Thread::Start() @@ -126,14 +115,14 @@ void Thread::Start() m_threadMutex->Lock(); #ifdef WIN32 - m_threadObj = (HANDLE)_beginthread(Thread::thread_handler, 0, (void *)this); - m_running = m_threadObj != nullptr; + m_threadObj = (HANDLE)_beginthread(Thread::thread_handler, 0, (void*)this); + m_running = m_threadObj != 0; #else pthread_attr_t m_attr; pthread_attr_init(&m_attr); pthread_attr_setdetachstate(&m_attr, PTHREAD_CREATE_DETACHED); pthread_attr_setinheritsched(&m_attr , PTHREAD_INHERIT_SCHED); - m_running = !pthread_create((pthread_t*)m_threadObj, &m_attr, Thread::thread_handler, (void *) this); + m_running = !pthread_create(&m_threadObj, &m_attr, Thread::thread_handler, (void *) this); pthread_attr_destroy(&m_attr); #endif @@ -161,9 +150,9 @@ bool Thread::Kill() m_threadMutex->Lock(); #ifdef WIN32 - bool terminated = TerminateThread((HANDLE)m_threadObj, 0) != 0; + bool terminated = TerminateThread(m_threadObj, 0) != 0; #else - bool terminated = pthread_cancel(*(pthread_t*)m_threadObj) == 0; + bool terminated = pthread_cancel(m_threadObj) == 0; #endif if (terminated) diff --git a/daemon/util/Thread.h b/daemon/util/Thread.h index 08e712bc..6cb986ff 100644 --- a/daemon/util/Thread.h +++ b/daemon/util/Thread.h @@ -30,7 +30,11 @@ class Mutex { private: - void* m_mutexObj; +#ifdef WIN32 + CRITICAL_SECTION m_mutexObj; +#else + pthread_mutex_t m_mutexObj; +#endif public: Mutex(); @@ -44,7 +48,11 @@ class Thread private: static std::unique_ptr m_threadMutex; static int m_threadCount; - void* m_threadObj; +#ifdef WIN32 + HANDLE m_threadObj; +#else + pthread_t m_threadObj; +#endif bool m_running; bool m_stopped; bool m_autoDestroy; diff --git a/daemon/util/Util.cpp b/daemon/util/Util.cpp index 9a7b5c98..4132c205 100644 --- a/daemon/util/Util.cpp +++ b/daemon/util/Util.cpp @@ -1569,15 +1569,15 @@ void URL::ParseUrl() m_valid = true; } + RegEx::RegEx(const char *pattern, int matchBufSize) { #ifdef HAVE_REGEX_H - m_context = malloc(sizeof(regex_t)); - m_valid = regcomp((regex_t*)m_context, pattern, REG_EXTENDED | REG_ICASE | (matchBufSize > 0 ? 0 : REG_NOSUB)) == 0; + m_valid = regcomp(&m_context, pattern, REG_EXTENDED | REG_ICASE | (matchBufSize > 0 ? 0 : REG_NOSUB)) == 0; m_matchBufSize = matchBufSize; if (matchBufSize > 0) { - m_matches = malloc(sizeof(regmatch_t) * matchBufSize); + m_matches = new regmatch_t[matchBufSize]; } else { @@ -1591,16 +1591,15 @@ RegEx::RegEx(const char *pattern, int matchBufSize) RegEx::~RegEx() { #ifdef HAVE_REGEX_H - regfree((regex_t*)m_context); - free(m_context); - free(m_matches); + regfree(&m_context); + delete[] m_matches; #endif } bool RegEx::Match(const char *str) { #ifdef HAVE_REGEX_H - return m_valid ? regexec((regex_t*)m_context, str, m_matchBufSize, (regmatch_t*)m_matches, 0) == 0 : false; + return m_valid ? regexec(&m_context, str, m_matchBufSize, m_matches, 0) == 0 : false; #else return false; #endif @@ -1612,8 +1611,7 @@ int RegEx::GetMatchCount() int count = 0; if (m_matches) { - regmatch_t* matches = (regmatch_t*)m_matches; - while (count < m_matchBufSize && matches[count].rm_so > -1) + while (count < m_matchBufSize && m_matches[count].rm_so > -1) { count++; } @@ -1627,8 +1625,7 @@ int RegEx::GetMatchCount() int RegEx::GetMatchStart(int index) { #ifdef HAVE_REGEX_H - regmatch_t* matches = (regmatch_t*)m_matches; - return matches[index].rm_so; + return m_matches[index].rm_so; #else return nullptr; #endif @@ -1637,8 +1634,7 @@ int RegEx::GetMatchStart(int index) int RegEx::GetMatchLen(int index) { #ifdef HAVE_REGEX_H - regmatch_t* matches = (regmatch_t*)m_matches; - return matches[index].rm_eo - matches[index].rm_so; + return m_matches[index].rm_eo - m_matches[index].rm_so; #else return 0; #endif @@ -1647,39 +1643,30 @@ int RegEx::GetMatchLen(int index) WildMask::WildMask(const char *pattern, bool wantsPositions) { - m_pattern = strdup(pattern); + m_pattern = pattern; m_wantsPositions = wantsPositions; - m_wildStart = nullptr; - m_wildLen = nullptr; - m_arrLen = 0; -} - -WildMask::~WildMask() -{ - free(m_pattern); - free(m_wildStart); - free(m_wildLen); } void WildMask::ExpandArray() { m_wildCount++; - if (m_wildCount > m_arrLen) - { - m_arrLen += 100; - m_wildStart = (int*)realloc(m_wildStart, sizeof(*m_wildStart) * m_arrLen); - m_wildLen = (int*)realloc(m_wildLen, sizeof(*m_wildLen) * m_arrLen); - } + m_wildStart.resize(m_wildCount); + m_wildLen.resize(m_wildCount); } // Based on code from http://bytes.com/topic/c/answers/212179-string-matching // Extended to save positions of matches. bool WildMask::Match(const char* text) { + m_wildCount = 0; + m_wildStart.clear(); + m_wildStart.reserve(100); + m_wildLen.clear(); + m_wildLen.reserve(100); + const char* pat = m_pattern; const char* str = text; const char *spos, *wpos; - m_wildCount = 0; bool qmark = false; bool star = false; diff --git a/daemon/util/Util.h b/daemon/util/Util.h index 30794c9e..d049956d 100644 --- a/daemon/util/Util.h +++ b/daemon/util/Util.h @@ -215,9 +215,11 @@ public: class RegEx { private: - void* m_context; +#ifdef HAVE_REGEX_H + regex_t m_context; + regmatch_t* m_matches; +#endif bool m_valid; - void* m_matches; int m_matchBufSize; public: @@ -233,18 +235,18 @@ public: class WildMask { private: - char* m_pattern; + typedef std::vector IntList; + + CString m_pattern; bool m_wantsPositions; int m_wildCount; - int* m_wildStart; - int* m_wildLen; - int m_arrLen; + IntList m_wildStart; + IntList m_wildLen; void ExpandArray(); public: WildMask(const char* pattern, bool wantsPositions = false); - ~WildMask(); bool Match(const char* text); int GetMatchCount() { return m_wildCount; } int GetMatchStart(int index) { return m_wildStart[index]; } diff --git a/tests/util/UtilTest.cpp b/tests/util/UtilTest.cpp index e689d801..47e78587 100644 --- a/tests/util/UtilTest.cpp +++ b/tests/util/UtilTest.cpp @@ -73,3 +73,13 @@ TEST_CASE("WebUtil: URLEncode", "[Util][Quick]") REQUIRE(strcmp(testString, correctedUrl) == 0); } + +TEST_CASE("Util: WildMask", "[Util][Quick]") +{ + WildMask mask("*.par2", true); + REQUIRE_FALSE(mask.Match("Debian V7 6 64 bit OS.nzb")); + REQUIRE_FALSE(mask.Match("Debian V7 6 64 bit OS.par2.nzb")); + REQUIRE(mask.Match("Debian V7 6 64 bit OS.par2")); + REQUIRE(mask.Match(".par2")); + REQUIRE_FALSE(mask.Match("par2")); +}