Switched to palloc_object and palloc0_object when possible

This commit is contained in:
Andrew Kane
2026-07-27 19:26:50 -07:00
parent 573040d3a2
commit d2e2aa0e63
10 changed files with 26 additions and 16 deletions

View File

@@ -105,6 +105,11 @@ typedef Pointer Item;
#define SeedRandom(seed) srandom(seed) #define SeedRandom(seed) srandom(seed)
#endif #endif
#if PG_VERSION_NUM < 160000
#define palloc_object(type) ((type *) palloc(sizeof(type)))
#define palloc0_object(type) ((type *) palloc0(sizeof(type)))
#endif
#define HnswIsElementTuple(tup) ((tup)->type == HNSW_ELEMENT_TUPLE_TYPE) #define HnswIsElementTuple(tup) ((tup)->type == HNSW_ELEMENT_TUPLE_TYPE)
#define HnswIsNeighborTuple(tup) ((tup)->type == HNSW_NEIGHBOR_TUPLE_TYPE) #define HnswIsNeighborTuple(tup) ((tup)->type == HNSW_NEIGHBOR_TUPLE_TYPE)

View File

@@ -930,7 +930,7 @@ HnswBeginParallel(HnswBuildState * buildstate, bool isconcurrent, int request)
Size estother; Size estother;
HnswShared *hnswshared; HnswShared *hnswshared;
char *hnswarea; char *hnswarea;
HnswLeader *hnswleader = (HnswLeader *) palloc0(sizeof(HnswLeader)); HnswLeader *hnswleader = palloc0_object(HnswLeader);
bool leaderparticipates = true; bool leaderparticipates = true;
int querylen; int querylen;
@@ -1151,7 +1151,7 @@ hnswbuild(Relation heap, Relation index, IndexInfo *indexInfo)
BuildIndex(heap, index, indexInfo, &buildstate, MAIN_FORKNUM); BuildIndex(heap, index, indexInfo, &buildstate, MAIN_FORKNUM);
result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult)); result = palloc_object(IndexBuildResult);
result->heap_tuples = buildstate.reltuples; result->heap_tuples = buildstate.reltuples;
result->index_tuples = buildstate.indtuples; result->index_tuples = buildstate.indtuples;

View File

@@ -136,7 +136,7 @@ hnswbeginscan(Relation index, int nkeys, int norderbys)
scan = RelationGetIndexScan(index, nkeys, norderbys); scan = RelationGetIndexScan(index, nkeys, norderbys);
so = (HnswScanOpaque) palloc(sizeof(HnswScanOpaqueData)); so = palloc_object(HnswScanOpaqueData);
so->typeInfo = HnswGetTypeInfo(index); so->typeInfo = HnswGetTypeInfo(index);
/* Set support functions */ /* Set support functions */

View File

