diff options
Diffstat (limited to 'src/OSSupport')
-rw-r--r-- | src/OSSupport/CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/OSSupport/Event.cpp | 4 | ||||
-rw-r--r-- | src/OSSupport/File.cpp | 30 | ||||
-rw-r--r-- | src/OSSupport/File.h | 8 | ||||
-rw-r--r-- | src/OSSupport/GZipFile.h | 2 | ||||
-rw-r--r-- | src/OSSupport/Semaphore.cpp | 107 | ||||
-rw-r--r-- | src/OSSupport/Semaphore.h | 17 | ||||
-rw-r--r-- | src/OSSupport/StackTrace.cpp | 9 |
8 files changed, 40 insertions, 143 deletions
diff --git a/src/OSSupport/CMakeLists.txt b/src/OSSupport/CMakeLists.txt index 0d3c9a63e..df47394ae 100644 --- a/src/OSSupport/CMakeLists.txt +++ b/src/OSSupport/CMakeLists.txt @@ -15,7 +15,6 @@ SET (SRCS IsThread.cpp NetworkInterfaceEnum.cpp NetworkSingleton.cpp - Semaphore.cpp ServerHandleImpl.cpp StackTrace.cpp TCPLinkImpl.cpp @@ -34,13 +33,16 @@ SET (HDRS Network.h NetworkSingleton.h Queue.h - Semaphore.h ServerHandleImpl.h StackTrace.h TCPLinkImpl.h UDPEndpointImpl.h ) +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + add_flags_cxx("-Wno-error=global-constructors -Wno-error=old-style-cast") +endif() + if(NOT MSVC) add_library(OSSupport ${SRCS} ${HDRS}) diff --git a/src/OSSupport/Event.cpp b/src/OSSupport/Event.cpp index 760e536a1..38144ead3 100644 --- a/src/OSSupport/Event.cpp +++ b/src/OSSupport/Event.cpp @@ -1,8 +1,8 @@ // Event.cpp -// Implements the cEvent object representing an OS-specific synchronization primitive that can be waited-for -// Implemented as an Event on Win and as a 1-semaphore on *nix +// Interfaces to the cEvent object representing a synchronization primitive that can be waited-for +// Implemented using C++11 condition variable and mutex #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules diff --git a/src/OSSupport/File.cpp b/src/OSSupport/File.cpp index 43105b230..6327b3505 100644 --- a/src/OSSupport/File.cpp +++ b/src/OSSupport/File.cpp @@ -180,7 +180,7 @@ int cFile::Write(const void * iBuffer, size_t iNumBytes) -int cFile::Seek (int iPosition) +long cFile::Seek (int iPosition) { ASSERT(IsOpen()); @@ -193,7 +193,7 @@ int cFile::Seek (int iPosition) { return -1; } - return (int)ftell(m_File); + return ftell(m_File); } @@ -201,7 +201,7 @@ int cFile::Seek (int iPosition) -int cFile::Tell (void) const +long cFile::Tell (void) const { ASSERT(IsOpen()); @@ -210,14 +210,14 @@ int cFile::Tell (void) const return -1; } - return (int)ftell(m_File); + return ftell(m_File); } -int cFile::GetSize(void) const +long cFile::GetSize(void) const { ASSERT(IsOpen()); @@ -226,7 +226,7 @@ int cFile::GetSize(void) const return -1; } - int CurPos = Tell(); + long CurPos = Tell(); if (CurPos < 0) { return -1; @@ -235,7 +235,7 @@ int cFile::GetSize(void) const { return -1; } - int res = Tell(); + long res = Tell(); if (fseek(m_File, (long)CurPos, SEEK_SET) != 0) { return -1; @@ -256,7 +256,19 @@ int cFile::ReadRestOfFile(AString & a_Contents) return -1; } - size_t DataSize = GetSize() - Tell(); + long TotalSize = GetSize(); + if (TotalSize < 0) + { + return -1; + } + + long Position = Tell(); + if (Position < 0) + { + return -1; + } + + auto DataSize = static_cast<size_t>(TotalSize - Position); // HACK: This depends on the internal knowledge that AString's data() function returns the internal buffer directly a_Contents.assign(DataSize, '\0'); @@ -349,7 +361,7 @@ bool cFile::IsFile(const AString & a_Path) -int cFile::GetSize(const AString & a_FileName) +long cFile::GetSize(const AString & a_FileName) { struct stat st; if (stat(a_FileName.c_str(), &st) == 0) diff --git a/src/OSSupport/File.h b/src/OSSupport/File.h index 1b5e71a17..6281d1494 100644 --- a/src/OSSupport/File.h +++ b/src/OSSupport/File.h @@ -87,13 +87,13 @@ public: int Write(const void * iBuffer, size_t iNumBytes); /** Seeks to iPosition bytes from file start, returns old position or -1 for failure; asserts if not open */ - int Seek (int iPosition); + long Seek (int iPosition); /** Returns the current position (bytes from file start) or -1 for failure; asserts if not open */ - int Tell (void) const; + long Tell (void) const; /** Returns the size of file, in bytes, or -1 for failure; asserts if not open */ - int GetSize(void) const; + long GetSize(void) const; /** Reads the file from current position till EOF into an AString; returns the number of bytes read or -1 for error */ int ReadRestOfFile(AString & a_Contents); @@ -119,7 +119,7 @@ public: static bool IsFile(const AString & a_Path); /** Returns the size of the file, or a negative number on error */ - static int GetSize(const AString & a_FileName); + static long GetSize(const AString & a_FileName); /** Creates a new folder with the specified name. Returns true if successful. Path may be relative or absolute */ static bool CreateFolder(const AString & a_FolderPath); diff --git a/src/OSSupport/GZipFile.h b/src/OSSupport/GZipFile.h index dfb4e8c31..286c98aff 100644 --- a/src/OSSupport/GZipFile.h +++ b/src/OSSupport/GZipFile.h @@ -37,7 +37,7 @@ public: int ReadRestOfFile(AString & a_Contents); /// Writes a_Contents into file, compressing it along the way. Returns true if successful. Multiple writes are supported. - bool Write(const AString & a_Contents) { return Write(a_Contents.data(), (int)(a_Contents.size())); } + bool Write(const AString & a_Contents) { return Write(a_Contents.data(), static_cast<int>(a_Contents.size())); } bool Write(const char * a_Data, int a_Size); diff --git a/src/OSSupport/Semaphore.cpp b/src/OSSupport/Semaphore.cpp deleted file mode 100644 index 6a2d57901..000000000 --- a/src/OSSupport/Semaphore.cpp +++ /dev/null @@ -1,107 +0,0 @@ - -#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules - - - - - -cSemaphore::cSemaphore( unsigned int a_MaxCount, unsigned int a_InitialCount /* = 0 */) -#ifndef _WIN32 - : m_bNamed( false) -#endif -{ -#ifndef _WIN32 - (void)a_MaxCount; - m_Handle = new sem_t; - if (sem_init( (sem_t*)m_Handle, 0, 0)) - { - LOG("WARNING cSemaphore: Could not create unnamed semaphore, fallback to named."); - delete (sem_t*)m_Handle; // named semaphores return their own address - m_bNamed = true; - - AString Name; - Printf(Name, "cSemaphore%p", this); - m_Handle = sem_open(Name.c_str(), O_CREAT, 777, a_InitialCount); - if (m_Handle == SEM_FAILED) - { - LOG("ERROR: Could not create Semaphore. (%i)", errno); - } - else - { - if (sem_unlink(Name.c_str()) != 0) - { - LOG("ERROR: Could not unlink cSemaphore. (%i)", errno); - } - } - } -#else - m_Handle = CreateSemaphore( - nullptr, // security attribute - a_InitialCount, // initial count - a_MaxCount, // maximum count - 0 // name (optional) - ); -#endif -} - - - - - -cSemaphore::~cSemaphore() -{ -#ifdef _WIN32 - CloseHandle( m_Handle); -#else - if (m_bNamed) - { - if (sem_close( (sem_t*)m_Handle) != 0) - { - LOG("ERROR: Could not close cSemaphore. (%i)", errno); - } - } - else - { - sem_destroy( (sem_t*)m_Handle); - delete (sem_t*)m_Handle; - } - m_Handle = 0; - -#endif -} - - - - - -void cSemaphore::Wait() -{ -#ifndef _WIN32 - if (sem_wait( (sem_t*)m_Handle) != 0) - { - LOG("ERROR: Could not wait for cSemaphore. (%i)", errno); - } -#else - WaitForSingleObject( m_Handle, INFINITE); -#endif -} - - - - - -void cSemaphore::Signal() -{ -#ifndef _WIN32 - if (sem_post( (sem_t*)m_Handle) != 0) - { - LOG("ERROR: Could not signal cSemaphore. (%i)", errno); - } -#else - ReleaseSemaphore( m_Handle, 1, nullptr); -#endif -} - - - - diff --git a/src/OSSupport/Semaphore.h b/src/OSSupport/Semaphore.h deleted file mode 100644 index 57fa4bdb2..000000000 --- a/src/OSSupport/Semaphore.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -class cSemaphore -{ -public: - cSemaphore( unsigned int a_MaxCount, unsigned int a_InitialCount = 0); - ~cSemaphore(); - - void Wait(); - void Signal(); -private: - void * m_Handle; // HANDLE pointer - -#ifndef _WIN32 - bool m_bNamed; -#endif -}; diff --git a/src/OSSupport/StackTrace.cpp b/src/OSSupport/StackTrace.cpp index 015a53ba0..1ec10f20e 100644 --- a/src/OSSupport/StackTrace.cpp +++ b/src/OSSupport/StackTrace.cpp @@ -12,6 +12,13 @@ #include <unistd.h> #endif +// FreeBSD uses size_t for the return type of backtrace() +#if defined(__FreeBSD__) && (__FreeBSD__ >= 10) + #define btsize size_t +#else + #define btsize int +#endif + @@ -34,7 +41,7 @@ void PrintStackTrace(void) // Use the backtrace() function to get and output the stackTrace: // Code adapted from http://stackoverflow.com/questions/77005/how-to-generate-a-stacktrace-when-my-gcc-c-app-crashes void * stackTrace[30]; - int numItems = backtrace(stackTrace, ARRAYCOUNT(stackTrace)); + btsize numItems = backtrace(stackTrace, ARRAYCOUNT(stackTrace)); backtrace_symbols_fd(stackTrace, numItems, STDERR_FILENO); #endif } |