mirror of
https://github.com/postgres/postgres.git
synced 2025-12-22 17:42:17 +03:00
Static assertions cleanup
Because we added StaticAssertStmt() first before StaticAssertDecl(), some uses as well as the instructions in c.h are now a bit backwards from the "native" way static assertions are meant to be used in C. This updates the guidance and moves some static assertions to better places. Specifically, since the addition of StaticAssertDecl(), we can put static assertions at the file level. This moves a number of static assertions out of function bodies, where they might have been stuck out of necessity, to perhaps better places at the file level or in header files. Also, when the static assertion appears in a position where a declaration is allowed, then using StaticAssertDecl() is more native than StaticAssertStmt(). Reviewed-by: John Naylor <john.naylor@enterprisedb.com> Discussion: https://www.postgresql.org/message-id/flat/941a04e7-dd6f-c0e4-8cdf-a33b3338cbda%40enterprisedb.com
This commit is contained in:
@@ -4187,7 +4187,8 @@ int64_div_fast_to_numeric(int64 val1, int log10val2)
|
||||
{
|
||||
static int pow10[] = {1, 10, 100, 1000};
|
||||
|
||||
StaticAssertStmt(lengthof(pow10) == DEC_DIGITS, "mismatch with DEC_DIGITS");
|
||||
StaticAssertDecl(lengthof(pow10) == DEC_DIGITS, "mismatch with DEC_DIGITS");
|
||||
|
||||
if (unlikely(pg_mul_s64_overflow(val1, pow10[DEC_DIGITS - m], &val1)))
|
||||
{
|
||||
/*
|
||||
|
||||
@@ -236,8 +236,6 @@ gin_tsquery_consistent(PG_FUNCTION_ARGS)
|
||||
* query.
|
||||
*/
|
||||
gcv.first_item = GETQUERY(query);
|
||||
StaticAssertStmt(sizeof(GinTernaryValue) == sizeof(bool),
|
||||
"sizes of GinTernaryValue and bool are not equal");
|
||||
gcv.check = (GinTernaryValue *) check;
|
||||
gcv.map_item_operand = (int *) (extra_data[0]);
|
||||
|
||||
|
||||
@@ -74,6 +74,14 @@ typedef struct
|
||||
#define PG_SNAPSHOT_MAX_NXIP \
|
||||
((MaxAllocSize - offsetof(pg_snapshot, xip)) / sizeof(FullTransactionId))
|
||||
|
||||
/*
|
||||
* Compile-time limits on the procarray (MAX_BACKENDS processes plus
|
||||
* MAX_BACKENDS prepared transactions) guarantee nxip won't be too large.
|
||||
*/
|
||||
StaticAssertDecl(MAX_BACKENDS * 2 <= PG_SNAPSHOT_MAX_NXIP,
|
||||
"possible overflow in pg_current_snapshot()");
|
||||
|
||||
|
||||
/*
|
||||
* Helper to get a TransactionId from a 64-bit xid with wraparound detection.
|
||||
*
|
||||
@@ -402,13 +410,6 @@ pg_current_snapshot(PG_FUNCTION_ARGS)
|
||||
if (cur == NULL)
|
||||
elog(ERROR, "no active snapshot set");
|
||||
|
||||
/*
|
||||
* Compile-time limits on the procarray (MAX_BACKENDS processes plus
|
||||
* MAX_BACKENDS prepared transactions) guarantee nxip won't be too large.
|
||||
*/
|
||||
StaticAssertStmt(MAX_BACKENDS * 2 <= PG_SNAPSHOT_MAX_NXIP,
|
||||
"possible overflow in pg_current_snapshot()");
|
||||
|
||||
/* allocate */
|
||||
nxip = cur->xcnt;
|
||||
snap = palloc(PG_SNAPSHOT_SIZE(nxip));
|
||||
|
||||
6
src/backend/utils/cache/syscache.c
vendored
6
src/backend/utils/cache/syscache.c
vendored
@@ -1040,6 +1040,9 @@ static const struct cachedesc cacheinfo[] = {
|
||||
}
|
||||
};
|
||||
|
||||
StaticAssertDecl(lengthof(cacheinfo) == SysCacheSize,
|
||||
"SysCacheSize does not match syscache.c's array");
|
||||
|
||||
static CatCache *SysCache[SysCacheSize];
|
||||
|
||||
static bool CacheInitialized = false;
|
||||
@@ -1068,9 +1071,6 @@ InitCatalogCache(void)
|
||||
{
|
||||
int cacheId;
|
||||
|
||||
StaticAssertStmt(lengthof(cacheinfo) == SysCacheSize,
|
||||
"SysCacheSize does not match syscache.c's array");
|
||||
|
||||
Assert(!CacheInitialized);
|
||||
|
||||
SysCacheRelationOidSize = SysCacheSupportingRelOidSize = 0;
|
||||
|
||||
@@ -306,7 +306,7 @@ AllocSetFreeIndex(Size size)
|
||||
tsize;
|
||||
|
||||
/* Statically assert that we only have a 16-bit input value. */
|
||||
StaticAssertStmt(ALLOC_CHUNK_LIMIT < (1 << 16),
|
||||
StaticAssertDecl(ALLOC_CHUNK_LIMIT < (1 << 16),
|
||||
"ALLOC_CHUNK_LIMIT must be less than 64kB");
|
||||
|
||||
tsize = size - 1;
|
||||
@@ -358,10 +358,10 @@ AllocSetContextCreateInternal(MemoryContext parent,
|
||||
AllocBlock block;
|
||||
|
||||
/* ensure MemoryChunk's size is properly maxaligned */
|
||||
StaticAssertStmt(ALLOC_CHUNKHDRSZ == MAXALIGN(ALLOC_CHUNKHDRSZ),
|
||||
StaticAssertDecl(ALLOC_CHUNKHDRSZ == MAXALIGN(ALLOC_CHUNKHDRSZ),
|
||||
"sizeof(MemoryChunk) is not maxaligned");
|
||||
/* check we have enough space to store the freelist link */
|
||||
StaticAssertStmt(sizeof(AllocFreeListLink) <= (1 << ALLOC_MINBITS),
|
||||
StaticAssertDecl(sizeof(AllocFreeListLink) <= (1 << ALLOC_MINBITS),
|
||||
"sizeof(AllocFreeListLink) larger than minimum allocation size");
|
||||
|
||||
/*
|
||||
|
||||
@@ -167,7 +167,7 @@ GenerationContextCreate(MemoryContext parent,
|
||||
GenerationBlock *block;
|
||||
|
||||
/* ensure MemoryChunk's size is properly maxaligned */
|
||||
StaticAssertStmt(Generation_CHUNKHDRSZ == MAXALIGN(Generation_CHUNKHDRSZ),
|
||||
StaticAssertDecl(Generation_CHUNKHDRSZ == MAXALIGN(Generation_CHUNKHDRSZ),
|
||||
"sizeof(MemoryChunk) is not maxaligned");
|
||||
|
||||
/*
|
||||
|
||||
@@ -151,7 +151,7 @@ SlabContextCreate(MemoryContext parent,
|
||||
int i;
|
||||
|
||||
/* ensure MemoryChunk's size is properly maxaligned */
|
||||
StaticAssertStmt(Slab_CHUNKHDRSZ == MAXALIGN(Slab_CHUNKHDRSZ),
|
||||
StaticAssertDecl(Slab_CHUNKHDRSZ == MAXALIGN(Slab_CHUNKHDRSZ),
|
||||
"sizeof(MemoryChunk) is not maxaligned");
|
||||
Assert(MAXALIGN(chunkSize) <= MEMORYCHUNK_MAX_VALUE);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user