summaryrefslogtreecommitdiff
path: root/hrtimer.cpp
diff options
context:
space:
mode:
authorweidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2004-06-20 17:56:15 +0000
committerweidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2004-06-20 17:56:15 +0000
commitc0aa5cf0c4df55a542a3f5c060fb840da4bc0b2d (patch)
tree2499058561875821704789052533e0c6c2c79891 /hrtimer.cpp
parent443398ff862b10dde571782ff8a1a068ad95e159 (diff)
downloadcryptopp-c0aa5cf0c4df55a542a3f5c060fb840da4bc0b2d.tar.gz
port to CodeWarrior 8.3
git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@175 57ff6487-cd31-0410-9ec3-f628ee90f5f0
Diffstat (limited to 'hrtimer.cpp')
-rw-r--r--hrtimer.cpp103
1 files changed, 54 insertions, 49 deletions
diff --git a/hrtimer.cpp b/hrtimer.cpp
index 904bc52..38763fb 100644
--- a/hrtimer.cpp
+++ b/hrtimer.cpp
@@ -4,8 +4,7 @@
#include "hrtimer.h"
#include "misc.h"
#include <stddef.h> // for NULL
-
-#ifdef HIGHRES_TIMER_AVAILABLE
+#include <time.h>
#if defined(CRYPTOPP_WIN32_AVAILABLE)
#include <windows.h>
@@ -19,33 +18,38 @@
NAMESPACE_BEGIN(CryptoPP)
-word64 Timer::GetCurrentTimerValue()
+double TimerBase::ConvertTo(word64 t, Unit unit)
{
-#if defined(CRYPTOPP_WIN32_AVAILABLE)
- LARGE_INTEGER now;
- if (!QueryPerformanceCounter(&now))
- throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceCounter failed with error " + IntToString(GetLastError()));
- return now.QuadPart;
-#elif defined(CRYPTOPP_UNIX_AVAILABLE)
- timeval now;
- gettimeofday(&now, NULL);
- return (word64)now.tv_sec * 1000000 + now.tv_usec;
-#endif
+ static unsigned long unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000};
+
+ assert(unit < sizeof(unitsPerSecondTable) / sizeof(unitsPerSecondTable[0]));
+ return (double)t * unitsPerSecondTable[unit] / TicksPerSecond();
}
-word64 Timer::TicksPerSecond()
+void TimerBase::StartTimer()
{
-#if defined(CRYPTOPP_WIN32_AVAILABLE)
- static LARGE_INTEGER freq = {0};
- if (freq.QuadPart == 0)
+ m_start = GetCurrentTimerValue();
+ m_started = true;
+}
+
+double TimerBase::ElapsedTimeAsDouble()
+{
+ if (m_stuckAtZero)
+ return 0;
+ else if (m_started)
+ return ConvertTo(GetCurrentTimerValue() - m_start, m_timerUnit);
+ else
{
- if (!QueryPerformanceFrequency(&freq))
- throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceFrequency failed with error " + IntToString(GetLastError()));
+ StartTimer();
+ return 0;
}
- return freq.QuadPart;
-#elif defined(CRYPTOPP_UNIX_AVAILABLE)
- return 1000000;
-#endif
+}
+
+unsigned long TimerBase::ElapsedTime()
+{
+ double elapsed = ElapsedTimeAsDouble();
+ assert(elapsed <= ULONG_MAX);
+ return (unsigned long)elapsed;
}
word64 ThreadUserTimer::GetCurrentTimerValue()
@@ -73,6 +77,8 @@ GetCurrentThreadNotImplemented:
tms now;
times(&now);
return now.tms_utime;
+#else
+ return clock();
#endif
}
@@ -83,43 +89,42 @@ word64 ThreadUserTimer::TicksPerSecond()
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
static const long ticksPerSecond = sysconf(_SC_CLK_TCK);
return ticksPerSecond;
+#else
+ return CLOCKS_PER_SEC;
#endif
}
-double TimerBase::ConvertTo(word64 t, Unit unit)
-{
- static unsigned long unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000};
-
- assert(unit < sizeof(unitsPerSecondTable) / sizeof(unitsPerSecondTable[0]));
- return (double)t * unitsPerSecondTable[unit] / TicksPerSecond();
-}
+#ifdef HIGHRES_TIMER_AVAILABLE
-void TimerBase::StartTimer()
+word64 Timer::GetCurrentTimerValue()
{
- m_start = GetCurrentTimerValue();
- m_started = true;
+#if defined(CRYPTOPP_WIN32_AVAILABLE)
+ LARGE_INTEGER now;
+ if (!QueryPerformanceCounter(&now))
+ throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceCounter failed with error " + IntToString(GetLastError()));
+ return now.QuadPart;
+#elif defined(CRYPTOPP_UNIX_AVAILABLE)
+ timeval now;
+ gettimeofday(&now, NULL);
+ return (word64)now.tv_sec * 1000000 + now.tv_usec;
+#endif
}
-double TimerBase::ElapsedTimeAsDouble()
+word64 Timer::TicksPerSecond()
{
- if (m_stuckAtZero)
- return 0;
- else if (m_started)
- return ConvertTo(GetCurrentTimerValue() - m_start, m_timerUnit);
- else
+#if defined(CRYPTOPP_WIN32_AVAILABLE)
+ static LARGE_INTEGER freq = {0};
+ if (freq.QuadPart == 0)
{
- StartTimer();
- return 0;
+ if (!QueryPerformanceFrequency(&freq))
+ throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceFrequency failed with error " + IntToString(GetLastError()));
}
+ return freq.QuadPart;
+#elif defined(CRYPTOPP_UNIX_AVAILABLE)
+ return 1000000;
+#endif
}
-unsigned long TimerBase::ElapsedTime()
-{
- double elapsed = ElapsedTimeAsDouble();
- assert(elapsed <= ULONG_MAX);
- return (unsigned long)elapsed;
-}
+#endif
NAMESPACE_END
-
-#endif