From 0cda8f44d1e0f9b03c23bba8f206e6eb15e3e662 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@otacoo.com>
Date: Fri, 20 Feb 2015 19:35:53 +0900
Subject: [PATCH 4/4] Switch remaining structures to FLEXIBLE_ARRAY_MEMBER in
 backend code

---
 src/backend/access/nbtree/nbtutils.c   |  4 ++--
 src/backend/access/transam/multixact.c |  4 ++--
 src/backend/access/transam/twophase.c  |  9 +++------
 src/backend/commands/tablespace.c      |  2 +-
 src/backend/commands/trigger.c         | 10 +++++-----
 src/backend/executor/nodeAgg.c         | 14 +++++++-------
 src/backend/postmaster/checkpointer.c  |  2 +-
 src/backend/storage/ipc/pmsignal.c     |  2 +-
 src/backend/storage/ipc/procarray.c    |  5 ++---
 src/backend/utils/cache/inval.c        | 12 ++++++------
 src/backend/utils/cache/typcache.c     |  2 +-
 11 files changed, 31 insertions(+), 35 deletions(-)

diff --git a/src/backend/access/nbtree/nbtutils.c b/src/backend/access/nbtree/nbtutils.c
index 43e048c..379dac9 100644
--- a/src/backend/access/nbtree/nbtutils.c
+++ b/src/backend/access/nbtree/nbtutils.c
@@ -1836,7 +1836,7 @@ typedef struct BTVacInfo
 	BTCycleId	cycle_ctr;		/* cycle ID most recently assigned */
 	int			num_vacuums;	/* number of currently active VACUUMs */
 	int			max_vacuums;	/* allocated length of vacuums[] array */
-	BTOneVacInfo vacuums[1];	/* VARIABLE LENGTH ARRAY */
+	BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER];
 } BTVacInfo;
 
 static BTVacInfo *btvacinfo;
@@ -1984,7 +1984,7 @@ BTreeShmemSize(void)
 {
 	Size		size;
 
-	size = offsetof(BTVacInfo, vacuums[0]);
+	size = offsetof(BTVacInfo, vacuums);
 	size = add_size(size, mul_size(MaxBackends, sizeof(BTOneVacInfo)));
 	return size;
 }
diff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.c
index b2cf770..e3db6f8 100644
--- a/src/backend/access/transam/multixact.c
+++ b/src/backend/access/transam/multixact.c
@@ -258,7 +258,7 @@ typedef struct MultiXactStateData
 	 * stored in pg_control and used as truncation point for pg_multixact.  At
 	 * checkpoint or restartpoint, unneeded segments are removed.
 	 */
-	MultiXactId perBackendXactIds[1];	/* VARIABLE LENGTH ARRAY */
+	MultiXactId perBackendXactIds[FLEXIBLE_ARRAY_MEMBER];
 } MultiXactStateData;
 
 /*
@@ -1745,7 +1745,7 @@ MultiXactShmemSize(void)
 	Size		size;
 
 #define SHARED_MULTIXACT_STATE_SIZE \
-	add_size(sizeof(MultiXactStateData), \
+	add_size(offsetof(MultiXactStateData, perBackendXactIds),	\
 			 mul_size(sizeof(MultiXactId) * 2, MaxOldestSlot))
 
 	size = SHARED_MULTIXACT_STATE_SIZE;
diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c
index 6c7029e..6edc227 100644
--- a/src/backend/access/transam/twophase.c
+++ b/src/backend/access/transam/twophase.c
@@ -134,12 +134,9 @@ typedef struct TwoPhaseStateData
 	/* Number of valid prepXacts entries. */
 	int			numPrepXacts;
 
-	/*
-	 * There are max_prepared_xacts items in this array, but C wants a
-	 * fixed-size array.
-	 */
-	GlobalTransaction prepXacts[1];		/* VARIABLE LENGTH ARRAY */
-} TwoPhaseStateData;			/* VARIABLE LENGTH STRUCT */
+	/* There are max_prepared_xacts items in this array */
+	GlobalTransaction prepXacts[FLEXIBLE_ARRAY_MEMBER];
+} TwoPhaseStateData;
 
 static TwoPhaseStateData *TwoPhaseState;
 
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index e098b9f..03cc8fe 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -1088,7 +1088,7 @@ GetDefaultTablespace(char relpersistence)
 typedef struct
 {
 	int			numSpcs;
-	Oid			tblSpcs[1];		/* VARIABLE LENGTH ARRAY */
+	Oid			tblSpcs[FLEXIBLE_ARRAY_MEMBER];
 } temp_tablespaces_extra;
 
 /* check_hook: validate new temp_tablespaces */
diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c
index 5c1c1be..4223398 100644
--- a/src/backend/commands/trigger.c
+++ b/src/backend/commands/trigger.c
@@ -3005,7 +3005,7 @@ typedef struct SetConstraintStateData
 	bool		all_isdeferred;
 	int			numstates;		/* number of trigstates[] entries in use */
 	int			numalloc;		/* allocated size of trigstates[] */
-	SetConstraintTriggerData trigstates[1];		/* VARIABLE LENGTH ARRAY */
+	SetConstraintTriggerData trigstates[FLEXIBLE_ARRAY_MEMBER];
 } SetConstraintStateData;
 
 typedef SetConstraintStateData *SetConstraintState;
@@ -4398,8 +4398,8 @@ SetConstraintStateCreate(int numalloc)
 	 */
 	state = (SetConstraintState)
 		MemoryContextAllocZero(TopTransactionContext,
-							   sizeof(SetConstraintStateData) +
-						   (numalloc - 1) *sizeof(SetConstraintTriggerData));
+							   offsetof(SetConstraintStateData, trigstates) +
+						   numalloc *sizeof(SetConstraintTriggerData));
 
 	state->numalloc = numalloc;
 
@@ -4440,8 +4440,8 @@ SetConstraintStateAddItem(SetConstraintState state,
 		newalloc = Max(newalloc, 8);	/* in case original has size 0 */
 		state = (SetConstraintState)
 			repalloc(state,
-					 sizeof(SetConstraintStateData) +
-					 (newalloc - 1) *sizeof(SetConstraintTriggerData));
+					 offsetof(SetConstraintStateData, trigstates) +
+					 newalloc * sizeof(SetConstraintTriggerData));
 		state->numalloc = newalloc;
 		Assert(state->numstates < state->numalloc);
 	}
diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c
index 8079d97..9ff0eff 100644
--- a/src/backend/executor/nodeAgg.c
+++ b/src/backend/executor/nodeAgg.c
@@ -297,9 +297,9 @@ typedef struct AggHashEntryData *AggHashEntry;
 typedef struct AggHashEntryData
 {
 	TupleHashEntryData shared;	/* common header for hash table entries */
-	/* per-aggregate transition status array - must be last! */
-	AggStatePerGroupData pergroup[1];	/* VARIABLE LENGTH ARRAY */
-}	AggHashEntryData;	/* VARIABLE LENGTH STRUCT */
+	/* per-aggregate transition status array */
+	AggStatePerGroupData pergroup[FLEXIBLE_ARRAY_MEMBER];
+}	AggHashEntryData;
 
 
 static void initialize_aggregates(AggState *aggstate,
@@ -941,8 +941,8 @@ build_hash_table(AggState *aggstate)
 	Assert(node->aggstrategy == AGG_HASHED);
 	Assert(node->numGroups > 0);
 
-	entrysize = sizeof(AggHashEntryData) +
-		(aggstate->numaggs - 1) * sizeof(AggStatePerGroupData);
+	entrysize = offsetof(AggHashEntryData, pergroup) +
+		aggstate->numaggs * sizeof(AggStatePerGroupData);
 
 	aggstate->hashtable = BuildTupleHashTable(node->numCols,
 											  node->grpColIdx,
@@ -1013,8 +1013,8 @@ hash_agg_entry_size(int numAggs)
 	Size		entrysize;
 
 	/* This must match build_hash_table */
-	entrysize = sizeof(AggHashEntryData) +
-		(numAggs - 1) * sizeof(AggStatePerGroupData);
+	entrysize = offsetof(AggHashEntryData, pergroup) +
+		numAggs * sizeof(AggStatePerGroupData);
 	entrysize = MAXALIGN(entrysize);
 	/* Account for hashtable overhead (assuming fill factor = 1) */
 	entrysize += 3 * sizeof(void *);
diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c
index 237be12..cfad08d 100644
--- a/src/backend/postmaster/checkpointer.c
+++ b/src/backend/postmaster/checkpointer.c
@@ -130,7 +130,7 @@ typedef struct
 
 	int			num_requests;	/* current # of requests */
 	int			max_requests;	/* allocated array size */
-	CheckpointerRequest requests[1];	/* VARIABLE LENGTH ARRAY */
+	CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER];
 } CheckpointerShmemStruct;
 
 static CheckpointerShmemStruct *CheckpointerShmem;
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index 0c89eb7..ea3fe20 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -66,7 +66,7 @@ struct PMSignalData
 	/* per-child-process flags */
 	int			num_child_flags;	/* # of entries in PMChildFlags[] */
 	int			next_child_flag;	/* next slot to try to assign */
