Determine HZ exactly as in apps/speed.c.
[openssl.git] / crypto / tmdiff.c
index a5b1c8b6c3102cddbcf0ca7c1404c8ff9123e626..307523ebba3b96248383b7911178d9809df8ee1e 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include "cryptlib.h"
-#include "tmdiff.h"
+#include <openssl/tmdiff.h>
+#if !defined(OPENSSL_SYS_MSDOS)
+#include OPENSSL_UNISTD
+#endif
 
 #ifdef TIMEB
-#undef WIN32
+#undef OPENSSL_SYS_WIN32
 #undef TIMES
 #endif
 
-#ifndef MSDOS
-#  ifndef WIN32
-#    define TIMES
-#  endif
+#if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32) && !(defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX_RHAPSODY) && !defined(OPENSSL_SYS_VXWORKS)
+# define TIMES
 #endif
 
-#ifndef VMS
-#  ifndef _IRIX
-#   include <time.h>
-#  endif
-#  ifdef TIMES
-#    include <sys/types.h>
-#    include <sys/times.h>
-#  endif
-#else /* VMS */
-#  include <types.h>
-       struct tms {
-               time_t tms_utime;
-               time_t tms_stime;
-               time_t tms_uchild;      /* I dunno...  */
-               time_t tms_uchildsys;   /* so these names are a guess :-) */
-               }
-#endif /* VMS */
+#ifndef _IRIX
+#  include <time.h>
+#endif
+#ifdef TIMES
+#  include <sys/types.h>
+#  include <sys/times.h>
+#endif
+
+/* Depending on the VMS version, the tms structure is perhaps defined.
+   The __TMS macro will show if it was.  If it wasn't defined, we should
+   undefine TIMES, since that tells the rest of the program how things
+   should be handled.                          -- Richard Levitte */
+#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
+#undef TIMES
+#endif
 
 #if defined(sun) || defined(__ultrix)
 #define _POSIX_SOURCE
 #include <sys/param.h>
 #endif
 
-#ifndef TIMES
+#if !defined(TIMES) && !defined(OPENSSL_SYS_VXWORKS)
 #include <sys/timeb.h>
 #endif
 
-#ifdef WIN32
+#ifdef OPENSSL_SYS_WIN32
 #include <windows.h>
 #endif
 
 /* The following if from times(3) man page.  It may need to be changed */
 #ifndef HZ
-# ifndef CLK_TCK
-#  ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
-#   ifndef VMS
-#    define HZ  100.0
-#   else /* VMS */
+# if defined(_SC_CLK_TCK) \
+     && (!defined(OPENSSL_SYS_VMS) || __CTRL_VER >= 70000000)
+#  define HZ ((double)sysconf(_SC_CLK_TCK))
+# else
+#  ifndef CLK_TCK
+#   ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
 #    define HZ  100.0
+#   else /* _BSD_CLK_TCK_ */
+#    define HZ ((double)_BSD_CLK_TCK_)
 #   endif
-#  else /* _BSD_CLK_TCK_ */
-#   define HZ ((double)_BSD_CLK_TCK_)
+#  else /* CLK_TCK */
+#   define HZ ((double)CLK_TCK)
 #  endif
-# else /* CLK_TCK */
-#  define HZ ((double)CLK_TCK)
 # endif
 #endif
 
@@ -125,57 +125,62 @@ typedef struct ms_tm
 #ifdef TIMES
        struct tms ms_tms;
 #else
-#  ifdef WIN32
+#  ifdef OPENSSL_SYS_WIN32
        HANDLE thread_id;
        FILETIME ms_win32;
 #  else
+#    ifdef OPENSSL_SYS_VXWORKS
+          unsigned long ticks;
+#    else
        struct timeb ms_timeb;
+#    endif
 #  endif
 #endif
        } MS_TM;
 
