summaryrefslogtreecommitdiff
path: root/src/backend/utils/cache
diff options
context:
space:
mode:
authorMichael Paquier2025-12-09 22:36:46 +0000
committerMichael Paquier2025-12-09 22:36:46 +0000
commit1b105f9472bdb9a68f709778afafb494997267bd (patch)
tree205727d30b0dd4f3060bc1f49a9efa724ee3ff80 /src/backend/utils/cache
parentc507ba55f5bfae900baa94f1c657e1d99da5c6dc (diff)
Use palloc_object() and palloc_array() in backend code
The idea is to encourage more the use of these new routines across the tree, as these offer stronger type safety guarantees than palloc(). This batch of changes includes most of the trivial changes suggested by the author for src/backend/. A total of 334 files are updated here. Among these files, 48 of them have their build change slightly; these are caused by line number changes as the new allocation formulas are simpler, shaving around 100 lines of code in total. Similar work has been done in 0c3c5c3b06a3 and 31d3847a37be. Author: David Geier <geidav.pg@gmail.com> Discussion: https://postgr.es/m/ad0748d4-3080-436e-b0bc-ac8f86a3466a@gmail.com
Diffstat (limited to 'src/backend/utils/cache')
-rw-r--r--src/backend/utils/cache/catcache.c4
-rw-r--r--src/backend/utils/cache/evtcache.c2
-rw-r--r--src/backend/utils/cache/inval.c2
-rw-r--r--src/backend/utils/cache/lsyscache.c6
-rw-r--r--src/backend/utils/cache/partcache.c24
-rw-r--r--src/backend/utils/cache/plancache.c15
-rw-r--r--src/backend/utils/cache/relcache.c30
-rw-r--r--src/backend/utils/cache/typcache.c2
8 files changed, 41 insertions, 44 deletions
diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c
index 02ae7d5a831..84f1f80607e 100644
--- a/src/backend/utils/cache/catcache.c
+++ b/src/backend/utils/cache/catcache.c
@@ -920,7 +920,7 @@ InitCatCache(int id,
*/
if (CacheHdr == NULL)
{
- CacheHdr = (CatCacheHeader *) palloc(sizeof(CatCacheHeader));
+ CacheHdr = palloc_object(CatCacheHeader);
slist_init(&CacheHdr->ch_caches);
CacheHdr->ch_ntup = 0;
#ifdef CATCACHE_STATS
@@ -2243,7 +2243,7 @@ CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp, Datum *arguments,
{
/* Set up keys for a negative cache entry */
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
- ct = (CatCTup *) palloc(sizeof(CatCTup));
+ ct = palloc_object(CatCTup);
/*
* Store keys - they'll point into separately allocated memory if not
diff --git a/src/backend/utils/cache/evtcache.c b/src/backend/utils/cache/evtcache.c
index 76ba2db5390..7f8e246e804 100644
--- a/src/backend/utils/cache/evtcache.c
+++ b/src/backend/utils/cache/evtcache.c
@@ -172,7 +172,7 @@ BuildEventTriggerCache(void)
oldcontext = MemoryContextSwitchTo(EventTriggerCacheContext);
/* Allocate new cache item. */
- item = palloc0(sizeof(EventTriggerCacheItem));
+ item = palloc0_object(EventTriggerCacheItem);
item->fnoid = form->evtfoid;
item->enabled = form->evtenabled;
diff --git a/src/backend/utils/cache/inval.c b/src/backend/utils/cache/inval.c
index 06f736cab45..868f8f6188f 100644
--- a/src/backend/utils/cache/inval.c
+++ b/src/backend/utils/cache/inval.c
@@ -758,7 +758,7 @@ PrepareInplaceInvalidationState(void)
Assert(inplaceInvalInfo == NULL);
/* gone after WAL insertion CritSection ends, so use current context */
- myInfo = (InvalidationInfo *) palloc0(sizeof(InvalidationInfo));
+ myInfo = palloc0_object(InvalidationInfo);
/* Stash our messages past end of the transactional messages, if any. */
if (transInvalInfo != NULL)
diff --git a/src/backend/utils/cache/lsyscache.c b/src/backend/utils/cache/lsyscache.c
index fa7cd7e06a7..5aa7a26d95c 100644
--- a/src/backend/utils/cache/lsyscache.c
+++ b/src/backend/utils/cache/lsyscache.c
@@ -702,8 +702,7 @@ get_op_index_interpretation(Oid opno)
if (cmptype == COMPARE_INVALID)
continue;
- thisresult = (OpIndexInterpretation *)
- palloc(sizeof(OpIndexInterpretation));
+ thisresult = palloc_object(OpIndexInterpretation);
thisresult->opfamily_id = op_form->amopfamily;
thisresult->cmptype = cmptype;
thisresult->oplefttype = op_form->amoplefttype;
@@ -748,8 +747,7 @@ get_op_index_interpretation(Oid opno)
continue;
/* OK, report it as COMPARE_NE */
- thisresult = (OpIndexInterpretation *)
- palloc(sizeof(OpIndexInterpretation));
+ thisresult = palloc_object(OpIndexInterpretation);
thisresult->opfamily_id = op_form->amopfamily;
thisresult->cmptype = COMPARE_NE;
thisresult->oplefttype = op_form->amoplefttype;
diff --git a/src/backend/utils/cache/partcache.c b/src/backend/utils/cache/partcache.c
index f5d7d70def0..67e88440038 100644
--- a/src/backend/utils/cache/partcache.c
+++ b/src/backend/utils/cache/partcache.c
@@ -167,18 +167,18 @@ RelationBuildPartitionKey(Relation relation)
/* Allocate assorted arrays in the partkeycxt, which we'll fill below */
oldcxt = MemoryContextSwitchTo(partkeycxt);
- key->partattrs = (AttrNumber *) palloc0(key->partnatts * sizeof(AttrNumber));
- key->partopfamily = (Oid *) palloc0(key->partnatts * sizeof(Oid));
- key->partopcintype = (Oid *) palloc0(key->partnatts * sizeof(Oid));
- key->partsupfunc = (FmgrInfo *) palloc0(key->partnatts * sizeof(FmgrInfo));
-
- key->partcollation = (Oid *) palloc0(key->partnatts * sizeof(Oid));
- key->parttypid = (Oid *) palloc0(key->partnatts * sizeof(Oid));
- key->parttypmod = (int32 *) palloc0(key->partnatts * sizeof(int32));
- key->parttyplen = (int16 *) palloc0(key->partnatts * sizeof(int16));
- key->parttypbyval = (bool *) palloc0(key->partnatts * sizeof(bool));
- key->parttypalign = (char *) palloc0(key->partnatts * sizeof(char));
- key->parttypcoll = (Oid *) palloc0(key->partnatts * sizeof(Oid));
+ key->partattrs = palloc0_array(AttrNumber, key->partnatts);
+ key->partopfamily = palloc0_array(Oid, key->partnatts);
+ key->partopcintype = palloc0_array(Oid, key->partnatts);
+ key->partsupfunc = palloc0_array(FmgrInfo, key->partnatts);
+
+ key->partcollation = palloc0_array(Oid, key->partnatts);
+ key->parttypid = palloc0_array(Oid, key->partnatts);
+ key->parttypmod = palloc0_array(int32, key->partnatts);
+ key->parttyplen = palloc0_array(int16, key->partnatts);
+ key->parttypbyval = palloc0_array(bool, key->partnatts);
+ key->parttypalign = palloc0_array(char, key->partnatts);
+ key->parttypcoll = palloc0_array(Oid, key->partnatts);
MemoryContextSwitchTo(oldcxt);
/* determine support function number to search for */
diff --git a/src/backend/utils/cache/plancache.c b/src/backend/utils/cache/plancache.c
index 6661d2c6b73..45261caaf47 100644
--- a/src/backend/utils/cache/plancache.c
+++ b/src/backend/utils/cache/plancache.c
@@ -207,7 +207,7 @@ CreateCachedPlan(RawStmt *raw_parse_tree,
*/
oldcxt = MemoryContextSwitchTo(source_context);
- plansource = (CachedPlanSource *) palloc0(sizeof(CachedPlanSource));
+ plansource = palloc0_object(CachedPlanSource);
plansource->magic = CACHEDPLANSOURCE_MAGIC;
plansource->raw_parse_tree = copyObject(raw_parse_tree);
plansource->analyzed_parse_tree = NULL;
@@ -307,7 +307,7 @@ CreateOneShotCachedPlan(RawStmt *raw_parse_tree,
* Create and fill the CachedPlanSource struct within the caller's memory
* context. Most fields are just left empty for the moment.
*/
- plansource = (CachedPlanSource *) palloc0(sizeof(CachedPlanSource));
+ plansource = palloc0_object(CachedPlanSource);
plansource->magic = CACHEDPLANSOURCE_MAGIC;
plansource->raw_parse_tree = raw_parse_tree;
plansource->analyzed_parse_tree = NULL;
@@ -469,7 +469,7 @@ CompleteCachedPlan(CachedPlanSource *plansource,
if (num_params > 0)
{
- plansource->param_types = (Oid *) palloc(num_params * sizeof(Oid));
+ plansource->param_types = palloc_array(Oid, num_params);
memcpy(plansource->param_types, param_types, num_params * sizeof(Oid));
}
else
@@ -1119,7 +1119,7 @@ BuildCachedPlan(CachedPlanSource *plansource, List *qlist,
/*
* Create and fill the CachedPlan struct within the new context.
*/
- plan = (CachedPlan *) palloc(sizeof(CachedPlan));
+ plan = palloc_object(CachedPlan);
plan->magic = CACHEDPLAN_MAGIC;
plan->stmt_list = plist;
@@ -1691,7 +1691,7 @@ CopyCachedPlan(CachedPlanSource *plansource)
oldcxt = MemoryContextSwitchTo(source_context);
- newsource = (CachedPlanSource *) palloc0(sizeof(CachedPlanSource));
+ newsource = palloc0_object(CachedPlanSource);
newsource->magic = CACHEDPLANSOURCE_MAGIC;
newsource->raw_parse_tree = copyObject(plansource->raw_parse_tree);
newsource->analyzed_parse_tree = copyObject(plansource->analyzed_parse_tree);
@@ -1700,8 +1700,7 @@ CopyCachedPlan(CachedPlanSource *plansource)
newsource->commandTag = plansource->commandTag;
if (plansource->num_params > 0)
{
- newsource->param_types = (Oid *)
- palloc(plansource->num_params * sizeof(Oid));
+ newsource->param_types = palloc_array(Oid, plansource->num_params);
memcpy(newsource->param_types, plansource->param_types,
plansource->num_params * sizeof(Oid));
}
@@ -1840,7 +1839,7 @@ GetCachedExpression(Node *expr)
oldcxt = MemoryContextSwitchTo(cexpr_context);
- cexpr = (CachedExpression *) palloc(sizeof(CachedExpression));
+ cexpr = palloc_object(CachedExpression);
cexpr->magic = CACHEDEXPR_MAGIC;
cexpr->expr = copyObject(expr);
cexpr->is_valid = true;
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 915d0bc9084..a4dc1cbe5ae 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -422,7 +422,7 @@ AllocateRelationDesc(Form_pg_class relp)
/*
* allocate and zero space for new relation descriptor
*/
- relation = (Relation) palloc0(sizeof(RelationData));
+ relation = palloc0_object(RelationData);
/* make sure relation is marked as having no open file yet */
relation->rd_smgr = NULL;
@@ -1902,7 +1902,7 @@ formrdesc(const char *relationName, Oid relationReltype,
/*
* allocate new relation desc, clear all fields of reldesc
*/
- relation = (Relation) palloc0(sizeof(RelationData));
+ relation = palloc0_object(RelationData);
/* make sure relation is marked as having no open file yet */
relation->rd_smgr = NULL;
@@ -1994,7 +1994,7 @@ formrdesc(const char *relationName, Oid relationReltype,
/* mark not-null status */
if (has_not_null)
{
- TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
+ TupleConstr *constr = palloc0_object(TupleConstr);
constr->has_not_null = true;
relation->rd_att->constr = constr;
@@ -3579,7 +3579,7 @@ RelationBuildLocalRelation(const char *relname,
/*
* allocate a new relation descriptor and fill in basic state fields.
*/
- rel = (Relation) palloc0(sizeof(RelationData));
+ rel = palloc0_object(RelationData);
/* make sure relation is marked as having no open file yet */
rel->rd_smgr = NULL;
@@ -3627,7 +3627,7 @@ RelationBuildLocalRelation(const char *relname,
if (has_not_null)
{
- TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
+ TupleConstr *constr = palloc0_object(TupleConstr);
constr->has_not_null = true;
rel->rd_att->constr = constr;
@@ -5670,9 +5670,9 @@ RelationGetExclusionInfo(Relation indexRelation,
indnkeyatts = IndexRelationGetNumberOfKeyAttributes(indexRelation);
/* Allocate result space in caller context */
- *operators = ops = (Oid *) palloc(sizeof(Oid) * indnkeyatts);
- *procs = funcs = (Oid *) palloc(sizeof(Oid) * indnkeyatts);
- *strategies = strats = (uint16 *) palloc(sizeof(uint16) * indnkeyatts);
+ *operators = ops = palloc_array(Oid, indnkeyatts);
+ *procs = funcs = palloc_array(Oid, indnkeyatts);
+ *strategies = strats = palloc_array(uint16, indnkeyatts);
/* Quick exit if we have the data cached already */
if (indexRelation->rd_exclstrats != NULL)
@@ -5763,9 +5763,9 @@ RelationGetExclusionInfo(Relation indexRelation,
/* Save a copy of the results in the relcache entry. */
oldcxt = MemoryContextSwitchTo(indexRelation->rd_indexcxt);
- indexRelation->rd_exclops = (Oid *) palloc(sizeof(Oid) * indnkeyatts);
- indexRelation->rd_exclprocs = (Oid *) palloc(sizeof(Oid) * indnkeyatts);
- indexRelation->rd_exclstrats = (uint16 *) palloc(sizeof(uint16) * indnkeyatts);
+ indexRelation->rd_exclops = palloc_array(Oid, indnkeyatts);
+ indexRelation->rd_exclprocs = palloc_array(Oid, indnkeyatts);
+ indexRelation->rd_exclstrats = palloc_array(uint16, indnkeyatts);
memcpy(indexRelation->rd_exclops, ops, sizeof(Oid) * indnkeyatts);
memcpy(indexRelation->rd_exclprocs, funcs, sizeof(Oid) * indnkeyatts);
memcpy(indexRelation->rd_exclstrats, strats, sizeof(uint16) * indnkeyatts);
@@ -5959,7 +5959,7 @@ RelationBuildPublicationDesc(Relation relation, PublicationDesc *pubdesc)
/* Now save copy of the descriptor in the relcache entry. */
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
- relation->rd_pubdesc = palloc(sizeof(PublicationDesc));
+ relation->rd_pubdesc = palloc_object(PublicationDesc);
memcpy(relation->rd_pubdesc, pubdesc, sizeof(PublicationDesc));
MemoryContextSwitchTo(oldcxt);
}
@@ -5967,7 +5967,7 @@ RelationBuildPublicationDesc(Relation relation, PublicationDesc *pubdesc)
static bytea **
CopyIndexAttOptions(bytea **srcopts, int natts)
{
- bytea **opts = palloc(sizeof(*opts) * natts);
+ bytea **opts = palloc_array(bytea *, natts);
for (int i = 0; i < natts; i++)
{
@@ -5999,7 +5999,7 @@ RelationGetIndexAttOptions(Relation relation, bool copy)
return copy ? CopyIndexAttOptions(opts, natts) : opts;
/* Get and parse opclass options. */
- opts = palloc0(sizeof(*opts) * natts);
+ opts = palloc0_array(bytea *, natts);
for (i = 0; i < natts; i++)
{
@@ -6292,7 +6292,7 @@ load_relcache_init_file(bool shared)
/* mark not-null status */
if (has_not_null)
{
- TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
+ TupleConstr *constr = palloc0_object(TupleConstr);
constr->has_not_null = true;
rel->rd_att->constr = constr;
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 6a347698edf..0c17d99d021 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -2764,7 +2764,7 @@ load_enum_cache_data(TypeCacheEntry *tcache)
* through.
*/
maxitems = 64;
- items = (EnumItem *) palloc(sizeof(EnumItem) * maxitems);
+ items = palloc_array(EnumItem, maxitems);
numitems = 0;
/* Scan pg_enum for the members of the target enum type. */