Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitca62947

Browse files
peterepull[bot]
authored andcommitted
Assorted examples of expanded type-safer palloc/pg_malloc API
This adds some uses of the new palloc/pg_malloc variants here andthere as a demonstration and test. This is kept separate from theactual API patch, since the latter might be backpatched at some point.Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>Discussion:https://www.postgresql.org/message-id/flat/bb755632-2a43-d523-36f8-a1e7a389a907@enterprisedb.com
1 parent5338b6e commitca62947

File tree

12 files changed

+90
-111
lines changed

12 files changed

+90
-111
lines changed

‎contrib/dblink/dblink.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ materializeResult(FunctionCallInfo fcinfo, PGconn *conn, PGresult *res)
972972
rsinfo->setDesc=tupdesc;
973973
MemoryContextSwitchTo(oldcontext);
974974

975-
values= (char**)palloc(nfields*sizeof(char*));
975+
values=palloc_array(char*,nfields);
976976

977977
/* put all tuples into the tuplestore */
978978
for (row=0;row<ntuples;row++)
@@ -1276,7 +1276,7 @@ storeRow(volatile storeInfo *sinfo, PGresult *res, bool first)
12761276
*/
12771277
if (sinfo->cstrs)
12781278
pfree(sinfo->cstrs);
1279-
sinfo->cstrs= (char**)palloc(nfields*sizeof(char*));
1279+
sinfo->cstrs=palloc_array(char*,nfields);
12801280
}
12811281

12821282
/* Should have a single-row result if we get here */
@@ -1618,7 +1618,7 @@ dblink_get_pkey(PG_FUNCTION_ARGS)
16181618
HeapTupletuple;
16191619
Datumresult;
16201620

1621-
values= (char**)palloc(2*sizeof(char*));
1621+
values=palloc_array(char*,2);
16221622
values[0]=psprintf("%d",call_cntr+1);
16231623
values[1]=results[call_cntr];
16241624

@@ -2083,7 +2083,7 @@ get_pkey_attnames(Relation rel, int16 *indnkeyatts)
20832083
*indnkeyatts=index->indnkeyatts;
20842084
if (*indnkeyatts>0)
20852085
{
2086-
result= (char**)palloc(*indnkeyatts*sizeof(char*));
2086+
result=palloc_array(char*,*indnkeyatts);
20872087

20882088
for (i=0;i<*indnkeyatts;i++)
20892089
result[i]=SPI_fname(tupdesc,index->indkey.values[i]);
@@ -2124,7 +2124,7 @@ get_text_array_contents(ArrayType *array, int *numitems)
21242124
get_typlenbyvalalign(ARR_ELEMTYPE(array),
21252125
&typlen,&typbyval,&typalign);
21262126

2127-
values= (char**)palloc(nitems*sizeof(char*));
2127+
values=palloc_array(char*,nitems);
21282128

21292129
ptr=ARR_DATA_PTR(array);
21302130
bitmap=ARR_NULLBITMAP(array);
@@ -2928,7 +2928,7 @@ validate_pkattnums(Relation rel,
29282928
errmsg("number of key attributes must be > 0")));
29292929

29302930
/* Allocate output array */
2931-
*pkattnums= (int*)palloc(pknumatts_arg*sizeof(int));
2931+
*pkattnums=palloc_array(int,pknumatts_arg);
29322932
*pknumatts=pknumatts_arg;
29332933

29342934
/* Validate attnums and convert to internal form */

‎src/backend/access/brin/brin.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ brinbeginscan(Relation r, int nkeys, int norderbys)
329329

330330
scan=RelationGetIndexScan(r,nkeys,norderbys);
331331

332-
opaque= (BrinOpaque*)palloc(sizeof(BrinOpaque));
332+
opaque=palloc_object(BrinOpaque);
333333
opaque->bo_rmAccess=brinRevmapInitialize(r,&opaque->bo_pagesPerRange,
334334
scan->xs_snapshot);
335335
opaque->bo_bdesc=brin_build_desc(r);
@@ -394,7 +394,7 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
394394
* don't look them up here; we do that lazily the first time we see a scan
395395
* key reference each of them. We rely on zeroing fn_oid to InvalidOid.
396396
*/
397-
consistentFn=palloc0(sizeof(FmgrInfo)*bdesc->bd_tupdesc->natts);
397+
consistentFn=palloc0_array(FmgrInfo,bdesc->bd_tupdesc->natts);
398398