-char *ms_time_new()
+char *ms_time_new(void)
        {
        MS_TM *ret;
 
-       ret=(MS_TM *)Malloc(sizeof(MS_TM));
+       ret=(MS_TM *)OPENSSL_malloc(sizeof(MS_TM));
        if (ret == NULL)
                return(NULL);
        memset(ret,0,sizeof(MS_TM));
-#ifdef WIN32
+#ifdef OPENSSL_SYS_WIN32
        ret->thread_id=GetCurrentThread();
 #endif
        return((char *)ret);
        }
 
-void ms_time_free(a)
-char *a;
+void ms_time_free(char *a)
        {
        if (a != NULL)
-               Free(a);
+               OPENSSL_free(a);
        }
 
-void ms_time_get(a)
-char *a;
+void ms_time_get(char *a)
        {
        MS_TM *tm=(MS_TM *)a;
-#ifdef WIN32
+#ifdef OPENSSL_SYS_WIN32
        FILETIME tmpa,tmpb,tmpc;
 #endif
 
 #ifdef TIMES
        times(&tm->ms_tms);
 #else
-#  ifdef WIN32
+#  ifdef OPENSSL_SYS_WIN32
        GetThreadTimes(tm->thread_id,&tmpa,&tmpb,&tmpc,&(tm->ms_win32));
 #  else
+#    ifdef OPENSSL_SYS_VXWORKS
+        tm->ticks = tickGet();
+#    else
        ftime(&tm->ms_timeb);
+#    endif
 #  endif
 #endif
        }
 
-double ms_time_diff(ap,bp)
-char *ap,*bp;
+double ms_time_diff(char *ap, char *bp)
        {
        MS_TM *a=(MS_TM *)ap;
        MS_TM *b=(MS_TM *)bp;
@@ -184,9 +189,13 @@ char *ap,*bp;
 #ifdef TIMES
        ret=(b->ms_tms.tms_utime-a->ms_tms.tms_utime)/HZ;
 #else
-# ifdef WIN32
+# ifdef OPENSSL_SYS_WIN32
        {
+#ifdef __GNUC__
+       signed long long la,lb;
+#else
        signed _int64 la,lb;
+#endif
        la=a->ms_win32.dwHighDateTime;
        lb=b->ms_win32.dwHighDateTime;
        la<<=32;
@@ -196,16 +205,19 @@ char *ap,*bp;
        ret=((double)(lb-la))/1e7;
        }
 # else
+#  ifdef OPENSSL_SYS_VXWORKS
+        ret = (double)(b->ticks - a->ticks) / (double)sysClkRateGet();
+#  else
        ret=     (double)(b->ms_timeb.time-a->ms_timeb.time)+
                (((double)b->ms_timeb.millitm)-
                ((double)a->ms_timeb.millitm))/1000.0;
 #  endif
+# endif
 #endif
        return((ret < 0.0000001)?0.0000001:ret);
        }
 
-int ms_time_cmp(ap,bp)
-char *ap,*bp;
+int ms_time_cmp(char *ap, char *bp)
        {
        MS_TM *a=(MS_TM *)ap,*b=(MS_TM *)bp;
        double d;
@@ -214,13 +226,17 @@ char *ap,*bp;
 #ifdef TIMES
        d=(b->ms_tms.tms_utime-a->ms_tms.tms_utime)/HZ;
 #else
-# ifdef WIN32
+# ifdef OPENSSL_SYS_WIN32
        d =(b->ms_win32.dwHighDateTime&0x000fffff)*10+b->ms_win32.dwLowDateTime/1e7;
        d-=(a->ms_win32.dwHighDateTime&0x000fffff)*10+a->ms_win32.dwLowDateTime/1e7;
 # else
+#  ifdef OPENSSL_SYS_VXWORKS
+        d = (b->ticks - a->ticks);
+#  else
        d=       (double)(b->ms_timeb.time-a->ms_timeb.time)+
                (((double)b->ms_timeb.millitm)-(double)a->ms_timeb.millitm)/1000.0;
 #  endif
+# endif
 #endif
        if (d == 0.0)
                ret=0;