diff options
| author | Michael Paquier | 2025-12-09 22:36:46 +0000 |
|---|---|---|
| committer | Michael Paquier | 2025-12-09 22:36:46 +0000 |
| commit | 1b105f9472bdb9a68f709778afafb494997267bd (patch) | |
| tree | 205727d30b0dd4f3060bc1f49a9efa724ee3ff80 /src/backend/parser | |
| parent | c507ba55f5bfae900baa94f1c657e1d99da5c6dc (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/parser')
| -rw-r--r-- | src/backend/parser/analyze.c | 5 | ||||
| -rw-r--r-- | src/backend/parser/gram.y | 92 | ||||
| -rw-r--r-- | src/backend/parser/parse_clause.c | 4 | ||||
| -rw-r--r-- | src/backend/parser/parse_expr.c | 4 | ||||
| -rw-r--r-- | src/backend/parser/parse_node.c | 2 | ||||
| -rw-r--r-- | src/backend/parser/parse_param.c | 4 | ||||
| -rw-r--r-- | src/backend/parser/parse_relation.c | 10 | ||||
| -rw-r--r-- | src/backend/parser/parse_type.c | 2 |
8 files changed, 61 insertions, 62 deletions
diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 05314e7e76b..92be345d9a8 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -2641,8 +2641,7 @@ addNSItemForReturning(ParseState *pstate, const char *aliasname, colnames = pstate->p_target_nsitem->p_rte->eref->colnames; numattrs = list_length(colnames); - nscolumns = (ParseNamespaceColumn *) - palloc(numattrs * sizeof(ParseNamespaceColumn)); + nscolumns = palloc_array(ParseNamespaceColumn, numattrs); memcpy(nscolumns, pstate->p_target_nsitem->p_nscolumns, numattrs * sizeof(ParseNamespaceColumn)); @@ -2652,7 +2651,7 @@ addNSItemForReturning(ParseState *pstate, const char *aliasname, nscolumns[i].p_varreturningtype = returning_type; /* build the nsitem, copying most fields from the target relation */ - nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem)); + nsitem = palloc_object(ParseNamespaceItem); nsitem->p_names = makeAlias(aliasname, colnames); nsitem->p_rte = pstate->p_target_nsitem->p_rte; nsitem->p_rtindex = pstate->p_target_nsitem->p_rtindex; diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index c3a0a354a9c..7856ce9d78f 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -4555,19 +4555,19 @@ OptWhereClause: key_actions: key_update { - KeyActions *n = palloc(sizeof(KeyActions)); + KeyActions *n = palloc_object(KeyActions); n->updateAction = $1; - n->deleteAction = palloc(sizeof(KeyAction)); + n->deleteAction = palloc_object(KeyAction); n->deleteAction->action = FKCONSTR_ACTION_NOACTION; n->deleteAction->cols = NIL; $$ = n; } | key_delete { - KeyActions *n = palloc(sizeof(KeyActions)); + KeyActions *n = palloc_object(KeyActions); - n->updateAction = palloc(sizeof(KeyAction)); + n->updateAction = palloc_object(KeyAction); n->updateAction->action = FKCONSTR_ACTION_NOACTION; n->updateAction->cols = NIL; n->deleteAction = $1; @@ -4575,7 +4575,7 @@ key_actions: } | key_update key_delete { - KeyActions *n = palloc(sizeof(KeyActions)); + KeyActions *n = palloc_object(KeyActions); n->updateAction = $1; n->deleteAction = $2; @@ -4583,7 +4583,7 @@ key_actions: } | key_delete key_update { - KeyActions *n = palloc(sizeof(KeyActions)); + KeyActions *n = palloc_object(KeyActions); n->updateAction = $2; n->deleteAction = $1; @@ -4591,12 +4591,12 @@ key_actions: } | /*EMPTY*/ { - KeyActions *n = palloc(sizeof(KeyActions)); + KeyActions *n = palloc_object(KeyActions); - n->updateAction = palloc(sizeof(KeyAction)); + n->updateAction = palloc_object(KeyAction); n->updateAction->action = FKCONSTR_ACTION_NOACTION; n->updateAction->cols = NIL; - n->deleteAction = palloc(sizeof(KeyAction)); + n->deleteAction = palloc_object(KeyAction); n->deleteAction->action = FKCONSTR_ACTION_NOACTION; n->deleteAction->cols = NIL; $$ = n; @@ -4624,7 +4624,7 @@ key_delete: ON DELETE_P key_action key_action: NO ACTION { - KeyAction *n = palloc(sizeof(KeyAction)); + KeyAction *n = palloc_object(KeyAction); n->action = FKCONSTR_ACTION_NOACTION; n->cols = NIL; @@ -4632,7 +4632,7 @@ key_action: } | RESTRICT { - KeyAction *n = palloc(sizeof(KeyAction)); + KeyAction *n = palloc_object(KeyAction); n->action = FKCONSTR_ACTION_RESTRICT; n->cols = NIL; @@ -4640,7 +4640,7 @@ key_action: } | CASCADE { - KeyAction *n = palloc(sizeof(KeyAction)); + KeyAction *n = palloc_object(KeyAction); n->action = FKCONSTR_ACTION_CASCADE; n->cols = NIL; @@ -4648,7 +4648,7 @@ key_action: } | SET NULL_P opt_column_list { - KeyAction *n = palloc(sizeof(KeyAction)); + KeyAction *n = palloc_object(KeyAction); n->action = FKCONSTR_ACTION_SETNULL; n->cols = $3; @@ -4656,7 +4656,7 @@ key_action: } | SET DEFAULT opt_column_list { - KeyAction *n = palloc(sizeof(KeyAction)); + KeyAction *n = palloc_object(KeyAction); n->action = FKCONSTR_ACTION_SETDEFAULT; n->cols = $3; @@ -5853,7 +5853,7 @@ import_qualification_type: import_qualification: import_qualification_type '(' relation_expr_list ')' { - ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual)); + ImportQual *n = palloc_object(ImportQual); n->type = $1; n->table_names = $3; @@ -5861,7 +5861,7 @@ import_qualification: } | /*EMPTY*/ { - ImportQual *n = (ImportQual *) palloc(sizeof(ImportQual)); + ImportQual *n = palloc_object(ImportQual); n->type = FDW_IMPORT_SCHEMA_ALL; n->table_names = NIL; $$ = n; @@ -7914,7 +7914,7 @@ parameter_name: privilege_target: qualified_name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_TABLE; @@ -7923,7 +7923,7 @@ privilege_target: } | TABLE qualified_name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_TABLE; @@ -7932,7 +7932,7 @@ privilege_target: } | SEQUENCE qualified_name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_SEQUENCE; @@ -7941,7 +7941,7 @@ privilege_target: } | FOREIGN DATA_P WRAPPER name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_FDW; @@ -7950,7 +7950,7 @@ privilege_target: } | FOREIGN SERVER name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_FOREIGN_SERVER; @@ -7959,7 +7959,7 @@ privilege_target: } | FUNCTION function_with_argtypes_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_FUNCTION; @@ -7968,7 +7968,7 @@ privilege_target: } | PROCEDURE function_with_argtypes_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_PROCEDURE; @@ -7977,7 +7977,7 @@ privilege_target: } | ROUTINE function_with_argtypes_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_ROUTINE; @@ -7986,7 +7986,7 @@ privilege_target: } | DATABASE name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_DATABASE; @@ -7995,7 +7995,7 @@ privilege_target: } | DOMAIN_P any_name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_DOMAIN; @@ -8004,7 +8004,7 @@ privilege_target: } | LANGUAGE name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_LANGUAGE; @@ -8013,7 +8013,7 @@ privilege_target: } | LARGE_P OBJECT_P NumericOnly_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_LARGEOBJECT; @@ -8022,7 +8022,7 @@ privilege_target: } | PARAMETER parameter_name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_PARAMETER_ACL; n->objs = $2; @@ -8030,7 +8030,7 @@ privilege_target: } | SCHEMA name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_SCHEMA; @@ -8039,7 +8039,7 @@ privilege_target: } | TABLESPACE name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_TABLESPACE; @@ -8048,7 +8048,7 @@ privilege_target: } | TYPE_P any_name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_OBJECT; n->objtype = OBJECT_TYPE; @@ -8057,7 +8057,7 @@ privilege_target: } | ALL TABLES IN_P SCHEMA name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_ALL_IN_SCHEMA; n->objtype = OBJECT_TABLE; @@ -8066,7 +8066,7 @@ privilege_target: } | ALL SEQUENCES IN_P SCHEMA name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_ALL_IN_SCHEMA; n->objtype = OBJECT_SEQUENCE; @@ -8075,7 +8075,7 @@ privilege_target: } | ALL FUNCTIONS IN_P SCHEMA name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_ALL_IN_SCHEMA; n->objtype = OBJECT_FUNCTION; @@ -8084,7 +8084,7 @@ privilege_target: } | ALL PROCEDURES IN_P SCHEMA name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_ALL_IN_SCHEMA; n->objtype = OBJECT_PROCEDURE; @@ -8093,7 +8093,7 @@ privilege_target: } | ALL ROUTINES IN_P SCHEMA name_list { - PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget)); + PrivTarget *n = palloc_object(PrivTarget); n->targtype = ACL_TARGET_ALL_IN_SCHEMA; n->objtype = OBJECT_ROUTINE; @@ -13393,7 +13393,7 @@ select_limit: } | offset_clause { - SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit)); + SelectLimit *n = palloc_object(SelectLimit); n->limitOffset = $1; n->limitCount = NULL; @@ -13413,7 +13413,7 @@ opt_select_limit: limit_clause: LIMIT select_limit_value { - SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit)); + SelectLimit *n = palloc_object(SelectLimit); n->limitOffset = NULL; n->limitCount = $2; @@ -13441,7 +13441,7 @@ limit_clause: */ | FETCH first_or_next select_fetch_first_value row_or_rows ONLY { - SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit)); + SelectLimit *n = palloc_object(SelectLimit); n->limitOffset = NULL; n->limitCount = $3; @@ -13453,7 +13453,7 @@ limit_clause: } | FETCH first_or_next select_fetch_first_value row_or_rows WITH TIES { - SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit)); + SelectLimit *n = palloc_object(SelectLimit); n->limitOffset = NULL; n->limitCount = $3; @@ -13465,7 +13465,7 @@ limit_clause: } | FETCH first_or_next row_or_rows ONLY { - SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit)); + SelectLimit *n = palloc_object(SelectLimit); n->limitOffset = NULL; n->limitCount = makeIntConst(1, -1); @@ -13477,7 +13477,7 @@ limit_clause: } | FETCH first_or_next row_or_rows WITH TIES { - SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit)); + SelectLimit *n = palloc_object(SelectLimit); n->limitOffset = NULL; n->limitCount = makeIntConst(1, -1); @@ -13572,7 +13572,7 @@ first_or_next: FIRST_P { $$ = 0; } group_clause: GROUP_P BY set_quantifier group_by_list { - GroupClause *n = (GroupClause *) palloc(sizeof(GroupClause)); + GroupClause *n = palloc_object(GroupClause); n->distinct = $3 == SET_QUANTIFIER_DISTINCT; n->all = false; @@ -13581,7 +13581,7 @@ group_clause: } | GROUP_P BY ALL { - GroupClause *n = (GroupClause *) palloc(sizeof(GroupClause)); + GroupClause *n = palloc_object(GroupClause); n->distinct = false; n->all = true; n->list = NIL; @@ -13589,7 +13589,7 @@ group_clause: } | /*EMPTY*/ { - GroupClause *n = (GroupClause *) palloc(sizeof(GroupClause)); + GroupClause *n = palloc_object(GroupClause); n->distinct = false; n->all = false; diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index ca26f6f61f2..944482207f3 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -733,7 +733,7 @@ transformRangeTableFunc(ParseState *pstate, RangeTableFunc *rtf) tf->ordinalitycol = -1; /* Process column specs */ - names = palloc(sizeof(char *) * list_length(rtf->columns)); + names = palloc_array(char *, list_length(rtf->columns)); colno = 0; foreach(col, rtf->columns) @@ -1573,7 +1573,7 @@ transformFromClauseItem(ParseState *pstate, Node *n, { ParseNamespaceItem *jnsitem; - jnsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem)); + jnsitem = palloc_object(ParseNamespaceItem); jnsitem->p_names = j->join_using_alias; jnsitem->p_rte = nsitem->p_rte; jnsitem->p_rtindex = nsitem->p_rtindex; diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 4524e49c326..6b8fa15fca3 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -2906,7 +2906,7 @@ make_row_comparison_op(ParseState *pstate, List *opname, * operators, and see which interpretations (cmptypes) exist for each * operator. */ - opinfo_lists = (List **) palloc(nopers * sizeof(List *)); + opinfo_lists = palloc_array(List *, nopers); cmptypes = NULL; i = 0; foreach(l, opexprs) @@ -3241,7 +3241,7 @@ getJsonEncodingConst(JsonFormat *format) { JsonEncoding encoding; const char *enc; - Name encname = palloc(sizeof(NameData)); + Name encname = palloc_object(NameData); if (!format || format->format_type == JS_FORMAT_DEFAULT || diff --git a/src/backend/parser/parse_node.c b/src/backend/parser/parse_node.c index 203b7a32178..cafeb87217c 100644 --- a/src/backend/parser/parse_node.c +++ b/src/backend/parser/parse_node.c @@ -40,7 +40,7 @@ make_parsestate(ParseState *parentParseState) { ParseState *pstate; - pstate = palloc0(sizeof(ParseState)); + pstate = palloc0_object(ParseState); pstate->parentParseState = parentParseState; diff --git a/src/backend/parser/parse_param.c b/src/backend/parser/parse_param.c index 930921626b6..772f3e3c1d8 100644 --- a/src/backend/parser/parse_param.c +++ b/src/backend/parser/parse_param.c @@ -68,7 +68,7 @@ void setup_parse_fixed_parameters(ParseState *pstate, const Oid *paramTypes, int numParams) { - FixedParamState *parstate = palloc(sizeof(FixedParamState)); + FixedParamState *parstate = palloc_object(FixedParamState); parstate->paramTypes = paramTypes; parstate->numParams = numParams; @@ -84,7 +84,7 @@ void setup_parse_variable_parameters(ParseState *pstate, Oid **paramTypes, int *numParams) { - VarParamState *parstate = palloc(sizeof(VarParamState)); + VarParamState *parstate = palloc_object(VarParamState); parstate->paramTypes = paramTypes; parstate->numParams = numParams; diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c index d544a69fc80..dd64f45478a 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -964,7 +964,7 @@ searchRangeTableForCol(ParseState *pstate, const char *alias, const char *colnam int location) { ParseState *orig_pstate = pstate; - FuzzyAttrMatchState *fuzzystate = palloc(sizeof(FuzzyAttrMatchState)); + FuzzyAttrMatchState *fuzzystate = palloc_object(FuzzyAttrMatchState); fuzzystate->distance = MAX_FUZZY_DISTANCE + 1; fuzzystate->rfirst = NULL; @@ -1336,7 +1336,7 @@ buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, } /* ... and build the nsitem */ - nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem)); + nsitem = palloc_object(ParseNamespaceItem); nsitem->p_names = rte->eref; nsitem->p_rte = rte; nsitem->p_rtindex = rtindex; @@ -1400,7 +1400,7 @@ buildNSItemFromLists(RangeTblEntry *rte, Index rtindex, } /* ... and build the nsitem */ - nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem)); + nsitem = palloc_object(ParseNamespaceItem); nsitem->p_names = rte->eref; nsitem->p_rte = rte; nsitem->p_rtindex = rtindex; @@ -1791,7 +1791,7 @@ addRangeTableEntryForFunction(ParseState *pstate, rte->eref = eref; /* Process each function ... */ - functupdescs = (TupleDesc *) palloc(nfuncs * sizeof(TupleDesc)); + functupdescs = palloc_array(TupleDesc, nfuncs); totalatts = 0; funcno = 0; @@ -2302,7 +2302,7 @@ addRangeTableEntryForJoin(ParseState *pstate, * Build a ParseNamespaceItem, but don't add it to the pstate's namespace * list --- caller must do that if appropriate. */ - nsitem = (ParseNamespaceItem *) palloc(sizeof(ParseNamespaceItem)); + nsitem = palloc_object(ParseNamespaceItem); nsitem->p_names = rte->eref; nsitem->p_rte = rte; nsitem->p_perminfo = NULL; diff --git a/src/backend/parser/parse_type.c b/src/backend/parser/parse_type.c index e07d65fc25b..9d7aa6967da 100644 --- a/src/backend/parser/parse_type.c +++ b/src/backend/parser/parse_type.c @@ -369,7 +369,7 @@ typenameTypeMod(ParseState *pstate, const TypeName *typeName, Type typ) * Currently, we allow simple numeric constants, string literals, and * identifiers; possibly this list could be extended. */ - datums = (Datum *) palloc(list_length(typeName->typmods) * sizeof(Datum)); + datums = palloc_array(Datum, list_length(typeName->typmods)); n = 0; foreach(l, typeName->typmods) { |