-	sig_atomic_t PMChildFlags[1];		/* VARIABLE LENGTH ARRAY */
+	sig_atomic_t PMChildFlags[FLEXIBLE_ARRAY_MEMBER];
 };
 
 NON_EXEC_STATIC volatile PMSignalData *PMSignalState = NULL;
diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c
index a1ebc72..39846d6 100644
--- a/src/backend/storage/ipc/procarray.c
+++ b/src/backend/storage/ipc/procarray.c
@@ -91,10 +91,9 @@ typedef struct ProcArrayStruct
 	TransactionId replication_slot_catalog_xmin;
 
 	/*
-	 * We declare pgprocnos[] as 1 entry because C wants a fixed-size array,
-	 * but actually it is maxProcs entries long.
+	 * Number of procs with maxProcs entries.
 	 */
-	int			pgprocnos[1];	/* VARIABLE LENGTH ARRAY */
+	int			pgprocnos[FLEXIBLE_ARRAY_MEMBER];
 } ProcArrayStruct;
 
 static ProcArrayStruct *procArray;
diff --git a/src/backend/utils/cache/inval.c b/src/backend/utils/cache/inval.c
index 0f2192c..8826a5d 100644
--- a/src/backend/utils/cache/inval.c
+++ b/src/backend/utils/cache/inval.c
@@ -122,8 +122,8 @@ typedef struct InvalidationChunk
 	struct InvalidationChunk *next;		/* list link */
 	int			nitems;			/* # items currently stored in chunk */
 	int			maxitems;		/* size of allocated array in this chunk */
-	SharedInvalidationMessage msgs[1];	/* VARIABLE LENGTH ARRAY */
-} InvalidationChunk;			/* VARIABLE LENGTH STRUCTURE */
+	SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER];
+} InvalidationChunk;
 
 typedef struct InvalidationListHeader
 {
@@ -225,8 +225,8 @@ AddInvalidationMessage(InvalidationChunk **listHdr,
 #define FIRSTCHUNKSIZE 32
 		chunk = (InvalidationChunk *)
 			MemoryContextAlloc(CurTransactionContext,
-							   sizeof(InvalidationChunk) +
-					(FIRSTCHUNKSIZE - 1) *sizeof(SharedInvalidationMessage));
+							   offsetof(InvalidationChunk, msgs) +
+					FIRSTCHUNKSIZE * sizeof(SharedInvalidationMessage));
 		chunk->nitems = 0;
 		chunk->maxitems = FIRSTCHUNKSIZE;
 		chunk->next = *listHdr;
@@ -239,8 +239,8 @@ AddInvalidationMessage(InvalidationChunk **listHdr,
 
 		chunk = (InvalidationChunk *)
 			MemoryContextAlloc(CurTransactionContext,
-							   sizeof(InvalidationChunk) +
-						 (chunksize - 1) *sizeof(SharedInvalidationMessage));
+							   offsetof(InvalidationChunk, msgs) +
+						 chunksize * sizeof(SharedInvalidationMessage));
 		chunk->nitems = 0;
 		chunk->maxitems = chunksize;
 		chunk->next = *listHdr;
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 82b6668..0492718 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -93,7 +93,7 @@ typedef struct TypeCacheEnumData
 	Oid			bitmap_base;	/* OID corresponding to bit 0 of bitmapset */
 	Bitmapset  *sorted_values;	/* Set of OIDs known to be in order */
 	int			num_values;		/* total number of values in enum */
-	EnumItem	enum_values[1]; /* VARIABLE LENGTH ARRAY */
+	EnumItem	enum_values[FLEXIBLE_ARRAY_MEMBER];
 } TypeCacheEnumData;
 
 /*
-- 
2.3.0

