Skip to content

Commit 31d3847

Browse files
committed
Use more palloc_object() and palloc_array() in contrib/
The idea is to encourage more the use of these new routines across the tree, as these offer stronger type safety guarantees than palloc(). In an ideal world, palloc() would then act as an internal routine of these flavors, whose footprint in the tree is minimal. The patch sent by the author is very large, and this chunk of changes represents something like 10% of the overall patch submitted. The code compiled is the same before and after this commit, using objdump to do some validation with a difference taken in-between. There are some diffs, which are caused by changes in line numbers because some of the new allocation formulas are shorter, for the following files: trgm_regexp.c, xpath.c and pg_walinspect.c. Author: David Geier <geidav.pg@gmail.com> Discussion: https://postgr.es/m/ad0748d4-3080-436e-b0bc-ac8f86a3466a@gmail.com
1 parent 2f04110 commit 31d3847

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+157
-161
lines changed

contrib/amcheck/verify_gin.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ ginReadTupleWithoutState(IndexTuple itup, int *nitems)
117117
}
118118
else
119119
{
120-
ipd = (ItemPointer) palloc(sizeof(ItemPointerData) * nipd);
120+
ipd = palloc_array(ItemPointerData, nipd);
121121
memcpy(ipd, ptr, sizeof(ItemPointerData) * nipd);
122122
}
123123
*nitems = nipd;
@@ -152,7 +152,7 @@ gin_check_posting_tree_parent_keys_consistency(Relation rel, BlockNumber posting
152152
leafdepth = -1;
153153

154154
/* Start the scan at the root page */
155-
stack = (GinPostingTreeScanItem *) palloc0(sizeof(GinPostingTreeScanItem));
155+
stack = palloc0_object(GinPostingTreeScanItem);
156156
stack->depth = 0;
157157
ItemPointerSetInvalid(&stack->parentkey);
158158
stack->parentblk = InvalidBlockNumber;
@@ -354,7 +354,7 @@ gin_check_posting_tree_parent_keys_consistency(Relation rel, BlockNumber posting
354354
stack->blkno, i)));
355355

356356
/* This is an internal page, recurse into the child. */
357-
ptr = (GinPostingTreeScanItem *) palloc(sizeof(GinPostingTreeScanItem));
357+
ptr = palloc_object(GinPostingTreeScanItem);
358358
ptr->depth = stack->depth + 1;
359359

