From 0c3c5c3b06a37c13a811ea93044202e06523b705 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Tue, 9 Dec 2025 14:53:17 +0900 Subject: 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 31d3847a37be. 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 Discussion: https://postgr.es/m/ad0748d4-3080-436e-b0bc-ac8f86a3466a@gmail.com --- src/test/modules/dummy_index_am/dummy_index_am.c | 2 +- src/test/modules/spgist_name_ops/spgist_name_ops.c | 10 +++++----- src/test/modules/test_bitmapset/test_bitmapset.c | 2 +- src/test/modules/test_integerset/test_integerset.c | 2 +- .../test_json_parser/test_json_parser_incremental.c | 2 +- src/test/modules/test_parser/test_parser.c | 4 ++-- src/test/modules/test_radixtree/test_radixtree.c | 2 +- src/test/modules/test_regex/test_regex.c | 14 ++++++-------- src/test/modules/test_resowner/test_resowner_many.c | 2 +- src/test/modules/test_rls_hooks/test_rls_hooks.c | 4 ++-- src/test/modules/worker_spi/worker_spi.c | 2 +- 11 files changed, 22 insertions(+), 24 deletions(-) (limited to 'src/test/modules') diff --git a/src/test/modules/dummy_index_am/dummy_index_am.c b/src/test/modules/dummy_index_am/dummy_index_am.c index 94ef639b6fc..a34382a5f79 100644 --- a/src/test/modules/dummy_index_am/dummy_index_am.c +++ b/src/test/modules/dummy_index_am/dummy_index_am.c @@ -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; diff --git a/src/test/modules/spgist_name_ops/spgist_name_ops.c b/src/test/modules/spgist_name_ops/spgist_name_ops.c index 38e54e0e0a4..f32dfb317fa 100644 --- a/src/test/modules/spgist_name_ops/spgist_name_ops.c +++ b/src/test/modules/spgist_name_ops/spgist_name_ops.c @@ -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++) diff --git a/src/test/modules/test_bitmapset/test_bitmapset.c b/src/test/modules/test_bitmapset/test_bitmapset.c index 8162285fcb3..69383a98e37 100644 --- a/src/test/modules/test_bitmapset/test_bitmapset.c +++ b/src/test/modules/test_bitmapset/test_bitmapset.c @@ -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++) diff --git a/src/test/modules/test_integerset/test_integerset.c b/src/test/modules/test_integerset/test_integerset.c index cfdc6762785..7ac1fe6cb23 100644 --- a/src/test/modules/test_integerset/test_integerset.c +++ b/src/test/modules/test_integerset/test_integerset.c @@ -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); diff --git a/src/test/modules/test_json_parser/test_json_parser_incremental.c b/src/test/modules/test_json_parser/test_json_parser_incremental.c index 8c78061ee46..a95ac798481 100644 --- a/src/test/modules/test_json_parser/test_json_parser_incremental.c +++ b/src/test/modules/test_json_parser/test_json_parser_incremental.c @@ -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; diff --git a/src/test/modules/test_parser/test_parser.c b/src/test/modules/test_parser/test_parser.c index 15ed3617cb5..353f9072819 100644 --- a/src/test/modules/test_parser/test_parser.c +++ b/src/test/modules/test_parser/test_parser.c @@ -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; diff --git a/src/test/modules/test_radixtree/test_radixtree.c b/src/test/modules/test_radixtree/test_radixtree.c index 606d8d3cd2d..031e8737d45 100644 --- a/src/test/modules/test_radixtree/test_radixtree.c +++ b/src/test/modules/test_radixtree/test_radixtree.c @@ -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) diff --git a/src/test/modules/test_regex/test_regex.c b/src/test/modules/test_regex/test_regex.c index 2548a0ef7b1..32ff9b13d8f 100644 --- a/src/test/modules/test_regex/test_regex.c +++ b/src/test/modules/test_regex/test_regex.c @@ -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 */ diff --git a/src/test/modules/test_resowner/test_resowner_many.c b/src/test/modules/test_resowner/test_resowner_many.c index 1f64939404f..e43f911244a 100644 --- a/src/test/modules/test_resowner/test_resowner_many.c +++ b/src/test/modules/test_resowner/test_resowner_many.c @@ -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); diff --git a/src/test/modules/test_rls_hooks/test_rls_hooks.c b/src/test/modules/test_rls_hooks/test_rls_hooks.c index b1f161cf7bb..86453f96147 100644 --- a/src/test/modules/test_rls_hooks/test_rls_hooks.c +++ b/src/test/modules/test_rls_hooks/test_rls_hooks.c @@ -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; diff --git a/src/test/modules/worker_spi/worker_spi.c b/src/test/modules/worker_spi/worker_spi.c index bea8339f464..1a21b8c8876 100644 --- a/src/test/modules/worker_spi/worker_spi.c +++ b/src/test/modules/worker_spi/worker_spi.c @@ -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"); -- cgit v1.2.3