]> granicus.if.org Git - postgresql/commitdiff
Centralize definition of integer limits.
authorAndres Freund <andres@anarazel.de>
Wed, 25 Mar 2015 21:39:42 +0000 (22:39 +0100)
committerAndres Freund <andres@anarazel.de>
Wed, 25 Mar 2015 21:39:42 +0000 (22:39 +0100)
Several submitted and even committed patches have run into the problem
that C89, our baseline, does not provide minimum/maximum values for
various integer datatypes. C99's stdint.h does, but we can't rely on
it.

Several parts of the code defined limits locally, so instead centralize
the definitions to c.h.

This patch also changes the more obvious usages of literal limit values;
there's more places that could be changed, but it's less clear whether
it's beneficial to change those.

Author: Andrew Gierth
Discussion: 87619tc5wc.fsf@news-spur.riddles.org.uk

18 files changed:
contrib/btree_gist/btree_ts.c
contrib/intarray/_int_gist.c
contrib/pgbench/pgbench.c
src/backend/access/transam/xlog.c
src/backend/tsearch/wparser_def.c
src/backend/utils/adt/int8.c
src/backend/utils/adt/numutils.c
src/backend/utils/adt/timestamp.c
src/backend/utils/adt/tsrank.c
src/backend/utils/adt/txid.c
src/include/c.h
src/include/datatype/timestamp.h
src/include/executor/instrument.h
src/include/nodes/parsenodes.h
src/include/pg_config_manual.h
src/include/port/atomics.h
src/include/storage/predicate_internals.h
src/include/utils/date.h

index b9c2b49ef3ccc5009a4241881def59ffd85ea39a..d472d494c9c261bcd509ab01f3d0e0e6768d4b4f 100644 (file)
@@ -153,7 +153,7 @@ ts_dist(PG_FUNCTION_ARGS)
                p->day = INT_MAX;
                p->month = INT_MAX;
 #ifdef HAVE_INT64_TIMESTAMP
-               p->time = INT64CONST(0x7FFFFFFFFFFFFFFF);
+               p->time = INT64_MAX;
 #else
                p->time = DBL_MAX;
 #endif
@@ -181,7 +181,7 @@ tstz_dist(PG_FUNCTION_ARGS)
                p->day = INT_MAX;
                p->month = INT_MAX;
 #ifdef HAVE_INT64_TIMESTAMP
-               p->time = INT64CONST(0x7FFFFFFFFFFFFFFF);
+               p->time = INT64_MAX;
 #else
                p->time = DBL_MAX;
 #endif
index 876a7b9a8ab0e7f5691a0308fbe11e84ba03aae9..07108eb15ebccb77327871c38dfc2eb1f2046a9f 100644 (file)
@@ -3,6 +3,8 @@
  */
 #include "postgres.h"
 
+#include <limits.h>
+
 #include "access/gist.h"
 #include "access/skey.h"
 
