Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

hrtimer.cpp

00001 // hrtimer.cpp - written and placed in the public domain by Wei Dai
00002 
00003 #include "pch.h"
00004 #include "hrtimer.h"
00005 #include <stddef.h>             // for NULL
00006 
00007 #ifdef HIGHRES_TIMER_AVAILABLE
00008 
00009 #if defined(CRYPTOPP_WIN32_AVAILABLE)
00010 #include <windows.h>
00011 #elif defined(CRYPTOPP_UNIX_AVAILABLE)
00012 #include <sys/time.h>
00013 #elif defined(macintosh)
00014 #include <Timer.h>
00015 #endif
00016 
00017 #include <assert.h>
00018 
00019 NAMESPACE_BEGIN(CryptoPP)
00020 
00021 word64 Timer::GetCurrentTimerValue()
00022 {
00023 #if defined(CRYPTOPP_WIN32_AVAILABLE)
00024         FILETIME now;
00025         GetSystemTimeAsFileTime(&now);
00026         return now.dwLowDateTime + ((word64)now.dwHighDateTime << 32);
00027 #elif defined(CRYPTOPP_UNIX_AVAILABLE)
00028         timeval now;
00029         gettimeofday(&now, NULL);
00030         return (word64)now.tv_sec * 1000000 + now.tv_usec;
00031 #elif defined(macintosh)
00032         UnsignedWide now;
00033         Microseconds(&now);
00034         return now.lo + ((word64)now.hi << 32);
00035 #endif
00036 }
00037 
00038 unsigned long Timer::ConvertTo(word64 t, Unit unit)
00039 {
00040         switch (unit)
00041         {
00042         case SECONDS:
00043                 return (unsigned long)(t / (TicksPerMillisecond() * 1000));
00044         case MILLISECONDS:
00045                 return (unsigned long)(t / TicksPerMillisecond());
00046         case MICROSECONDS:
00047                 assert(TicksPerMillisecond() % 1000 == 0);
00048                 return (unsigned long)(t / (TicksPerMillisecond() / 1000));
00049         }
00050         assert(false);
00051         return 0;
00052 }
00053 
00054 void Timer::StartTimer()
00055 {
00056         m_start = GetCurrentTimerValue();
00057         m_started = true;
00058 }
00059 
00060 unsigned long Timer::ElapsedTime()
00061 {
00062         if (m_stuckAtZero)
00063                 return 0;
00064         else if (m_started)
00065                 return ConvertTo(GetCurrentTimerValue() - m_start, m_timerUnit);
00066         else
00067         {
00068                 StartTimer();
00069                 return 0;
00070         }
00071 }
00072 
00073 NAMESPACE_END
00074 
00075 #endif

Generated on Sun Mar 14 20:44:26 2004 for Crypto++ by doxygen 1.3.6-20040222