1
1
/* -----------------------------------------------------------------------
2
2
* formatting.c
3
3
*
4
- * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.154 2009/02/07 14:16:45 momjian Exp $
4
+ * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.155 2009/03/12 00:53:25 tgl Exp $
5
5
*
6
6
*
7
7
* Portions Copyright (c) 1999-2009, PostgreSQL Global Development Group
@@ -392,12 +392,10 @@ static int DCHCounter = 0;
392
392
393
393
/* global cache for --- number part */
394
394
static NUMCacheEntry NUMCache [NUM_CACHE_FIELDS + 1 ];
395
- static NUMCacheEntry * last_NUMCacheEntry ;
396
395
397
396
static int n_NUMCache = 0 ; /* number of entries */
398
397
static int NUMCounter = 0 ;
399
-
400
- #define MAX_INT32 (2147483600)
398
+ static NUMCacheEntry * last_NUMCacheEntry = NUMCache + 0 ;
401
399
402
400
/* ----------
403
401
* For char->date/time conversion
@@ -2765,10 +2763,10 @@ DCH_from_char(FormatNode *node, char *in, TmFromChar *out)
2765
2763
static DCHCacheEntry *
2766
2764
DCH_cache_getnew (char * str )
2767
2765
{
2768
- DCHCacheEntry * ent = NULL ;
2766
+ DCHCacheEntry * ent ;
2769
2767
2770
- /* counter overload check - paranoia? */
2771
- if (DCHCounter + DCH_CACHE_FIELDS >= MAX_INT32 )
2768
+ /* counter overflow check - paranoia? */
2769
+ if (DCHCounter >= ( INT_MAX - DCH_CACHE_FIELDS - 1 ) )
2772
2770
{
2773
2771
DCHCounter = 0 ;
2774
2772
@@ -2777,7 +2775,7 @@ DCH_cache_getnew(char *str)
2777
2775
}
2778
2776
2779
2777
/*
2780
- * Cache is full - needs remove any older entry
2778
+ * If cache is full, remove oldest entry
2781
2779
*/
2782
2780
if (n_DCHCache > DCH_CACHE_FIELDS )
2783
2781
{
@@ -2786,7 +2784,7 @@ DCH_cache_getnew(char *str)
2786
2784
#ifdef DEBUG_TO_FROM_CHAR
2787
2785
elog (DEBUG_elog_output , "cache is full (%d)" , n_DCHCache );
2788
2786
#endif
2789
- for (ent = DCHCache ; ent <= (DCHCache + DCH_CACHE_FIELDS ); ent ++ )
2787
+ for (ent = DCHCache + 1 ; ent <= (DCHCache + DCH_CACHE_FIELDS ); ent ++ )
2790
2788
{
2791
2789
if (ent -> age < old -> age )
2792
2790
old = ent ;
@@ -2811,35 +2809,30 @@ DCH_cache_getnew(char *str)
2811
2809
++ n_DCHCache ;
2812
2810
return ent ;
2813
2811
}
2814
-
2815
- return NULL ; /* never */
2816
2812
}
2817
2813
2818
2814
static DCHCacheEntry *
2819
2815
DCH_cache_search (char * str )
2820
2816
{
2821
- int i = 0 ;
2817
+ int i ;
2822
2818
DCHCacheEntry * ent ;
2823
2819
2824
- /* counter overload check - paranoia? */
2825
- if (DCHCounter + DCH_CACHE_FIELDS >= MAX_INT32 )
2820
+ /* counter overflow check - paranoia? */
2821
+ if (DCHCounter >= ( INT_MAX - DCH_CACHE_FIELDS - 1 ) )
2826
2822
{
2827
2823
DCHCounter = 0 ;
2828
2824
2829
2825
for (ent = DCHCache ; ent <= (DCHCache + DCH_CACHE_FIELDS ); ent ++ )
2830
2826
ent -> age = (++ DCHCounter );
2831
2827
}
2832
2828
2833
- for (ent = DCHCache ; ent <= ( DCHCache + DCH_CACHE_FIELDS ); ent ++ )
2829
+ for (i = 0 , ent = DCHCache ; i < n_DCHCache ; i ++ , ent ++ )
2834
2830
{
2835
- if (i == n_DCHCache )
2836
- break ;
2837
2831
if (strcmp (ent -> str , str ) == 0 )
2838
2832
{
2839
2833
ent -> age = (++ DCHCounter );
2840
2834
return ent ;
2841
2835
}
2842
- i ++ ;
2843
2836
}
2844
2837
2845
2838
return NULL ;
@@ -3371,10 +3364,10 @@ do { \
3371
3364
static NUMCacheEntry *
3372
3365
NUM_cache_getnew (char * str )
3373
3366
{
3374
- NUMCacheEntry * ent = NULL ;
3367
+ NUMCacheEntry * ent ;
3375
3368
3376
- /* counter overload check - paranoia? */
3377
- if (NUMCounter + NUM_CACHE_FIELDS >= MAX_INT32 )
3369
+ /* counter overflow check - paranoia? */
3370
+ if (NUMCounter >= ( INT_MAX - NUM_CACHE_FIELDS - 1 ) )
3378
3371
{
3379
3372
NUMCounter = 0 ;
3380
3373
@@ -3383,7 +3376,7 @@ NUM_cache_getnew(char *str)
3383
3376
}
3384
3377
3385
3378
/*
3386
- * Cache is full - needs remove any older entry
3379
+ * If cache is full, remove oldest entry
3387
3380
*/
3388
3381
if (n_NUMCache > NUM_CACHE_FIELDS )
3389
3382
{
@@ -3392,13 +3385,13 @@ NUM_cache_getnew(char *str)
3392
3385
#ifdef DEBUG_TO_FROM_CHAR
3393
3386
elog (DEBUG_elog_output , "Cache is full (%d)" , n_NUMCache );
3394
3387
#endif
3395
-
3396
3388
for (ent = NUMCache ; ent <= (NUMCache + NUM_CACHE_FIELDS ); ent ++ )
3397
3389
{
3398
3390
/*
3399
- * entry removed via NUM_cache_remove() can be used here
3391
+ * entry removed via NUM_cache_remove() can be used here,
3392
+ * which is why it's worth scanning first entry again
3400
3393
*/
3401
- if (* ent -> str == '\0' )
3394
+ if (ent -> str [ 0 ] == '\0' )
3402
3395
{
3403
3396
old = ent ;
3404
3397
break ;
@@ -3412,7 +3405,6 @@ NUM_cache_getnew(char *str)
3412
3405
StrNCpy (old -> str , str , NUM_CACHE_SIZE + 1 );
3413
3406
/* old->format fill parser */
3414
3407
old -> age = (++ NUMCounter );
3415
-
3416
3408
ent = old ;
3417
3409
}
3418
3410
else
@@ -3430,35 +3422,32 @@ NUM_cache_getnew(char *str)
3430
3422
zeroize_NUM (& ent -> Num );
3431
3423
3432
3424
last_NUMCacheEntry = ent ;
3433
- return ent ; /* never */
3425
+ return ent ;
3434
3426
}
3435
3427
3436
3428
static NUMCacheEntry *
3437
3429
NUM_cache_search (char * str )
3438
3430
{
3439
- int i = 0 ;
3431
+ int i ;
3440
3432
NUMCacheEntry * ent ;
3441
3433
3442
- /* counter overload check - paranoia? */
3443
- if (NUMCounter + NUM_CACHE_FIELDS >= MAX_INT32 )
3434
+ /* counter overflow check - paranoia? */
3435
+ if (NUMCounter >= ( INT_MAX - NUM_CACHE_FIELDS - 1 ) )
3444
3436
{
3445
3437
NUMCounter = 0 ;
3446
3438
3447
3439
for (ent = NUMCache ; ent <= (NUMCache + NUM_CACHE_FIELDS ); ent ++ )
3448
3440
ent -> age = (++ NUMCounter );
3449
3441
}
3450
3442
3451
- for (ent = NUMCache ; ent <= ( NUMCache + NUM_CACHE_FIELDS ); ent ++ )
3443
+ for (i = 0 , ent = NUMCache ; i < n_NUMCache ; i ++ , ent ++ )
3452
3444
{
3453
- if (i == n_NUMCache )
3454
- break ;
3455
3445
if (strcmp (ent -> str , str ) == 0 )
3456
3446
{
3457
3447
ent -> age = (++ NUMCounter );
3458
3448
last_NUMCacheEntry = ent ;
3459
3449
return ent ;
3460
3450
}
3461
- i ++ ;
3462
3451
}
3463
3452
3464
3453
return NULL ;
@@ -3470,7 +3459,7 @@ NUM_cache_remove(NUMCacheEntry *ent)
3470
3459
#ifdef DEBUG_TO_FROM_CHAR
3471
3460
elog (DEBUG_elog_output , "REMOVING ENTRY (%s)" , ent -> str );
3472
3461
#endif
3473
- * ent -> str = '\0' ;
3462
+ ent -> str [ 0 ] = '\0' ;
3474
3463
ent -> age = 0 ;
3475
3464
}
3476
3465
0 commit comments