1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-19 17:02:53 +03:00

Use palloc_object() and palloc_array() in more areas of the tree

The idea is to encourage more the use of these new routines across the
tree, as these offer stronger type safety guarantees than palloc().

The following paths are included in this batch, treating all the areas
proposed by the author for the most trivial changes, except src/backend
(by far the largest batch):
src/bin/
src/common/
src/fe_utils/
src/include/
src/pl/
src/test/
src/tutorial/

Similar work has been done in 31d3847a37.

The code compiles the same before and after this commit, with the
following exceptions due to changes in line numbers because some of the
new allocation formulas are shorter:
blkreftable.c
pgfnames.c
pl_exec.c

Author: David Geier <geidav.pg@gmail.com>
Discussion: https://postgr.es/m/ad0748d4-3080-436e-b0bc-ac8f86a3466a@gmail.com
This commit is contained in:
Michael Paquier
2025-12-09 14:53:17 +09:00
parent aa749bde32
commit 0c3c5c3b06
43 changed files with 156 additions and 161 deletions

View File

@@ -138,7 +138,7 @@ dibuild(Relation heap, Relation index, IndexInfo *indexInfo)
{
IndexBuildResult *result;
result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
result = palloc_object(IndexBuildResult);
/* let's pretend that no tuples were scanned */
result->heap_tuples = 0;

View File

@@ -171,7 +171,7 @@ spgist_name_choose(PG_FUNCTION_ARGS)
}
out->result.splitTuple.prefixNNodes = 1;
out->result.splitTuple.prefixNodeLabels =
(Datum *) palloc(sizeof(Datum));
palloc_object(Datum);
out->result.splitTuple.prefixNodeLabels[0] =
Int16GetDatum(*(unsigned char *) (prefixStr + commonLen));
@@ -243,7 +243,7 @@ spgist_name_choose(PG_FUNCTION_ARGS)
out->result.splitTuple.prefixHasPrefix = in->hasPrefix;
out->result.splitTuple.prefixPrefixDatum = in->prefixDatum;
out->result.splitTuple.prefixNNodes = 1;
out->result.splitTuple.prefixNodeLabels = (Datum *) palloc(sizeof(Datum));
out->result.splitTuple.prefixNodeLabels = palloc_object(Datum);
out->result.splitTuple.prefixNodeLabels[0] = Int16GetDatum(-2);
out->result.splitTuple.childNodeN = 0;
out->result.splitTuple.postfixHasPrefix = false;
@@ -318,9 +318,9 @@ spgist_name_inner_consistent(PG_FUNCTION_ARGS)
* and see if it's consistent with the query. If so, emit an entry into
* the output arrays.
*/
out->nodeNumbers = (int *) palloc(sizeof(int) * in->nNodes);
out->levelAdds = (int *) palloc(sizeof(int) * in->nNodes);
out->reconstructedValues = (Datum *) palloc(sizeof(Datum) * in->nNodes);
out->nodeNumbers = palloc_array(int, in->nNodes);
out->levelAdds = palloc_array(int, in->nNodes);
out->reconstructedValues = palloc_array(Datum, in->nNodes);
out->nNodes = 0;
for (i = 0; i < in->nNodes; i++)

View File

@@ -622,7 +622,7 @@ test_random_operations(PG_FUNCTION_ARGS)
* still possible if all the operations hit the "0" case during phase 4
* where multiple operation types are mixed together.
*/
members = palloc(sizeof(int) * num_ops);
members = palloc_array(int, num_ops);
/* Phase 1: Random insertions in first set */
for (int i = 0; i < num_ops / 2; i++)

View File

