Sophie

Sophie

distrib > Mandriva > 10.2 > i586 > media > contrib > by-pkgid > 7457b841ac8136d3a1a9d3d960c5252e > files > 1280

libcryptopp-doc-5.2.1-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Crypto++: hrtimer.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.7 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div>
<h1>hrtimer.cpp</h1><pre class="fragment"><div>00001 <span class="comment">// hrtimer.cpp - written and placed in the public domain by Wei Dai</span>
00002 
00003 <span class="preprocessor">#include "pch.h"</span>
00004 <span class="preprocessor">#include "hrtimer.h"</span>
00005 <span class="preprocessor">#include "misc.h"</span>
00006 <span class="preprocessor">#include &lt;stddef.h&gt;</span>             <span class="comment">// for NULL</span>
00007 <span class="preprocessor">#include &lt;time.h&gt;</span>
00008 
00009 <span class="preprocessor">#if defined(CRYPTOPP_WIN32_AVAILABLE)</span>
00010 <span class="preprocessor"></span><span class="preprocessor">#include &lt;windows.h&gt;</span>
00011 <span class="preprocessor">#elif defined(CRYPTOPP_UNIX_AVAILABLE)</span>
00012 <span class="preprocessor"></span><span class="preprocessor">#include &lt;sys/time.h&gt;</span>
00013 <span class="preprocessor">#include &lt;sys/times.h&gt;</span>
00014 <span class="preprocessor">#include &lt;unistd.h&gt;</span>
00015 <span class="preprocessor">#endif</span>
00016 <span class="preprocessor"></span>
00017 <span class="preprocessor">#include &lt;assert.h&gt;</span>
00018 
00019 NAMESPACE_BEGIN(CryptoPP)
00020 
00021 double <a class="code" href="class_timer_base.html">TimerBase</a>::ConvertTo(word64 t, Unit unit)
00022 {
00023         <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000};
00024 
00025         assert(unit &lt; <span class="keyword">sizeof</span>(unitsPerSecondTable) / <span class="keyword">sizeof</span>(unitsPerSecondTable[0]));
00026         <span class="keywordflow">return</span> (<span class="keywordtype">double</span>)t * unitsPerSecondTable[unit] / TicksPerSecond();
00027 }
00028 
00029 <span class="keywordtype">void</span> TimerBase::StartTimer()
00030 {
00031         m_start = GetCurrentTimerValue();
00032         m_started = <span class="keyword">true</span>;
00033 }
00034 
00035 <span class="keywordtype">double</span> TimerBase::ElapsedTimeAsDouble()
00036 {
00037         <span class="keywordflow">if</span> (m_stuckAtZero)
00038                 <span class="keywordflow">return</span> 0;
00039         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (m_started)
00040                 <span class="keywordflow">return</span> ConvertTo(GetCurrentTimerValue() - m_start, m_timerUnit);
00041         <span class="keywordflow">else</span>
00042         {
00043                 StartTimer();
00044                 <span class="keywordflow">return</span> 0;
00045         }
00046 }
00047 
00048 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> TimerBase::ElapsedTime()
00049 {
00050         <span class="keywordtype">double</span> elapsed = ElapsedTimeAsDouble();
00051         assert(elapsed &lt;= ULONG_MAX);
00052         <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)elapsed;
00053 }
00054 
00055 word64 ThreadUserTimer::GetCurrentTimerValue()
00056 {
00057 <span class="preprocessor">#if defined(CRYPTOPP_WIN32_AVAILABLE)</span>
00058 <span class="preprocessor"></span>        <span class="keyword">static</span> <span class="keywordtype">bool</span> getCurrentThreadImplemented = <span class="keyword">true</span>;
00059         <span class="keywordflow">if</span> (getCurrentThreadImplemented)
00060         {
00061                 FILETIME now, ignored;
00062                 <span class="keywordflow">if</span> (!GetThreadTimes(GetCurrentThread(), &amp;ignored, &amp;ignored, &amp;ignored, &amp;now))
00063                 {
00064                         DWORD lastError = GetLastError();
00065                         <span class="keywordflow">if</span> (lastError == ERROR_CALL_NOT_IMPLEMENTED)
00066                         {
00067                                 getCurrentThreadImplemented = <span class="keyword">false</span>;
00068                                 <span class="keywordflow">goto</span> GetCurrentThreadNotImplemented;
00069                         }
00070                         <span class="keywordflow">throw</span> <a class="code" href="class_exception.html">Exception</a>(Exception::OTHER_ERROR, <span class="stringliteral">"ThreadUserTimer: GetThreadTimes failed with error "</span> + IntToString(lastError));
00071                 }
00072                 <span class="keywordflow">return</span> now.dwLowDateTime + ((word64)now.dwHighDateTime &lt;&lt; 32);
00073         }
00074 GetCurrentThreadNotImplemented:
00075         <span class="keywordflow">return</span> (word64)clock() * (10*1000*1000 / CLOCKS_PER_SEC);
00076 <span class="preprocessor">#elif defined(CRYPTOPP_UNIX_AVAILABLE)</span>
00077 <span class="preprocessor"></span>        tms now;
00078         times(&amp;now);
00079         <span class="keywordflow">return</span> now.tms_utime;
00080 <span class="preprocessor">#else</span>
00081 <span class="preprocessor"></span>        <span class="keywordflow">return</span> clock();
00082 <span class="preprocessor">#endif</span>
00083 <span class="preprocessor"></span>}
00084 
00085 word64 ThreadUserTimer::TicksPerSecond()
00086 {
00087 <span class="preprocessor">#if defined(CRYPTOPP_WIN32_AVAILABLE)</span>
00088 <span class="preprocessor"></span>        <span class="keywordflow">return</span> 10*1000*1000;
00089 <span class="preprocessor">#elif defined(CRYPTOPP_UNIX_AVAILABLE)</span>
00090 <span class="preprocessor"></span>        <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">long</span> ticksPerSecond = sysconf(_SC_CLK_TCK);
00091         <span class="keywordflow">return</span> ticksPerSecond;
00092 <span class="preprocessor">#else</span>
00093 <span class="preprocessor"></span>        <span class="keywordflow">return</span> CLOCKS_PER_SEC;
00094 <span class="preprocessor">#endif</span>
00095 <span class="preprocessor"></span>}
00096 
00097 <span class="preprocessor">#ifdef HIGHRES_TIMER_AVAILABLE</span>
00098 <span class="preprocessor"></span>
00099 word64 Timer::GetCurrentTimerValue()
00100 {
00101 <span class="preprocessor">#if defined(CRYPTOPP_WIN32_AVAILABLE)</span>
00102 <span class="preprocessor"></span>        LARGE_INTEGER now;
00103         <span class="keywordflow">if</span> (!QueryPerformanceCounter(&amp;now))
00104                 <span class="keywordflow">throw</span> <a class="code" href="class_exception.html">Exception</a>(Exception::OTHER_ERROR, <span class="stringliteral">"Timer: QueryPerformanceCounter failed with error "</span> + IntToString(GetLastError()));
00105         <span class="keywordflow">return</span> now.QuadPart;
00106 <span class="preprocessor">#elif defined(CRYPTOPP_UNIX_AVAILABLE)</span>
00107 <span class="preprocessor"></span>        timeval now;
00108         gettimeofday(&amp;now, NULL);
00109         <span class="keywordflow">return</span> (word64)now.tv_sec * 1000000 + now.tv_usec;
00110 <span class="preprocessor">#endif</span>
00111 <span class="preprocessor"></span>}
00112 
00113 word64 Timer::TicksPerSecond()
00114 {
00115 <span class="preprocessor">#if defined(CRYPTOPP_WIN32_AVAILABLE)</span>
00116 <span class="preprocessor"></span>        <span class="keyword">static</span> LARGE_INTEGER freq = {0};
00117         <span class="keywordflow">if</span> (freq.QuadPart == 0)
00118         {
00119                 <span class="keywordflow">if</span> (!QueryPerformanceFrequency(&amp;freq))
00120                         <span class="keywordflow">throw</span> <a class="code" href="class_exception.html">Exception</a>(Exception::OTHER_ERROR, <span class="stringliteral">"Timer: QueryPerformanceFrequency failed with error "</span> + IntToString(GetLastError()));
00121         }
00122         <span class="keywordflow">return</span> freq.QuadPart;
00123 <span class="preprocessor">#elif defined(CRYPTOPP_UNIX_AVAILABLE)</span>
00124 <span class="preprocessor"></span>        <span class="keywordflow">return</span> 1000000;
00125 <span class="preprocessor">#endif</span>
00126 <span class="preprocessor"></span>}
00127 
00128 <span class="preprocessor">#endif</span>
00129 <span class="preprocessor"></span>
00130 NAMESPACE_END
</div></pre><hr size="1"><address style="align: right;"><small>Generated on Sun Nov 7 08:23:57 2004 for Crypto++ by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 ></a> 1.3.7 </small></address>
</body>
</html>