Fossil SCM

Reworked the timer IDs to be positive values to simplify error checking a bit.

stephan 2013-05-08 20:09 trunk
Commit 799458977efbd13267b415156ed934c2617e7be4
2 files changed +4 -2 +25 -21
+4 -2
--- src/json.c
+++ src/json.c
@@ -693,13 +693,15 @@
693693
** up. e.g. it must not use cgi_parameter() and friends because this
694694
** must be called before those data are initialized.
695695
*/
696696
void json_main_bootstrap(){
697697
cson_value * v;
698
+ assert( (NULL == g.json.gc.v) &&
699
+ "json_main_bootstrap() was called twice!" );
700
+
698701
g.json.timerId = fossil_timer_start();
699
- assert( (NULL == g.json.gc.v) && "cgi_json_bootstrap() was called twice!" );
700
-
702
+
701703
/* g.json.gc is our "garbage collector" - where we put JSON values
702704
which need a long lifetime but don't have a logical parent to put
703705
them in.
704706
*/
705707
v = cson_value_new_array();
706708
--- src/json.c
+++ src/json.c
@@ -693,13 +693,15 @@
693 ** up. e.g. it must not use cgi_parameter() and friends because this
694 ** must be called before those data are initialized.
695 */
696 void json_main_bootstrap(){
697 cson_value * v;
 
 
 
698 g.json.timerId = fossil_timer_start();
699 assert( (NULL == g.json.gc.v) && "cgi_json_bootstrap() was called twice!" );
700
701 /* g.json.gc is our "garbage collector" - where we put JSON values
702 which need a long lifetime but don't have a logical parent to put
703 them in.
704 */
705 v = cson_value_new_array();
706
--- src/json.c
+++ src/json.c
@@ -693,13 +693,15 @@
693 ** up. e.g. it must not use cgi_parameter() and friends because this
694 ** must be called before those data are initialized.
695 */
696 void json_main_bootstrap(){
697 cson_value * v;
698 assert( (NULL == g.json.gc.v) &&
699 "json_main_bootstrap() was called twice!" );
700
701 g.json.timerId = fossil_timer_start();
702
 
703 /* g.json.gc is our "garbage collector" - where we put JSON values
704 which need a long lifetime but don't have a logical parent to put
705 them in.
706 */
707 v = cson_value_new_array();
708
+25 -21
--- src/util.c
+++ src/util.c
@@ -183,11 +183,11 @@
183183
** Internal helper type for fossil_timer_xxx().
184184
*/
185185
static struct FossilTimer {
186186
sqlite3_uint64 u; /* "User" CPU times */
187187
sqlite3_uint64 s; /* "System" CPU times */
188
- char used; /* 1 if timer is allocated, else 0. */
188
+ int id; /* positive if allocated, else 0. */
189189
} fossilTimer = { 0U, 0U, 0 };
190190
enum FossilTimerEnum {
191191
FOSSIL_TIMER_COUNT = 10 /* Number of timers we can stack. */
192192
};
193193
static struct FossilTimer fossilTimerList[FOSSIL_TIMER_COUNT] = {{0,0,0}};
@@ -201,12 +201,12 @@
201201
** The system has a fixed number of timers, and they can be
202202
** "deallocated" by passing this function's return value to
203203
** fossil_timer_stop() Adjust FOSSIL_TIMER_COUNT to set the number of
204204
** available timers.
205205
**
206
-** Returns -1 on error (no more timers available), with 0 or greater
207
-** being valid timer IDs.
206
+** Returns 0 on error (no more timers available), with 1+ being valid
207
+** timer IDs.
208208
*/
209209
int fossil_timer_start(){
210210
int i;
211211
static char once = 0;
212212
if(!once){
@@ -213,30 +213,30 @@
213213
memset(&fossilTimerList, 0,
214214
sizeof(fossilTimerList)/sizeof(fossilTimerList[0]));
215215
}
216216
for( i = 0; i < FOSSIL_TIMER_COUNT; ++i ){
217217
struct FossilTimer * ft = &fossilTimerList[i];
218
- if(ft->used) continue;
219
- ft->used = 1;
218
+ if(ft->id) continue;
219
+ ft->id = i+1;
220220
fossil_cpu_times( &ft->u, &ft->s );
221221
break;
222222
}
223
- return (i<FOSSIL_TIMER_COUNT) ? i : -1;
223
+ return (i<FOSSIL_TIMER_COUNT) ? i+1 : 0;
224224
}
225225
226226
/*
227227
** Returns the difference in CPU times in microseconds since
228228
** fossil_timer_start() was called and returned the given timer ID (or
229229
** since it was last reset). Returns 0 if timerId is out of range.
230230
*/
231231
sqlite3_uint64 fossil_timer_fetch(int timerId){
232
- if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){
232
+ if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){
233233
return 0;
234234
}else{
235
- struct FossilTimer * start = &fossilTimerList[timerId];
236
- if( ! start->used ){
237
- fossil_fatal("Invalid call to reset a non-allocated "
235
+ struct FossilTimer * start = &fossilTimerList[timerId-1];
236
+ if( !start->id ){
237
+ fossil_fatal("Invalid call to fetch a non-allocated "
238238
"timer (#%d)", timerId);
239239
/*NOTREACHED*/
240240
}else{
241241
sqlite3_uint64 eu = 0, es = 0;
242242
fossil_cpu_times( &eu, &es );
@@ -248,15 +248,15 @@
248248
/*
249249
** Resets the timer associated with the given ID, as obtained via
250250
** fossil_timer_start(), to the current CPU time values.
251251
*/
252252
sqlite3_uint64 fossil_timer_reset(int timerId){
253
- if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){
253
+ if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){
254254
return 0;
255255
}else{
256
- struct FossilTimer * start = &fossilTimerList[timerId];
257
- if( ! start->used ){
256
+ struct FossilTimer * start = &fossilTimerList[timerId-1];
257
+ if( !start->id ){
258258
fossil_fatal("Invalid call to reset a non-allocated "
259259
"timer (#%d)", timerId);
260260
/*NOTREACHED*/
261261
}else{
262262
sqlite3_uint64 const rc = fossil_timer_fetch(timerId);
@@ -267,22 +267,24 @@
267267
}
268268
269269
/**
270270
"Deallocates" the fossil timer identified by the given timer ID.
271271
returns the difference (in uSec) between the last time that timer
272
- was started or reset. Returns 0 if timerId is out of range. It is
273
- not legal to re-use the passed-in timerId after calling this
274
- until/unless it is re-initialized using fossil_timer_start() (NOT
272
+ was started or reset. Returns 0 if timerId is out of range (but
273
+ note that, due to system-level precision restrictions, this
274
+ function might return 0 on success, too!). It is not legal to
275
+ re-use the passed-in timerId after calling this until/unless it is
276
+ re-initialized using fossil_timer_start() (NOT
275277
fossil_timer_reset()).
276278
*/
277279
sqlite3_uint64 fossil_timer_stop(int timerId){
278
- if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){
280
+ if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){
279281
return 0;
280282
}else{
281283
sqlite3_uint64 const rc = fossil_timer_fetch(timerId);
282
- struct FossilTimer * t = &fossilTimerList[timerId];
283
- t->used = 0;
284
+ struct FossilTimer * t = &fossilTimerList[timerId-1];
285
+ t->id = 0;
284286
t->u = t->s = 0U;
285287
return rc;
286288
}
287289
}
288290
@@ -289,11 +291,13 @@
289291
/*
290292
** Returns true (non-0) if the given timer ID (as returned from
291293
** fossil_timer_start() is currently active.
292294
*/
293295
int fossil_timer_is_active( int timerId ){
294
- if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){
296
+ if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){
295297
return 0;
296298
}else{
297
- return fossilTimerList[timerId].used;
299
+ int const rc = fossilTimerList[timerId-1].id;
300
+ assert(!rc || (rc == timerId));
301
+ return fossilTimerList[timerId-1].id;
298302
}
299303
}
300304
--- src/util.c
+++ src/util.c
@@ -183,11 +183,11 @@
183 ** Internal helper type for fossil_timer_xxx().
184 */
185 static struct FossilTimer {
186 sqlite3_uint64 u; /* "User" CPU times */
187 sqlite3_uint64 s; /* "System" CPU times */
188 char used; /* 1 if timer is allocated, else 0. */
189 } fossilTimer = { 0U, 0U, 0 };
190 enum FossilTimerEnum {
191 FOSSIL_TIMER_COUNT = 10 /* Number of timers we can stack. */
192 };
193 static struct FossilTimer fossilTimerList[FOSSIL_TIMER_COUNT] = {{0,0,0}};
@@ -201,12 +201,12 @@
201 ** The system has a fixed number of timers, and they can be
202 ** "deallocated" by passing this function's return value to
203 ** fossil_timer_stop() Adjust FOSSIL_TIMER_COUNT to set the number of
204 ** available timers.
205 **
206 ** Returns -1 on error (no more timers available), with 0 or greater
207 ** being valid timer IDs.
208 */
209 int fossil_timer_start(){
210 int i;
211 static char once = 0;
212 if(!once){
@@ -213,30 +213,30 @@
213 memset(&fossilTimerList, 0,
214 sizeof(fossilTimerList)/sizeof(fossilTimerList[0]));
215 }
216 for( i = 0; i < FOSSIL_TIMER_COUNT; ++i ){
217 struct FossilTimer * ft = &fossilTimerList[i];
218 if(ft->used) continue;
219 ft->used = 1;
220 fossil_cpu_times( &ft->u, &ft->s );
221 break;
222 }
223 return (i<FOSSIL_TIMER_COUNT) ? i : -1;
224 }
225
226 /*
227 ** Returns the difference in CPU times in microseconds since
228 ** fossil_timer_start() was called and returned the given timer ID (or
229 ** since it was last reset). Returns 0 if timerId is out of range.
230 */
231 sqlite3_uint64 fossil_timer_fetch(int timerId){
232 if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){
233 return 0;
234 }else{
235 struct FossilTimer * start = &fossilTimerList[timerId];
236 if( ! start->used ){
237 fossil_fatal("Invalid call to reset a non-allocated "
238 "timer (#%d)", timerId);
239 /*NOTREACHED*/
240 }else{
241 sqlite3_uint64 eu = 0, es = 0;
242 fossil_cpu_times( &eu, &es );
@@ -248,15 +248,15 @@
248 /*
249 ** Resets the timer associated with the given ID, as obtained via
250 ** fossil_timer_start(), to the current CPU time values.
251 */
252 sqlite3_uint64 fossil_timer_reset(int timerId){
253 if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){
254 return 0;
255 }else{
256 struct FossilTimer * start = &fossilTimerList[timerId];
257 if( ! start->used ){
258 fossil_fatal("Invalid call to reset a non-allocated "
259 "timer (#%d)", timerId);
260 /*NOTREACHED*/
261 }else{
262 sqlite3_uint64 const rc = fossil_timer_fetch(timerId);
@@ -267,22 +267,24 @@
267 }
268
269 /**
270 "Deallocates" the fossil timer identified by the given timer ID.
271 returns the difference (in uSec) between the last time that timer
272 was started or reset. Returns 0 if timerId is out of range. It is
273 not legal to re-use the passed-in timerId after calling this
274 until/unless it is re-initialized using fossil_timer_start() (NOT
 
 
275 fossil_timer_reset()).
276 */
277 sqlite3_uint64 fossil_timer_stop(int timerId){
278 if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){
279 return 0;
280 }else{
281 sqlite3_uint64 const rc = fossil_timer_fetch(timerId);
282 struct FossilTimer * t = &fossilTimerList[timerId];
283 t->used = 0;
284 t->u = t->s = 0U;
285 return rc;
286 }
287 }
288
@@ -289,11 +291,13 @@
289 /*
290 ** Returns true (non-0) if the given timer ID (as returned from
291 ** fossil_timer_start() is currently active.
292 */
293 int fossil_timer_is_active( int timerId ){
294 if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){
295 return 0;
296 }else{
297 return fossilTimerList[timerId].used;
 
 
298 }
299 }
300
--- src/util.c
+++ src/util.c
@@ -183,11 +183,11 @@
183 ** Internal helper type for fossil_timer_xxx().
184 */
185 static struct FossilTimer {
186 sqlite3_uint64 u; /* "User" CPU times */
187 sqlite3_uint64 s; /* "System" CPU times */
188 int id; /* positive if allocated, else 0. */
189 } fossilTimer = { 0U, 0U, 0 };
190 enum FossilTimerEnum {
191 FOSSIL_TIMER_COUNT = 10 /* Number of timers we can stack. */
192 };
193 static struct FossilTimer fossilTimerList[FOSSIL_TIMER_COUNT] = {{0,0,0}};
@@ -201,12 +201,12 @@
201 ** The system has a fixed number of timers, and they can be
202 ** "deallocated" by passing this function's return value to
203 ** fossil_timer_stop() Adjust FOSSIL_TIMER_COUNT to set the number of
204 ** available timers.
205 **
206 ** Returns 0 on error (no more timers available), with 1+ being valid
207 ** timer IDs.
208 */
209 int fossil_timer_start(){
210 int i;
211 static char once = 0;
212 if(!once){
@@ -213,30 +213,30 @@
213 memset(&fossilTimerList, 0,
214 sizeof(fossilTimerList)/sizeof(fossilTimerList[0]));
215 }
216 for( i = 0; i < FOSSIL_TIMER_COUNT; ++i ){
217 struct FossilTimer * ft = &fossilTimerList[i];
218 if(ft->id) continue;
219 ft->id = i+1;
220 fossil_cpu_times( &ft->u, &ft->s );
221 break;
222 }
223 return (i<FOSSIL_TIMER_COUNT) ? i+1 : 0;
224 }
225
226 /*
227 ** Returns the difference in CPU times in microseconds since
228 ** fossil_timer_start() was called and returned the given timer ID (or
229 ** since it was last reset). Returns 0 if timerId is out of range.
230 */
231 sqlite3_uint64 fossil_timer_fetch(int timerId){
232 if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){
233 return 0;
234 }else{
235 struct FossilTimer * start = &fossilTimerList[timerId-1];
236 if( !start->id ){
237 fossil_fatal("Invalid call to fetch a non-allocated "
238 "timer (#%d)", timerId);
239 /*NOTREACHED*/
240 }else{
241 sqlite3_uint64 eu = 0, es = 0;
242 fossil_cpu_times( &eu, &es );
@@ -248,15 +248,15 @@
248 /*
249 ** Resets the timer associated with the given ID, as obtained via
250 ** fossil_timer_start(), to the current CPU time values.
251 */
252 sqlite3_uint64 fossil_timer_reset(int timerId){
253 if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){
254 return 0;
255 }else{
256 struct FossilTimer * start = &fossilTimerList[timerId-1];
257 if( !start->id ){
258 fossil_fatal("Invalid call to reset a non-allocated "
259 "timer (#%d)", timerId);
260 /*NOTREACHED*/
261 }else{
262 sqlite3_uint64 const rc = fossil_timer_fetch(timerId);
@@ -267,22 +267,24 @@
267 }
268
269 /**
270 "Deallocates" the fossil timer identified by the given timer ID.
271 returns the difference (in uSec) between the last time that timer
272 was started or reset. Returns 0 if timerId is out of range (but
273 note that, due to system-level precision restrictions, this
274 function might return 0 on success, too!). It is not legal to
275 re-use the passed-in timerId after calling this until/unless it is
276 re-initialized using fossil_timer_start() (NOT
277 fossil_timer_reset()).
278 */
279 sqlite3_uint64 fossil_timer_stop(int timerId){
280 if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){
281 return 0;
282 }else{
283 sqlite3_uint64 const rc = fossil_timer_fetch(timerId);
284 struct FossilTimer * t = &fossilTimerList[timerId-1];
285 t->id = 0;
286 t->u = t->s = 0U;
287 return rc;
288 }
289 }
290
@@ -289,11 +291,13 @@
291 /*
292 ** Returns true (non-0) if the given timer ID (as returned from
293 ** fossil_timer_start() is currently active.
294 */
295 int fossil_timer_is_active( int timerId ){
296 if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){
297 return 0;
298 }else{
299 int const rc = fossilTimerList[timerId-1].id;
300 assert(!rc || (rc == timerId));
301 return fossilTimerList[timerId-1].id;
302 }
303 }
304

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button