diff --git a/src/hnsw.h b/src/hnsw.h index e8d1423..f265c7c 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -105,6 +105,11 @@ typedef Pointer Item; #define SeedRandom(seed) srandom(seed) #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 HnswIsNeighborTuple(tup) ((tup)->type == HNSW_NEIGHBOR_TUPLE_TYPE) diff --git a/src/hnswbuild.c b/src/hnswbuild.c index 7b1bf2a..7d19146 100644 --- a/src/hnswbuild.c +++ b/src/hnswbuild.c @@ -930,7 +930,7 @@ HnswBeginParallel(HnswBuildState * buildstate, bool isconcurrent, int request) Size estother; HnswShared *hnswshared; char *hnswarea; - HnswLeader *hnswleader = (HnswLeader *) palloc0(sizeof(HnswLeader)); + HnswLeader *hnswleader = palloc0_object(HnswLeader); bool leaderparticipates = true; int querylen; @@ -1151,7 +1151,7 @@ hnswbuild(Relation heap, Relation index, IndexInfo *indexInfo) BuildIndex(heap, index, indexInfo, &buildstate, MAIN_FORKNUM); - result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult)); + result = palloc_object(IndexBuildResult); result->heap_tuples = buildstate.reltuples; result->index_tuples = buildstate.indtuples; diff --git a/src/hnswscan.c b/src/hnswscan.c index 32cdb7d..1c50efb 100644 --- a/src/hnswscan.c +++ b/src/hnswscan.c @@ -136,7 +136,7 @@ hnswbeginscan(Relation index, int nkeys, int norderbys) scan = RelationGetIndexScan(index, nkeys, norderbys); - so = (HnswScanOpaque) palloc(sizeof(HnswScanOpaqueData)); + so = palloc_object(HnswScanOpaqueData); so->typeInfo = HnswGetTypeInfo(index); /* Set support functions */ diff --git a/src/hnswutils.c b/src/hnswutils.c index 896e66d..ac36d2a 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -282,7 +282,7 @@ HnswAddHeapTid(HnswElement element, ItemPointer heaptid) HnswElement HnswInitElementFromBlock(BlockNumber blkno, OffsetNumber offno) { - HnswElement element = palloc(sizeof(HnswElementData)); + HnswElement element = palloc_object(HnswElementData); char *base = NULL; element->blkno = blkno; @@ -596,7 +596,7 @@ GetElementDistance(char *base, HnswElement element, HnswQuery * q, HnswSupport * static HnswSearchCandidate * HnswInitSearchCandidate(char *base, HnswElement element, double distance) { - HnswSearchCandidate *sc = palloc(sizeof(HnswSearchCandidate)); + HnswSearchCandidate *sc = palloc_object(HnswSearchCandidate); HnswPtrStore(base, sc->element, element); sc->distance = distance; @@ -1328,7 +1328,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint foreach(lc2, w) { HnswSearchCandidate *sc = lfirst(lc2); - HnswCandidate *hc = palloc(sizeof(HnswCandidate)); + HnswCandidate *hc = palloc_object(HnswCandidate); hc->element = sc->element; hc->distance = sc->distance; diff --git a/src/hnswvacuum.c b/src/hnswvacuum.c index 804a573..88905db 100644 --- a/src/hnswvacuum.c +++ b/src/hnswvacuum.c @@ -737,7 +737,7 @@ InitVacuumState(HnswVacuumState * vacuumstate, IndexVacuumInfo *info, IndexBulkD Relation index = info->index; if (stats == NULL) - stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult)); + stats = palloc0_object(IndexBulkDeleteResult); vacuumstate->index = index; vacuumstate->stats = stats; diff --git a/src/ivfbuild.c b/src/ivfbuild.c index d3f0a7d..dd540b2 100644 --- a/src/ivfbuild.c +++ b/src/ivfbuild.c @@ -662,7 +662,7 @@ IvfflatParallelScanAndSort(IvfflatSpool * ivfspool, IvfflatShared * ivfshared, S IndexInfo *indexInfo; /* Initialize local tuplesort coordination state */ - coordinate = palloc0(sizeof(SortCoordinateData)); + coordinate = palloc0_object(SortCoordinateData); coordinate->isWorker = true; coordinate->nParticipants = -1; coordinate->sharedsort = sharedsort; @@ -757,7 +757,7 @@ IvfflatParallelBuildMain(dsm_segment *seg, shm_toc *toc) indexRel = index_open(ivfshared->indexrelid, indexLockmode); /* Initialize worker's own spool */ - ivfspool = (IvfflatSpool *) palloc0(sizeof(IvfflatSpool)); + ivfspool = palloc0_object(IvfflatSpool); ivfspool->heap = heapRel; ivfspool->index = indexRel; @@ -812,7 +812,7 @@ IvfflatLeaderParticipateAsWorker(IvfflatBuildState * buildstate) int sortmem; /* Allocate memory and initialize private spool */ - leaderworker = (IvfflatSpool *) palloc0(sizeof(IvfflatSpool)); + leaderworker = palloc0_object(IvfflatSpool); leaderworker->heap = buildstate->heap; leaderworker->index = buildstate->index; @@ -838,7 +838,7 @@ IvfflatBeginParallel(IvfflatBuildState * buildstate, bool isconcurrent, int requ IvfflatShared *ivfshared; Sharedsort *sharedsort; char *ivfcenters; - IvfflatLeader *ivfleader = (IvfflatLeader *) palloc0(sizeof(IvfflatLeader)); + IvfflatLeader *ivfleader = palloc0_object(IvfflatLeader); bool leaderparticipates = true; int querylen; @@ -987,7 +987,7 @@ AssignTuples(IvfflatBuildState * buildstate) /* Set up coordination state if at least one worker launched */ if (buildstate->ivfleader) { - coordinate = (SortCoordinate) palloc0(sizeof(SortCoordinateData)); + coordinate = palloc0_object(SortCoordinateData); coordinate->isWorker = false; coordinate->nParticipants = buildstate->ivfleader->nparticipanttuplesorts; coordinate->sharedsort = buildstate->ivfleader->sharedsort; @@ -1072,7 +1072,7 @@ ivfflatbuild(Relation heap, Relation index, IndexInfo *indexInfo) BuildIndex(heap, index, indexInfo, &buildstate, MAIN_FORKNUM); - result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult)); + result = palloc_object(IndexBuildResult); result->heap_tuples = buildstate.reltuples; result->index_tuples = buildstate.indtuples; diff --git a/src/ivfflat.h b/src/ivfflat.h index a04585a..6a628f7 100644 --- a/src/ivfflat.h +++ b/src/ivfflat.h @@ -89,6 +89,11 @@ typedef Pointer Item; #define SeedRandom(seed) srandom(seed) #endif +#if PG_VERSION_NUM < 160000 +#define palloc_object(type) ((type *) palloc(sizeof(type))) +#define palloc0_object(type) ((type *) palloc0(sizeof(type))) +#endif + /* Variables */ extern int ivfflat_probes; extern int ivfflat_iterative_scan; diff --git a/src/ivfscan.c b/src/ivfscan.c index 35b10f3..00aad28 100644 --- a/src/ivfscan.c +++ b/src/ivfscan.c @@ -276,7 +276,7 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys) if (maxProbes > lists) maxProbes = lists; - so = (IvfflatScanOpaque) palloc(sizeof(IvfflatScanOpaqueData)); + so = palloc_object(IvfflatScanOpaqueData); so->typeInfo = IvfflatGetTypeInfo(index); so->first = true; so->probes = probes; diff --git a/src/ivfutils.c b/src/ivfutils.c index 02e0839..534cb24 100644 --- a/src/ivfutils.c +++ b/src/ivfutils.c @@ -31,7 +31,7 @@ VectorArrayInit(int maxlen, int dimensions, Size itemsize) /* Ensure items are aligned to prevent UB */ itemsize = MAXALIGN(itemsize); - res = palloc(sizeof(VectorArrayData)); + res = palloc_object(VectorArrayData); res->length = 0; res->maxlen = maxlen; res->dim = dimensions; diff --git a/src/ivfvacuum.c b/src/ivfvacuum.c index 5408609..088770f 100644 --- a/src/ivfvacuum.c +++ b/src/ivfvacuum.c @@ -24,7 +24,7 @@ ivfflatbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, BufferAccessStrategy bas = GetAccessStrategy(BAS_BULKREAD); if (stats == NULL) - stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult)); + stats = palloc0_object(IndexBulkDeleteResult); /* Iterate over list pages */ while (BlockNumberIsValid(blkno))