Switched to palloc_array and palloc0_array where possible

This commit is contained in:
Andrew Kane
2026-07-27 21:51:15 -07:00
parent bc4e6a13a2
commit 1be6148eca
9 changed files with 30 additions and 15 deletions

View File

@@ -27,8 +27,12 @@
#include "parser/scansup.h"
#endif
#if PG_VERSION_NUM < 160000
#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
#endif
#define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1)
#define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1))
#define CreateStateDatums(dim) palloc_array(Datum, (dim + 1))
/*
* Get a half from a message buffer
@@ -513,7 +517,7 @@ halfvec_to_float4(PG_FUNCTION_ARGS)
Datum *datums;
ArrayType *result;
datums = (Datum *) palloc(sizeof(Datum) * vec->dim);
datums = palloc_array(Datum, vec->dim);
for (int i = 0; i < vec->dim; i++)
datums[i] = Float4GetDatum(HalfToFloat4(vec->x[i]));

View File

@@ -108,6 +108,7 @@ typedef Pointer Item;
#if PG_VERSION_NUM < 160000
#define palloc_object(type) ((type *) palloc(sizeof(type)))
#define palloc0_object(type) ((type *) palloc0(sizeof(type)))
#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
#endif
#define HnswIsElementTuple(tup) ((tup)->type == HNSW_ELEMENT_TUPLE_TYPE)

View File

@@ -831,7 +831,7 @@ HnswSearchLayer(char *base, HnswQuery * q, List *ep, int ef, int lc, Relation in
HnswNeighborArray *localNeighborhood = NULL;
Size neighborhoodSize = 0;
int lm = HnswGetLayerM(m, lc);
HnswUnvisited *unvisited = palloc(lm * sizeof(HnswUnvisited));
HnswUnvisited *unvisited = palloc_array(HnswUnvisited, lm);
int unvisitedLength;
bool inMemory = index == NULL;
@@ -1074,7 +1074,7 @@ SelectNeighbors(char *base, List *c, int lm, HnswSupport * support, bool *closer
if (list_length(w) <= lm)
return w;
wd = palloc(sizeof(HnswCandidate *) * list_length(w));
wd = palloc_array(HnswCandidate *, list_length(w));
/* Ensure order of candidates is deterministic for closer caching */
if (sortCandidates)

View File

@@ -396,7 +396,7 @@ InitBuildState(IvfflatBuildState * buildstate, Relation heap, Relation index, In
buildstate->centers = VectorArrayInit(buildstate->lists, buildstate->dimensions, buildstate->itemsize);
/* TODO Move allocation to page creation */
buildstate->listInfo = palloc(sizeof(ListInfo) * buildstate->lists);
buildstate->listInfo = palloc_array(ListInfo, buildstate->lists);
buildstate->tmpCtx = AllocSetContextCreate(CurrentMemoryContext,
"Ivfflat build temporary context",
@@ -404,8 +404,8 @@ InitBuildState(IvfflatBuildState * buildstate, Relation heap, Relation index, In
#ifdef IVFFLAT_KMEANS_DEBUG
buildstate->inertia = 0;
buildstate->listSums = palloc0(sizeof(double) * buildstate->lists);
buildstate->listCounts = palloc0(sizeof(int) * buildstate->lists);
buildstate->listSums = palloc0_array(double, buildstate->lists);
buildstate->listCounts = palloc0_array(int, buildstate->lists);
#endif
buildstate->ivfleader = NULL;

View File

@@ -92,6 +92,8 @@ typedef Pointer Item;
#if PG_VERSION_NUM < 160000
#define palloc_object(type) ((type *) palloc(sizeof(type)))
#define palloc0_object(type) ((type *) palloc0(sizeof(type)))
#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
#define palloc0_array(type, count) ((type *) palloc0(sizeof(type) * (count)))
#endif
/* Variables */

View File

@@ -26,7 +26,7 @@ InitCenters(Relation index, VectorArray samples, VectorArray centers, float *low
FmgrInfo *procinfo;
Oid collation;
int64 j;
float *weight = palloc(samples->length * sizeof(float));
float *weight = palloc_array(float, samples->length);
int numCenters = centers->maxlen;
int numSamples = samples->length;
@@ -113,7 +113,7 @@ RandomCenters(Relation index, VectorArray centers, const IvfflatTypeInfo * typeI
int dimensions = centers->dim;
FmgrInfo *normprocinfo = IvfflatOptionalProcInfo(index, IVFFLAT_KMEANS_NORM_PROC);
Oid collation = index->rd_indcollation[0];
float *x = (float *) palloc(sizeof(float) * dimensions);
float *x = palloc_array(float, dimensions);
/* Fill with random data */
while (centers->length < centers->maxlen)
@@ -480,7 +480,7 @@ ElkanKmeans(Relation index, VectorArray samples, VectorArray centers, const Ivff
static void
CheckElements(VectorArray centers, const IvfflatTypeInfo * typeInfo)
{
float *scratch = palloc(sizeof(float) * centers->dim);
float *scratch = palloc_array(float, centers->dim);
for (int i = 0; i < centers->length; i++)
{

View File

@@ -318,9 +318,9 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
so->bas = GetAccessStrategy(BAS_BULKREAD);
so->listQueue = pairingheap_allocate(CompareLists, scan);
so->listPages = palloc(maxProbes * sizeof(BlockNumber));
so->listPages = palloc_array(BlockNumber, maxProbes);
so->listIndex = 0;
so->lists = palloc(maxProbes * sizeof(IvfflatScanList));
so->lists = palloc_array(IvfflatScanList, maxProbes);
MemoryContextSwitchTo(oldCtx);

View File

@@ -26,6 +26,10 @@
#include "parser/scansup.h"
#endif
#if PG_VERSION_NUM < 160000
#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
#endif
typedef struct SparseInputElement
{
int32 index;
@@ -223,7 +227,7 @@ sparsevec_in(PG_FUNCTION_ARGS)
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("sparsevec cannot have more than %d non-zero elements", SPARSEVEC_MAX_NNZ)));
elements = palloc(maxNnz * sizeof(SparseInputElement));
elements = palloc_array(SparseInputElement, maxNnz);
pt = lit;

View File

@@ -30,8 +30,12 @@
#include "parser/scansup.h"
#endif
#if PG_VERSION_NUM < 160000
#define palloc_array(type, count) ((type *) palloc(sizeof(type) * (count)))
#endif
#define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1)
#define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1))
#define CreateStateDatums(dim) palloc_array(Datum, (dim + 1))
#if defined(USE_TARGET_CLONES) && !defined(__FMA__)
#define VECTOR_TARGET_CLONES __attribute__((target_clones("default", "fma")))
@@ -516,7 +520,7 @@ vector_to_float4(PG_FUNCTION_ARGS)
Datum *datums;
ArrayType *result;
datums = (Datum *) palloc(sizeof(Datum) * vec->dim);
datums = palloc_array(Datum, vec->dim);
for (int i = 0; i < vec->dim; i++)
datums[i] = Float4GetDatum(vec->x[i]);