mirror of
https://github.com/postgres/postgres.git
synced 2025-11-09 06:21:09 +03:00
Assorted examples of expanded type-safer palloc/pg_malloc API
This adds some uses of the new palloc/pg_malloc variants here and there as a demonstration and test. This is kept separate from the actual 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
This commit is contained in:
@@ -329,7 +329,7 @@ brinbeginscan(Relation r, int nkeys, int norderbys)
|
||||
|
||||
scan = RelationGetIndexScan(r, nkeys, norderbys);
|
||||
|
||||
opaque = (BrinOpaque *) palloc(sizeof(BrinOpaque));
|
||||
opaque = palloc_object(BrinOpaque);
|
||||
opaque->bo_rmAccess = brinRevmapInitialize(r, &opaque->bo_pagesPerRange,
|
||||
scan->xs_snapshot);
|
||||
opaque->bo_bdesc = brin_build_desc(r);
|
||||
@@ -394,7 +394,7 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
|
||||
* don't look them up here; we do that lazily the first time we see a scan
|
||||
* key reference each of them. We rely on zeroing fn_oid to InvalidOid.
|
||||
*/
|
||||
consistentFn = palloc0(sizeof(FmgrInfo) * bdesc->bd_tupdesc->natts);
|
||||
consistentFn = palloc0_array(FmgrInfo, bdesc->bd_tupdesc->natts);
|
||||
|
||||
/*
|
||||
* 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)
|
||||
/*
|
||||
* Return statistics
|
||||
*/
|
||||
result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
|
||||
result = palloc_object(IndexBuildResult);
|
||||
|
||||
result->heap_tuples = reltuples;
|
||||
result->index_tuples = idxtuples;
|
||||
@@ -925,7 +925,7 @@ brinbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
|
||||
{
|
||||
/* allocate stats if first time through, else re-use existing struct */
|
||||
if (stats == NULL)
|
||||
stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult));
|
||||
stats = palloc0_object(IndexBulkDeleteResult);
|
||||
|
||||
return stats;
|
||||
}
|
||||
@@ -944,7 +944,7 @@ brinvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
|
||||
return stats;
|
||||
|
||||
if (!stats)
|
||||
stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult));
|
||||
stats = palloc0_object(IndexBulkDeleteResult);
|
||||
stats->num_pages = RelationGetNumberOfBlocks(info->index);
|
||||
/* rest of stats is initialized by zeroing */
|
||||
|
||||
@@ -1204,7 +1204,7 @@ brin_build_desc(Relation rel)
|
||||
* Obtain BrinOpcInfo for each indexed column. While at it, accumulate
|
||||
* the number of columns stored, since the number is opclass-defined.
|
||||
*/
|
||||
opcinfo = (BrinOpcInfo **) palloc(sizeof(BrinOpcInfo *) * tupdesc->natts);
|
||||
opcinfo = palloc_array(BrinOpcInfo*, tupdesc->natts);
|
||||
for (keyno = 0; keyno < tupdesc->natts; keyno++)
|
||||
{
|
||||
FmgrInfo *opcInfoFn;
|
||||
@@ -1276,7 +1276,7 @@ initialize_brin_buildstate(Relation idxRel, BrinRevmap *revmap,
|
||||
{
|
||||
BrinBuildState *state;
|
||||
|
||||
state = palloc(sizeof(BrinBuildState));
|
||||
state = palloc_object(BrinBuildState);
|
||||
|
||||
state->bs_irel = idxRel;
|
||||
state->bs_numtuples = 0;
|
||||
|
||||
@@ -505,7 +505,7 @@ ginHeapTupleFastCollect(GinState *ginstate,
|
||||
* resizing (since palloc likes powers of 2).
|
||||
*/
|
||||
collector->lentuples = pg_nextpower2_32(Max(16, nentries));
|
||||
collector->tuples = (IndexTuple *) palloc(sizeof(IndexTuple) * collector->lentuples);
|
||||
collector->tuples = palloc_array(IndexTuple, collector->lentuples);
|
||||
}
|
||||
else if (collector->lentuples < collector->ntuples + nentries)
|
||||
{
|
||||
@@ -515,8 +515,8 @@ ginHeapTupleFastCollect(GinState *ginstate,
|
||||
* MaxAllocSize/sizeof(IndexTuple), causing an error in repalloc.
|
||||
*/
|
||||
collector->lentuples = pg_nextpower2_32(collector->ntuples + nentries);
|
||||
collector->tuples = (IndexTuple *) repalloc(collector->tuples,
|
||||
sizeof(IndexTuple) * collector->lentuples);
|
||||
collector->tuples = repalloc_array(collector->tuples,
|
||||
IndexTuple, collector->lentuples);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -665,9 +665,8 @@ shiftList(Relation index, Buffer metabuffer, BlockNumber newHead,
|
||||
static void
|
||||
initKeyArray(KeyArray *keys, int32 maxvalues)
|
||||
{
|
||||
keys->keys = (Datum *) palloc(sizeof(Datum) * maxvalues);
|
||||
keys->categories = (GinNullCategory *)
|
||||
palloc(sizeof(GinNullCategory) * maxvalues);
|
||||
keys->keys = palloc_array(Datum, maxvalues);
|
||||
keys->categories = palloc_array(GinNullCategory, maxvalues);
|
||||
keys->nvalues = 0;
|
||||
keys->maxvalues = maxvalues;
|
||||
}
|
||||
@@ -679,10 +678,8 @@ addDatum(KeyArray *keys, Datum datum, GinNullCategory category)
|
||||
if (keys->nvalues >= keys->maxvalues)
|
||||
{
|
||||
keys->maxvalues *= 2;
|
||||
keys->keys = (Datum *)
|
||||
repalloc(keys->keys, sizeof(Datum) * keys->maxvalues);
|
||||
keys->categories = (GinNullCategory *)
|
||||
repalloc(keys->categories, sizeof(GinNullCategory) * keys->maxvalues);
|
||||
keys->keys = repalloc_array(keys->keys, Datum, keys->maxvalues);
|
||||
keys->categories = repalloc_array(keys->categories, GinNullCategory, keys->maxvalues);
|
||||
}
|
||||
|
||||
keys->keys[keys->nvalues] = datum;
|
||||
|
||||
Reference in New Issue
Block a user