mt.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002  * Copyright (c) 2006, Novell, Inc.
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions are 
00007  * met:
00008  * 
00009  *     * Redistributions of source code must retain the above copyright 
00010  *       notice, this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright 
00012  *       notice, this list of conditions and the following disclaimer in the 
00013  *       documentation and/or other materials provided with the distribution.
00014  *     * Neither the name of the Novell nor the names of its contributors 
00015  *       may be used to endorse or promote products derived from this 
00016  *       software without specific prior written permission.
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00020  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00021  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
00026  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *--------------------------------------------------------------------------*/
00030 
00039 #include "mt.h"
00040 
00041 #include <string.h>
00042 #include <stdlib.h>
00043 
00044 #ifdef _WIN32
00045 # include <windows.h>
00046 #else
00047 # include <time.h>
00048 #endif
00049 
00054 #ifdef _WIN32
00055 # define mt_sint64 __int64
00056 # define mt_uint64 unsigned mt_sint64
00057 #else
00058 # define mt_sint64 long long
00059 # define mt_uint64 unsigned mt_sint64
00060 #endif
00061 
00064 typedef struct mt_tag
00065 {
00066    int sz;              
00067    int cp;              
00068    mt_uint64 freq;      
00069    mt_uint64 times[1];  
00070 } MTimer;
00071 
00075 /*---------------------------------------------------------------------------
00076  */
00077 MultiTimer MtCreate(int count)
00078 {
00079    MTimer * t;
00080 
00081    t = (MTimer *)malloc(sizeof(*t) + count * sizeof(t->times));
00082    if (t != 0)
00083    {
00084       memset(t, 0, sizeof(*t) + count * sizeof(t->times));
00085 #ifdef _WIN32
00086       {
00087          LARGE_INTEGER fr, st;
00088          QueryPerformanceFrequency(&fr);
00089          t->freq = ((mt_uint64)fr.HighPart << 32) + fr.LowPart;
00090          QueryPerformanceCounter(&st);
00091          t->times[0] = ((mt_uint64)st.HighPart << 32) + st.LowPart;
00092       }
00093 #else
00094       t->freq = CLOCKS_PER_SEC;
00095       t->times[0] = clock();
00096 #endif
00097       t->sz = count;
00098    }
00099    return (MultiTimer *)t;
00100 }
00101 
00102 /*---------------------------------------------------------------------------
00103  */
00104 void MtRecord(MultiTimer mt)
00105 {
00106    MTimer * t = (MTimer *)mt;
00107 
00108    if (t->cp < t->sz)
00109    {
00110 #ifdef _WIN32
00111       LARGE_INTEGER tm;
00112       QueryPerformanceCounter(&tm);
00113       t->times[++t->cp] = ((mt_uint64)tm.HighPart << 32) + tm.LowPart;
00114 #else
00115       t->times[++t->cp] = clock();
00116 #endif
00117    }
00118 }
00119 
00120 /*---------------------------------------------------------------------------
00121  */
00122 unsigned long MtGetRelativeNS(MultiTimer mt, int idx)
00123 {
00124 #define NS_PER_SEC 1000000000
00125 
00126    MTimer * t = (MTimer *)mt;
00127 
00128    if (idx > t->cp)
00129       return 0;
00130    return (unsigned long)((((t->times[idx + 1] - t->times[idx]) 
00131          * NS_PER_SEC) / t->freq) & 0xFFFFFFFF);
00132 }
00133 
00134 /*---------------------------------------------------------------------------
00135  */
00136 void MtDestroy(MultiTimer mt) 
00137 {
00138    free(mt); 
00139 }
00140 

Generated on Thu Aug 20 22:33:05 2009 for OpenXDAS by  doxygen 1.5.6