Version 6.3
Copyright © 2005 - 2012 Lowell D. Thomas
APG
  … ABNF Parser Generator
All Data Structures Files Functions Variables Typedefs Macros Pages
timer.c
Go to the documentation of this file.
1 /*******************************************************************************
2  APG Version 6.3
3  Copyright (C) 2005 - 2012 Lowell D. Thomas, all rights reserved
4 
5  author: Lowell D. Thomas
6  email: lowell@coasttocoastresearch.com
7  website: http://www.coasttocoastresearch.com
8 
9  This program is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see
21  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
22  or write to the Free Software Foundation, Inc.,
23  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 *******************************************************************************/
25 #include "ApgUtilities.h"
26 
33 typedef struct
34 {
35  void* vpMemCtx;
36  apg_uint uiStarted;
37  double dStartTime;
38  double dEndTime;
39  double dDuration; // milliseconds
40  double dClocksPerSec;
41  void* vpValidate;
42 } APG_TIMER;
51 void* vpTimerCtor(void* vpMemCtx)
52 {
53  void* vpRet = NULL;
54  APG_TIMER* spCtx = NULL;
55  while(APG_TRUE)
56  {
57 
58  if(!vpMemCtx){break;}
59  if(!uiMemValidate(vpMemCtx)){break;}
60  spCtx = (APG_TIMER*)vpMemAlloc(vpMemCtx, sizeof(APG_TIMER));
61  if(!spCtx){break;}
62  memset((void*)spCtx, 0, sizeof(APG_TIMER));
63  spCtx->vpMemCtx = vpMemCtx;
64  spCtx->dClocksPerSec = (double)CLOCKS_PER_SEC;
65  spCtx->vpValidate = (void*)spCtx;
66  vpRet = (void*)spCtx;
67  break;
68  }
69  if(vpMemCtx)
70  {
71  if(!vpRet && spCtx){vMemFree(vpMemCtx, (void*)spCtx);}
72  }
73  return vpRet;
74 }
75 
80 void vTimerDtor(void* vpCtx)
81 {
82  APG_TIMER* spCtx = (APG_TIMER*)vpCtx;
83  if(vpCtx && spCtx->vpValidate == vpCtx)
84  {
85  spCtx->vpValidate = NULL;
86  vMemFree(spCtx->vpMemCtx, vpCtx);
87  }
88 }
89 
96 apg_uint uiTimerStart(void* vpCtx)
97 {
98  apg_uint uiRet = APG_FALSE;
99  APG_TIMER* spCtx = (APG_TIMER*)vpCtx;
100  if(vpCtx && spCtx->vpValidate == vpCtx)
101  {
102  spCtx->dEndTime = 0.0;
103  spCtx->dDuration = 0.0;
104  spCtx->dStartTime = (double)clock();
105  spCtx->uiStarted = APG_TRUE;
106  uiRet = APG_TRUE;
107  }
108  return uiRet;
109 }
110 
117 apg_uint uiTimerStop(void* vpCtx)
118 {
119  apg_uint uiRet = APG_FALSE;
120  APG_TIMER* spCtx = (APG_TIMER*)vpCtx;
121  if(vpCtx && spCtx->vpValidate == vpCtx && spCtx->uiStarted)
122  {
123  spCtx->dEndTime = (double)clock();
124  spCtx->dDuration = (spCtx->dEndTime - spCtx->dStartTime) / spCtx->dClocksPerSec;
125  uiRet = APG_TRUE;
126  }
127  return uiRet;
128 }
129 
151 apg_uint uiTimerRates(void* vpCtx, double* dpRates, apg_uint uiLen, double dConversion)
152 {
153  apg_uint uiRet = APG_FALSE;
154  APG_TIMER* spCtx = (APG_TIMER*)vpCtx;
155  if(vpCtx && spCtx->vpValidate == vpCtx && spCtx->dDuration != 0.0)
156  {
157  while(uiLen--)
158  {
159  if(*dpRates != 0.0)
160  {
161  *dpRates = *dpRates/( dConversion * spCtx->dDuration);
162  }
163  dpRates++;
164  }
165  uiRet = APG_TRUE;
166  }
167  return uiRet;
168 }
169 
185 double dTimerStartTime(void* vpCtx, double dConversion)
186 {
187  double dRet = 0.0;
188  APG_TIMER* spCtx = (APG_TIMER*)vpCtx;
189  if(vpCtx && spCtx->vpValidate == vpCtx)
190  {
191  dRet = dConversion * spCtx->dStartTime;
192  }
193  return dRet;
194 }
195 
211 double dTimerStopTime(void* vpCtx, double dConversion)
212 {
213  double dRet = 0.0;
214  APG_TIMER* spCtx = (APG_TIMER*)vpCtx;
215  if(vpCtx && spCtx->vpValidate == vpCtx)
216  {
217  dRet = dConversion * spCtx->dEndTime;
218  }
219  return dRet;
220 }
221 
237 double dTimerDuration(void* vpCtx, double dConversion)
238 {
239  double dRet = 0.0;
240  APG_TIMER* spCtx = (APG_TIMER*)vpCtx;
241  if(vpCtx && spCtx->vpValidate == vpCtx)
242  {
243  dRet = dConversion * spCtx->dDuration;
244  }
245  return dRet;
246 }
vpTimerCtor
void * vpTimerCtor(void *vpMemCtx)
Definition: timer.c:51
dTimerStopTime
double dTimerStopTime(void *vpCtx, double dConversion)
Definition: timer.c:211
uiTimerRates
apg_uint uiTimerRates(void *vpCtx, double *dpRates, apg_uint uiLen, double dConversion)
Definition: timer.c:151
apg_uint
unsigned int apg_uint
Definition: Apg.h:169
uiTimerStop
apg_uint uiTimerStop(void *vpCtx)
Definition: timer.c:117
vMemFree
void vMemFree(void *vpCtx, void *vpData)
Definition: Memory.c:157
vpMemAlloc
void * vpMemAlloc(void *vpCtx, apg_uint uiBytes)
Definition: Memory.c:130
uiTimerStart
apg_uint uiTimerStart(void *vpCtx)
Definition: timer.c:96
dTimerDuration
double dTimerDuration(void *vpCtx, double dConversion)
Definition: timer.c:237
APG_TRUE
#define APG_TRUE
Definition: Apg.h:187
dTimerStartTime
double dTimerStartTime(void *vpCtx, double dConversion)
Definition: timer.c:185
ApgUtilities.h
Declarations of all APG utility functions.
APG_FALSE
#define APG_FALSE
Definition: Apg.h:190
vTimerDtor
void vTimerDtor(void *vpCtx)
Definition: timer.c:80
uiMemValidate
apg_uint uiMemValidate(void *vpCtx)
Definition: Memory.c:115
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/licenses.html or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.