Fossil SCM
Reworked the timer IDs to be positive values to simplify error checking a bit.
Commit
799458977efbd13267b415156ed934c2617e7be4
Parent
590406df3c5cb6d…
2 files changed
+4
-2
+25
-21
+4
-2
| --- src/json.c | ||
| +++ src/json.c | ||
| @@ -693,13 +693,15 @@ | ||
| 693 | 693 | ** up. e.g. it must not use cgi_parameter() and friends because this |
| 694 | 694 | ** must be called before those data are initialized. |
| 695 | 695 | */ |
| 696 | 696 | void json_main_bootstrap(){ |
| 697 | 697 | cson_value * v; |
| 698 | + assert( (NULL == g.json.gc.v) && | |
| 699 | + "json_main_bootstrap() was called twice!" ); | |
| 700 | + | |
| 698 | 701 | g.json.timerId = fossil_timer_start(); |
| 699 | - assert( (NULL == g.json.gc.v) && "cgi_json_bootstrap() was called twice!" ); | |
| 700 | - | |
| 702 | + | |
| 701 | 703 | /* g.json.gc is our "garbage collector" - where we put JSON values |
| 702 | 704 | which need a long lifetime but don't have a logical parent to put |
| 703 | 705 | them in. |
| 704 | 706 | */ |
| 705 | 707 | v = cson_value_new_array(); |
| 706 | 708 |
| --- 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 @@ | ||
| 183 | 183 | ** Internal helper type for fossil_timer_xxx(). |
| 184 | 184 | */ |
| 185 | 185 | static struct FossilTimer { |
| 186 | 186 | sqlite3_uint64 u; /* "User" CPU times */ |
| 187 | 187 | sqlite3_uint64 s; /* "System" CPU times */ |
| 188 | - char used; /* 1 if timer is allocated, else 0. */ | |
| 188 | + int id; /* positive if allocated, else 0. */ | |
| 189 | 189 | } fossilTimer = { 0U, 0U, 0 }; |
| 190 | 190 | enum FossilTimerEnum { |
| 191 | 191 | FOSSIL_TIMER_COUNT = 10 /* Number of timers we can stack. */ |
| 192 | 192 | }; |
| 193 | 193 | static struct FossilTimer fossilTimerList[FOSSIL_TIMER_COUNT] = {{0,0,0}}; |
| @@ -201,12 +201,12 @@ | ||
| 201 | 201 | ** The system has a fixed number of timers, and they can be |
| 202 | 202 | ** "deallocated" by passing this function's return value to |
| 203 | 203 | ** fossil_timer_stop() Adjust FOSSIL_TIMER_COUNT to set the number of |
| 204 | 204 | ** available timers. |
| 205 | 205 | ** |
| 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. | |
| 208 | 208 | */ |
| 209 | 209 | int fossil_timer_start(){ |
| 210 | 210 | int i; |
| 211 | 211 | static char once = 0; |
| 212 | 212 | if(!once){ |
| @@ -213,30 +213,30 @@ | ||
| 213 | 213 | memset(&fossilTimerList, 0, |
| 214 | 214 | sizeof(fossilTimerList)/sizeof(fossilTimerList[0])); |
| 215 | 215 | } |
| 216 | 216 | for( i = 0; i < FOSSIL_TIMER_COUNT; ++i ){ |
| 217 | 217 | struct FossilTimer * ft = &fossilTimerList[i]; |
| 218 | - if(ft->used) continue; | |
| 219 | - ft->used = 1; | |
| 218 | + if(ft->id) continue; | |
| 219 | + ft->id = i+1; | |
| 220 | 220 | fossil_cpu_times( &ft->u, &ft->s ); |
| 221 | 221 | break; |
| 222 | 222 | } |
| 223 | - return (i<FOSSIL_TIMER_COUNT) ? i : -1; | |
| 223 | + return (i<FOSSIL_TIMER_COUNT) ? i+1 : 0; | |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | /* |
| 227 | 227 | ** Returns the difference in CPU times in microseconds since |
| 228 | 228 | ** fossil_timer_start() was called and returned the given timer ID (or |
| 229 | 229 | ** since it was last reset). Returns 0 if timerId is out of range. |
| 230 | 230 | */ |
| 231 | 231 | sqlite3_uint64 fossil_timer_fetch(int timerId){ |
| 232 | - if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){ | |
| 232 | + if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){ | |
| 233 | 233 | return 0; |
| 234 | 234 | }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 " | |
| 238 | 238 | "timer (#%d)", timerId); |
| 239 | 239 | /*NOTREACHED*/ |
| 240 | 240 | }else{ |
| 241 | 241 | sqlite3_uint64 eu = 0, es = 0; |
| 242 | 242 | fossil_cpu_times( &eu, &es ); |
| @@ -248,15 +248,15 @@ | ||
| 248 | 248 | /* |
| 249 | 249 | ** Resets the timer associated with the given ID, as obtained via |
| 250 | 250 | ** fossil_timer_start(), to the current CPU time values. |
| 251 | 251 | */ |
| 252 | 252 | sqlite3_uint64 fossil_timer_reset(int timerId){ |
| 253 | - if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){ | |
| 253 | + if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){ | |
| 254 | 254 | return 0; |
| 255 | 255 | }else{ |
| 256 | - struct FossilTimer * start = &fossilTimerList[timerId]; | |
| 257 | - if( ! start->used ){ | |
| 256 | + struct FossilTimer * start = &fossilTimerList[timerId-1]; | |
| 257 | + if( !start->id ){ | |
| 258 | 258 | fossil_fatal("Invalid call to reset a non-allocated " |
| 259 | 259 | "timer (#%d)", timerId); |
| 260 | 260 | /*NOTREACHED*/ |
| 261 | 261 | }else{ |
| 262 | 262 | sqlite3_uint64 const rc = fossil_timer_fetch(timerId); |
| @@ -267,22 +267,24 @@ | ||
| 267 | 267 | } |
| 268 | 268 | |
| 269 | 269 | /** |
| 270 | 270 | "Deallocates" the fossil timer identified by the given timer ID. |
| 271 | 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 | |
| 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 | |
| 275 | 277 | fossil_timer_reset()). |
| 276 | 278 | */ |
| 277 | 279 | sqlite3_uint64 fossil_timer_stop(int timerId){ |
| 278 | - if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){ | |
| 280 | + if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){ | |
| 279 | 281 | return 0; |
| 280 | 282 | }else{ |
| 281 | 283 | 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; | |
| 284 | 286 | t->u = t->s = 0U; |
| 285 | 287 | return rc; |
| 286 | 288 | } |
| 287 | 289 | } |
| 288 | 290 | |
| @@ -289,11 +291,13 @@ | ||
| 289 | 291 | /* |
| 290 | 292 | ** Returns true (non-0) if the given timer ID (as returned from |
| 291 | 293 | ** fossil_timer_start() is currently active. |
| 292 | 294 | */ |
| 293 | 295 | int fossil_timer_is_active( int timerId ){ |
| 294 | - if(timerId<0 || timerId>=FOSSIL_TIMER_COUNT){ | |
| 296 | + if(timerId<1 || timerId>FOSSIL_TIMER_COUNT){ | |
| 295 | 297 | return 0; |
| 296 | 298 | }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; | |
| 298 | 302 | } |
| 299 | 303 | } |
| 300 | 304 |
| --- 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 |