399399
/*
400400
* Make room for per-attribute lists of scan keys that we'll pass to the
@@ -881,7 +881,7 @@ brinbuild(Relation heap, Relation index, IndexInfo *indexInfo)
881881
/*
882882
* Return statistics
883883
*/
884-
result= (IndexBuildResult*)palloc(sizeof(IndexBuildResult));
884+
result=palloc_object(IndexBuildResult);
885885

886886
result->heap_tuples=reltuples;
887887
result->index_tuples=idxtuples;
@@ -925,7 +925,7 @@ brinbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
925925
{
926926
/* allocate stats if first time through, else re-use existing struct */
927927
if (stats==NULL)
928-
stats= (IndexBulkDeleteResult*)palloc0(sizeof(IndexBulkDeleteResult));
928+
stats=palloc0_object(IndexBulkDeleteResult);
929929

930930
returnstats;
931931
}
@@ -944,7 +944,7 @@ brinvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
944944
returnstats;
945945

946946
if (!stats)
947-
stats= (IndexBulkDeleteResult*)palloc0(sizeof(IndexBulkDeleteResult));
947+
stats=palloc0_object(IndexBulkDeleteResult);
948948
stats->num_pages=RelationGetNumberOfBlocks(info->index);
949949
/* rest of stats is initialized by zeroing */
950950

