#172: use system objects directly

instead of dynamic creation, which were used to avoid system includes;
that’s become unimportant after using of precompiled headers.
This commit is contained in:
Andrey Prygunkov
2016-02-23 22:31:27 +01:00
parent 390481f814
commit e8afb4e331
5 changed files with 62 additions and 66 deletions

View File

@@ -34,47 +34,44 @@ std::unique_ptr<Mutex> 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)

View File

@@ -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<Mutex> 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;

View File

@@ -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;

View File

@@ -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<int> 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]; }

View File

@@ -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"));
}