~zooko/cryptopp/trunk

« back to all changes in this revision

Viewing changes to hrtimer.cpp

  • Committer: weidai
  • Date: 2007-05-04 15:04:58 UTC
  • Revision ID: svn-v4:57ff6487-cd31-0410-9ec3-f628ee90f5f0:trunk/c5:328
reduce risk of random number reuse after VM rollback

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
#include "hrtimer.h"
5
5
#include "misc.h"
6
6
#include <stddef.h>             // for NULL
7
 
#include <time.h>
8
7
 
9
8
#if defined(CRYPTOPP_WIN32_AVAILABLE)
10
9
#include <windows.h>
18
17
 
19
18
NAMESPACE_BEGIN(CryptoPP)
20
19
 
21
 
double TimerBase::ConvertTo(word64 t, Unit unit)
 
20
#ifndef CRYPTOPP_IMPORTS
 
21
 
 
22
double TimerBase::ConvertTo(TimerWord t, Unit unit)
22
23
{
23
24
        static unsigned long unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000};
24
25
 
25
26
        assert(unit < sizeof(unitsPerSecondTable) / sizeof(unitsPerSecondTable[0]));
26
 
#if defined(_MSC_VER) && (_MSC_VER < 1300)
27
 
        // MSVC 6 workaround
28
 
        return (double)(__int64)t * unitsPerSecondTable[unit] / (__int64)TicksPerSecond();
29
 
#else
30
 
        return (double)t * unitsPerSecondTable[unit] / TicksPerSecond();
31
 
#endif
32
 
                
 
27
        return (double)CRYPTOPP_VC6_INT64 t * unitsPerSecondTable[unit] / CRYPTOPP_VC6_INT64 TicksPerSecond();
33
28
}
34
29
 
35
30
void TimerBase::StartTimer()
45
40
 
46
41
        if (m_started)
47
42
        {
48
 
                word64 now = GetCurrentTimerValue();
 
43
                TimerWord now = GetCurrentTimerValue();
49
44
                if (m_last < now)       // protect against OS bugs where time goes backwards
50
45
                        m_last = now;
51
46
                return ConvertTo(m_last - m_start, m_timerUnit);
62
57
        return (unsigned long)elapsed;
63
58
}
64
59
 
65
 
word64 ThreadUserTimer::GetCurrentTimerValue()
 
60
TimerWord Timer::GetCurrentTimerValue()
 
61
{
 
62
#if defined(CRYPTOPP_WIN32_AVAILABLE)
 
63
        LARGE_INTEGER now;
 
64
        if (!QueryPerformanceCounter(&now))
 
65
                throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceCounter failed with error " + IntToString(GetLastError()));
 
66
        return now.QuadPart;
 
67
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
 
68
        timeval now;
 
69
        gettimeofday(&now, NULL);
 
70
        return (TimerWord)now.tv_sec * 1000000 + now.tv_usec;
 
71
#else
 
72
        clock_t now;
 
73
        return clock();
 
74
#endif
 
75
}
 
76
 
 
77
TimerWord Timer::TicksPerSecond()
 
78
{
 
79
#if defined(CRYPTOPP_WIN32_AVAILABLE)
 
80
        static LARGE_INTEGER freq = {0};
 
81
        if (freq.QuadPart == 0)
 
82
        {
 
83
                if (!QueryPerformanceFrequency(&freq))
 
84
                        throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceFrequency failed with error " + IntToString(GetLastError()));
 
85
        }
 
86
        return freq.QuadPart;
 
87
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
 
88
        return 1000000;
 
89
#elif defined(CLOCKS_PER_SEC)
 
90
        return CLOCKS_PER_SEC;
 
91
#elif defined(CLK_TCK)
 
92
        return CLK_TCK;
 
93
#else
 
94
        return 1000000;
 
95
#endif
 
96
}
 
97
 
 
98
#endif  // #ifndef CRYPTOPP_IMPORTS
 
99
 
 
100
TimerWord ThreadUserTimer::GetCurrentTimerValue()
66
101
{
67
102
#if defined(CRYPTOPP_WIN32_AVAILABLE)
68
103
        static bool getCurrentThreadImplemented = true;
79
114
                        }
80
115
                        throw Exception(Exception::OTHER_ERROR, "ThreadUserTimer: GetThreadTimes failed with error " + IntToString(lastError));
81
116
                }
82
 
                return now.dwLowDateTime + ((word64)now.dwHighDateTime << 32);
 
117
                return now.dwLowDateTime + ((TimerWord)now.dwHighDateTime << 32);
83
118
        }
84
119
GetCurrentThreadNotImplemented:
85
 
        return (word64)clock() * (10*1000*1000 / CLOCKS_PER_SEC);
 
120
        return (TimerWord)clock() * (10*1000*1000 / CLOCKS_PER_SEC);
86
121
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
87
122
        tms now;
88
123
        times(&now);
92
127
#endif
93
128
}
94
129
 
95
 
word64 ThreadUserTimer::TicksPerSecond()
 
130
TimerWord ThreadUserTimer::TicksPerSecond()
96
131
{
97
132
#if defined(CRYPTOPP_WIN32_AVAILABLE)
98
133
        return 10*1000*1000;
104
139
#endif
105
140
}
106
141
 
107
 
#ifdef HIGHRES_TIMER_AVAILABLE
108
 
 
109
 
word64 Timer::GetCurrentTimerValue()
110
 
{
111
 
#if defined(CRYPTOPP_WIN32_AVAILABLE)
112
 
        LARGE_INTEGER now;
113
 
        if (!QueryPerformanceCounter(&now))
114
 
                throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceCounter failed with error " + IntToString(GetLastError()));
115
 
        return now.QuadPart;
116
 
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
117
 
        timeval now;
118
 
        gettimeofday(&now, NULL);
119
 
        return (word64)now.tv_sec * 1000000 + now.tv_usec;
120
 
#endif
121
 
}
122
 
 
123
 
word64 Timer::TicksPerSecond()
124
 
{
125
 
#if defined(CRYPTOPP_WIN32_AVAILABLE)
126
 
        static LARGE_INTEGER freq = {0};
127
 
        if (freq.QuadPart == 0)
128
 
        {
129
 
                if (!QueryPerformanceFrequency(&freq))
130
 
                        throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceFrequency failed with error " + IntToString(GetLastError()));
131
 
        }
132
 
        return freq.QuadPart;
133
 
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
134
 
        return 1000000;
135
 
#endif
136
 
}
137
 
 
138
 
#endif
139
 
 
140
142
NAMESPACE_END