@@ -1204,7 +1204,7 @@ brin_build_desc(Relation rel)
12041204
* Obtain BrinOpcInfo for each indexed column. While at it, accumulate
12051205
* the number of columns stored, since the number is opclass-defined.
12061206
*/
1207-
opcinfo= (BrinOpcInfo**)palloc(sizeof(BrinOpcInfo*)*tupdesc->natts);
1207+
opcinfo=palloc_array(BrinOpcInfo*,tupdesc->natts);
12081208
for (keyno=0;keyno<tupdesc->natts;keyno++)
12091209
{
12101210
FmgrInfo*opcInfoFn;
@@ -1276,7 +1276,7 @@ initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap,
12761276
{
12771277
BrinBuildState*state;
12781278

1279-
state=palloc(sizeof(BrinBuildState));
1279+
state=palloc_object(BrinBuildState);
12801280

12811281
state->bs_irel=idxRel;
12821282
state->bs_numtuples=0;

‎src/backend/access/gin/ginfast.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ ginHeapTupleFastCollect(GinState *ginstate,
505505
* resizing (since palloc likes powers of 2).
506506
*/
507507
collector->lentuples=pg_nextpower2_32(Max(16,nentries));
508-
collector->tuples= (IndexTuple*)palloc(sizeof(IndexTuple)*collector->lentuples);
508+
collector->tuples=palloc_array(IndexTuple,collector->lentuples);
509509
}
510510
elseif (collector->lentuples<collector->ntuples+nentries)
511511
{
@@ -515,8 +515,8 @@ ginHeapTupleFastCollect(GinState *ginstate,
515515
* MaxAllocSize/sizeof(IndexTuple), causing an error in repalloc.
516516
*/
517517
collector->lentuples=pg_nextpower2_32(collector->ntuples+nentries);
518-
collector->tuples=(IndexTuple*)repalloc(collector->tuples,
519-
sizeof(IndexTuple)*collector->lentuples);
518+
collector->tuples=repalloc_array(collector->tuples,
519+
IndexTuple,collector->lentuples);
520520
}
521521

522522
/*
@@ -665,9 +665,8 @@ shiftList(Relation index, Buffer metabuffer, BlockNumber newHead,
665665
staticvoid
666666
initKeyArray(KeyArray*keys,int32maxvalues)
667667
{
668-
keys->keys= (Datum*)palloc(sizeof(Datum)*maxvalues);
669-
keys->categories= (GinNullCategory*)
670-
palloc(sizeof(GinNullCategory)*maxvalues);
668+
keys->keys=palloc_array(Datum,maxvalues);
669+
keys->categories=palloc_array(GinNullCategory,maxvalues);
671670
keys->nvalues=0;
672671
keys->maxvalues=maxvalues;
673672
}
@@ -679,10 +678,8 @@ addDatum(KeyArray *keys, Datum datum, GinNullCategory category)
679678
if (keys->nvalues >=keys->maxvalues)
680679
{
681680
keys->maxvalues *=2;
682-
keys->keys= (Datum*)
683-
repalloc(keys->keys,sizeof(Datum)*keys->maxvalues);
684-
keys->categories= (GinNullCategory*)
685-
repalloc(keys->categories,sizeof(GinNullCategory)*keys->maxvalues);
681+
keys->keys=repalloc_array(keys->keys,Datum,keys->maxvalues);
682+
keys->categories=repalloc_array(keys->categories,GinNullCategory,keys->maxvalues);
686683
}
687684

688685
keys->keys[keys->nvalues]=datum;

‎src/backend/commands/indexcmds.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,10 @@ CheckIndexCompatible(Oid oldId,
229229
*/
230230
indexInfo=makeIndexInfo(numberOfAttributes,numberOfAttributes,
231231
accessMethodId,NIL,NIL, false, false, false, false);
232-
typeObjectId= (Oid*)palloc(numberOfAttributes*sizeof(Oid));
233-
collationObjectId= (Oid*)palloc(numberOfAttributes*sizeof(Oid));
234-
classObjectId= (Oid*)palloc(numberOfAttributes*sizeof(Oid));
235-
coloptions= (int16*)palloc(numberOfAttributes*sizeof(int16));
232+
typeObjectId=palloc_array(Oid,numberOfAttributes);
233+
collationObjectId=palloc_array(Oid,numberOfAttributes);
234+
classObjectId=palloc_array(Oid,numberOfAttributes);
235+
coloptions=palloc_array(int16,numberOfAttributes);
236236
ComputeIndexAttrs(indexInfo,
237237
typeObjectId,collationObjectId,classObjectId,
238238
coloptions,attributeList,
@@ -895,10 +895,10 @@ DefineIndex(Oid relationId,
895895
!concurrent,
896896
concurrent);
897897

898-
typeObjectId= (Oid*)palloc(numberOfAttributes*sizeof(Oid));
899-
collationObjectId= (Oid*)palloc(numberOfAttributes*sizeof(Oid));
900-
classObjectId= (Oid*)palloc(numberOfAttributes*sizeof(Oid));
901-
coloptions= (int16*)palloc(numberOfAttributes*sizeof(int16));
898+
typeObjectId=palloc_array(Oid,numberOfAttributes);
899+
collationObjectId=palloc_array(Oid,numberOfAttributes);
900+
classObjectId=palloc_array(Oid,numberOfAttributes);
901+
coloptions=palloc_array(int16,numberOfAttributes);
902902
ComputeIndexAttrs(indexInfo,
903903
typeObjectId,collationObjectId,classObjectId,
904904
coloptions,allIndexParams,
@@ -1210,7 +1210,7 @@ DefineIndex(Oid relationId,
12101210
if ((!stmt->relation||stmt->relation->inh)&&partdesc->nparts>0)
12111211
{
12121212
intnparts=partdesc->nparts;
1213-
Oid*part_oids=palloc(sizeof(Oid)*nparts);
1213+
Oid*part_oids=palloc_array(Oid,nparts);
12141214
boolinvalidate_parent= false;
12151215
RelationparentIndex;
12161216
TupleDescparentDesc;
@@ -1786,9 +1786,9 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
17861786
if (exclusionOpNames)
17871787
{
17881788
Assert(list_length(exclusionOpNames)==nkeycols);
1789-
indexInfo->ii_ExclusionOps= (Oid*)palloc(sizeof(Oid)*nkeycols);
1790-
indexInfo->ii_ExclusionProcs= (Oid*)palloc(sizeof(Oid)*nkeycols);
1791-
indexInfo->ii_ExclusionStrats= (uint16*)palloc(sizeof(uint16)*nkeycols);
1789+
indexInfo->ii_ExclusionOps=palloc_array(Oid,nkeycols);
1790+
indexInfo->ii_ExclusionProcs=palloc_array(Oid,nkeycols);
1791+
indexInfo->ii_ExclusionStrats=palloc_array(uint16,nkeycols);
17921792
nextExclOp=list_head(exclusionOpNames);
17931793
}
17941794
else
@@ -2112,7 +2112,7 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
21122112

21132113
if (!indexInfo->ii_OpclassOptions)
21142114
indexInfo->ii_OpclassOptions=
2115-
palloc0(sizeof(Datum)*indexInfo->ii_NumIndexAttrs);
2115+
palloc0_array(Datum,indexInfo->ii_NumIndexAttrs);
21162116

21172117
indexInfo->ii_OpclassOptions[attn]=
21182118
transformRelOptions((Datum)0,attribute->opclassopts,
@@ -3459,7 +3459,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
34593459
/* Save the list of relation OIDs in private context */
34603460
oldcontext=MemoryContextSwitchTo(private_context);
34613461

3462-
idx=palloc(sizeof(ReindexIndexInfo));
3462+
idx=palloc_object(ReindexIndexInfo);
34633463
idx->indexId=cellOid;
34643464
/* other fields set later */
34653465

@@ -3508,7 +3508,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
35083508
*/
35093509
oldcontext=MemoryContextSwitchTo(private_context);
35103510

3511-
idx=palloc(sizeof(ReindexIndexInfo));
3511+
idx=palloc_object(ReindexIndexInfo);
35123512
idx->indexId=cellOid;
35133513
indexIds=lappend(indexIds,idx);
35143514
/* other fields set later */
@@ -3589,7 +3589,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
35893589
* Save the list of relation OIDs in private context. Note
35903590
* that invalid indexes are allowed here.
35913591
*/
3592-
idx=palloc(sizeof(ReindexIndexInfo));
3592+
idx=palloc_object(ReindexIndexInfo);
35933593
idx->indexId=relationOid;
35943594
indexIds=lappend(indexIds,idx);
35953595
/* other fields set later */
@@ -3734,7 +3734,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
37343734
*/
37353735
oldcontext=MemoryContextSwitchTo(private_context);
37363736

3737-
newidx=palloc(sizeof(ReindexIndexInfo));
3737+
newidx=palloc_object(ReindexIndexInfo);
37383738
newidx->indexId=newIndexId;
37393739
newidx->safe=idx->safe;
37403740
newidx->tableId=idx->tableId;
@@ -3748,10 +3748,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
37483748
* avoid multiple locks taken on the same relation, instead we rely on
37493749
* parentRelationIds built earlier.
37503750
*/
3751-
lockrelid=palloc(sizeof(*lockrelid));
3751+
lockrelid=palloc_object(LockRelId);
37523752
*lockrelid=indexRel->rd_lockInfo.lockRelId;
37533753
relationLocks=lappend(relationLocks,lockrelid);
3754-
lockrelid=palloc(sizeof(*lockrelid));
3754+
lockrelid=palloc_object(LockRelId);
37553755
*lockrelid=newIndexRel->rd_lockInfo.lockRelId;
37563756
relationLocks=lappend(relationLocks,lockrelid);
37573757

@@ -3783,11 +3783,11 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
37833783
oldcontext=MemoryContextSwitchTo(private_context);
37843784

37853785
/* Add lockrelid of heap relation to the list of locked relations */
3786-
lockrelid=palloc(sizeof(*lockrelid));
3786+
lockrelid=palloc_object(LockRelId);
37873787
*lockrelid=heapRelation->rd_lockInfo.lockRelId;
37883788
relationLocks=lappend(relationLocks,lockrelid);
37893789

3790-
heaplocktag= (LOCKTAG*)palloc(sizeof(LOCKTAG));
3790+
heaplocktag=palloc_object(LOCKTAG);
37913791

37923792
/* Save the LOCKTAG for this parent relation for the wait phase */
37933793
SET_LOCKTAG_RELATION(*heaplocktag,lockrelid->dbId,lockrelid->relId);

‎src/backend/commands/prepare.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ PrepareQuery(ParseState *pstate, PrepareStmt *stmt,
9898
inti;
9999
ListCell*l;
100100

101-
argtypes= (Oid*)palloc(nargs*sizeof(Oid));
101+
argtypes=palloc_array(Oid,nargs);
102102
i=0;
103103

104104
foreach(l,stmt->argtypes)
@@ -698,7 +698,7 @@ pg_prepared_statement(PG_FUNCTION_ARGS)
698698
{
699699
Oid*result_types;
700700

701-
result_types= (Oid*)palloc(result_desc->natts*sizeof(Oid));
701+
result_types=palloc_array(Oid,result_desc->natts);
702702
for (inti=0;i<result_desc->natts;i++)
703703
result_types[i]=result_desc->attrs[i].atttypid;
704704
values[4]=build_regtype_array(result_types,result_desc->natts);
@@ -732,7 +732,7 @@ build_regtype_array(Oid *param_types, int num_params)
732732
ArrayType*result;
733733
inti;
734734

735-
tmp_ary= (Datum*)palloc(num_params*sizeof(Datum));
735+
tmp_ary=palloc_array(Datum,num_params);
736736

737737
for (i=0;i<num_params;i++)
738738
tmp_ary[i]=ObjectIdGetDatum(param_types[i]);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp