From 19700f816e9272cee9a6470ea0442be5468cc860 Mon Sep 17 00:00:00 2001 From: "Alexander G. Pronchenkov" Date: Tue, 26 Jan 2010 16:42:20 +0500 Subject: [PATCH] Minor changes: fixed type of NL. --- compat/hirestimeofday.cpp | 314 +++++++++++++++++++------------------- compat/hirestimeofday.h | 104 ++++++------- compat/stdint.h | 36 ++--- compat/unixio.cpp | 66 ++++---- compat/unixio.h | 56 +++---- compat/util.cpp | 78 +++++----- compat/util.h | 56 +++---- 7 files changed, 355 insertions(+), 355 deletions(-) diff --git a/compat/hirestimeofday.cpp b/compat/hirestimeofday.cpp index f5aad7c..2f5ef33 100644 --- a/compat/hirestimeofday.cpp +++ b/compat/hirestimeofday.cpp @@ -1,157 +1,157 @@ -/* - * Inspired by - * - http://msdn.microsoft.com/en-us/library/ms644904%28VS.85%29.aspx - * - Python-2.6.3/Modules/timemodule.c - */ - -#include -#include "hirestimeofday.h" - -#ifndef _WIN32 -#include -#endif - -namespace p2tp { - -HiResTimeOfDay* HiResTimeOfDay::_instance = 0; - -HiResTimeOfDay* HiResTimeOfDay::Instance() -{ - if (_instance == 0) - _instance = new HiResTimeOfDay(); - return _instance; -} - - -#ifdef _WIN32 -#include -#include - - -HiResTimeOfDay::HiResTimeOfDay(void) -{ - frequency = getFrequency(); - epochstart = getFTime(); - epochcounter = getCounter(); -} - - -tint HiResTimeOfDay::getTimeUSec(void) -{ - LARGE_INTEGER currentcounter; - tint currentstart; - - currentstart = getFTime(); - currentcounter = getCounter(); - - if (currentcounter.QuadPart < epochcounter.QuadPart) - { - - // Wrap around detected, reestablish baseline - epochstart = currentstart; - epochcounter = currentcounter; - } - return epochstart + (1000000*(currentcounter.QuadPart-epochcounter.QuadPart))/frequency.QuadPart; -} - - -// Private -tint HiResTimeOfDay::getFTime() -{ - struct timeb t; - ftime(&t); - tint usec; - usec = t.time * 1000000; - usec += t.millitm * 1000; - return usec; -} - - - -LARGE_INTEGER HiResTimeOfDay::getFrequency(void) -{ - LARGE_INTEGER proc_freq; - - if (!::QueryPerformanceFrequency(&proc_freq)) - std::cerr << "HiResTimeOfDay: QueryPerformanceFrequency() failed"; - - return proc_freq; -} - -LARGE_INTEGER HiResTimeOfDay::getCounter() -{ - LARGE_INTEGER counter; - - DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0); - if (!::QueryPerformanceCounter(&counter)) - std::cerr << "HiResTimeOfDay: QueryPerformanceCounter() failed"; - ::SetThreadAffinityMask(::GetCurrentThread(), oldmask); - - return counter; -} - -#else - -HiResTimeOfDay::HiResTimeOfDay(void) -{ -} - - -tint HiResTimeOfDay::getTimeUSec(void) -{ - struct timeval t; - gettimeofday(&t,NULL); - tint ret; - ret = t.tv_sec; - ret *= 1000000; - ret += t.tv_usec; - return ret; -} -#endif - - - - -// ARNOTODO: move to p2tp.cpp - -#ifdef _WIN32 -static WSADATA _WSAData; -#endif - -void LibraryInit(void) -{ -#ifdef _WIN32 - // win32 requires you to initialize the Winsock DLL with the desired - // specification version - WORD wVersionRequested; - wVersionRequested = MAKEWORD(2, 2); - WSAStartup(wVersionRequested, &_WSAData); -#endif -} - - -} // end of namespace - - - - -#ifdef TEST -#include - -using namespace p2tp; - -int main() -{ - HiResTimeOfDay *t = HiResTimeOfDay::Instance(); - for (int i=0; i<100; i++) - { - tint st = t->getTimeUSec(); - Sleep(1000); - tint et = t->getTimeUSec(); - tint diff = et - st; - std::cout << "diffxTime is " << diff << "\n"; - } - return 0; -} -#endif - +/* + * Inspired by + * - http://msdn.microsoft.com/en-us/library/ms644904%28VS.85%29.aspx + * - Python-2.6.3/Modules/timemodule.c + */ + +#include +#include "hirestimeofday.h" + +#ifndef _WIN32 +#include +#endif + +namespace p2tp { + +HiResTimeOfDay* HiResTimeOfDay::_instance = 0; + +HiResTimeOfDay* HiResTimeOfDay::Instance() +{ + if (_instance == 0) + _instance = new HiResTimeOfDay(); + return _instance; +} + + +#ifdef _WIN32 +#include +#include + + +HiResTimeOfDay::HiResTimeOfDay(void) +{ + frequency = getFrequency(); + epochstart = getFTime(); + epochcounter = getCounter(); +} + + +tint HiResTimeOfDay::getTimeUSec(void) +{ + LARGE_INTEGER currentcounter; + tint currentstart; + + currentstart = getFTime(); + currentcounter = getCounter(); + + if (currentcounter.QuadPart < epochcounter.QuadPart) + { + + // Wrap around detected, reestablish baseline + epochstart = currentstart; + epochcounter = currentcounter; + } + return epochstart + (1000000*(currentcounter.QuadPart-epochcounter.QuadPart))/frequency.QuadPart; +} + + +// Private +tint HiResTimeOfDay::getFTime() +{ + struct timeb t; + ftime(&t); + tint usec; + usec = t.time * 1000000; + usec += t.millitm * 1000; + return usec; +} + + + +LARGE_INTEGER HiResTimeOfDay::getFrequency(void) +{ + LARGE_INTEGER proc_freq; + + if (!::QueryPerformanceFrequency(&proc_freq)) + std::cerr << "HiResTimeOfDay: QueryPerformanceFrequency() failed"; + + return proc_freq; +} + +LARGE_INTEGER HiResTimeOfDay::getCounter() +{ + LARGE_INTEGER counter; + + DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0); + if (!::QueryPerformanceCounter(&counter)) + std::cerr << "HiResTimeOfDay: QueryPerformanceCounter() failed"; + ::SetThreadAffinityMask(::GetCurrentThread(), oldmask); + + return counter; +} + +#else + +HiResTimeOfDay::HiResTimeOfDay(void) +{ +} + + +tint HiResTimeOfDay::getTimeUSec(void) +{ + struct timeval t; + gettimeofday(&t,NULL); + tint ret; + ret = t.tv_sec; + ret *= 1000000; + ret += t.tv_usec; + return ret; +} +#endif + + + + +// ARNOTODO: move to p2tp.cpp + +#ifdef _WIN32 +static WSADATA _WSAData; +#endif + +void LibraryInit(void) +{ +#ifdef _WIN32 + // win32 requires you to initialize the Winsock DLL with the desired + // specification version + WORD wVersionRequested; + wVersionRequested = MAKEWORD(2, 2); + WSAStartup(wVersionRequested, &_WSAData); +#endif +} + + +} // end of namespace + + + + +#ifdef TEST +#include + +using namespace p2tp; + +int main() +{ + HiResTimeOfDay *t = HiResTimeOfDay::Instance(); + for (int i=0; i<100; i++) + { + tint st = t->getTimeUSec(); + Sleep(1000); + tint et = t->getTimeUSec(); + tint diff = et - st; + std::cout << "diffxTime is " << diff << "\n"; + } + return 0; +} +#endif + diff --git a/compat/hirestimeofday.h b/compat/hirestimeofday.h index 731da1e..be2e60c 100644 --- a/compat/hirestimeofday.h +++ b/compat/hirestimeofday.h @@ -1,52 +1,52 @@ -/* - * Written by Arno Bakker - * see LICENSE.txt for license information - * - * Singleton class to retrieve a time-of-day in UTC in usec in a platform- - * independent manner. - */ -#ifndef HIRESTIMEOFDAY_H -#define HIRESTIMEOFDAY_H - -#ifdef _MSC_VER -#include "compat/stdint.h" -#else -#include -#endif -#ifdef _WIN32 -#include -#endif - -namespace p2tp { - -typedef int64_t tint; -#define TINT_SEC ((tint)1000000) -#define TINT_MSEC ((tint)1000) -#define TINT_uSEC ((tint)1) -#define TINT_NEVER ((tint)0x7fffffffffffffffLL) - - -class HiResTimeOfDay -{ -public: - HiResTimeOfDay(void); - tint getTimeUSec(void); - static HiResTimeOfDay* Instance(); - -private: -#ifdef _WIN32 - tint epochstart; // in usec - LARGE_INTEGER epochcounter; - LARGE_INTEGER last; - LARGE_INTEGER frequency; - - tint HiResTimeOfDay::getFTime(); - LARGE_INTEGER getFrequency(void); - LARGE_INTEGER getCounter(void); -#endif - - static HiResTimeOfDay* _instance; -}; - -}; -#endif +/* + * Written by Arno Bakker + * see LICENSE.txt for license information + * + * Singleton class to retrieve a time-of-day in UTC in usec in a platform- + * independent manner. + */ +#ifndef HIRESTIMEOFDAY_H +#define HIRESTIMEOFDAY_H + +#ifdef _MSC_VER +#include "compat/stdint.h" +#else +#include +#endif +#ifdef _WIN32 +#include +#endif + +namespace p2tp { + +typedef int64_t tint; +#define TINT_SEC ((tint)1000000) +#define TINT_MSEC ((tint)1000) +#define TINT_uSEC ((tint)1) +#define TINT_NEVER ((tint)0x7fffffffffffffffLL) + + +class HiResTimeOfDay +{ +public: + HiResTimeOfDay(void); + tint getTimeUSec(void); + static HiResTimeOfDay* Instance(); + +private: +#ifdef _WIN32 + tint epochstart; // in usec + LARGE_INTEGER epochcounter; + LARGE_INTEGER last; + LARGE_INTEGER frequency; + + tint HiResTimeOfDay::getFTime(); + LARGE_INTEGER getFrequency(void); + LARGE_INTEGER getCounter(void); +#endif + + static HiResTimeOfDay* _instance; +}; + +}; +#endif diff --git a/compat/stdint.h b/compat/stdint.h index 4586324..6cb10e6 100644 --- a/compat/stdint.h +++ b/compat/stdint.h @@ -1,18 +1,18 @@ -/* - * Written by Arno Bakker - * see LICENSE.txt for license information - */ - -#ifndef STDINT_H_ -#define STDINT_H_ - -typedef unsigned char uint8_t; -typedef signed char int8_t; -typedef unsigned short uint16_t; -typedef short int16_t; -typedef unsigned int uint32_t; -typedef int int32_t; -typedef __int64 int64_t; -typedef unsigned __int64 uint64_t; - -#endif /* STDINT_H_ */ +/* + * Written by Arno Bakker + * see LICENSE.txt for license information + */ + +#ifndef STDINT_H_ +#define STDINT_H_ + +typedef unsigned char uint8_t; +typedef signed char int8_t; +typedef unsigned short uint16_t; +typedef short int16_t; +typedef unsigned int uint32_t; +typedef int int32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; + +#endif /* STDINT_H_ */ diff --git a/compat/unixio.cpp b/compat/unixio.cpp index 12f3e36..614a31b 100644 --- a/compat/unixio.cpp +++ b/compat/unixio.cpp @@ -1,33 +1,33 @@ -/* - * Written by Arno Bakker - * see LICENSE.txt for license information - */ -#ifdef _WIN32 - -#include "unixio.h" -#include -#include -#include - -size_t pread(int fildes, void *buf, size_t nbyte, long offset) -{ - _lseek(fildes,offset,SEEK_SET); - return read(fildes,buf,nbyte); -} - -size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset) -{ - _lseek(fildes,offset,SEEK_SET); - return write(fildes,buf,nbyte); -} - - -int inet_aton(const char *cp, struct in_addr *inp) -{ - inp->S_un.S_addr = inet_addr(cp); - return 1; -} - - - -#endif +/* + * Written by Arno Bakker + * see LICENSE.txt for license information + */ +#ifdef _WIN32 + +#include "unixio.h" +#include +#include +#include + +size_t pread(int fildes, void *buf, size_t nbyte, long offset) +{ + _lseek(fildes,offset,SEEK_SET); + return read(fildes,buf,nbyte); +} + +size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset) +{ + _lseek(fildes,offset,SEEK_SET); + return write(fildes,buf,nbyte); +} + + +int inet_aton(const char *cp, struct in_addr *inp) +{ + inp->S_un.S_addr = inet_addr(cp); + return 1; +} + + + +#endif diff --git a/compat/unixio.h b/compat/unixio.h index 46771c8..f19757c 100644 --- a/compat/unixio.h +++ b/compat/unixio.h @@ -1,29 +1,29 @@ -/* - * Written by Arno Bakker - * see LICENSE.txt for license information - * - * Defines UNIX like I/O calls and parameters for Win32 - */ -#ifdef _WIN32 - -#ifndef UNIXIO_H_ -#define UNIXIO_H_ - -#define open(a,b,c) _open(a,b,c) -#define S_IRUSR _S_IREAD -#define S_IWUSR _S_IWRITE -#define S_IRGRP _S_IREAD -#define S_IROTH _S_IREAD -#define ftruncate(a, b) _chsize(a,b) - -size_t pread(int fildes, void *buf, size_t nbyte, long offset); -/** UNIX pread approximation. Does change file pointer. Is not thread-safe */ -size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset); -/** UNIX pwrite approximation. Does change file pointer. Is not thread-safe */ - -int inet_aton(const char *cp, struct in_addr *inp); - - -#endif /* UNIXIO_H_ */ - +/* + * Written by Arno Bakker + * see LICENSE.txt for license information + * + * Defines UNIX like I/O calls and parameters for Win32 + */ +#ifdef _WIN32 + +#ifndef UNIXIO_H_ +#define UNIXIO_H_ + +#define open(a,b,c) _open(a,b,c) +#define S_IRUSR _S_IREAD +#define S_IWUSR _S_IWRITE +#define S_IRGRP _S_IREAD +#define S_IROTH _S_IREAD +#define ftruncate(a, b) _chsize(a,b) + +size_t pread(int fildes, void *buf, size_t nbyte, long offset); +/** UNIX pread approximation. Does change file pointer. Is not thread-safe */ +size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset); +/** UNIX pwrite approximation. Does change file pointer. Is not thread-safe */ + +int inet_aton(const char *cp, struct in_addr *inp); + + +#endif /* UNIXIO_H_ */ + #endif // WIN32 \ No newline at end of file diff --git a/compat/util.cpp b/compat/util.cpp index 281dfac..6493ecc 100644 --- a/compat/util.cpp +++ b/compat/util.cpp @@ -1,39 +1,39 @@ -/* - * Written by Arno Bakker - * see LICENSE.txt for license information - */ - -#include "util.h" -#include - -#ifdef _WIN32 -#include -#include -#endif - -namespace p2tp -{ - -std::string gettmpdir(void) -{ -#ifdef _WIN32 - DWORD result = ::GetTempPath(0, _T("")); - if (result == 0) - throw std::runtime_error("Could not get system temp path"); - - std::vector tempPath(result + 1); - result = ::GetTempPath(static_cast(tempPath.size()), &tempPath[0]); - if((result == 0) || (result >= tempPath.size())) - throw std::runtime_error("Could not get system temp path"); - - return std::string(tempPath.begin(), tempPath.begin() + static_cast(result)); -#else - return std::string("/tmp/"); -#endif -} - - - - -}; // namespace - +/* + * Written by Arno Bakker + * see LICENSE.txt for license information + */ + +#include "util.h" +#include + +#ifdef _WIN32 +#include +#include +#endif + +namespace p2tp +{ + +std::string gettmpdir(void) +{ +#ifdef _WIN32 + DWORD result = ::GetTempPath(0, _T("")); + if (result == 0) + throw std::runtime_error("Could not get system temp path"); + + std::vector tempPath(result + 1); + result = ::GetTempPath(static_cast(tempPath.size()), &tempPath[0]); + if((result == 0) || (result >= tempPath.size())) + throw std::runtime_error("Could not get system temp path"); + + return std::string(tempPath.begin(), tempPath.begin() + static_cast(result)); +#else + return std::string("/tmp/"); +#endif +} + + + + +}; // namespace + diff --git a/compat/util.h b/compat/util.h index aa99df6..265bc75 100644 --- a/compat/util.h +++ b/compat/util.h @@ -1,28 +1,28 @@ -/* - * util.h - * - * Created on: 20-Oct-2009 - * Author: arno - */ - -#ifndef UTIL_H_ -#define UTIL_H_ - -#include - -namespace p2tp -{ - /** - * Return path of temporary directory. - * - * From http://msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspx - * - * TODO: Unicode... (gets hairy with open() call on Linux. Win32 has _wopen) - */ - std::string gettmpdir(void); - - void print_error(const char* msg); - -}; - -#endif /* UTIL_H_ */ +/* + * util.h + * + * Created on: 20-Oct-2009 + * Author: arno + */ + +#ifndef UTIL_H_ +#define UTIL_H_ + +#include + +namespace p2tp +{ + /** + * Return path of temporary directory. + * + * From http://msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspx + * + * TODO: Unicode... (gets hairy with open() call on Linux. Win32 has _wopen) + */ + std::string gettmpdir(void); + + void print_error(const char* msg); + +}; + +#endif /* UTIL_H_ */ -- 2.20.1