@@ -385,7 +385,7 @@ test_single_value_and_filler(uint64 value, uint64 filler_min, uint64 filler_max)
intset = intset_create();
iter_expected = palloc(sizeof(uint64) * (filler_max - filler_min + 1));
iter_expected = palloc_array(uint64, filler_max - filler_min + 1);
if (value < filler_min)
{
intset_add_member(intset, value);

View File

@@ -124,7 +124,7 @@ main(int argc, char **argv)
break;
case 's': /* do semantic processing */
testsem = &sem;
sem.semstate = palloc(sizeof(struct DoState));
sem.semstate = palloc_object(struct DoState);
((struct DoState *) sem.semstate)->lex = lex;
((struct DoState *) sem.semstate)->buf = makeStringInfo();
need_strings = true;

View File

@@ -46,7 +46,7 @@ PG_FUNCTION_INFO_V1(testprs_lextype);
Datum
testprs_start(PG_FUNCTION_ARGS)
{
ParserState *pst = (ParserState *) palloc0(sizeof(ParserState));
ParserState *pst = palloc0_object(ParserState);
pst->buffer = (char *) PG_GETARG_POINTER(0);
pst->len = PG_GETARG_INT32(1);
@@ -112,7 +112,7 @@ testprs_lextype(PG_FUNCTION_ARGS)
* the same lexids like Teodor in the default word parser; in this way we
* can reuse the headline function of the default word parser.
*/
LexDescr *descr = (LexDescr *) palloc(sizeof(LexDescr) * (2 + 1));
LexDescr *descr = palloc_array(LexDescr, 2 + 1);
/* there are only two types in this parser */
descr[0].lexid = 3;

View File

@@ -183,7 +183,7 @@ test_basic(rt_node_class_test_elem *test_info, int shift, bool asc)
elog(NOTICE, "testing node %s with shift %d and %s keys",
test_info->class_name, shift, asc ? "ascending" : "descending");
keys = palloc(sizeof(uint64) * children);
keys = palloc_array(uint64, children);
for (int i = 0; i < children; i++)
{
if (asc)

View File

@@ -107,10 +107,8 @@ test_regex(PG_FUNCTION_ARGS)
true);
/* Pre-create workspace that build_test_match_result needs */
matchctx->elems = (Datum *) palloc(sizeof(Datum) *
(matchctx->npatterns + 1));
matchctx->nulls = (bool *) palloc(sizeof(bool) *
(matchctx->npatterns + 1));
matchctx->elems = palloc_array(Datum, matchctx->npatterns + 1);
matchctx->nulls = palloc_array(bool, matchctx->npatterns + 1);
MemoryContextSwitchTo(oldcontext);
funcctx->user_fctx = matchctx;
@@ -436,7 +434,7 @@ setup_test_matches(text *orig_str,
Oid collation,
bool use_subpatterns)
{
test_regex_ctx *matchctx = palloc0(sizeof(test_regex_ctx));
test_regex_ctx *matchctx = palloc0_object(test_regex_ctx);
int eml = pg_database_encoding_max_length();
int orig_len;
pg_wchar *wide_str;
@@ -457,7 +455,7 @@ setup_test_matches(text *orig_str,
/* convert string to pg_wchar form for matching */
orig_len = VARSIZE_ANY_EXHDR(orig_str);
wide_str = (pg_wchar *) palloc(sizeof(pg_wchar) * (orig_len + 1));
wide_str = palloc_array(pg_wchar, orig_len + 1);
wide_len = pg_mb2wchar_with_len(VARDATA_ANY(orig_str), wide_str, orig_len);
/* do we want to remember subpatterns? */
@@ -474,7 +472,7 @@ setup_test_matches(text *orig_str,
}
/* temporary output space for RE package */
pmatch = palloc(sizeof(regmatch_t) * pmatch_len);
pmatch = palloc_array(regmatch_t, pmatch_len);
/*
* the real output space (grown dynamically if needed)
@@ -483,7 +481,7 @@ setup_test_matches(text *orig_str,
* than at 2^27
*/
array_len = re_flags->glob ? 255 : 31;
matchctx->match_locs = (int *) palloc(sizeof(int) * array_len);
matchctx->match_locs = palloc_array(int, array_len);
array_idx = 0;
/* search for the pattern, perhaps repeatedly */

View File

@@ -121,7 +121,7 @@ RememberManyTestResources(ResourceOwner owner,
for (int i = 0; i < nresources; i++)
{
ManyTestResource *mres = palloc(sizeof(ManyTestResource));
ManyTestResource *mres = palloc_object(ManyTestResource);
mres->kind = &kinds[kind_idx];
dlist_node_init(&mres->node);

View File

@@ -44,7 +44,7 @@ List *
test_rls_hooks_permissive(CmdType cmdtype, Relation relation)
{
List *policies = NIL;
RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
RowSecurityPolicy *policy = palloc0_object(RowSecurityPolicy);
Datum role;
FuncCall *n;
Node *e;
@@ -112,7 +112,7 @@ List *
test_rls_hooks_restrictive(CmdType cmdtype, Relation relation)
{
List *policies = NIL;
RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
RowSecurityPolicy *policy = palloc0_object(RowSecurityPolicy);
Datum role;
FuncCall *n;
Node *e;

View File

@@ -142,7 +142,7 @@ worker_spi_main(Datum main_arg)
char *p;
bits32 flags = 0;
table = palloc(sizeof(worktable));
table = palloc_object(worktable);
sprintf(name, "schema%d", index);
table->schema = pstrdup(name);
table->name = pstrdup("counted");

View File

@@ -198,7 +198,7 @@ widget_in(PG_FUNCTION_ARGS)
errmsg("invalid input syntax for type %s: \"%s\"",
"widget", str)));
result = (WIDGET *) palloc(sizeof(WIDGET));
result = palloc_object(WIDGET);
result->center.x = atof(coord[0]);
result->center.y = atof(coord[1]);
result->radius = atof(coord[2]);