Assorted examples of expanded type-safer palloc/pg_malloc API
authorPeter Eisentraut <[email protected]>
Mon, 12 Sep 2022 06:31:56 +0000 (08:31 +0200)
committerPeter Eisentraut <[email protected]>
Mon, 12 Sep 2022 06:45:03 +0000 (08:45 +0200)
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 <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/bb755632-2a43-d523-36f8-a1e7a389a907@enterprisedb.com

12 files changed:
contrib/dblink/dblink.c
src/backend/access/brin/brin.c
src/backend/access/gin/ginfast.c
src/backend/commands/indexcmds.c
src/backend/commands/prepare.c
src/backend/executor/nodeHash.c
src/backend/tcop/postgres.c
src/bin/pg_dump/common.c
src/bin/pg_dump/pg_backup_tar.c
src/bin/psql/startup.c
src/common/config_info.c
src/common/controldata_utils.c

index 7940387920ca5cb887820c9c160507f2b1016607..3df3f9bbe9fa6be5fcb782f4e9dc7ede3de010b9 100644 (file)
@@ -972,7 +972,7 @@ materializeResult(FunctionCallInfo fcinfo, PGconn *conn, PGresult *res)
                        rsinfo->setDesc = tupdesc;
                        MemoryContextSwitchTo(oldcontext);
 
-                       values = (char **) palloc(nfields * sizeof(char *));
+                       values = palloc_array(char *, nfields);
 
                        /* put all tuples into the tuplestore */
                        for (row = 0; row < ntuples; row++)
@@ -1276,7 +1276,7 @@ storeRow(volatile storeInfo *sinfo, PGresult *res, bool first)
                 */
                if (sinfo->cstrs)
                        pfree(sinfo->cstrs);
-               sinfo->cstrs = (char **) palloc(nfields * sizeof(char *));
+               sinfo->cstrs = palloc_array(char *, nfields);
        }
 
        /* Should have a single-row result if we get here */
@@ -1618,7 +1618,7 @@ dblink_get_pkey(PG_FUNCTION_ARGS)
                HeapTuple       tuple;
                Datum           result;
 
-               values = (char **) palloc(2 * sizeof(char *));
+               values = palloc_array(char *, 2);
                values[0] = psprintf("%d", call_cntr + 1);
                values[1] = results[call_cntr];
 
@@ -2083,7 +2083,7 @@ get_pkey_attnames(Relation rel, int16 *indnkeyatts)
                        *indnkeyatts = index->indnkeyatts;
                        if (*indnkeyatts > 0)
                        {
-                               result = (char **) palloc(*indnkeyatts * sizeof(char *));
+                               result = palloc_array(char *, *indnkeyatts);
 
                                for (i = 0; i < *indnkeyatts; i++)
                                        result[i] = SPI_fname(tupdesc, index->indkey.values[i]);
@@ -2124,7 +2124,7 @@ get_text_array_contents(ArrayType *array, int *numitems)
        get_typlenbyvalalign(ARR_ELEMTYPE(array),
                                                 &typlen, &typbyval, &typalign);
 
-       values = (char **) palloc(nitems * sizeof(char *));
+       values = palloc_array(char *, nitems);
 
        ptr = ARR_DATA_PTR(array);
        bitmap = ARR_NULLBITMAP(array);
@@ -2928,7 +2928,7 @@ validate_pkattnums(Relation rel,
                                 errmsg("number of key attributes must be > 0")));
 
        /* Allocate output array */
-       *pkattnums = (int *) palloc(pknumatts_arg * sizeof(int));
+       *pkattnums = palloc_array(int, pknumatts_arg);
        *pknumatts = pknumatts_arg;
 
        /* Validate attnums and convert to internal form */
