summaryrefslogtreecommitdiff
path: root/src/backend/access/gist
diff options
context:
space:
mode:
authorMichael Paquier2025-12-09 22:36:46 +0000
committerMichael Paquier2025-12-09 22:36:46 +0000
commit1b105f9472bdb9a68f709778afafb494997267bd (patch)
tree205727d30b0dd4f3060bc1f49a9efa724ee3ff80 /src/backend/access/gist
parentc507ba55f5bfae900baa94f1c657e1d99da5c6dc (diff)
Use palloc_object() and palloc_array() in backend code
The idea is to encourage more the use of these new routines across the tree, as these offer stronger type safety guarantees than palloc(). This batch of changes includes most of the trivial changes suggested by the author for src/backend/. A total of 334 files are updated here. Among these files, 48 of them have their build change slightly; these are caused by line number changes as the new allocation formulas are simpler, shaving around 100 lines of code in total. Similar work has been done in 0c3c5c3b06a3 and 31d3847a37be. Author: David Geier <geidav.pg@gmail.com> Discussion: https://postgr.es/m/ad0748d4-3080-436e-b0bc-ac8f86a3466a@gmail.com
Diffstat (limited to 'src/backend/access/gist')
-rw-r--r--src/backend/access/gist/gist.c22
-rw-r--r--src/backend/access/gist/gistbuild.c10
-rw-r--r--src/backend/access/gist/gistbuildbuffers.c14
-rw-r--r--src/backend/access/gist/gistproc.c26
-rw-r--r--src/backend/access/gist/gistscan.c6
-rw-r--r--src/backend/access/gist/gistsplit.c8
-rw-r--r--src/backend/access/gist/gistutil.c4
-rw-r--r--src/backend/access/gist/gistvacuum.c4
8 files changed, 45 insertions, 49 deletions
diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c
index 3fb1a1285c5..c26d8538f05 100644
--- a/src/backend/access/gist/gist.c
+++ b/src/backend/access/gist/gist.c
@@ -43,7 +43,7 @@ static void gistprunepage(Relation rel, Page page, Buffer buffer,
#define ROTATEDIST(d) do { \
- SplitPageLayout *tmp = (SplitPageLayout *) palloc0(sizeof(SplitPageLayout)); \
+ SplitPageLayout *tmp = palloc0_object(SplitPageLayout); \
tmp->block.blkno = InvalidBlockNumber; \
tmp->buffer = InvalidBuffer; \
tmp->next = (d); \
@@ -392,7 +392,7 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
/* Prepare a vector of all the downlinks */
for (ptr = dist; ptr; ptr = ptr->next)
ndownlinks++;
- downlinks = palloc(sizeof(IndexTuple) * ndownlinks);
+ downlinks = palloc_array(IndexTuple, ndownlinks);
for (i = 0, ptr = dist; ptr; ptr = ptr->next)
downlinks[i++] = ptr->itup;
@@ -410,7 +410,7 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
/* Prepare split-info to be returned to caller */
for (ptr = dist; ptr; ptr = ptr->next)
{
- GISTPageSplitInfo *si = palloc(sizeof(GISTPageSplitInfo));
+ GISTPageSplitInfo *si = palloc_object(GISTPageSplitInfo);
si->buf = ptr->buffer;
si->downlink = ptr->itup;
@@ -823,7 +823,7 @@ gistdoinsert(Relation r, IndexTuple itup, Size freespace,
xlocked = false;
/* descend to the chosen child */
- item = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
+ item = palloc0_object(GISTInsertStack);
item->blkno = childblkno;
item->parent = stack;
item->downlinkoffnum = downlinkoffnum;
@@ -923,7 +923,7 @@ gistFindPath(Relation r, BlockNumber child, OffsetNumber *downlinkoffnum)
*ptr;
BlockNumber blkno;
- top = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
+ top = palloc0_object(GISTInsertStack);
top->blkno = GIST_ROOT_BLKNO;
top->downlinkoffnum = InvalidOffsetNumber;
@@ -975,7 +975,7 @@ gistFindPath(Relation r, BlockNumber child, OffsetNumber *downlinkoffnum)
* leaf pages, and we assume that there can't be any non-leaf
* pages behind leaf pages.
*/
- ptr = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
+ ptr = palloc0_object(GISTInsertStack);
ptr->blkno = GistPageGetOpaque(page)->rightlink;
ptr->downlinkoffnum = InvalidOffsetNumber;
ptr->parent = top->parent;
@@ -1000,7 +1000,7 @@ gistFindPath(Relation r, BlockNumber child, OffsetNumber *downlinkoffnum)
else
{
/* Append this child to the list of pages to visit later */
- ptr = (GISTInsertStack *) palloc0(sizeof(GISTInsertStack));
+ ptr = palloc0_object(GISTInsertStack);
ptr->blkno = blkno;
ptr->downlinkoffnum = i;
ptr->parent = top;
@@ -1218,7 +1218,7 @@ gistfixsplit(GISTInsertState *state, GISTSTATE *giststate)
*/
for (;;)
{
- GISTPageSplitInfo *si = palloc(sizeof(GISTPageSplitInfo));
+ GISTPageSplitInfo *si = palloc_object(GISTPageSplitInfo);
IndexTuple downlink;
page = BufferGetPage(buf);
@@ -1482,8 +1482,8 @@ gistSplit(Relation r,
gistSplitByKey(r, page, itup, len, giststate, &v, 0);
/* form left and right vector */
- lvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * (len + 1));
- rvectup = (IndexTuple *) palloc(sizeof(IndexTuple) * (len + 1));
+ lvectup = palloc_array(IndexTuple, len + 1);
+ rvectup = palloc_array(IndexTuple, len + 1);
for (i = 0; i < v.splitVector.spl_nleft; i++)
lvectup[i] = itup[v.splitVector.spl_left[i] - 1];
@@ -1552,7 +1552,7 @@ initGISTstate(Relation index)
oldCxt = MemoryContextSwitchTo(scanCxt);
/* Create and fill in the GISTSTATE */
- giststate = (GISTSTATE *) palloc(sizeof(GISTSTATE));
+ giststate = palloc_object(GISTSTATE);
giststate->scanCxt = scanCxt;
giststate->tempCxt = scanCxt; /* caller must change this if needed */
diff --git a/src/backend/access/gist/gistbuild.c b/src/backend/access/gist/gistbuild.c
index be0fd5b753d..b9fa196149d 100644
--- a/src/backend/access/gist/gistbuild.c
+++ b/src/backend/access/gist/gistbuild.c
@@ -346,7 +346,7 @@ gistbuild(Relation heap, Relation index, IndexInfo *indexInfo)
/*
* Return statistics
*/
- result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
+ result = palloc_object(IndexBuildResult);
result->heap_tuples = reltuples;
result->index_tuples = (double) buildstate.indtuples;
@@ -409,7 +409,7 @@ gist_indexsortbuild(GISTBuildState *state)
state->bulkstate = smgr_bulk_start_rel(state->indexrel, MAIN_FORKNUM);
/* Allocate a temporary buffer for the first leaf page batch. */
- levelstate = palloc0(sizeof(GistSortedBuildLevelState));
+ levelstate = palloc0_object(GistSortedBuildLevelState);
levelstate->pages[0] = palloc(BLCKSZ);
levelstate->parent = NULL;
gistinitpage(levelstate->pages[0], F_LEAF);
@@ -526,7 +526,7 @@ gist_indexsortbuild_levelstate_flush(GISTBuildState *state,
else
{
/* Create split layout from single page */
- dist = (SplitPageLayout *) palloc0(sizeof(SplitPageLayout));
+ dist = palloc0_object(SplitPageLayout);
union_tuple = gistunion(state->indexrel, itvec, vect_len,
state->giststate);
dist->itup = union_tuple;
@@ -597,7 +597,7 @@ gist_indexsortbuild_levelstate_flush(GISTBuildState *state,
parent = levelstate->parent;
if (parent == NULL)
{
- parent = palloc0(sizeof(GistSortedBuildLevelState));
+ parent = palloc0_object(GistSortedBuildLevelState);
parent->pages[0] = palloc(BLCKSZ);
parent->parent = NULL;
gistinitpage(parent->pages[0], 0);
@@ -1154,7 +1154,7 @@ gistbufferinginserttuples(GISTBuildState *buildstate, Buffer buffer, int level,
/* Create an array of all the downlink tuples */
ndownlinks = list_length(splitinfo);
- downlinks = (IndexTuple *) palloc(sizeof(IndexTuple) * ndownlinks);
+ downlinks = palloc_array(IndexTuple, ndownlinks);
i = 0;
foreach(lc, splitinfo)
{
diff --git a/src/backend/access/gist/gistbuildbuffers.c b/src/backend/access/gist/gistbuildbuffers.c
index 0707254d18e..c86d17c4a56 100644
--- a/src/backend/access/gist/gistbuildbuffers.c
+++ b/src/backend/access/gist/gistbuildbuffers.c
@@ -46,7 +46,7 @@ gistInitBuildBuffers(int pagesPerBuffer, int levelStep, int maxLevel)
GISTBuildBuffers *gfbb;
HASHCTL hashCtl;
- gfbb = palloc(sizeof(GISTBuildBuffers));
+ gfbb = palloc_object(GISTBuildBuffers);
gfbb->pagesPerBuffer = pagesPerBuffer;
gfbb->levelStep = levelStep;
@@ -60,7 +60,7 @@ gistInitBuildBuffers(int pagesPerBuffer, int levelStep, int maxLevel)
/* Initialize free page management. */
gfbb->nFreeBlocks = 0;
gfbb->freeBlocksLen = 32;
- gfbb->freeBlocks = (long *) palloc(gfbb->freeBlocksLen * sizeof(long));
+ gfbb->freeBlocks = palloc_array(long, gfbb->freeBlocksLen);
/*
* Current memory context will be used for all in-memory data structures
@@ -87,8 +87,7 @@ gistInitBuildBuffers(int pagesPerBuffer, int levelStep, int maxLevel)
* buffers are inserted here when they are created.
*/
gfbb->buffersOnLevelsLen = 1;
- gfbb->buffersOnLevels = (List **) palloc(sizeof(List *) *
- gfbb->buffersOnLevelsLen);
+ gfbb->buffersOnLevels = palloc_array(List *, gfbb->buffersOnLevelsLen);
gfbb->buffersOnLevels[0] = NIL;
/*
@@ -96,8 +95,7 @@ gistInitBuildBuffers(int pagesPerBuffer, int levelStep, int maxLevel)
* into main memory.
*/
gfbb->loadedBuffersLen = 32;
- gfbb->loadedBuffers = (GISTNodeBuffer **) palloc(gfbb->loadedBuffersLen *
- sizeof(GISTNodeBuffer *));
+ gfbb->loadedBuffers = palloc_array(GISTNodeBuffer *, gfbb->loadedBuffersLen);
gfbb->loadedBuffersCount = 0;
gfbb->rootlevel = maxLevel;
@@ -582,9 +580,7 @@ gistRelocateBuildBuffersOnSplit(GISTBuildBuffers *gfbb, GISTSTATE *giststate,
* Allocate memory for information about relocation buffers.
*/
splitPagesCount = list_length(splitinfo);
- relocationBuffersInfos =
- (RelocationBufferInfo *) palloc(sizeof(RelocationBufferInfo) *
- splitPagesCount);
+ relocationBuffersInfos = palloc_array(RelocationBufferInfo, splitPagesCount);
/*
* Fill relocation buffers information for node buffers of pages produced
diff --git a/src/backend/access/gist/gistproc.c b/src/backend/access/gist/gistproc.c
index f2ec6cbe2e5..9ac06504be1 100644
--- a/src/backend/access/gist/gistproc.c
+++ b/src/backend/access/gist/gistproc.c
@@ -171,7 +171,7 @@ gist_box_union(PG_FUNCTION_ARGS)
*pageunion;
numranges = entryvec->n;
- pageunion = (BOX *) palloc(sizeof(BOX));
+ pageunion = palloc_object(BOX);
cur = DatumGetBoxP(entryvec->vector[0].key);
memcpy(pageunion, cur, sizeof(BOX));
@@ -237,7 +237,7 @@ fallbackSplit(GistEntryVector *entryvec, GIST_SPLITVEC *v)
v->spl_left[v->spl_nleft] = i;
if (unionL == NULL)
{
- unionL = (BOX *) palloc(sizeof(BOX));
+ unionL = palloc_object(BOX);
*unionL = *cur;
}
else
@@ -250,7 +250,7 @@ fallbackSplit(GistEntryVector *entryvec, GIST_SPLITVEC *v)
v->spl_right[v->spl_nright] = i;
if (unionR == NULL)
{
- unionR = (BOX *) palloc(sizeof(BOX));
+ unionR = palloc_object(BOX);
*unionR = *cur;
}
else
@@ -698,8 +698,8 @@ gist_box_picksplit(PG_FUNCTION_ARGS)
v->spl_nright = 0;
/* Allocate bounding boxes of left and right groups */
- leftBox = palloc0(sizeof(BOX));
- rightBox = palloc0(sizeof(BOX));
+ leftBox = palloc0_object(BOX);
+ rightBox = palloc0_object(BOX);
/*
* Allocate an array for "common entries" - entries which can be placed to
@@ -1042,10 +1042,10 @@ gist_poly_compress(PG_FUNCTION_ARGS)
POLYGON *in = DatumGetPolygonP(entry->key);
BOX *r;
- r = (BOX *) palloc(sizeof(BOX));
+ r = palloc_object(BOX);
memcpy(r, &(in->boundbox), sizeof(BOX));
- retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
+ retval = palloc_object(GISTENTRY);
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, false);
@@ -1107,13 +1107,13 @@ gist_circle_compress(PG_FUNCTION_ARGS)
CIRCLE *in = DatumGetCircleP(entry->key);
BOX *r;
- r = (BOX *) palloc(sizeof(BOX));
+ r = palloc_object(BOX);
r->high.x = float8_pl(in->center.x, in->radius);
r->low.x = float8_mi(in->center.x, in->radius);
r->high.y = float8_pl(in->center.y, in->radius);
r->low.y = float8_mi(in->center.y, in->radius);
- retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
+ retval = palloc_object(GISTENTRY);
gistentryinit(*retval, PointerGetDatum(r),
entry->rel, entry->page,
entry->offset, false);
@@ -1171,9 +1171,9 @@ gist_point_compress(PG_FUNCTION_ARGS)
if (entry->leafkey) /* Point, actually */
{
- BOX *box = palloc(sizeof(BOX));
+ BOX *box = palloc_object(BOX);
Point *point = DatumGetPointP(entry->key);
- GISTENTRY *retval = palloc(sizeof(GISTENTRY));
+ GISTENTRY *retval = palloc_object(GISTENTRY);
box->high = box->low = *point;
@@ -1200,9 +1200,9 @@ gist_point_fetch(PG_FUNCTION_ARGS)
Point *r;
GISTENTRY *retval;
- retval = palloc(sizeof(GISTENTRY));
+ retval = palloc_object(GISTENTRY);
- r = (Point *) palloc(sizeof(Point));
+ r = palloc_object(Point);
r->x = in->high.x;
r->y = in->high.y;
gistentryinit(*retval, PointerGetDatum(r),
diff --git a/src/backend/access/gist/gistscan.c b/src/backend/access/gist/gistscan.c
index 700fa959d03..01b8ff0b6fa 100644
--- a/src/backend/access/gist/gistscan.c
+++ b/src/backend/access/gist/gistscan.c
@@ -90,7 +90,7 @@ gistbeginscan(Relation r, int nkeys, int norderbys)
oldCxt = MemoryContextSwitchTo(giststate->scanCxt);
/* initialize opaque data */
- so = (GISTScanOpaque) palloc0(sizeof(GISTScanOpaqueData));
+ so = palloc0_object(GISTScanOpaqueData);
so->giststate = giststate;
giststate->tempCxt = createTempGistContext();
so->queue = NULL;
@@ -101,8 +101,8 @@ gistbeginscan(Relation r, int nkeys, int norderbys)
so->qual_ok = true; /* in case there are zero keys */
if (scan->numberOfOrderBys > 0)
{
- scan->xs_orderbyvals = palloc0(sizeof(Datum) * scan->numberOfOrderBys);
- scan->xs_orderbynulls = palloc(sizeof(bool) * scan->numberOfOrderBys);
+ scan->xs_orderbyvals = palloc0_array(Datum, scan->numberOfOrderBys);
+ scan->xs_orderbynulls = palloc_array(bool, scan->numberOfOrderBys);
memset(scan->xs_orderbynulls, true, sizeof(bool) * scan->numberOfOrderBys);
}
diff --git a/src/backend/access/gist/gistsplit.c b/src/backend/access/gist/gistsplit.c
index 49838ceb07b..21fea505d9c 100644
--- a/src/backend/access/gist/gistsplit.c
+++ b/src/backend/access/gist/gistsplit.c
@@ -51,7 +51,7 @@ gistunionsubkeyvec(GISTSTATE *giststate, IndexTuple *itvec,
int i,
cleanedLen = 0;
- cleanedItVec = (IndexTuple *) palloc(sizeof(IndexTuple) * gsvp->len);
+ cleanedItVec = palloc_array(IndexTuple, gsvp->len);
for (i = 0; i < gsvp->len; i++)
{
@@ -501,7 +501,7 @@ gistUserPicksplit(Relation r, GistEntryVector *entryvec, int attno, GistSplitVec
* Locate don't-care tuples, if any. If there are none, the split is
* optimal, so just fall out and return false.
*/
- v->spl_dontcare = (bool *) palloc0(sizeof(bool) * (entryvec->n + 1));
+ v->spl_dontcare = palloc0_array(bool, entryvec->n + 1);
NumDontCare = findDontCares(r, giststate, entryvec->vector, v, attno);
@@ -738,9 +738,9 @@ gistSplitByKey(Relation r, Page page, IndexTuple *itup, int len,
* call will overwrite that with its own result.
*/
backupSplit = v->splitVector;
- backupSplit.spl_left = (OffsetNumber *) palloc(sizeof(OffsetNumber) * len);
+ backupSplit.spl_left = palloc_array(OffsetNumber, len);
memcpy(backupSplit.spl_left, v->splitVector.spl_left, sizeof(OffsetNumber) * v->splitVector.spl_nleft);
- backupSplit.spl_right = (OffsetNumber *) palloc(sizeof(OffsetNumber) * len);
+ backupSplit.spl_right = palloc_array(OffsetNumber, len);
memcpy(backupSplit.spl_right, v->splitVector.spl_right, sizeof(OffsetNumber) * v->splitVector.spl_nright);
/* Recursively decide how to split the don't-care tuples */
diff --git a/src/backend/access/gist/gistutil.c b/src/backend/access/gist/gistutil.c
index 0a29a6013ef..6fc56c3c893 100644
--- a/src/backend/access/gist/gistutil.c
+++ b/src/backend/access/gist/gistutil.c
@@ -100,7 +100,7 @@ gistextractpage(Page page, int *len /* out */ )
maxoff = PageGetMaxOffsetNumber(page);
*len = maxoff;
- itvec = palloc(sizeof(IndexTuple) * maxoff);
+ itvec = palloc_array(IndexTuple, maxoff);
for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
itvec[i - FirstOffsetNumber] = (IndexTuple) PageGetItem(page, PageGetItemId(page, i));
@@ -113,7 +113,7 @@ gistextractpage(Page page, int *len /* out */ )
IndexTuple *
gistjoinvector(IndexTuple *itvec, int *len, IndexTuple *additvec, int addlen)
{
- itvec = (IndexTuple *) repalloc(itvec, sizeof(IndexTuple) * ((*len) + addlen));
+ itvec = repalloc_array(itvec, IndexTuple, (*len) + addlen);
memmove(&itvec[*len], additvec, sizeof(IndexTuple) * addlen);
*len += addlen;
return itvec;
diff --git a/src/backend/access/gist/gistvacuum.c b/src/backend/access/gist/gistvacuum.c
index b925eda2b9b..7591ad4da1a 100644
--- a/src/backend/access/gist/gistvacuum.c
+++ b/src/backend/access/gist/gistvacuum.c
@@ -61,7 +61,7 @@ gistbulkdelete(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);
gistvacuumscan(info, stats, callback, callback_state);
@@ -85,7 +85,7 @@ gistvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
*/
if (stats == NULL)
{
- stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult));
+ stats = palloc0_object(IndexBulkDeleteResult);
gistvacuumscan(info, stats, NULL, NULL);
}