@@ -191,7 +193,7 @@ g_int_compress(PG_FUNCTION_ARGS)
                cand = 1;
                while (len > MAXNUMRANGE * 2)
                {
-                       min = 0x7fffffff;
+                       min = INT_MAX;
                        for (i = 2; i < len; i += 2)
                                if (min > (dr[i] - dr[i - 1]))
                                {
index 706fdf5b197bb328ae829dc3639409ac2e42e0b9..822adfd5817478ef6fa02e9b6e11cec7ef155171 100644 (file)
 #include <sys/resource.h>              /* for getrlimit */
 #endif
 
-#ifndef INT64_MAX
-#define INT64_MAX      INT64CONST(0x7FFFFFFFFFFFFFFF)
-#endif
-
 #ifndef M_PI
 #define M_PI 3.14159265358979323846
 #endif
@@ -453,7 +449,7 @@ strtoint64(const char *str)
                 */
                if (strncmp(ptr, "9223372036854775808", 19) == 0)
                {
-                       result = -INT64CONST(0x7fffffffffffffff) - 1;
+                       result = INT64_MIN;
                        ptr += 19;
                        goto gotdigits;
                }
index 2c6ae12b8d6bc62c4601670ae28f1899efaa801b..751253ba03c2c32359ee055703b4ab571dd59cf9 100644 (file)
@@ -1408,7 +1408,7 @@ WALInsertLockAcquireExclusive(void)
        {
                LWLockAcquireWithVar(&WALInsertLocks[i].l.lock,
                                                         &WALInsertLocks[i].l.insertingAt,
-                                                        UINT64CONST(0xFFFFFFFFFFFFFFFF));
+                                                        UINT64_MAX);
        }
        LWLockAcquireWithVar(&WALInsertLocks[i].l.lock,
                                                 &WALInsertLocks[i].l.insertingAt,
index ca5b54358acdf7d1906f16855c5875670edd663b..d95fdb1fabc049e7a82c52058edf23d9215dbe72 100644 (file)
@@ -14,6 +14,8 @@
 
 #include "postgres.h"
 
+#include <limits.h>
+
 #include "catalog/pg_collation.h"
 #include "commands/defrem.h"
 #include "tsearch/ts_locale.h"
@@ -2047,7 +2049,7 @@ hlCover(HeadlineParsedText *prs, TSQuery query, int *p, int *q)
        int                     pos = *p;
 
        *q = -1;
-       *p = 0x7fffffff;
+       *p = INT_MAX;
 
        for (j = 0; j < query->size; j++)
        {
@@ -2258,7 +2260,7 @@ mark_hl_fragments(HeadlineParsedText *prs, TSQuery query, int highlight,
        for (f = 0; f < max_fragments; f++)
        {
                maxitems = 0;
-               minwords = 0x7fffffff;
+               minwords = INT32_MAX;
                minI = -1;
 
                /*
index 56d909a4f65665e73ba522011b051e11c91743ea..b3a11918870cf4ec3bdb15a5233061c3cfcf5448 100644 (file)
@@ -78,7 +78,7 @@ scanint8(const char *str, bool errorOK, int64 *result)
                 */
                if (strncmp(ptr, "9223372036854775808", 19) == 0)
                {
-                       tmp = -INT64CONST(0x7fffffffffffffff) - 1;
+                       tmp = INT64_MIN;
                        ptr += 19;
                        goto gotdigits;
                }
index d77799acfe3afda65879f08ac8fc9afcf3863038..585da1e7322940c8b17a6d649a24405be77a1b4c 100644 (file)
@@ -190,7 +190,7 @@ pg_lltoa(int64 value, char *a)
         * Avoid problems with the most negative integer not being representable
         * as a positive integer.
         */
-       if (value == (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1))
+       if (value == INT64_MIN)
        {
                memcpy(a, "-9223372036854775808", 21);
                return;
index 723c67087e59d88b187eb9b48ddf05c7e95120fa..33e859d35e1319ec83691c24d2e818548f8d9a01 100644 (file)
 
 #define SAMESIGN(a,b)  (((a) < 0) == ((b) < 0))
 
-#ifndef INT64_MAX
-#define INT64_MAX      INT64CONST(0x7FFFFFFFFFFFFFFF)
-#endif
-
-#ifndef INT64_MIN
-#define INT64_MIN      (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
-#endif
-
 /* Set at postmaster start */
 TimestampTz PgStartTime;
 
index 733203ec7b52422f6df5de2abc1ebbbc3b776055..a3066fa79485d4f8dd6bf9bccaefb7d6870869e0 100644 (file)
@@ -13,6 +13,7 @@
  */
 #include "postgres.h"
 
+#include <limits.h>
 #include <math.h>
 
 #include "tsearch/ts_utils.h"
@@ -555,7 +556,7 @@ Cover(DocRepresentation *doc, int len, QueryRepresentation *qr, CoverExt *ext)
 
        memset(qr->operandexist, 0, sizeof(bool) * qr->query->size);
 
-       ext->p = 0x7fffffff;
+       ext->p = INT_MAX;
        ext->q = 0;
        ptr = doc + ext->pos;
 
index f973ef936c5a3c122dbb851f1a2f0c007d28ec05..31f8033ae4846f8bd0149db8f0b81edb2c1969d9 100644 (file)
@@ -34,7 +34,7 @@
 
 
 /* txid will be signed int8 in database, so must limit to 63 bits */
-#define MAX_TXID   UINT64CONST(0x7FFFFFFFFFFFFFFF)
+#define MAX_TXID   ((uint64) INT64_MAX)
 
 /* Use unsigned variant internally */
 typedef uint64 txid;
index 744721860c55a81f0f1b02465d53191edfd24906..e7ee5105a272f60e95393ecf5e87d009064318ad 100644 (file)
@@ -249,6 +249,36 @@ typedef uint8 bits8;                       /* >= 8 bits */
 typedef uint16 bits16;                 /* >= 16 bits */
 typedef uint32 bits32;                 /* >= 32 bits */
 
+/* should be defined in stdint.h, but we guarantee them here */
+#ifndef INT8_MIN
+#define INT8_MIN       (-0x7F-1)
+#endif
+#ifndef INT8_MAX
+#define INT8_MAX       (0x7F)
+#endif
+#ifndef INT16_MIN
+#define INT16_MIN      (-0x7FFF-1)
+#endif
+#ifndef INT16_MAX
+#define INT16_MAX      (0x7FFF)
+#endif
+#ifndef INT32_MIN
+#define INT32_MIN      (-0x7FFFFFFF-1)
+#endif
+#ifndef INT32_MAX
+#define INT32_MAX      (0x7FFFFFFF)
+#endif
+
+#ifndef UINT8_MAX
+#define UINT8_MAX      (0xFF)
+#endif
+#ifndef UINT16_MAX
+#define UINT16_MAX     (0xFFFF)
+#endif
+#ifndef UINT32_MAX
+#define UINT32_MAX     (0xFFFFFFFF)
+#endif
+
 /*
  * 64-bit integers
  */
@@ -284,6 +314,17 @@ typedef unsigned long long int uint64;
 #define UINT64CONST(x) ((uint64) x)
 #endif
 
+/* should be defined in stdint.h, but we guarantee them here */
+#ifndef INT64_MIN
+#define INT64_MIN      (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
+#endif
+#ifndef INT64_MAX
+#define INT64_MAX      INT64CONST(0x7FFFFFFFFFFFFFFF)
+#endif
+#ifndef UINT64_MAX
+#define UINT64_MAX     UINT64CONST(0xFFFFFFFFFFFFFFFF)
+#endif
+
 /* snprintf format strings to use for 64-bit integers */
 #define INT64_FORMAT "%" INT64_MODIFIER "d"
 #define UINT64_FORMAT "%" INT64_MODIFIER "u"
index 6dfaf23cc56e1501534dc3f392d15ac7081385bf..d3450d69a8e41f2148ebbf1946f43ee61e5db633 100644 (file)
@@ -119,8 +119,8 @@ typedef struct
  * DT_NOBEGIN represents timestamp -infinity; DT_NOEND represents +infinity
  */
 #ifdef HAVE_INT64_TIMESTAMP
-#define DT_NOBEGIN             (-INT64CONST(0x7fffffffffffffff) - 1)
-#define DT_NOEND               (INT64CONST(0x7fffffffffffffff))
+#define DT_NOBEGIN             INT64_MIN
+#define DT_NOEND               INT64_MAX
 #else                                                  /* !HAVE_INT64_TIMESTAMP */
 #ifdef HUGE_VAL
 #define DT_NOBEGIN             (-HUGE_VAL)
index 1c3b2b0ffb1830ea884bee7d665419541793e6e3..db9d1e8ff05702629d2f793e2b6bfd57622fac50 100644 (file)
@@ -38,7 +38,7 @@ typedef enum InstrumentOption
        INSTRUMENT_TIMER = 1 << 0,      /* needs timer (and row counts) */
        INSTRUMENT_BUFFERS = 1 << 1,    /* needs buffer usage */
        INSTRUMENT_ROWS = 1 << 2,       /* needs row count */
-       INSTRUMENT_ALL = 0x7FFFFFFF
+       INSTRUMENT_ALL = INT32_MAX
 } InstrumentOption;
 
 typedef struct Instrumentation
index ec0d0eaa4ac2dda4abf96bee56e854755d70d542..2893ceff18302f0e91e12889be35edd3094deba2 100644 (file)
@@ -587,7 +587,7 @@ typedef enum TableLikeOption
        CREATE_TABLE_LIKE_INDEXES = 1 << 2,
        CREATE_TABLE_LIKE_STORAGE = 1 << 3,
        CREATE_TABLE_LIKE_COMMENTS = 1 << 4,
-       CREATE_TABLE_LIKE_ALL = 0x7FFFFFFF
+       CREATE_TABLE_LIKE_ALL = INT32_MAX
 } TableLikeOption;
 
 /*
index 5cfc0ae1e80cefb93e991c87889721d5ea33871a..d3e9888b7b98455107bad58c927fd01473465b09 100644 (file)
@@ -48,7 +48,7 @@
 /*
  * Set the upper and lower bounds of sequence values.
  */
-#define SEQ_MAXVALUE   INT64CONST(0x7FFFFFFFFFFFFFFF)
+#define SEQ_MAXVALUE   INT64_MAX
 #define SEQ_MINVALUE   (-SEQ_MAXVALUE)
 
 /*
  * the older rand() function, which is often different from --- and
  * considerably inferior to --- random().
  */
-#define MAX_RANDOM_VALUE  (0x7FFFFFFF)
+#define MAX_RANDOM_VALUE  INT32_MAX
 
 /*
  * On PPC machines, decide whether to use the mutex hint bit in LWARX
index 89868b59c3a065ea8bb371b59fe239cd05ab93c6..99173644a43865cfb976b44e921844e6401a3cd0 100644 (file)
@@ -489,7 +489,7 @@ STATIC_IF_INLINE uint64
 pg_atomic_fetch_sub_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
 {
        AssertPointerAlignment(ptr, 8);
-       Assert(sub_ != -INT64CONST(0x7FFFFFFFFFFFFFFF) - 1);
+       Assert(sub_ != INT64_MIN);
        return pg_atomic_fetch_sub_u64_impl(ptr, sub_);
 }
 
@@ -518,7 +518,7 @@ STATIC_IF_INLINE uint64
 pg_atomic_sub_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
 {
        AssertPointerAlignment(ptr, 8);
-       Assert(sub_ != -INT64CONST(0x7FFFFFFFFFFFFFFF) - 1);
+       Assert(sub_ != INT64_MIN);
        return pg_atomic_sub_fetch_u64_impl(ptr, sub_);
 }
 
index 2aaf58060d589b1d5793bef323aab9229e90bafd..8ecf923b1fb43ba12f37edcd55834786cf885f65 100644 (file)
@@ -33,7 +33,7 @@ typedef uint64 SerCommitSeqNo;
  *       at that point.  It's earlier than all normal sequence numbers,
  *       and is only used by recovered prepared transactions
  */
-#define InvalidSerCommitSeqNo          ((SerCommitSeqNo) UINT64CONST(0xFFFFFFFFFFFFFFFF))
+#define InvalidSerCommitSeqNo          ((SerCommitSeqNo) UINT64_MAX)
 #define RecoverySerCommitSeqNo         ((SerCommitSeqNo) 1)
 #define FirstNormalSerCommitSeqNo      ((SerCommitSeqNo) 2)
 
index b57f4bb3ea25f73569fdf32ad4f13fc436441375..f07011c3851fad19198384cadeeaf74ce75795e4 100644 (file)
@@ -35,11 +35,9 @@ typedef struct
 
 /*
  * Infinity and minus infinity must be the max and min values of DateADT.
- * We could use INT_MIN and INT_MAX here, but seems better to not assume that
- * int32 == int.
  */
-#define DATEVAL_NOBEGIN                ((DateADT) (-0x7fffffff - 1))
-#define DATEVAL_NOEND          ((DateADT) 0x7fffffff)
+#define DATEVAL_NOBEGIN                ((DateADT) INT32_MIN)
+#define DATEVAL_NOEND          ((DateADT) INT32_MAX)
 
 #define DATE_NOBEGIN(j)                ((j) = DATEVAL_NOBEGIN)
 #define DATE_IS_NOBEGIN(j)     ((j) == DATEVAL_NOBEGIN)