@@ -282,7 +282,7 @@ HnswAddHeapTid(HnswElement element, ItemPointer heaptid)
HnswElement HnswElement
HnswInitElementFromBlock(BlockNumber blkno, OffsetNumber offno) HnswInitElementFromBlock(BlockNumber blkno, OffsetNumber offno)
{ {
HnswElement element = palloc(sizeof(HnswElementData)); HnswElement element = palloc_object(HnswElementData);
char *base = NULL; char *base = NULL;
element->blkno = blkno; element->blkno = blkno;
@@ -596,7 +596,7 @@ GetElementDistance(char *base, HnswElement element, HnswQuery * q, HnswSupport *
static HnswSearchCandidate * static HnswSearchCandidate *
HnswInitSearchCandidate(char *base, HnswElement element, double distance) HnswInitSearchCandidate(char *base, HnswElement element, double distance)
{ {
HnswSearchCandidate *sc = palloc(sizeof(HnswSearchCandidate)); HnswSearchCandidate *sc = palloc_object(HnswSearchCandidate);
HnswPtrStore(base, sc->element, element); HnswPtrStore(base, sc->element, element);
sc->distance = distance; sc->distance = distance;
@@ -1328,7 +1328,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint
foreach(lc2, w) foreach(lc2, w)
{ {
HnswSearchCandidate *sc = lfirst(lc2); HnswSearchCandidate *sc = lfirst(lc2);
HnswCandidate *hc = palloc(sizeof(HnswCandidate)); HnswCandidate *hc = palloc_object(HnswCandidate);
hc->element = sc->element; hc->element = sc->element;
hc->distance = sc->distance; hc->distance = sc->distance;

View File

@@ -737,7 +737,7 @@ InitVacuumState(HnswVacuumState * vacuumstate, IndexVacuumInfo *info, IndexBulkD
Relation index = info->index; Relation index = info->index;
if (stats == NULL) if (stats == NULL)
stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult)); stats = palloc0_object(IndexBulkDeleteResult);
vacuumstate->index = index; vacuumstate->index = index;
vacuumstate->stats = stats; vacuumstate->stats = stats;

View File

@@ -662,7 +662,7 @@ IvfflatParallelScanAndSort(IvfflatSpool * ivfspool, IvfflatShared * ivfshared, S
IndexInfo *indexInfo; IndexInfo *indexInfo;
/* Initialize local tuplesort coordination state */ /* Initialize local tuplesort coordination state */
coordinate = palloc0(sizeof(SortCoordinateData)); coordinate = palloc0_object(SortCoordinateData);
coordinate->isWorker = true; coordinate->isWorker = true;
coordinate->nParticipants = -1; coordinate->nParticipants = -1;
coordinate->sharedsort = sharedsort; coordinate->sharedsort = sharedsort;
@@ -757,7 +757,7 @@ IvfflatParallelBuildMain(dsm_segment *seg, shm_toc *toc)
indexRel = index_open(ivfshared->indexrelid, indexLockmode); indexRel = index_open(ivfshared->indexrelid, indexLockmode);
/* Initialize worker's own spool */ /* Initialize worker's own spool */
ivfspool = (IvfflatSpool *) palloc0(sizeof(IvfflatSpool)); ivfspool = palloc0_object(IvfflatSpool);
ivfspool->heap = heapRel; ivfspool->heap = heapRel;
ivfspool->index = indexRel; ivfspool->index = indexRel;
@@ -812,7 +812,7 @@ IvfflatLeaderParticipateAsWorker(IvfflatBuildState * buildstate)
int sortmem; int sortmem;
/* Allocate memory and initialize private spool */ /* Allocate memory and initialize private spool */
leaderworker = (IvfflatSpool *) palloc0(sizeof(IvfflatSpool)); leaderworker = palloc0_object(IvfflatSpool);
leaderworker->heap = buildstate->heap; leaderworker->heap = buildstate->heap;
leaderworker->index = buildstate->index; leaderworker->index = buildstate->index;
@@ -838,7 +838,7 @@ IvfflatBeginParallel(IvfflatBuildState * buildstate, bool isconcurrent, int requ
IvfflatShared *ivfshared; IvfflatShared *ivfshared;
Sharedsort *sharedsort; Sharedsort *sharedsort;
char *ivfcenters; char *ivfcenters;
IvfflatLeader *ivfleader = (IvfflatLeader *) palloc0(sizeof(IvfflatLeader)); IvfflatLeader *ivfleader = palloc0_object(IvfflatLeader);
bool leaderparticipates = true; bool leaderparticipates = true;
int querylen; int querylen;
@@ -987,7 +987,7 @@ AssignTuples(IvfflatBuildState * buildstate)
/* Set up coordination state if at least one worker launched */ /* Set up coordination state if at least one worker launched */
if (buildstate->ivfleader) if (buildstate->ivfleader)
{ {
coordinate = (SortCoordinate) palloc0(sizeof(SortCoordinateData)); coordinate = palloc0_object(SortCoordinateData);
coordinate->isWorker = false; coordinate->isWorker = false;
coordinate->nParticipants = buildstate->ivfleader->nparticipanttuplesorts; coordinate->nParticipants = buildstate->ivfleader->nparticipanttuplesorts;
coordinate->sharedsort = buildstate->ivfleader->sharedsort; coordinate->sharedsort = buildstate->ivfleader->sharedsort;
@@ -1072,7 +1072,7 @@ ivfflatbuild(Relation heap, Relation index, IndexInfo *indexInfo)
BuildIndex(heap, index, indexInfo, &buildstate, MAIN_FORKNUM); BuildIndex(heap, index, indexInfo, &buildstate, MAIN_FORKNUM);
result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult)); result = palloc_object(IndexBuildResult);
result->heap_tuples = buildstate.reltuples; result->heap_tuples = buildstate.reltuples;
result->index_tuples = buildstate.indtuples; result->index_tuples = buildstate.indtuples;

View File

@@ -89,6 +89,11 @@ typedef Pointer Item;
#define SeedRandom(seed) srandom(seed) #define SeedRandom(seed) srandom(seed)
#endif #endif
#if PG_VERSION_NUM < 160000
#define palloc_object(type) ((type *) palloc(sizeof(type)))
#define palloc0_object(type) ((type *) palloc0(sizeof(type)))
#endif
/* Variables */ /* Variables */
extern int ivfflat_probes; extern int ivfflat_probes;
extern int ivfflat_iterative_scan; extern int ivfflat_iterative_scan;

View File

@@ -276,7 +276,7 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
if (maxProbes > lists) if (maxProbes > lists)
maxProbes = lists; maxProbes = lists;
so = (IvfflatScanOpaque) palloc(sizeof(IvfflatScanOpaqueData)); so = palloc_object(IvfflatScanOpaqueData);
so->typeInfo = IvfflatGetTypeInfo(index); so->typeInfo = IvfflatGetTypeInfo(index);
so->first = true; so->first = true;
so->probes = probes; so->probes = probes;

View File

@@ -31,7 +31,7 @@ VectorArrayInit(int maxlen, int dimensions, Size itemsize)
/* Ensure items are aligned to prevent UB */ /* Ensure items are aligned to prevent UB */
itemsize = MAXALIGN(itemsize); itemsize = MAXALIGN(itemsize);
res = palloc(sizeof(VectorArrayData)); res = palloc_object(VectorArrayData);
res->length = 0; res->length = 0;
res->maxlen = maxlen; res->maxlen = maxlen;
res->dim = dimensions; res->dim = dimensions;

View File

@@ -24,7 +24,7 @@ ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
BufferAccessStrategy bas = GetAccessStrategy(BAS_BULKREAD); BufferAccessStrategy bas = GetAccessStrategy(BAS_BULKREAD);
if (stats == NULL) if (stats == NULL)
stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult)); stats = palloc0_object(IndexBulkDeleteResult);
/* Iterate over list pages */ /* Iterate over list pages */
while (BlockNumberIsValid(blkno)) while (BlockNumberIsValid(blkno))