360360
/*
@@ -412,7 +412,7 @@ gin_check_parent_keys_consistency(Relation rel,
412412
leafdepth = -1;
413413

414414
/* Start the scan at the root page */
415-
stack = (GinScanItem *) palloc0(sizeof(GinScanItem));
415+
stack = palloc0_object(GinScanItem);
416416
stack->depth = 0;
417417
stack->parenttup = NULL;
418418
stack->parentblk = InvalidBlockNumber;
@@ -473,7 +473,7 @@ gin_check_parent_keys_consistency(Relation rel,
473473

474474
elog(DEBUG3, "split detected for blk: %u, parent blk: %u", stack->blkno, stack->parentblk);
475475

476-
ptr = (GinScanItem *) palloc(sizeof(GinScanItem));
476+
ptr = palloc_object(GinScanItem);
477477
ptr->depth = stack->depth;
478478
ptr->parenttup = CopyIndexTuple(stack->parenttup);
479479
ptr->parentblk = stack->parentblk;
@@ -601,7 +601,7 @@ gin_check_parent_keys_consistency(Relation rel,
601601
{
602602
GinScanItem *ptr;
603603

604-
ptr = (GinScanItem *) palloc(sizeof(GinScanItem));
604+
ptr = palloc_object(GinScanItem);
605605
ptr->depth = stack->depth + 1;
606606
/* last tuple in layer has no high key */
607607
if (i == maxoff && rightlink == InvalidBlockNumber)

contrib/amcheck/verify_heapam.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,7 @@ check_tuple_attribute(HeapCheckContext *ctx)
18381838
{
18391839
ToastedAttribute *ta;
18401840

1841-
ta = (ToastedAttribute *) palloc0(sizeof(ToastedAttribute));
1841+
ta = palloc0_object(ToastedAttribute);
18421842

18431843
VARATT_EXTERNAL_GET_POINTER(ta->toast_pointer, attr);
18441844
ta->blkno = ctx->blkno;

contrib/amcheck/verify_nbtree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ bt_check_every_level(Relation rel, Relation heaprel, bool heapkeyspace,
399399
/*
400400
* Initialize state for entire verification operation
401401
*/
402-
state = palloc0(sizeof(BtreeCheckState));
402+
state = palloc0_object(BtreeCheckState);
403403
state->rel = rel;
404404
state->heaprel = heaprel;
405405
state->heapkeyspace = heapkeyspace;

contrib/basebackup_to_shell/basebackup_to_shell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ shell_get_sink(bbsink *next_sink, void *detail_arg)
136136
* We remember the current value of basebackup_to_shell.shell_command to
137137
* be certain that it can't change under us during the backup.
138138
*/
139-
sink = palloc0(sizeof(bbsink_shell));
139+
sink = palloc0_object(bbsink_shell);
140140
*((const bbsink_ops **) &sink->base.bbs_ops) = &bbsink_shell_ops;
141141
sink->base.bbs_next = next_sink;
142142
sink->target_detail = detail_arg;

contrib/bloom/blinsert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ blbuild(Relation heap, Relation index, IndexInfo *indexInfo)
151151

152152
MemoryContextDelete(buildstate.tmpCtx);
153153

154-
result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
154+
result = palloc_object(IndexBuildResult);
155155
result->heap_tuples = reltuples;
156156
result->index_tuples = buildstate.indtuples;
157157

contrib/bloom/blscan.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ blbeginscan(Relation r, int nkeys, int norderbys)
2929

3030
scan = RelationGetIndexScan(r, nkeys, norderbys);
3131

32-
so = (BloomScanOpaque) palloc(sizeof(BloomScanOpaqueData));
32+
so = (BloomScanOpaque) palloc_object(BloomScanOpaqueData);
3333
initBloomState(&so->state, scan->indexRelation);
3434
so->sign = NULL;
3535

@@ -86,7 +86,7 @@ blgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
8686
/* New search: have to calculate search signature */
8787
ScanKey skey = scan->keyData;
8888

89-
so->sign = palloc0(sizeof(BloomSignatureWord) * so->state.opts.bloomLength);
89+
so->sign = palloc0_array(BloomSignatureWord, so->state.opts.bloomLength);
9090

9191
for (i = 0; i < scan->numberOfKeys; i++)
9292
{

contrib/bloom/blutils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ makeDefaultBloomOptions(void)
8686
BloomOptions *opts;
8787
int i;
8888

89-
opts = (BloomOptions *) palloc0(sizeof(BloomOptions));
89+
opts = palloc0_object(BloomOptions);
9090
/* Convert DEFAULT_BLOOM_LENGTH from # of bits to # of words */
9191
opts->bloomLength = (DEFAULT_BLOOM_LENGTH + SIGNWORDBITS - 1) / SIGNWORDBITS;
9292
for (i = 0; i < INDEX_MAX_KEYS; i++)

contrib/bloom/blvacuum.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ blbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
4242
GenericXLogState *gxlogState;
4343

4444
if (stats == NULL)
45-
stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult));
45+
stats = palloc0_object(IndexBulkDeleteResult);
4646

4747
initBloomState(&state, index);
4848

@@ -171,7 +171,7 @@ blvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
171171
return stats;
172172

173173
if (stats == NULL)
174-
stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult));
174+
stats = palloc0_object(IndexBulkDeleteResult);
175175

176176
/*
177177
* Iterate over the pages: insert deleted pages into FSM and collect

contrib/btree_gin/btree_gin.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ gin_btree_extract_value(FunctionCallInfo fcinfo, bool is_varlena)
5252
{
5353
Datum datum = PG_GETARG_DATUM(0);
5454
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
55-
Datum *entries = (Datum *) palloc(sizeof(Datum));
55+
Datum *entries = palloc_object(Datum);
5656

5757
/* Ensure that values stored in the index are not toasted */
5858
if (is_varlena)
@@ -75,9 +75,9 @@ gin_btree_extract_query(FunctionCallInfo fcinfo,
7575
StrategyNumber strategy = PG_GETARG_UINT16(2);
7676
bool **partialmatch = (bool **) PG_GETARG_POINTER(3);
7777
Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4);
78-
Datum *entries = (Datum *) palloc(sizeof(Datum));
79-
QueryInfo *data = (QueryInfo *) palloc(sizeof(QueryInfo));
80-
bool *ptr_partialmatch = (bool *) palloc(sizeof(bool));
78+
Datum *entries = palloc_object(Datum);
79+
QueryInfo *data = palloc_object(QueryInfo);
80+
bool *ptr_partialmatch = palloc_object(bool);
8181
int btree_strat,
8282
rhs_code;
8383

@@ -140,7 +140,7 @@ gin_btree_extract_query(FunctionCallInfo fcinfo,
140140
data->orig_datum = datum;
141141
data->entry_datum = entries[0];
142142
data->typecmp = cmp_fns[rhs_code];
143-
*extra_data = (Pointer *) palloc(sizeof(Pointer));
143+
*extra_data = palloc_object(Pointer);
144144
**extra_data = (Pointer) data;
145145

146146
PG_RETURN_POINTER(entries);
@@ -579,7 +579,7 @@ GIN_SUPPORT(time, leftmostvalue_time, time_rhs_is_varlena, NULL, time_cmp_fns)
579579
static Datum
580580
leftmostvalue_timetz(void)
581581
{
582-
TimeTzADT *v = palloc(sizeof(TimeTzADT));
582+
TimeTzADT *v = palloc_object(TimeTzADT);
583583

584584
v->time = 0;
585585
v->zone = -24 * 3600; /* XXX is that true? */
@@ -639,7 +639,7 @@ GIN_SUPPORT(date, leftmostvalue_date, date_rhs_is_varlena, date_cvt_fns, date_cm
639639
static Datum
640640
leftmostvalue_interval(void)
641641
{
642-
Interval *v = palloc(sizeof(Interval));
642+
Interval *v = palloc_object(Interval);
643643

644644
INTERVAL_NOBEGIN(v);
645645

@@ -657,7 +657,7 @@ GIN_SUPPORT(interval, leftmostvalue_interval, interval_rhs_is_varlena, NULL, int
657657
static Datum
658658
leftmostvalue_macaddr(void)
659659
{
660-
macaddr *v = palloc0(sizeof(macaddr));
660+
macaddr *v = palloc0_object(macaddr);
661661

662662
return MacaddrPGetDatum(v);
663663
}
@@ -673,7 +673,7 @@ GIN_SUPPORT(macaddr, leftmostvalue_macaddr, macaddr_rhs_is_varlena, NULL, macadd
673673
static Datum
674674
leftmostvalue_macaddr8(void)
675675
{
676-
macaddr8 *v = palloc0(sizeof(macaddr8));
676+
macaddr8 *v = palloc0_object(macaddr8);
677677

678678
return Macaddr8PGetDatum(v);
679679
}
@@ -910,7 +910,7 @@ leftmostvalue_uuid(void)
910910
* palloc0 will create the UUID with all zeroes:
911911
* "00000000-0000-0000-0000-000000000000"
912912
*/
913-
pg_uuid_t *retval = (pg_uuid_t *) palloc0(sizeof(pg_uuid_t));
913+
pg_uuid_t *retval = palloc0_object(pg_uuid_t);
914914

915915
return UUIDPGetDatum(retval);
916916
}

contrib/btree_gist/btree_inet.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ gbt_inet_compress(PG_FUNCTION_ARGS)
9797

9898
if (entry->leafkey)
9999
{
100-
inetKEY *r = (inetKEY *) palloc(sizeof(inetKEY));
100+
inetKEY *r = palloc_object(inetKEY);
101101
bool failure = false;
102102

103-
retval = palloc(sizeof(GISTENTRY));
103+
retval = palloc_object(GISTENTRY);
104104
r->lower = convert_network_to_scalar(entry->key, INETOID, &failure);
105105
Assert(!failure);
106106
r->upper = r->lower;

0 commit comments

Comments
 (0)