index 69f21abfb590c1a43fb38a97960397f572103f6b..dcf2d2c4b5081b88459b2a30f4a87a0f7051e034 100644 (file)
@@ -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;
index 6c677447a95168956abbe9017379a8538067ccf8..ab4bb67d4b456b4d9db22a8e5af763a513107d18 100644 (file)
@@ -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;
index 3c6e09815e0a9a118264474f9cbd49e07fd19b66..9167bab0c55ff6a563e25f5c0c7c06c675dc3fe2 100644 (file)
@@ -229,10 +229,10 @@ CheckIndexCompatible(Oid oldId,
         */
        indexInfo = makeIndexInfo(numberOfAttributes, numberOfAttributes,
                                                          accessMethodId, NIL, NIL, false, false, false, false);
-       typeObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
-       collationObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
-       classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
-       coloptions = (int16 *) palloc(numberOfAttributes * sizeof(int16));
+       typeObjectId = palloc_array(Oid, numberOfAttributes);
+       collationObjectId = palloc_array(Oid, numberOfAttributes);
+       classObjectId = palloc_array(Oid, numberOfAttributes);
+       coloptions = palloc_array(int16, numberOfAttributes);
        ComputeIndexAttrs(indexInfo,
                                          typeObjectId, collationObjectId, classObjectId,
                                          coloptions, attributeList,
@@ -895,10 +895,10 @@ DefineIndex(Oid relationId,
                                                          !concurrent,
                                                          concurrent);
 
-       typeObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
-       collationObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
-       classObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
-       coloptions = (int16 *) palloc(numberOfAttributes * sizeof(int16));
+       typeObjectId = palloc_array(Oid, numberOfAttributes);
+       collationObjectId = palloc_array(Oid, numberOfAttributes);
+       classObjectId = palloc_array(Oid, numberOfAttributes);
+       coloptions = palloc_array(int16, numberOfAttributes);
        ComputeIndexAttrs(indexInfo,
                                          typeObjectId, collationObjectId, classObjectId,
                                          coloptions, allIndexParams,
@@ -1210,7 +1210,7 @@ DefineIndex(Oid relationId,
                if ((!stmt->relation || stmt->relation->inh) && partdesc->nparts > 0)
                {
                        int                     nparts = partdesc->nparts;
-                       Oid                *part_oids = palloc(sizeof(Oid) * nparts);
+                       Oid                *part_oids = palloc_array(Oid, nparts);
                        bool            invalidate_parent = false;
                        Relation        parentIndex;
                        TupleDesc       parentDesc;
@@ -1786,9 +1786,9 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
        if (exclusionOpNames)
        {
                Assert(list_length(exclusionOpNames) == nkeycols);
-               indexInfo->ii_ExclusionOps = (Oid *) palloc(sizeof(Oid) * nkeycols);
-               indexInfo->ii_ExclusionProcs = (Oid *) palloc(sizeof(Oid) * nkeycols);
-               indexInfo->ii_ExclusionStrats = (uint16 *) palloc(sizeof(uint16) * nkeycols);
+               indexInfo->ii_ExclusionOps = palloc_array(Oid, nkeycols);
+               indexInfo->ii_ExclusionProcs = palloc_array(Oid, nkeycols);
+               indexInfo->ii_ExclusionStrats = palloc_array(uint16, nkeycols);
                nextExclOp = list_head(exclusionOpNames);
        }
        else
@@ -2112,7 +2112,7 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
 
                        if (!indexInfo->ii_OpclassOptions)
                                indexInfo->ii_OpclassOptions =
-                                       palloc0(sizeof(Datum) * indexInfo->ii_NumIndexAttrs);
+                                       palloc0_array(Datum, indexInfo->ii_NumIndexAttrs);
 
                        indexInfo->ii_OpclassOptions[attn] =
                                transformRelOptions((Datum) 0, attribute->opclassopts,
@@ -3459,7 +3459,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
                                                /* Save the list of relation OIDs in private context */
                                                oldcontext = MemoryContextSwitchTo(private_context);
 
-                                               idx = palloc(sizeof(ReindexIndexInfo));
+                                               idx = palloc_object(ReindexIndexInfo);
                                                idx->indexId = cellOid;
                                                /* other fields set later */
 
@@ -3508,7 +3508,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
                                                         */
                                                        oldcontext = MemoryContextSwitchTo(private_context);
 
-                                                       idx = palloc(sizeof(ReindexIndexInfo));
+                                                       idx = palloc_object(ReindexIndexInfo);
                                                        idx->indexId = cellOid;
                                                        indexIds = lappend(indexIds, idx);
                                                        /* other fields set later */
@@ -3589,7 +3589,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
                                 * Save the list of relation OIDs in private context.  Note
                                 * that invalid indexes are allowed here.
                                 */
-                               idx = palloc(sizeof(ReindexIndexInfo));
+                               idx = palloc_object(ReindexIndexInfo);
                                idx->indexId = relationOid;
                                indexIds = lappend(indexIds, idx);
                                /* other fields set later */
@@ -3734,7 +3734,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
                 */
                oldcontext = MemoryContextSwitchTo(private_context);
 
-               newidx = palloc(sizeof(ReindexIndexInfo));
+               newidx = palloc_object(ReindexIndexInfo);
                newidx->indexId = newIndexId;
                newidx->safe = idx->safe;
                newidx->tableId = idx->tableId;
@@ -3748,10 +3748,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
                 * avoid multiple locks taken on the same relation, instead we rely on
                 * parentRelationIds built earlier.
                 */
-               lockrelid = palloc(sizeof(*lockrelid));
+               lockrelid = palloc_object(LockRelId);
                *lockrelid = indexRel->rd_lockInfo.lockRelId;
                relationLocks = lappend(relationLocks, lockrelid);
-               lockrelid = palloc(sizeof(*lockrelid));
+               lockrelid = palloc_object(LockRelId);
                *lockrelid = newIndexRel->rd_lockInfo.lockRelId;
                relationLocks = lappend(relationLocks, lockrelid);
 
@@ -3783,11 +3783,11 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
                oldcontext = MemoryContextSwitchTo(private_context);
 
                /* Add lockrelid of heap relation to the list of locked relations */
-               lockrelid = palloc(sizeof(*lockrelid));
+               lockrelid = palloc_object(LockRelId);
                *lockrelid = heapRelation->rd_lockInfo.lockRelId;
                relationLocks = lappend(relationLocks, lockrelid);
 
-               heaplocktag = (LOCKTAG *) palloc(sizeof(LOCKTAG));
+               heaplocktag = palloc_object(LOCKTAG);
 
                /* Save the LOCKTAG for this parent relation for the wait phase */
                SET_LOCKTAG_RELATION(*heaplocktag, lockrelid->dbId, lockrelid->relId);
index 579825c1593c44f9cd89a9007f8c8d99f2a85895..c4b54d054757b8728ae941f427819f665cea289c 100644 (file)
@@ -98,7 +98,7 @@ PrepareQuery(ParseState *pstate, PrepareStmt *stmt,
                int                     i;
                ListCell   *l;
 
-               argtypes = (Oid *) palloc(nargs * sizeof(Oid));
+               argtypes = palloc_array(Oid, nargs);
                i = 0;
 
                foreach(l, stmt->argtypes)
@@ -698,7 +698,7 @@ pg_prepared_statement(PG_FUNCTION_ARGS)
                        {
                                Oid                *result_types;
 
-                               result_types = (Oid *) palloc(result_desc->natts * sizeof(Oid));
+                               result_types = palloc_array(Oid, result_desc->natts);
                                for (int i = 0; i < result_desc->natts; i++)
                                        result_types[i] = result_desc->attrs[i].atttypid;
                                values[4] = build_regtype_array(result_types, result_desc->natts);
@@ -732,7 +732,7 @@ build_regtype_array(Oid *param_types, int num_params)
        ArrayType  *result;
        int                     i;
 
-       tmp_ary = (Datum *) palloc(num_params * sizeof(Datum));
+       tmp_ary = palloc_array(Datum, num_params);
 
        for (i = 0; i < num_params; i++)
                tmp_ary[i] = ObjectIdGetDatum(param_types[i]);
index 841896c7781312ad0e9a958af57cc5ceb7a1a745..77dd1dae8bb1be77c5797cc1db789d755f18b6c7 100644 (file)
@@ -479,7 +479,7 @@ ExecHashTableCreate(HashState *state, List *hashOperators, List *hashCollations,
         * per-query memory context.  Everything else should be kept inside the
         * subsidiary hashCxt or batchCxt.
         */
-       hashtable = (HashJoinTable) palloc(sizeof(HashJoinTableData));
+       hashtable = palloc_object(HashJoinTableData);
        hashtable->nbuckets = nbuckets;
        hashtable->nbuckets_original = nbuckets;
        hashtable->nbuckets_optimal = nbuckets;
@@ -540,12 +540,10 @@ ExecHashTableCreate(HashState *state, List *hashOperators, List *hashCollations,
         * remember whether the join operators are strict.
         */
        nkeys = list_length(hashOperators);
-       hashtable->outer_hashfunctions =
-               (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
-       hashtable->inner_hashfunctions =
-               (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
-       hashtable->hashStrict = (bool *) palloc(nkeys * sizeof(bool));
-       hashtable->collations = (Oid *) palloc(nkeys * sizeof(Oid));
+       hashtable->outer_hashfunctions = palloc_array(FmgrInfo, nkeys);
+       hashtable->inner_hashfunctions = palloc_array(FmgrInfo, nkeys);
+       hashtable->hashStrict = palloc_array(bool, nkeys);
+       hashtable->collations = palloc_array(Oid, nkeys);
        i = 0;
        forboth(ho, hashOperators, hc, hashCollations)
        {
@@ -569,10 +567,8 @@ ExecHashTableCreate(HashState *state, List *hashOperators, List *hashCollations,
                 * allocate and initialize the file arrays in hashCxt (not needed for
                 * parallel case which uses shared tuplestores instead of raw files)
                 */
-               hashtable->innerBatchFile = (BufFile **)
-                       palloc0(nbatch * sizeof(BufFile *));
-               hashtable->outerBatchFile = (BufFile **)
-                       palloc0(nbatch * sizeof(BufFile *));
+               hashtable->innerBatchFile = palloc0_array(BufFile *, nbatch);
+               hashtable->outerBatchFile = palloc0_array(BufFile *, nbatch);
                /* The files will not be opened until needed... */
                /* ... but make sure we have temp tablespaces established for them */
                PrepareTempTablespaces();
@@ -636,8 +632,7 @@ ExecHashTableCreate(HashState *state, List *hashOperators, List *hashCollations,
                 */
                MemoryContextSwitchTo(hashtable->batchCxt);
 
-               hashtable->buckets.unshared = (HashJoinTuple *)
-                       palloc0(nbuckets * sizeof(HashJoinTuple));
+               hashtable->buckets.unshared = palloc0_array(HashJoinTuple, nbuckets);
 
                /*
                 * Set up for skew optimization, if possible and there's a need for
@@ -937,20 +932,16 @@ ExecHashIncreaseNumBatches(HashJoinTable hashtable)
        if (hashtable->innerBatchFile == NULL)
        {
                /* we had no file arrays before */
-               hashtable->innerBatchFile = (BufFile **)
-                       palloc0(nbatch * sizeof(BufFile *));
-               hashtable->outerBatchFile = (BufFile **)
-                       palloc0(nbatch * sizeof(BufFile *));
+               hashtable->innerBatchFile = palloc0_array(BufFile *, nbatch);
+               hashtable->outerBatchFile = palloc0_array(BufFile *, nbatch);
                /* time to establish the temp tablespaces, too */
                PrepareTempTablespaces();
        }
        else
        {
                /* enlarge arrays and zero out added entries */
-               hashtable->innerBatchFile = (BufFile **)
-                       repalloc(hashtable->innerBatchFile, nbatch * sizeof(BufFile *));
-               hashtable->outerBatchFile = (BufFile **)
-                       repalloc(hashtable->outerBatchFile, nbatch * sizeof(BufFile *));
+               hashtable->innerBatchFile = repalloc_array(hashtable->innerBatchFile, BufFile *, nbatch);
+               hashtable->outerBatchFile = repalloc_array(hashtable->outerBatchFile, BufFile *, nbatch);
                MemSet(hashtable->innerBatchFile + oldnbatch, 0,
                           (nbatch - oldnbatch) * sizeof(BufFile *));
                MemSet(hashtable->outerBatchFile + oldnbatch, 0,
@@ -977,8 +968,8 @@ ExecHashIncreaseNumBatches(HashJoinTable hashtable)
                hashtable->log2_nbuckets = hashtable->log2_nbuckets_optimal;
 
                hashtable->buckets.unshared =
-                       repalloc(hashtable->buckets.unshared,
-                                        sizeof(HashJoinTuple) * hashtable->nbuckets);
+                       repalloc_array(hashtable->buckets.unshared,
+                                                  HashJoinTuple, hashtable->nbuckets);
        }
 
        /*
@@ -1371,7 +1362,7 @@ ExecParallelHashRepartitionRest(HashJoinTable hashtable)
        /* Get our hands on the previous generation of batches. */
        old_batches = (ParallelHashJoinBatch *)
                dsa_get_address(hashtable->area, pstate->old_batches);
-       old_inner_tuples = palloc0(sizeof(SharedTuplestoreAccessor *) * old_nbatch);
+       old_inner_tuples = palloc0_array(SharedTuplestoreAccessor *, old_nbatch);
        for (i = 1; i < old_nbatch; ++i)
        {
                ParallelHashJoinBatch *shared =
@@ -1477,8 +1468,8 @@ ExecHashIncreaseNumBuckets(HashJoinTable hashtable)
         * chunks)
         */
        hashtable->buckets.unshared =
-               (HashJoinTuple *) repalloc(hashtable->buckets.unshared,
-                                                                  hashtable->nbuckets * sizeof(HashJoinTuple));
+               repalloc_array(hashtable->buckets.unshared,
+                                          HashJoinTuple, hashtable->nbuckets);
 
        memset(hashtable->buckets.unshared, 0,
                   hashtable->nbuckets * sizeof(HashJoinTuple));
@@ -2170,8 +2161,7 @@ ExecHashTableReset(HashJoinTable hashtable)
        oldcxt = MemoryContextSwitchTo(hashtable->batchCxt);
 
        /* Reallocate and reinitialize the hash bucket headers. */
-       hashtable->buckets.unshared = (HashJoinTuple *)
-               palloc0(nbuckets * sizeof(HashJoinTuple));
+       hashtable->buckets.unshared = palloc0_array(HashJoinTuple, nbuckets);
 
        hashtable->spaceUsed = 0;
 
@@ -2666,8 +2656,7 @@ ExecShutdownHash(HashState *node)
 {
        /* Allocate save space if EXPLAIN'ing and we didn't do so already */
        if (node->ps.instrument && !node->hinstrument)
-               node->hinstrument = (HashInstrumentation *)
-                       palloc0(sizeof(HashInstrumentation));
+               node->hinstrument = palloc0_object(HashInstrumentation);
        /* Now accumulate data for the current (final) hash table */
        if (node->hinstrument && node->hashtable)
                ExecHashAccumInstrumentation(node->hinstrument, node->hashtable);
@@ -2977,8 +2966,8 @@ ExecParallelHashJoinSetUpBatches(HashJoinTable hashtable, int nbatch)
 
        /* Allocate this backend's accessor array. */
        hashtable->nbatch = nbatch;
-       hashtable->batches = (ParallelHashJoinBatchAccessor *)
-               palloc0(sizeof(ParallelHashJoinBatchAccessor) * hashtable->nbatch);
+       hashtable->batches =
+               palloc0_array(ParallelHashJoinBatchAccessor, hashtable->nbatch);
 
        /* Set up the shared state, tuplestores and backend-local accessors. */
        for (i = 0; i < hashtable->nbatch; ++i)
@@ -3083,8 +3072,8 @@ ExecParallelHashEnsureBatchAccessors(HashJoinTable hashtable)
 
        /* Allocate this backend's accessor array. */
        hashtable->nbatch = pstate->nbatch;
-       hashtable->batches = (ParallelHashJoinBatchAccessor *)
-               palloc0(sizeof(ParallelHashJoinBatchAccessor) * hashtable->nbatch);
+       hashtable->batches =
+               palloc0_array(ParallelHashJoinBatchAccessor, hashtable->nbatch);
 
        /* Find the base of the pseudo-array of ParallelHashJoinBatch objects. */
        batches = (ParallelHashJoinBatch *)
index 7bec4e4ff58f373e86954cafe47aeb9be8590466..c6ca3b5b3d495f124c99578cde542a2de69ae998 100644 (file)
@@ -1660,7 +1660,7 @@ exec_bind_message(StringInfo input_message)
        numPFormats = pq_getmsgint(input_message, 2);
        if (numPFormats > 0)
        {
-               pformats = (int16 *) palloc(numPFormats * sizeof(int16));
+               pformats = palloc_array(int16, numPFormats);
                for (int i = 0; i < numPFormats; i++)
                        pformats[i] = pq_getmsgint(input_message, 2);
        }
@@ -1848,8 +1848,7 @@ exec_bind_message(StringInfo input_message)
                                                oldcxt = MemoryContextSwitchTo(MessageContext);
 
                                                if (knownTextValues == NULL)
-                                                       knownTextValues =
-                                                               palloc0(numParams * sizeof(char *));
+                                                       knownTextValues = palloc0_array(char *, numParams);
 
                                                if (log_parameter_max_length_on_error < 0)
                                                        knownTextValues[paramno] = pstrdup(pstring);
@@ -1958,7 +1957,7 @@ exec_bind_message(StringInfo input_message)
        numRFormats = pq_getmsgint(input_message, 2);
        if (numRFormats > 0)
        {
-               rformats = (int16 *) palloc(numRFormats * sizeof(int16));
+               rformats = palloc_array(int16, numRFormats);
                for (int i = 0; i < numRFormats; i++)
                        rformats[i] = pq_getmsgint(input_message, 2);
        }
@@ -4517,7 +4516,7 @@ PostgresMain(const char *dbname, const char *username)
                                        numParams = pq_getmsgint(&input_message, 2);
                                        if (numParams > 0)
                                        {
-                                               paramTypes = (Oid *) palloc(numParams * sizeof(Oid));
+                                               paramTypes = palloc_array(Oid, numParams);
                                                for (int i = 0; i < numParams; i++)
                                                        paramTypes[i] = pq_getmsgint(&input_message, 4);
                                        }
index 794e6e7ce99e3397c330ca9d1bd01de3e6ca6c2c..395f817fa8f103639bf8b17a938f148d817201ea 100644 (file)
@@ -400,7 +400,7 @@ flagInhIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
                        if (parentidx == NULL)
                                continue;
 
-                       attachinfo = (IndexAttachInfo *) pg_malloc(sizeof(IndexAttachInfo));
+                       attachinfo = pg_malloc_object(IndexAttachInfo);
 
                        attachinfo->dobj.objType = DO_INDEX_ATTACH;
                        attachinfo->dobj.catId.tableoid = 0;
@@ -530,7 +530,7 @@ flagInhAttrs(DumpOptions *dopt, TableInfo *tblinfo, int numTables)
                        {
                                AttrDefInfo *attrDef;
 
-                               attrDef = (AttrDefInfo *) pg_malloc(sizeof(AttrDefInfo));
+                               attrDef = pg_malloc_object(AttrDefInfo);
                                attrDef->dobj.objType = DO_ATTRDEF;
                                attrDef->dobj.catId.tableoid = 0;
                                attrDef->dobj.catId.oid = 0;
@@ -600,14 +600,12 @@ AssignDumpId(DumpableObject *dobj)
                if (allocedDumpIds <= 0)
                {
                        newAlloc = 256;
-                       dumpIdMap = (DumpableObject **)
-                               pg_malloc(newAlloc * sizeof(DumpableObject *));
+                       dumpIdMap = pg_malloc_array(DumpableObject *, newAlloc);
                }
                else
                {
                        newAlloc = allocedDumpIds * 2;
-                       dumpIdMap = (DumpableObject **)
-                               pg_realloc(dumpIdMap, newAlloc * sizeof(DumpableObject *));
+                       dumpIdMap = pg_realloc_array(dumpIdMap, DumpableObject *, newAlloc);
                }
                memset(dumpIdMap + allocedDumpIds, 0,
                           (newAlloc - allocedDumpIds) * sizeof(DumpableObject *));
@@ -700,8 +698,7 @@ getDumpableObjects(DumpableObject ***objs, int *numObjs)
        int                     i,
                                j;
 
-       *objs = (DumpableObject **)
-               pg_malloc(allocedDumpIds * sizeof(DumpableObject *));
+       *objs = pg_malloc_array(DumpableObject *, allocedDumpIds);
        j = 0;
        for (i = 1; i < allocedDumpIds; i++)
        {
@@ -724,15 +721,13 @@ addObjectDependency(DumpableObject *dobj, DumpId refId)
                if (dobj->allocDeps <= 0)
                {
                        dobj->allocDeps = 16;
-                       dobj->dependencies = (DumpId *)
-                               pg_malloc(dobj->allocDeps * sizeof(DumpId));
+                       dobj->dependencies = pg_malloc_array(DumpId, dobj->allocDeps);
                }
                else
                {
                        dobj->allocDeps *= 2;
-                       dobj->dependencies = (DumpId *)
-                               pg_realloc(dobj->dependencies,
-                                                  dobj->allocDeps * sizeof(DumpId));
+                       dobj->dependencies = pg_realloc_array(dobj->dependencies,
+                                                                                                 DumpId, dobj->allocDeps);
                }
        }
        dobj->dependencies[dobj->nDeps++] = refId;
@@ -990,8 +985,7 @@ findParentsByOid(TableInfo *self,
 
        if (numParents > 0)
        {
-               self->parents = (TableInfo **)
-                       pg_malloc(sizeof(TableInfo *) * numParents);
+               self->parents = pg_malloc_array(TableInfo *, numParents);
                j = 0;
                for (i = 0; i < numInherits; i++)
                {
index bfc49b66d2c9897c815812e174da853d4e7dd26c..7960b81c0e6d83ea93f8f1242f1b57a4bc8334d1 100644 (file)
@@ -151,7 +151,7 @@ InitArchiveFmt_Tar(ArchiveHandle *AH)
        /*
         * Set up some special context used in compressing data.
         */
-       ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
+       ctx = pg_malloc0_object(lclContext);
        AH->formatData = (void *) ctx;
        ctx->filePos = 0;
        ctx->isSpecialScript = 0;
@@ -240,7 +240,7 @@ _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
        lclTocEntry *ctx;
        char            fn[K_STD_BUF_SIZE];
 
-       ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
+       ctx = pg_malloc0_object(lclTocEntry);
        if (te->dataDumper != NULL)
        {
                snprintf(fn, sizeof(fn), "%d.dat", te->dumpId);
@@ -272,7 +272,7 @@ _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
 
        if (ctx == NULL)
        {
-               ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
+               ctx = pg_malloc0_object(lclTocEntry);
                te->formatData = (void *) ctx;
        }
 
@@ -337,7 +337,7 @@ tarOpen(ArchiveHandle *AH, const char *filename, char mode)
        {
                int                     old_umask;
 
-               tm = pg_malloc0(sizeof(TAR_MEMBER));
+               tm = pg_malloc0_object(TAR_MEMBER);
 
                /*
                 * POSIX does not require, but permits, tmpfile() to restrict file
@@ -1052,7 +1052,7 @@ static TAR_MEMBER *
 _tarPositionTo(ArchiveHandle *AH, const char *filename)
 {
        lclContext *ctx = (lclContext *) AH->formatData;
-       TAR_MEMBER *th = pg_malloc0(sizeof(TAR_MEMBER));
+       TAR_MEMBER *th = pg_malloc0_object(TAR_MEMBER);
        char            c;
        char            header[TAR_BLOCK_SIZE];
        size_t          i,
index e2e5678e2d2cac9ddd55300458295e3091a7d616..f5b9e268f200c4ce0147e4d0b5374caaffcd89fa 100644 (file)
@@ -247,8 +247,8 @@ main(int argc, char *argv[])
        do
        {
 #define PARAMS_ARRAY_SIZE      8
-               const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
-               const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
+               const char **keywords = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
+               const char **values = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
 
                keywords[0] = "host";
                values[0] = options.host;
@@ -750,7 +750,7 @@ simple_action_list_append(SimpleActionList *list,
 {
        SimpleActionListCell *cell;
 
-       cell = (SimpleActionListCell *) pg_malloc(sizeof(SimpleActionListCell));
+       cell = pg_malloc_object(SimpleActionListCell);
 
        cell->next = NULL;
        cell->action = action;
index aa643b63fe21e8d99ed2dd4b5f74d741e50e4994..891a1505ccfe1ae120124d5582703ee112223690 100644 (file)
@@ -39,7 +39,7 @@ get_configdata(const char *my_exec_path, size_t *configdata_len)
 
        /* Adjust this to match the number of items filled below */
        *configdata_len = 23;
-       configdata = (ConfigData *) palloc(*configdata_len * sizeof(ConfigData));
+       configdata = palloc_array(ConfigData, *configdata_len);
 
        configdata[i].name = pstrdup("BINDIR");
        strlcpy(path, my_exec_path, sizeof(path));
index 4c0da6e124e79997fd45909e024a6c496caa6bbf..66168543a6eb80fbf2520d7f3f8e8dd186b14346 100644 (file)
@@ -59,7 +59,7 @@ get_controlfile(const char *DataDir, bool *crc_ok_p)
 
        AssertArg(crc_ok_p);
 
-       ControlFile = palloc(sizeof(ControlFileData));
+       ControlFile = palloc_object(ControlFileData);
        snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
 
 #ifndef FRONTEND