- /*\r
- * Inspired by\r
- * - http://msdn.microsoft.com/en-us/library/ms644904%28VS.85%29.aspx\r
- * - Python-2.6.3/Modules/timemodule.c\r
- */\r
- \r
- #include <iostream>\r
- #include "hirestimeofday.h"\r
- \r
- #ifndef _WIN32\r
- #include <sys/time.h>\r
- #endif\r
- \r
- namespace swift {\r
- \r
- HiResTimeOfDay* HiResTimeOfDay::_instance = 0;\r
- \r
- HiResTimeOfDay* HiResTimeOfDay::Instance()\r
- {\r
- if (_instance == 0)\r
- _instance = new HiResTimeOfDay();\r
- return _instance;\r
- }\r
- \r
- \r
- #ifdef _WIN32\r
- #include <windows.h>\r
- #include <sys/timeb.h>\r
- \r
- \r
- HiResTimeOfDay::HiResTimeOfDay(void)\r
- {\r
- frequency = getFrequency();\r
- epochstart = getFTime();\r
- epochcounter = getCounter();\r
- }\r
- \r
- \r
- tint HiResTimeOfDay::getTimeUSec(void)\r
- {\r
- LARGE_INTEGER currentcounter;\r
- tint currentstart;\r
- \r
- currentstart = getFTime();\r
- currentcounter = getCounter();\r
- \r
- if (currentcounter.QuadPart < epochcounter.QuadPart)\r
- {\r
- \r
- // Wrap around detected, reestablish baseline\r
- epochstart = currentstart;\r
- epochcounter = currentcounter;\r
- }\r
- return epochstart + (1000000*(currentcounter.QuadPart-epochcounter.QuadPart))/frequency.QuadPart;\r
- }\r
- \r
- \r
- // Private\r
- tint HiResTimeOfDay::getFTime()\r
- {\r
- struct timeb t;\r
- ftime(&t);\r
- tint usec;\r
- usec = t.time * 1000000;\r
- usec += t.millitm * 1000;\r
- return usec;\r
- }\r
- \r
- \r
- \r
- LARGE_INTEGER HiResTimeOfDay::getFrequency(void)\r
- {\r
- LARGE_INTEGER proc_freq;\r
- \r
- if (!::QueryPerformanceFrequency(&proc_freq))\r
- std::cerr << "HiResTimeOfDay: QueryPerformanceFrequency() failed";\r
- \r
- return proc_freq;\r
- }\r
- \r
- LARGE_INTEGER HiResTimeOfDay::getCounter()\r
- {\r
- LARGE_INTEGER counter;\r
- \r
- DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);\r
- if (!::QueryPerformanceCounter(&counter))\r
- std::cerr << "HiResTimeOfDay: QueryPerformanceCounter() failed";\r
- ::SetThreadAffinityMask(::GetCurrentThread(), oldmask);\r
- \r
- return counter;\r
- }\r
- \r
- #else\r
- \r
- HiResTimeOfDay::HiResTimeOfDay(void)\r
- {\r
- }\r
- \r
- \r
- tint HiResTimeOfDay::getTimeUSec(void)\r
- {\r
- struct timeval t;\r
- gettimeofday(&t,NULL);\r
- tint ret;\r
- ret = t.tv_sec;\r
- ret *= 1000000;\r
- ret += t.tv_usec;\r
- return ret;\r
- }\r
- #endif\r
- \r
- \r
- \r
- \r
- // ARNOTODO: move to swift.cpp\r
- \r
- #ifdef _WIN32\r
- static WSADATA _WSAData;\r
- #endif\r
- \r
- void LibraryInit(void)\r
- {\r
- #ifdef _WIN32\r
- // win32 requires you to initialize the Winsock DLL with the desired\r
- // specification version\r
- WORD wVersionRequested;\r
- wVersionRequested = MAKEWORD(2, 2);\r
- WSAStartup(wVersionRequested, &_WSAData);\r
- #endif\r
- }\r
- \r
- \r
- } // end of namespace\r
- \r
- \r
- \r
- \r
- #ifdef TEST\r
- #include <iostream>\r
- \r
- using namespace swift;\r
- \r
- int main()\r
- {\r
- HiResTimeOfDay *t = HiResTimeOfDay::Instance();\r
- for (int i=0; i<100; i++)\r
- {\r
- tint st = t->getTimeUSec();\r
- Sleep(1000);\r
- tint et = t->getTimeUSec();\r
- tint diff = et - st;\r
- std::cout << "diffxTime is " << diff << "\n";\r
- }\r
- return 0;\r
- }\r
- #endif\r
- \r
+ /*
+ * Inspired by
+ * - http://msdn.microsoft.com/en-us/library/ms644904%28VS.85%29.aspx
+ * - Python-2.6.3/Modules/timemodule.c
+ */
+
+ #include <iostream>
+ #include "hirestimeofday.h"
+
+ #ifndef _WIN32
+ #include <sys/time.h>
+ #endif
+
-namespace p2tp {
++namespace swift {
+
+ HiResTimeOfDay* HiResTimeOfDay::_instance = 0;
+
+ HiResTimeOfDay* HiResTimeOfDay::Instance()
+ {
+ if (_instance == 0)
+ _instance = new HiResTimeOfDay();
+ return _instance;
+ }
+
+
+ #ifdef _WIN32
+ #include <windows.h>
+ #include <sys/timeb.h>
+
+
+ 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
++// ARNOTODO: move to swift.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 <iostream>
+
-using namespace p2tp;
++using namespace swift;
+
+ 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
+
- /*\r
- * Written by Arno Bakker\r
- * see LICENSE.txt for license information\r
- *\r
- * Singleton class to retrieve a time-of-day in UTC in usec in a platform-\r
- * independent manner.\r
- */\r
- #ifndef HIRESTIMEOFDAY_H\r
- #define HIRESTIMEOFDAY_H\r
- \r
- #ifdef _MSC_VER\r
- #include "compat/stdint.h"\r
- #else\r
- #include <stdint.h>\r
- #endif\r
- #ifdef _WIN32\r
- #include <windows.h>\r
- #endif\r
- \r
- namespace swift {\r
- \r
- typedef int64_t tint;\r
- #define TINT_SEC ((tint)1000000)\r
- #define TINT_MSEC ((tint)1000)\r
- #define TINT_uSEC ((tint)1)\r
- #define TINT_NEVER ((tint)0x7fffffffffffffffLL)\r
- \r
- \r
- class HiResTimeOfDay\r
- {\r
- public:\r
- HiResTimeOfDay(void);\r
- tint getTimeUSec(void);\r
- static HiResTimeOfDay* Instance();\r
- \r
- private:\r
- #ifdef _WIN32\r
- tint epochstart; // in usec\r
- LARGE_INTEGER epochcounter;\r
- LARGE_INTEGER last;\r
- LARGE_INTEGER frequency;\r
- \r
- tint HiResTimeOfDay::getFTime();\r
- LARGE_INTEGER getFrequency(void);\r
- LARGE_INTEGER getCounter(void);\r
- #endif\r
- \r
- static HiResTimeOfDay* _instance;\r
- };\r
- \r
- };\r
- #endif\r
+ /*
+ * 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 <stdint.h>
+ #endif
+ #ifdef _WIN32
+ #include <windows.h>
+ #endif
+
-namespace p2tp {
++namespace swift {
+
+ 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
- /*\r
- * Written by Arno Bakker\r
- * see LICENSE.txt for license information\r
- */\r
- \r
- #include "util.h"\r
- #include <vector>\r
- \r
- #ifdef _WIN32\r
- #include <windows.h>\r
- #include <Tchar.h>\r
- #endif\r
- \r
- namespace swift\r
- {\r
- \r
- std::string gettmpdir(void)\r
- {\r
- #ifdef _WIN32\r
- DWORD result = ::GetTempPath(0, _T(""));\r
- if (result == 0)\r
- throw std::runtime_error("Could not get system temp path");\r
- \r
- std::vector<TCHAR> tempPath(result + 1);\r
- result = ::GetTempPath(static_cast<DWORD>(tempPath.size()), &tempPath[0]);\r
- if((result == 0) || (result >= tempPath.size()))\r
- throw std::runtime_error("Could not get system temp path");\r
- \r
- return std::string(tempPath.begin(), tempPath.begin() + static_cast<std::size_t>(result));\r
- #else\r
- return std::string("/tmp/");\r
- #endif\r
- }\r
- \r
- \r
- \r
- \r
- }; // namespace\r
- \r
+ /*
+ * Written by Arno Bakker
+ * see LICENSE.txt for license information
+ */
+
+ #include "util.h"
+ #include <vector>
+
+ #ifdef _WIN32
+ #include <windows.h>
+ #include <Tchar.h>
+ #endif
+
-namespace p2tp
++namespace swift
+ {
+
+ 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<TCHAR> tempPath(result + 1);
+ result = ::GetTempPath(static_cast<DWORD>(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<std::size_t>(result));
+ #else
+ return std::string("/tmp/");
+ #endif
+ }
+
+
+
+
+ }; // namespace
+