summaryrefslogtreecommitdiff
path: root/hrtimer.cpp
diff options
context:
space:
mode:
authorweidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2007-05-04 15:04:58 +0000
committerweidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2007-05-04 15:04:58 +0000
commitcdd57fd49c847250ce5d8e3cc39b7d882a529fc3 (patch)
treeaf7086b6bdb722b9a90c1abe0dd994166f32040b /hrtimer.cpp
parent061bcd669cf286d0043e1060bb36761df2df907e (diff)
downloadcryptopp-cdd57fd49c847250ce5d8e3cc39b7d882a529fc3.tar.gz
reduce risk of random number reuse after VM rollback
git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@328 57ff6487-cd31-0410-9ec3-f628ee90f5f0
Diffstat (limited to 'hrtimer.cpp')
-rw-r--r--hrtimer.cpp76
1 files changed, 42 insertions, 34 deletions
diff --git a/hrtimer.cpp b/hrtimer.cpp
index 71149ba..2496774 100644
--- a/hrtimer.cpp
+++ b/hrtimer.cpp
@@ -4,7 +4,6 @@
#include "hrtimer.h"
#include "misc.h"
#include <stddef.h> // for NULL
-#include <time.h>
#if defined(CRYPTOPP_WIN32_AVAILABLE)
#include <windows.h>
@@ -18,6 +17,8 @@
NAMESPACE_BEGIN(CryptoPP)
+#ifndef CRYPTOPP_IMPORTS
+
double TimerBase::ConvertTo(TimerWord t, Unit unit)
{
static unsigned long unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000};
@@ -56,6 +57,46 @@ unsigned long TimerBase::ElapsedTime()
return (unsigned long)elapsed;
}
+TimerWord Timer::GetCurrentTimerValue()
+{
+#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 (TimerWord)now.tv_sec * 1000000 + now.tv_usec;
+#else
+ clock_t now;
+ return clock();
+#endif
+}
+
+TimerWord Timer::TicksPerSecond()
+{
+#if defined(CRYPTOPP_WIN32_AVAILABLE)
+ static LARGE_INTEGER freq = {0};
+ if (freq.QuadPart == 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;
+#elif defined(CLOCKS_PER_SEC)
+ return CLOCKS_PER_SEC;
+#elif defined(CLK_TCK)
+ return CLK_TCK;
+#else
+ return 1000000;
+#endif
+}
+
+#endif // #ifndef CRYPTOPP_IMPORTS
+
TimerWord ThreadUserTimer::GetCurrentTimerValue()
{
#if defined(CRYPTOPP_WIN32_AVAILABLE)
@@ -98,37 +139,4 @@ TimerWord ThreadUserTimer::TicksPerSecond()
#endif
}
-#ifdef HIGHRES_TIMER_AVAILABLE
-
-TimerWord Timer::GetCurrentTimerValue()
-{
-#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 (TimerWord)now.tv_sec * 1000000 + now.tv_usec;
-#endif
-}
-
-TimerWord Timer::TicksPerSecond()
-{
-#if defined(CRYPTOPP_WIN32_AVAILABLE)
- static LARGE_INTEGER freq = {0};
- if (freq.QuadPart == 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
-}
-
-#endif // HIGHRES_TIMER_AVAILABLE
-
NAMESPACE_END