Use palloc_object() and palloc_array() in more areas of the tree
authorMichael Paquier <michael@paquier.xyz>
Tue, 9 Dec 2025 05:53:17 +0000 (14:53 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 9 Dec 2025 05:53:17 +0000 (14:53 +0900)
The idea is to encourage more the use of these new routines across the
tree, as these offer stronger type safety guarantees than palloc().

The following paths are included in this batch, treating all the areas
proposed by the author for the most trivial changes, except src/backend
(by far the largest batch):
src/bin/
src/common/
src/fe_utils/
src/include/
src/pl/
src/test/
src/tutorial/

Similar work has been done in 31d3847a37be.

The code compiles the same before and after this commit, with the
following exceptions due to changes in line numbers because some of the
new allocation formulas are shorter:
blkreftable.c
pgfnames.c
pl_exec.c

Author: David Geier <geidav.pg@gmail.com>
Discussion: https://postgr.es/m/ad0748d4-3080-436e-b0bc-ac8f86a3466a@gmail.com

43 files changed:
doc/src/sgml/gist.sgml
doc/src/sgml/xfunc.sgml
doc/src/sgml/xtypes.sgml
src/bin/pg_basebackup/astreamer_inject.c
src/bin/pg_combinebackup/load_manifest.c
src/bin/pg_dump/common.c
src/bin/pg_verifybackup/astreamer_verify.c
src/bin/pg_verifybackup/pg_verifybackup.c
src/bin/scripts/vacuuming.c
src/common/blkreftable.c
src/common/parse_manifest.c
src/common/pgfnames.c
src/common/rmtree.c
src/common/stringinfo.c
src/fe_utils/astreamer_file.c
src/fe_utils/astreamer_gzip.c
src/fe_utils/astreamer_lz4.c
src/fe_utils/astreamer_tar.c
src/fe_utils/astreamer_zstd.c
src/include/lib/radixtree.h
src/pl/plperl/plperl.c
src/pl/plpgsql/src/pl_comp.c
src/pl/plpgsql/src/pl_exec.c
src/pl/plpgsql/src/pl_gram.y
src/pl/plpython/plpy_procedure.c
src/pl/plpython/plpy_spi.c
src/pl/plpython/plpy_typeio.c
src/pl/tcl/pltcl.c
src/test/modules/dummy_index_am/dummy_index_am.c
src/test/modules/spgist_name_ops/spgist_name_ops.c
src/test/modules/test_bitmapset/test_bitmapset.c
src/test/modules/test_integerset/test_integerset.c
src/test/modules/test_json_parser/test_json_parser_incremental.c
src/test/modules/test_parser/test_parser.c
src/test/modules/test_radixtree/test_radixtree.c
src/test/modules/test_regex/test_regex.c
src/test/modules/test_resowner/test_resowner_many.c
src/test/modules/test_rls_hooks/test_rls_hooks.c
src/test/modules/worker_spi/worker_spi.c
src/test/regress/regress.c
src/timezone/pgtz.c
src/tutorial/complex.c
src/tutorial/funcs.c

index ee86e1700552095816dae94ec23472e9a48c32b2..5cbea8c3d558cc02e49b635c3677f0cd963b8aba 100644 (file)
@@ -506,11 +506,11 @@ my_compress(PG_FUNCTION_ARGS)
     if (entry-&gt;leafkey)
     {
         /* replace entry-&gt;key with a compressed version */
-        compressed_data_type *compressed_data = palloc(sizeof(compressed_data_type));
+        compressed_data_type *compressed_data = palloc_object(compressed_data_type);
 
         /* fill *compressed_data from entry-&gt;key ... */
 
-        retval = palloc(sizeof(GISTENTRY));
+        retval = palloc_object(GISTENTRY);
         gistentryinit(*retval, PointerGetDatum(compressed_data),
                       entry-&gt;rel, entry-&gt;page, entry-&gt;offset, FALSE);
     }
@@ -921,8 +921,8 @@ my_fetch(PG_FUNCTION_ARGS)
     fetched_data_type *fetched_data;
     GISTENTRY  *retval;
 
-    retval = palloc(sizeof(GISTENTRY));
-    fetched_data = palloc(sizeof(fetched_data_type));
+    retval = palloc_object(GISTENTRY);
+    fetched_data = palloc_object(fetched_data_type);
 
     /*
      * Convert 'fetched_data' into the a Datum of the original datatype.
index 8e936af465c4f831bfd1829d08a830d37d65ea35..7c76ab8c34950e63f50b82b342823380671a3847 100644 (file)
@@ -2491,7 +2491,7 @@ makepoint(PG_FUNCTION_ARGS)
     /* Here, the pass-by-reference nature of Point is not hidden. */
     Point     *pointx = PG_GETARG_POINT_P(0);
     Point     *pointy = PG_GETARG_POINT_P(1);
-    Point     *new_point = (Point *) palloc(sizeof(Point));
+    Point     *new_point = palloc_object(Point);
 
     new_point->x = pointx->x;
     new_point->y = pointy->y;
index e67e5bdf4c4acfdedfb57c2b35dc57e2773a04ae..df56d1c3ace68b3c6a27cee058e1ddc7d08afa08 100644 (file)
@@ -89,7 +89,7 @@ complex_in(PG_FUNCTION_ARGS)
                  errmsg("invalid input syntax for type %s: \"%s\"",
                         "complex", str)));
 
-    result = (Complex *) palloc(sizeof(Complex));
+    result = palloc_object(Complex);
     result->x = x;
     result->y = y;
     PG_RETURN_POINTER(result);
index 15334e458ad1e581be62d9a828659e8458f539a3..e77de72f7ac14a6efe604682b1bc7431d08746dc 100644 (file)
@@ -68,7 +68,7 @@ astreamer_recovery_injector_new(astreamer *next,
 {
    astreamer_recovery_injector *streamer;
 
-   streamer = palloc0(sizeof(astreamer_recovery_injector));
+   streamer = palloc0_object(astreamer_recovery_injector);
    *((const astreamer_ops **) &streamer->base.bbs_ops) =
        &astreamer_recovery_injector_ops;
    streamer->base.bbs_next = next;
index 8e0d04a26a6a73d5b14e773f7260b3711dca8a16..44db0d2b1645722b151565ada0ca29e6443ce785 100644 (file)
@@ -298,7 +298,7 @@ combinebackup_per_wal_range_cb(JsonManifestParseContext *context,
    manifest_wal_range *range;
 
    /* Allocate and initialize a struct describing this WAL range. */
-   range = palloc(sizeof(manifest_wal_range));
+   range = palloc_object(manifest_wal_range);
    range->tli = tli;
    range->start_lsn = start_lsn;
    range->end_lsn = end_lsn;
index 4e7303ea6317abc9a797f94614061fba12e3f732..0007e78667f26b3c580e23d6b4b04c76a29a28b6 100644 (file)
@@ -353,7 +353,7 @@ flagInhTables(Archive *fout, TableInfo *tblinfo, int numTables,
                         tblinfo[i].numParents,
                         tblinfo[i].dobj.name);
 
-           attachinfo = (TableAttachInfo *) palloc(sizeof(TableAttachInfo));
+           attachinfo = palloc_object(TableAttachInfo);
            attachinfo->dobj.objType = DO_TABLE_ATTACH;
            attachinfo->dobj.catId.tableoid = 0;
            attachinfo->dobj.catId.oid = 0;
index 33cf67670a70d7557473cb605aa1daa01749da51..e503a3ec487c21386568753c3c151180c9921e45 100644 (file)
@@ -69,7 +69,7 @@ astreamer_verify_content_new(astreamer *next, verifier_context *context,
 {
    astreamer_verify *streamer;
 
-   streamer = palloc0(sizeof(astreamer_verify));
+   streamer = palloc0_object(astreamer_verify);
    *((const astreamer_ops **) &streamer->base.bbs_ops) =
        &astreamer_verify_ops;
 
index 8d5befa947f5090acc12b192fcfb15de2ecc5a38..c9b24df7c053f45c66fc9c443b2c37bb0f9990b0 100644 (file)
@@ -584,7 +584,7 @@ verifybackup_per_wal_range_cb(JsonManifestParseContext *context,
    manifest_wal_range *range;
 
    /* Allocate and initialize a struct describing this WAL range. */
-   range = palloc(sizeof(manifest_wal_range));
+   range = palloc_object(manifest_wal_range);
    range->tli = tli;
    range->start_lsn = start_lsn;
    range->end_lsn = end_lsn;
index f836f21fb03134d7f9e33d753eb06d88ba7ef73f..9f44cae02ae7afe0f522b1f2e07f503077e397b4 100644 (file)
@@ -530,7 +530,7 @@ retrieve_objects(PGconn *conn, vacuumingOptions *vacopts,
    PQExpBufferData catalog_query;
    PGresult   *res;
    SimpleStringListCell *cell;
-   SimpleStringList *found_objs = palloc0(sizeof(SimpleStringList));
+   SimpleStringList *found_objs = palloc0_object(SimpleStringList);
    bool        objects_listed = false;
 
    initPQExpBuffer(&catalog_query);
index b935baf9ad4bb754f714132a8f79a8bae9089bba..0e08f4e40edfe324d3f8fa8bb93e8daf90954681 100644 (file)
@@ -234,7 +234,7 @@ static void BlockRefTableFileTerminate(BlockRefTableBuffer *buffer);
 BlockRefTable *
 CreateEmptyBlockRefTable(void)
 {
-   BlockRefTable *brtab = palloc(sizeof(BlockRefTable));
+   BlockRefTable *brtab = palloc_object(BlockRefTable);
 
    /*
     * Even completely empty database has a few hundred relation forks, so it
@@ -497,7 +497,7 @@ WriteBlockRefTable(BlockRefTable *brtab,
 
        /* Extract entries into serializable format and sort them. */
        sdata =
-           palloc(brtab->hash->members * sizeof(BlockRefTableSerializedEntry));
+           palloc_array(BlockRefTableSerializedEntry, brtab->hash->members);
        blockreftable_start_iterate(brtab->hash, &it);
        while ((brtentry = blockreftable_iterate(brtab->hash, &it)) != NULL)
        {
@@ -584,7 +584,7 @@ CreateBlockRefTableReader(io_callback_fn read_callback,
    uint32      magic;
 
    /* Initialize data structure. */
-   reader = palloc0(sizeof(BlockRefTableReader));
+   reader = palloc0_object(BlockRefTableReader);
    reader->buffer.io_callback = read_callback;
    reader->buffer.io_callback_arg = read_callback_arg;
    reader->error_filename = error_filename;
@@ -660,7 +660,7 @@ BlockRefTableReaderNextRelation(BlockRefTableReader *reader,
    /* Read chunk size array. */
    if (reader->chunk_size != NULL)
        pfree(reader->chunk_size);
-   reader->chunk_size = palloc(sentry.nchunks * sizeof(uint16));
+   reader->chunk_size = palloc_array(uint16, sentry.nchunks);
    BlockRefTableRead(reader, reader->chunk_size,
                      sentry.nchunks * sizeof(uint16));
 
@@ -794,7 +794,7 @@ CreateBlockRefTableWriter(io_callback_fn write_callback,
    uint32      magic = BLOCKREFTABLE_MAGIC;
 
    /* Prepare buffer and CRC check and save callbacks. */
-   writer = palloc0(sizeof(BlockRefTableWriter));
+   writer = palloc0_object(BlockRefTableWriter);
    writer->buffer.io_callback = write_callback;
    writer->buffer.io_callback_arg = write_callback_arg;
    INIT_CRC32C(writer->buffer.crc);
@@ -874,7 +874,7 @@ DestroyBlockRefTableWriter(BlockRefTableWriter *writer)
 BlockRefTableEntry *
 CreateBlockRefTableEntry(RelFileLocator rlocator, ForkNumber forknum)
 {
-   BlockRefTableEntry *entry = palloc0(sizeof(BlockRefTableEntry));
+   BlockRefTableEntry *entry = palloc0_object(BlockRefTableEntry);
 
    memcpy(&entry->key.rlocator, &rlocator, sizeof(RelFileLocator));
    entry->key.forknum = forknum;
@@ -997,10 +997,9 @@ BlockRefTableEntryMarkBlockModified(BlockRefTableEntry *entry,
 
        if (entry->nchunks == 0)
        {
-           entry->chunk_size = palloc0(sizeof(uint16) * max_chunks);
-           entry->chunk_usage = palloc0(sizeof(uint16) * max_chunks);
-           entry->chunk_data =
-               palloc0(sizeof(BlockRefTableChunk) * max_chunks);
+           entry->chunk_size = palloc0_array(uint16, max_chunks);
+           entry->chunk_usage = palloc0_array(uint16, max_chunks);
+           entry->chunk_data = palloc0_array(BlockRefTableChunk, max_chunks);
        }
        else
        {
@@ -1029,7 +1028,7 @@ BlockRefTableEntryMarkBlockModified(BlockRefTableEntry *entry,
    if (entry->chunk_size[chunkno] == 0)
    {
        entry->chunk_data[chunkno] =
-           palloc(sizeof(uint16) * INITIAL_ENTRIES_PER_CHUNK);
+           palloc_array(uint16, INITIAL_ENTRIES_PER_CHUNK);
        entry->chunk_size[chunkno] = INITIAL_ENTRIES_PER_CHUNK;
        entry->chunk_data[chunkno][0] = chunkoffset;
        entry->chunk_usage[chunkno] = 1;
index 58e0948100f4b6f32ac451bc0154575dfac1f92f..cc5fa0e5e0773d538268597e1044604c6a267fe6 100644 (file)
@@ -132,8 +132,8 @@ json_parse_manifest_incremental_init(JsonManifestParseContext *context)
    JsonManifestParseState *parse;
    pg_cryptohash_ctx *manifest_ctx;
 
-   incstate = palloc(sizeof(JsonManifestParseIncrementalState));
-   parse = palloc(sizeof(JsonManifestParseState));
+   incstate = palloc_object(JsonManifestParseIncrementalState);
+   parse = palloc_object(JsonManifestParseState);
 
    parse->context = context;
    parse->state = JM_EXPECT_TOPLEVEL_START;
index 8fb79105714b6839cee2f64589d116e2e6f669b8..591535a24b8dee02cfe48718c40b4ac112281708 100644 (file)
@@ -49,7 +49,7 @@ pgfnames(const char *path)
        return NULL;
    }
 
-   filenames = (char **) palloc(fnsize * sizeof(char *));
+   filenames = palloc_array(char *, fnsize);
 
    while (errno = 0, (file = readdir(dir)) != NULL)
    {
@@ -58,8 +58,7 @@ pgfnames(const char *path)
            if (numnames + 1 >= fnsize)
            {
                fnsize *= 2;
-               filenames = (char **) repalloc(filenames,
-                                              fnsize * sizeof(char *));
+               filenames = repalloc_array(filenames, char *, fnsize);
            }
            filenames[numnames++] = pstrdup(file->d_name);
        }
index 2f364f84ae5b2d64e710aaf0f5f6a9e2ffdbb081..47cd0a4d8a10898992f98c0e0a626c3caf9749da 100644 (file)
@@ -64,7 +64,7 @@ rmtree(const char *path, bool rmtopdir)
        return false;
    }
 
-   dirnames = (char **) palloc(sizeof(char *) * dirnames_capacity);
+   dirnames = palloc_array(char *, dirnames_capacity);
 
    while (errno = 0, (de = readdir(dir)))
    {
index 22d03807697ece9f581857ac2fb603d1297d3714..a3e77088f8ebd203abaa4573555516de680255d3 100644 (file)
@@ -57,7 +57,7 @@ initStringInfoInternal(StringInfo str, int initsize)
 static inline StringInfo
 makeStringInfoInternal(int initsize)
 {
-   StringInfo  res = (StringInfo) palloc(sizeof(StringInfoData));
+   StringInfo  res = palloc_object(StringInfoData);
 
    initStringInfoInternal(res, initsize);
    return res;
index c685628508636365b4fe59d0d58e5f196d79ce47..940f0f5a1a6a1505bf70525eb8f0a86b8e22474e 100644 (file)
@@ -82,7 +82,7 @@ astreamer_plain_writer_new(char *pathname, FILE *file)
 {
    astreamer_plain_writer *streamer;
 
-   streamer = palloc0(sizeof(astreamer_plain_writer));
+   streamer = palloc0_object(astreamer_plain_writer);
    *((const astreamer_ops **) &streamer->base.bbs_ops) =
        &astreamer_plain_writer_ops;
 
@@ -189,7 +189,7 @@ astreamer_extractor_new(const char *basepath,
 {
    astreamer_extractor *streamer;
 
-   streamer = palloc0(sizeof(astreamer_extractor));
+   streamer = palloc0_object(astreamer_extractor);
    *((const astreamer_ops **) &streamer->base.bbs_ops) =
        &astreamer_extractor_ops;
    streamer->basepath = pstrdup(basepath);
index a395f57edcd70f60f5d4c691737217648fd20df9..06e2670d3638c9b0ad12ee9e437309c113b86cba 100644 (file)
@@ -102,7 +102,7 @@ astreamer_gzip_writer_new(char *pathname, FILE *file,
 #ifdef HAVE_LIBZ
    astreamer_gzip_writer *streamer;
 
-   streamer = palloc0(sizeof(astreamer_gzip_writer));
+   streamer = palloc0_object(astreamer_gzip_writer);
    *((const astreamer_ops **) &streamer->base.bbs_ops) =
        &astreamer_gzip_writer_ops;
 
@@ -241,7 +241,7 @@ astreamer_gzip_decompressor_new(astreamer *next)
 
    Assert(next != NULL);
 
-   streamer = palloc0(sizeof(astreamer_gzip_decompressor));
+   streamer = palloc0_object(astreamer_gzip_decompressor);
    *((const astreamer_ops **) &streamer->base.bbs_ops) =
        &astreamer_gzip_decompressor_ops;
 
index 5f581d1de3769bdad98a39f2726c80cd0b8619cc..6aead209fd294fe87fb5c3a51e04b0222aae69ec 100644 (file)
@@ -78,7 +78,7 @@ astreamer_lz4_compressor_new(astreamer *next, pg_compress_specification *compres
 
    Assert(next != NULL);
 
-   streamer = palloc0(sizeof(astreamer_lz4_frame));
+   streamer = palloc0_object(astreamer_lz4_frame);
    *((const astreamer_ops **) &streamer->base.bbs_ops) =
        &astreamer_lz4_compressor_ops;
 
@@ -282,7 +282,7 @@ astreamer_lz4_decompressor_new(astreamer *next)
 
    Assert(next != NULL);
 
-   streamer = palloc0(sizeof(astreamer_lz4_frame));
+   streamer = palloc0_object(astreamer_lz4_frame);
    *((const astreamer_ops **) &streamer->base.bbs_ops) =
        &astreamer_lz4_decompressor_ops;
 
index 088e2357920c539d38818eca389928562c4af763..896f8ab4970e972ae01b60d6e38af4cfa9543a55 100644 (file)
@@ -94,7 +94,7 @@ astreamer_tar_parser_new(astreamer *next)
 {
    astreamer_tar_parser *streamer;
 
-   streamer = palloc0(sizeof(astreamer_tar_parser));
+   streamer = palloc0_object(astreamer_tar_parser);
    *((const astreamer_ops **) &streamer->base.bbs_ops) =
        &astreamer_tar_parser_ops;
    streamer->base.bbs_next = next;
@@ -357,7 +357,7 @@ astreamer_tar_archiver_new(astreamer *next)
 {
    astreamer_tar_archiver *streamer;
 
-   streamer = palloc0(sizeof(astreamer_tar_archiver));
+   streamer = palloc0_object(astreamer_tar_archiver);
    *((const astreamer_ops **) &streamer->base.bbs_ops) =
        &astreamer_tar_archiver_ops;
    streamer->base.bbs_next = next;
@@ -463,7 +463,7 @@ astreamer_tar_terminator_new(astreamer *next)
 {
    astreamer  *streamer;
 
-   streamer = palloc0(sizeof(astreamer));
+   streamer = palloc0_object(astreamer);
    *((const astreamer_ops **) &streamer->bbs_ops) =
        &astreamer_tar_terminator_ops;
    streamer->bbs_next = next;
index bacdcc150c40f44a87732e3a949cd020decd100f..6666f1abeb372b5a9c6d2b7e75e966531608afb3 100644 (file)
@@ -75,7 +75,7 @@ astreamer_zstd_compressor_new(astreamer *next, pg_compress_specification *compre
 
    Assert(next != NULL);
 
-   streamer = palloc0(sizeof(astreamer_zstd_frame));
+   streamer = palloc0_object(astreamer_zstd_frame);
 
    *((const astreamer_ops **) &streamer->base.bbs_ops) =
        &astreamer_zstd_compressor_ops;
@@ -266,7 +266,7 @@ astreamer_zstd_decompressor_new(astreamer *next)
 
    Assert(next != NULL);
 
-   streamer = palloc0(sizeof(astreamer_zstd_frame));
+   streamer = palloc0_object(astreamer_zstd_frame);
    *((const astreamer_ops **) &streamer->base.bbs_ops) =
        &astreamer_zstd_decompressor_ops;
 
index ead7500fe13dde3fe628c2e9964fc52dec961892..adf81c6f0ebf91a1296046a3bb70620e3f929e68 100644 (file)
@@ -1825,7 +1825,7 @@ RT_CREATE(MemoryContext ctx)
    dsa_pointer dp;
 #endif
 
-   tree = (RT_RADIX_TREE *) palloc0(sizeof(RT_RADIX_TREE));
+   tree = palloc0_object(RT_RADIX_TREE);
 
 #ifdef RT_SHMEM
    tree->dsa = dsa;
@@ -1835,7 +1835,7 @@ RT_CREATE(MemoryContext ctx)
    tree->ctl->magic = RT_RADIX_TREE_MAGIC;
    LWLockInitialize(&tree->ctl->lock, tranche_id);
 #else
-   tree->ctl = (RT_RADIX_TREE_CONTROL *) palloc0(sizeof(RT_RADIX_TREE_CONTROL));
+   tree->ctl = palloc0_object(RT_RADIX_TREE_CONTROL);
 
    /* Create a slab context for each size class */
    for (int i = 0; i < RT_NUM_SIZE_CLASSES; i++)
@@ -1868,7 +1868,7 @@ RT_ATTACH(dsa_area *dsa, RT_HANDLE handle)
    RT_RADIX_TREE *tree;
    dsa_pointer control;
 
-   tree = (RT_RADIX_TREE *) palloc0(sizeof(RT_RADIX_TREE));
+   tree = palloc0_object(RT_RADIX_TREE);
 
    /* Find the control object in shared memory */
    control = handle;
@@ -2057,7 +2057,7 @@ RT_BEGIN_ITERATE(RT_RADIX_TREE * tree)
    RT_ITER    *iter;
    RT_CHILD_PTR root;
 
-   iter = (RT_ITER *) palloc0(sizeof(RT_ITER));
+   iter = palloc0_object(RT_ITER);
    iter->tree = tree;
 
    Assert(RT_PTR_ALLOC_IS_VALID(tree->ctl->root));
index 73ba1748fe0a828b51a7ccd8c0e01ef6128587e6..02eced3b2c532200a4243014e4613242d9558019 100644 (file)
@@ -1082,8 +1082,8 @@ plperl_build_tuple_result(HV *perlhash, TupleDesc td)
    HE         *he;
    HeapTuple   tup;
 
-   values = palloc0(sizeof(Datum) * td->natts);
-   nulls = palloc(sizeof(bool) * td->natts);
+   values = palloc0_array(Datum, td->natts);
+   nulls = palloc_array(bool, td->natts);
    memset(nulls, true, sizeof(bool) * td->natts);
 
    hv_iterinit(perlhash);
@@ -1502,7 +1502,7 @@ plperl_ref_from_pg_array(Datum arg, Oid typid)
     * Currently we make no effort to cache any of the stuff we look up here,
     * which is bad.
     */
-   info = palloc0(sizeof(plperl_array_info));
+   info = palloc0_object(plperl_array_info);
 
    /* get element type information, including output conversion function */
    get_type_io_data(elementtype, IOFunc_output,
@@ -1538,7 +1538,7 @@ plperl_ref_from_pg_array(Datum arg, Oid typid)
                          &nitems);
 
        /* Get total number of elements in each dimension */
-       info->nelems = palloc(sizeof(int) * info->ndims);
+       info->nelems = palloc_array(int, info->ndims);
        info->nelems[0] = nitems;
        for (i = 1; i < info->ndims; i++)
            info->nelems[i] = info->nelems[i - 1] / dims[i - 1];
@@ -2797,7 +2797,7 @@ compile_plperl_function(Oid fn_oid, bool is_trigger, bool is_event_trigger)
         * struct prodesc and subsidiary data must all live in proc_cxt.
         ************************************************************/
        oldcontext = MemoryContextSwitchTo(proc_cxt);
-       prodesc = (plperl_proc_desc *) palloc0(sizeof(plperl_proc_desc));
+       prodesc = palloc0_object(plperl_proc_desc);
        prodesc->proname = pstrdup(NameStr(procStruct->proname));
        MemoryContextSetIdentifier(proc_cxt, prodesc->proname);
        prodesc->fn_cxt = proc_cxt;
@@ -3596,7 +3596,7 @@ plperl_spi_prepare(char *query, int argc, SV **argv)
                                         "PL/Perl spi_prepare query",
                                         ALLOCSET_SMALL_SIZES);
        MemoryContextSwitchTo(plan_cxt);
-       qdesc = (plperl_query_desc *) palloc0(sizeof(plperl_query_desc));
+       qdesc = palloc0_object(plperl_query_desc);
        snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
        qdesc->plan_cxt = plan_cxt;
        qdesc->nargs = argc;
index f6976689a69279d19e1d9a9f713d4793f95ddadb..4d90a0c2f063a4cb2fb0b0fe2dc5d3ab8a467657 100644 (file)
@@ -298,8 +298,8 @@ plpgsql_compile_callback(FunctionCallInfo fcinfo,
                                               forValidator,
                                               plpgsql_error_funcname);
 
-           in_arg_varnos = (int *) palloc(numargs * sizeof(int));
-           out_arg_variables = (PLpgSQL_variable **) palloc(numargs * sizeof(PLpgSQL_variable *));
+           in_arg_varnos = palloc_array(int, numargs);
+           out_arg_variables = palloc_array(PLpgSQL_variable *, numargs);
 
            MemoryContextSwitchTo(func_cxt);
 
@@ -772,7 +772,7 @@ plpgsql_compile_inline(char *proc_source)
    plpgsql_check_syntax = check_function_bodies;
 
    /* Function struct does not live past current statement */
-   function = (PLpgSQL_function *) palloc0(sizeof(PLpgSQL_function));
+   function = palloc0_object(PLpgSQL_function);
 
    plpgsql_curr_compile = function;
 
@@ -954,7 +954,7 @@ add_dummy_return(PLpgSQL_function *function)
    {
        PLpgSQL_stmt_block *new;
 
-       new = palloc0(sizeof(PLpgSQL_stmt_block));
+       new = palloc0_object(PLpgSQL_stmt_block);
        new->cmd_type = PLPGSQL_STMT_BLOCK;
        new->stmtid = ++function->nstatements;
        new->body = list_make1(function->action);
@@ -966,7 +966,7 @@ add_dummy_return(PLpgSQL_function *function)
    {
        PLpgSQL_stmt_return *new;
 
-       new = palloc0(sizeof(PLpgSQL_stmt_return));
+       new = palloc0_object(PLpgSQL_stmt_return);
        new->cmd_type = PLPGSQL_STMT_RETURN;
        new->stmtid = ++function->nstatements;
        new->expr = NULL;
@@ -1776,7 +1776,7 @@ plpgsql_build_variable(const char *refname, int lineno, PLpgSQL_type *dtype,
                /* Ordinary scalar datatype */
                PLpgSQL_var *var;
 
-               var = palloc0(sizeof(PLpgSQL_var));
+               var = palloc0_object(PLpgSQL_var);
                var->dtype = PLPGSQL_DTYPE_VAR;
                var->refname = pstrdup(refname);
                var->lineno = lineno;
@@ -1833,7 +1833,7 @@ plpgsql_build_record(const char *refname, int lineno,
 {
    PLpgSQL_rec *rec;
 
-   rec = palloc0(sizeof(PLpgSQL_rec));
+   rec = palloc0_object(PLpgSQL_rec);
    rec->dtype = PLPGSQL_DTYPE_REC;
    rec->refname = pstrdup(refname);
    rec->lineno = lineno;
@@ -1859,14 +1859,14 @@ build_row_from_vars(PLpgSQL_variable **vars, int numvars)
    PLpgSQL_row *row;
    int         i;
 
-   row = palloc0(sizeof(PLpgSQL_row));
+   row = palloc0_object(PLpgSQL_row);
    row->dtype = PLPGSQL_DTYPE_ROW;
    row->refname = "(unnamed row)";
    row->lineno = -1;
    row->rowtupdesc = CreateTemplateTupleDesc(numvars);
    row->nfields = numvars;
-   row->fieldnames = palloc(numvars * sizeof(char *));
-   row->varnos = palloc(numvars * sizeof(int));
+   row->fieldnames = palloc_array(char *, numvars);
+   row->varnos = palloc_array(int, numvars);
 
    for (i = 0; i < numvars; i++)
    {
@@ -1940,7 +1940,7 @@ plpgsql_build_recfield(PLpgSQL_rec *rec, const char *fldname)
    }
 
    /* nope, so make a new one */
-   recfield = palloc0(sizeof(PLpgSQL_recfield));
+   recfield = palloc0_object(PLpgSQL_recfield);
    recfield->dtype = PLPGSQL_DTYPE_RECFIELD;
    recfield->fieldname = pstrdup(fldname);
    recfield->recparentno = rec->dno;
@@ -2004,7 +2004,7 @@ build_datatype(HeapTuple typeTup, int32 typmod,
                 errmsg("type \"%s\" is only a shell",
                        NameStr(typeStruct->typname))));
 
-   typ = (PLpgSQL_type *) palloc(sizeof(PLpgSQL_type));
+   typ = palloc_object(PLpgSQL_type);
 
    typ->typname = pstrdup(NameStr(typeStruct->typname));
    typ->typoid = typeStruct->oid;
@@ -2184,7 +2184,7 @@ plpgsql_parse_err_condition(char *condname)
 
    if (strcmp(condname, "others") == 0)
    {
-       new = palloc(sizeof(PLpgSQL_condition));
+       new = palloc_object(PLpgSQL_condition);
        new->sqlerrstate = PLPGSQL_OTHERS;
        new->condname = condname;
        new->next = NULL;
@@ -2196,7 +2196,7 @@ plpgsql_parse_err_condition(char *condname)
    {
        if (strcmp(condname, exception_label_map[i].label) == 0)
        {
-           new = palloc(sizeof(PLpgSQL_condition));
+           new = palloc_object(PLpgSQL_condition);
            new->sqlerrstate = exception_label_map[i].sqlerrstate;
            new->condname = condname;
            new->next = prev;
@@ -2258,7 +2258,7 @@ plpgsql_finish_datums(PLpgSQL_function *function)
    int         i;
 
    function->ndatums = plpgsql_nDatums;
-   function->datums = palloc(sizeof(PLpgSQL_datum *) * plpgsql_nDatums);
+   function->datums = palloc_array(PLpgSQL_datum *, plpgsql_nDatums);
    for (i = 0; i < plpgsql_nDatums; i++)
    {
        function->datums[i] = plpgsql_Datums[i];
@@ -2323,7 +2323,7 @@ plpgsql_add_initdatums(int **varnos)
    {
        if (n > 0)
        {
-           *varnos = (int *) palloc(sizeof(int) * n);
+           *varnos = palloc_array(int, n);
 
            n = 0;
            for (i = datums_last; i < plpgsql_nDatums; i++)
index d19425b7a71aeb959910950b1dd8fdf2d43fabb8..63598aba8a8047548b9e58d43fdc06046b1f7aef 100644 (file)
@@ -1318,8 +1318,7 @@ copy_plpgsql_datums(PLpgSQL_execstate *estate,
    int         i;
 
    /* Allocate local datum-pointer array */
-   estate->datums = (PLpgSQL_datum **)
-       palloc(sizeof(PLpgSQL_datum *) * ndatums);
+   estate->datums = palloc_array(PLpgSQL_datum *, ndatums);
 
    /*
     * To reduce palloc overhead, we make a single palloc request for all the
@@ -1497,7 +1496,7 @@ plpgsql_fulfill_promise(PLpgSQL_execstate *estate,
                int         lbs[1];
                int         i;
 
-               elems = palloc(sizeof(Datum) * nelems);
+               elems = palloc_array(Datum, nelems);
                for (i = 0; i < nelems; i++)
                    elems[i] = CStringGetTextDatum(estate->trigdata->tg_trigger->tgargs[i]);
                dims[0] = nelems;
@@ -2340,11 +2339,11 @@ make_callstmt_target(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
     */
    MemoryContextSwitchTo(estate->func->fn_cxt);
 
-   row = (PLpgSQL_row *) palloc0(sizeof(PLpgSQL_row));
+   row = palloc0_object(PLpgSQL_row);
    row->dtype = PLPGSQL_DTYPE_ROW;
    row->refname = "(unnamed row)";
    row->lineno = -1;
-   row->varnos = (int *) palloc(numargs * sizeof(int));
+   row->varnos = palloc_array(int, numargs);
 
    MemoryContextSwitchTo(get_eval_mcontext(estate));
 
index 17568d82554d21146573820ce0170b8201cb48a2..cd1fe5a509ff3d205aea70fae98c9a71ee0fa219 100644 (file)
@@ -426,7 +426,7 @@ pl_block        : decl_sect K_BEGIN proc_sect exception_sect K_END opt_label
                    {
                        PLpgSQL_stmt_block *new;
 
-                       new = palloc0(sizeof(PLpgSQL_stmt_block));
+                       new = palloc0_object(PLpgSQL_stmt_block);
 
                        new->cmd_type   = PLPGSQL_STMT_BLOCK;
                        new->lineno     = plpgsql_location_to_lineno(@2, yyscanner);
@@ -606,14 +606,14 @@ decl_cursor_args :
                        int         i;
                        ListCell   *l;
 
-                       new = palloc0(sizeof(PLpgSQL_row));
+                       new = palloc0_object(PLpgSQL_row);
                        new->dtype = PLPGSQL_DTYPE_ROW;
                        new->refname = "(unnamed row)";
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->rowtupdesc = NULL;
                        new->nfields = list_length($2);
-                       new->fieldnames = palloc(new->nfields * sizeof(char *));
-                       new->varnos = palloc(new->nfields * sizeof(int));
+                       new->fieldnames = palloc_array(char *, new->nfields);
+                       new->varnos = palloc_array(int, new->nfields);
 
                        i = 0;
                        foreach (l, $2)
@@ -898,7 +898,7 @@ stmt_perform    : K_PERFORM
                        PLpgSQL_stmt_perform *new;
                        int         startloc;
 
-                       new = palloc0(sizeof(PLpgSQL_stmt_perform));
+                       new = palloc0_object(PLpgSQL_stmt_perform);
                        new->cmd_type = PLPGSQL_STMT_PERFORM;
                        new->lineno   = plpgsql_location_to_lineno(@1, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -934,7 +934,7 @@ stmt_call       : K_CALL
                    {
                        PLpgSQL_stmt_call *new;
 
-                       new = palloc0(sizeof(PLpgSQL_stmt_call));
+                       new = palloc0_object(PLpgSQL_stmt_call);
                        new->cmd_type = PLPGSQL_STMT_CALL;
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -953,7 +953,7 @@ stmt_call       : K_CALL
                        /* use the same structures as for CALL, for simplicity */
                        PLpgSQL_stmt_call *new;
 
-                       new = palloc0(sizeof(PLpgSQL_stmt_call));
+                       new = palloc0_object(PLpgSQL_stmt_call);
                        new->cmd_type = PLPGSQL_STMT_CALL;
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -992,7 +992,7 @@ stmt_assign     : T_DATUM
                        }
 
                        check_assignable($1.datum, @1, yyscanner);
-                       new = palloc0(sizeof(PLpgSQL_stmt_assign));
+                       new = palloc0_object(PLpgSQL_stmt_assign);
                        new->cmd_type = PLPGSQL_STMT_ASSIGN;
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -1015,7 +1015,7 @@ stmt_getdiag  : K_GET getdiag_area_opt K_DIAGNOSTICS getdiag_list ';'
                        PLpgSQL_stmt_getdiag *new;
                        ListCell       *lc;
 
-                       new = palloc0(sizeof(PLpgSQL_stmt_getdiag));
+                       new = palloc0_object(PLpgSQL_stmt_getdiag);
                        new->cmd_type = PLPGSQL_STMT_GETDIAG;
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -1101,7 +1101,7 @@ getdiag_list_item : getdiag_target assign_operator getdiag_item
                    {
                        PLpgSQL_diag_item *new;
 
-                       new = palloc(sizeof(PLpgSQL_diag_item));
+                       new = palloc_object(PLpgSQL_diag_item);
                        new->target = $1->dno;
                        new->kind = $3;
 
@@ -1191,7 +1191,7 @@ stmt_if           : K_IF expr_until_then proc_sect stmt_elsifs stmt_else K_END K_IF ';'
                    {
                        PLpgSQL_stmt_if *new;
 
-                       new = palloc0(sizeof(PLpgSQL_stmt_if));
+                       new = palloc0_object(PLpgSQL_stmt_if);
                        new->cmd_type = PLPGSQL_STMT_IF;
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -1212,7 +1212,7 @@ stmt_elsifs       :
                    {
                        PLpgSQL_if_elsif *new;
 
-                       new = palloc0(sizeof(PLpgSQL_if_elsif));
+                       new = palloc0_object(PLpgSQL_if_elsif);
                        new->lineno = plpgsql_location_to_lineno(@2, yyscanner);
                        new->cond = $3;
                        new->stmts = $4;
@@ -1264,7 +1264,7 @@ case_when_list    : case_when_list case_when
 
 case_when      : K_WHEN expr_until_then proc_sect
                    {
-                       PLpgSQL_case_when *new = palloc(sizeof(PLpgSQL_case_when));
+                       PLpgSQL_case_when *new = palloc_object(PLpgSQL_case_when);
 
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->expr = $2;
@@ -1296,7 +1296,7 @@ stmt_loop     : opt_loop_label K_LOOP loop_body
                    {
                        PLpgSQL_stmt_loop *new;
 
-                       new = palloc0(sizeof(PLpgSQL_stmt_loop));
+                       new = palloc0_object(PLpgSQL_stmt_loop);
                        new->cmd_type = PLPGSQL_STMT_LOOP;
                        new->lineno = plpgsql_location_to_lineno(@2, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -1314,7 +1314,7 @@ stmt_while        : opt_loop_label K_WHILE expr_until_loop loop_body
                    {
                        PLpgSQL_stmt_while *new;
 
-                       new = palloc0(sizeof(PLpgSQL_stmt_while));
+                       new = palloc0_object(PLpgSQL_stmt_while);
                        new->cmd_type = PLPGSQL_STMT_WHILE;
                        new->lineno = plpgsql_location_to_lineno(@2, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -1380,7 +1380,7 @@ for_control       : for_variable K_IN
                                                        "LOOP or USING",
                                                        &term, &yylval, &yylloc, yyscanner);
 
-                           new = palloc0(sizeof(PLpgSQL_stmt_dynfors));
+                           new = palloc0_object(PLpgSQL_stmt_dynfors);
                            new->cmd_type = PLPGSQL_STMT_DYNFORS;
                            new->stmtid = ++plpgsql_curr_compile->nstatements;
                            if ($1.row)
@@ -1426,7 +1426,7 @@ for_control       : for_variable K_IN
                            PLpgSQL_stmt_forc *new;
                            PLpgSQL_var *cursor = (PLpgSQL_var *) yylval.wdatum.datum;
 
-                           new = (PLpgSQL_stmt_forc *) palloc0(sizeof(PLpgSQL_stmt_forc));
+                           new = palloc0_object(PLpgSQL_stmt_forc);
                            new->cmd_type = PLPGSQL_STMT_FORC;
                            new->stmtid = ++plpgsql_curr_compile->nstatements;
                            new->curvar = cursor->dno;
@@ -1545,7 +1545,7 @@ for_control       : for_variable K_IN
                                                                                  NULL),
                                                           true);
 
-                               new = palloc0(sizeof(PLpgSQL_stmt_fori));
+                               new = palloc0_object(PLpgSQL_stmt_fori);
                                new->cmd_type = PLPGSQL_STMT_FORI;
                                new->stmtid = ++plpgsql_curr_compile->nstatements;
                                new->var = fvar;
@@ -1573,7 +1573,7 @@ for_control       : for_variable K_IN
                                check_sql_expr(expr1->query, expr1->parseMode,
                                               expr1loc, yyscanner);
 
-                               new = palloc0(sizeof(PLpgSQL_stmt_fors));
+                               new = palloc0_object(PLpgSQL_stmt_fors);
                                new->cmd_type = PLPGSQL_STMT_FORS;
                                new->stmtid = ++plpgsql_curr_compile->nstatements;
                                if ($1.row)
@@ -1675,7 +1675,7 @@ stmt_foreach_a    : opt_loop_label K_FOREACH for_variable foreach_slice K_IN K_ARRA
                    {
                        PLpgSQL_stmt_foreach_a *new;
 
-                       new = palloc0(sizeof(PLpgSQL_stmt_foreach_a));
+                       new = palloc0_object(PLpgSQL_stmt_foreach_a);
                        new->cmd_type = PLPGSQL_STMT_FOREACH_A;
                        new->lineno = plpgsql_location_to_lineno(@2, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -1723,7 +1723,7 @@ stmt_exit     : exit_type opt_label opt_exitcond
                    {
                        PLpgSQL_stmt_exit *new;
 
-                       new = palloc0(sizeof(PLpgSQL_stmt_exit));
+                       new = palloc0_object(PLpgSQL_stmt_exit);
                        new->cmd_type = PLPGSQL_STMT_EXIT;
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
                        new->is_exit = $1;
@@ -1813,7 +1813,7 @@ stmt_raise        : K_RAISE
                        PLpgSQL_stmt_raise *new;
                        int         tok;
 
-                       new = palloc(sizeof(PLpgSQL_stmt_raise));
+                       new = palloc_object(PLpgSQL_stmt_raise);
 
                        new->cmd_type = PLPGSQL_STMT_RAISE;
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
@@ -1959,7 +1959,7 @@ stmt_assert       : K_ASSERT
                        PLpgSQL_stmt_assert *new;
                        int         tok;
 
-                       new = palloc(sizeof(PLpgSQL_stmt_assert));
+                       new = palloc_object(PLpgSQL_stmt_assert);
 
                        new->cmd_type = PLPGSQL_STMT_ASSERT;
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
@@ -2045,7 +2045,7 @@ stmt_dynexecute : K_EXECUTE
                                                  NULL, &endtoken,
                                                  &yylval, &yylloc, yyscanner);
 
-                       new = palloc(sizeof(PLpgSQL_stmt_dynexecute));
+                       new = palloc_object(PLpgSQL_stmt_dynexecute);
                        new->cmd_type = PLPGSQL_STMT_DYNEXECUTE;
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -2103,7 +2103,7 @@ stmt_open     : K_OPEN cursor_variable
                        PLpgSQL_stmt_open *new;
                        int         tok;
 
-                       new = palloc0(sizeof(PLpgSQL_stmt_open));
+                       new = palloc0_object(PLpgSQL_stmt_open);
                        new->cmd_type = PLPGSQL_STMT_OPEN;
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -2229,7 +2229,7 @@ stmt_close        : K_CLOSE cursor_variable ';'
                    {
                        PLpgSQL_stmt_close *new;
 
-                       new = palloc(sizeof(PLpgSQL_stmt_close));
+                       new = palloc_object(PLpgSQL_stmt_close);
                        new->cmd_type = PLPGSQL_STMT_CLOSE;
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -2250,7 +2250,7 @@ stmt_commit       : K_COMMIT opt_transaction_chain ';'
                    {
                        PLpgSQL_stmt_commit *new;
 
-                       new = palloc(sizeof(PLpgSQL_stmt_commit));
+                       new = palloc_object(PLpgSQL_stmt_commit);
                        new->cmd_type = PLPGSQL_STMT_COMMIT;
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -2264,7 +2264,7 @@ stmt_rollback : K_ROLLBACK opt_transaction_chain ';'
                    {
                        PLpgSQL_stmt_rollback *new;
 
-                       new = palloc(sizeof(PLpgSQL_stmt_rollback));
+                       new = palloc_object(PLpgSQL_stmt_rollback);
                        new->cmd_type = PLPGSQL_STMT_ROLLBACK;
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -2327,7 +2327,7 @@ exception_sect    :
                         * current block.
                         */
                        int         lineno = plpgsql_location_to_lineno(@1, yyscanner);
-                       PLpgSQL_exception_block *new = palloc(sizeof(PLpgSQL_exception_block));
+                       PLpgSQL_exception_block *new = palloc_object(PLpgSQL_exception_block);
                        PLpgSQL_variable *var;
 
                        plpgsql_curr_compile->has_exception_block = true;
@@ -2375,7 +2375,7 @@ proc_exception    : K_WHEN proc_conditions K_THEN proc_sect
                    {
                        PLpgSQL_exception *new;
 
-                       new = palloc0(sizeof(PLpgSQL_exception));
+                       new = palloc0_object(PLpgSQL_exception);
                        new->lineno = plpgsql_location_to_lineno(@1, yyscanner);
                        new->conditions = $2;
                        new->action = $4;
@@ -2420,7 +2420,7 @@ proc_condition    : any_identifier
                                if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
                                    yyerror(&yylloc, NULL, yyscanner, "invalid SQLSTATE code");
 
-                               new = palloc(sizeof(PLpgSQL_condition));
+                               new = palloc_object(PLpgSQL_condition);
                                new->sqlerrstate =
                                    MAKE_SQLSTATE(sqlstatestr[0],
                                                  sqlstatestr[1],
@@ -2671,7 +2671,7 @@ static PLpgSQL_expr *
 make_plpgsql_expr(const char *query,
                  RawParseMode parsemode)
 {
-   PLpgSQL_expr *expr = palloc0(sizeof(PLpgSQL_expr));
+   PLpgSQL_expr *expr = palloc0_object(PLpgSQL_expr);
 
    expr->query = pstrdup(query);
    expr->parseMode = parsemode;
@@ -3181,7 +3181,7 @@ make_execsql_stmt(int firsttoken, int location, PLword *word, YYSTYPE *yylvalp,
 
    check_sql_expr(expr->query, expr->parseMode, location, yyscanner);
 
-   execsql = palloc0(sizeof(PLpgSQL_stmt_execsql));
+   execsql = palloc0_object(PLpgSQL_stmt_execsql);
    execsql->cmd_type = PLPGSQL_STMT_EXECSQL;
    execsql->lineno = plpgsql_location_to_lineno(location, yyscanner);
    execsql->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -3208,7 +3208,7 @@ read_fetch_direction(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
     * We create the PLpgSQL_stmt_fetch struct here, but only fill in the
     * fields arising from the optional direction clause
     */
-   fetch = (PLpgSQL_stmt_fetch *) palloc0(sizeof(PLpgSQL_stmt_fetch));
+   fetch = (PLpgSQL_stmt_fetch *) palloc0_object(PLpgSQL_stmt_fetch);
    fetch->cmd_type = PLPGSQL_STMT_FETCH;
    fetch->stmtid = ++plpgsql_curr_compile->nstatements;
    /* set direction defaults: */
@@ -3360,7 +3360,7 @@ make_return_stmt(int location, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yysc
 {
    PLpgSQL_stmt_return *new;
 
-   new = palloc0(sizeof(PLpgSQL_stmt_return));
+   new = palloc0_object(PLpgSQL_stmt_return);
    new->cmd_type = PLPGSQL_STMT_RETURN;
    new->lineno = plpgsql_location_to_lineno(location, yyscanner);
    new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -3448,7 +3448,7 @@ make_return_next_stmt(int location, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t
                 errmsg("cannot use RETURN NEXT in a non-SETOF function"),
                 parser_errposition(location)));
 
-   new = palloc0(sizeof(PLpgSQL_stmt_return_next));
+   new = palloc0_object(PLpgSQL_stmt_return_next);
    new->cmd_type = PLPGSQL_STMT_RETURN_NEXT;
    new->lineno = plpgsql_location_to_lineno(location, yyscanner);
    new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -3512,7 +3512,7 @@ make_return_query_stmt(int location, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_
                 errmsg("cannot use RETURN QUERY in a non-SETOF function"),
                 parser_errposition(location)));
 
-   new = palloc0(sizeof(PLpgSQL_stmt_return_query));
+   new = palloc0_object(PLpgSQL_stmt_return_query);
    new->cmd_type = PLPGSQL_STMT_RETURN_QUERY;
    new->lineno = plpgsql_location_to_lineno(location, yyscanner);
    new->stmtid = ++plpgsql_curr_compile->nstatements;
@@ -3706,14 +3706,14 @@ read_into_scalar_list(char *initial_name,
     */
    plpgsql_push_back_token(tok, yylvalp, yyllocp, yyscanner);
 
-   row = palloc0(sizeof(PLpgSQL_row));
+   row = palloc0_object(PLpgSQL_row);
    row->dtype = PLPGSQL_DTYPE_ROW;
    row->refname = "(unnamed row)";
    row->lineno = plpgsql_location_to_lineno(initial_location, yyscanner);
    row->rowtupdesc = NULL;
    row->nfields = nfields;
-   row->fieldnames = palloc(sizeof(char *) * nfields);
-   row->varnos = palloc(sizeof(int) * nfields);
+   row->fieldnames = palloc_array(char *, nfields);
+   row->varnos = palloc_array(int, nfields);
    while (--nfields >= 0)
    {
        row->fieldnames[nfields] = fieldnames[nfields];
@@ -3741,14 +3741,14 @@ make_scalar_list1(char *initial_name,
 
    check_assignable(initial_datum, location, yyscanner);
 
-   row = palloc0(sizeof(PLpgSQL_row));
+   row = palloc0_object(PLpgSQL_row);
    row->dtype = PLPGSQL_DTYPE_ROW;
    row->refname = "(unnamed row)";
    row->lineno = lineno;
    row->rowtupdesc = NULL;
    row->nfields = 1;
-   row->fieldnames = palloc(sizeof(char *));
-   row->varnos = palloc(sizeof(int));
+   row->fieldnames = palloc_object(char *);
+   row->varnos = palloc_object(int);
    row->fieldnames[0] = initial_name;
    row->varnos[0] = initial_datum->dno;
 
@@ -3955,7 +3955,7 @@ read_cursor_args(PLpgSQL_var *cursor, int until, YYSTYPE *yylvalp, YYLTYPE *yyll
     * Read the arguments, one by one.
     */
    row = (PLpgSQL_row *) plpgsql_Datums[cursor->cursor_explicit_argrow];
-   argv = (char **) palloc0(row->nfields * sizeof(char *));
+   argv = (char **) palloc0_array(char *, row->nfields);
 
    for (argc = 0; argc < row->nfields; argc++)
    {
@@ -4091,7 +4091,7 @@ read_raise_options(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t yyscanner)
        if ((tok = yylex(yylvalp, yyllocp, yyscanner)) == 0)
            yyerror(yyllocp, NULL, yyscanner, "unexpected end of function definition");
 
-       opt = (PLpgSQL_raise_option *) palloc(sizeof(PLpgSQL_raise_option));
+       opt = palloc_object(PLpgSQL_raise_option);
 
        if (tok_is_keyword(tok, yylvalp,
                           K_ERRCODE, "errcode"))
@@ -4182,7 +4182,7 @@ make_case(int location, PLpgSQL_expr *t_expr,
 {
    PLpgSQL_stmt_case *new;
 
-   new = palloc(sizeof(PLpgSQL_stmt_case));
+   new = palloc_object(PLpgSQL_stmt_case);
    new->cmd_type = PLPGSQL_STMT_CASE;
    new->lineno = plpgsql_location_to_lineno(location, yyscanner);
    new->stmtid = ++plpgsql_curr_compile->nstatements;
index 655ab1d09eea51eb630c7cd490c61a5a298944f3..750ba586e0c85a44033a52b04e018e77cd866599 100644 (file)
@@ -177,7 +177,7 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, PLyTrigType is_trigger)
 
    oldcxt = MemoryContextSwitchTo(cxt);
 
-   proc = (PLyProcedure *) palloc0(sizeof(PLyProcedure));
+   proc = palloc0_object(PLyProcedure);
    proc->mcxt = cxt;
 
    PG_TRY();
@@ -293,8 +293,8 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, PLyTrigType is_trigger)
            }
 
            /* Allocate arrays for per-input-argument data */
-           proc->argnames = (char **) palloc0(sizeof(char *) * proc->nargs);
-           proc->args = (PLyDatumToOb *) palloc0(sizeof(PLyDatumToOb) * proc->nargs);
+           proc->argnames = (char **) palloc0_array(char *, proc->nargs);
+           proc->args = (PLyDatumToOb *) palloc0_array(PLyDatumToOb, proc->nargs);
 
            for (i = pos = 0; i < total; i++)
            {
index 1e386aadcca2bc4de841ee21ce51b873e7ba3acf..46f2ca0f792a8d5d76e344fc8f9da04f9a7b817f 100644 (file)
@@ -65,8 +65,8 @@ PLy_spi_prepare(PyObject *self, PyObject *args)
    nargs = list ? PySequence_Length(list) : 0;
 
    plan->nargs = nargs;
-   plan->types = nargs ? palloc0(sizeof(Oid) * nargs) : NULL;
-   plan->args = nargs ? palloc0(sizeof(PLyObToDatum) * nargs) : NULL;
+   plan->types = nargs ? palloc0_array(Oid, nargs) : NULL;
+   plan->args = nargs ? palloc0_array(PLyObToDatum, nargs) : NULL;
 
    MemoryContextSwitchTo(oldcontext);
 
index d88d10068f3a754688b497af89a1b52647432631..1f69109b081c027d38eca240fcb4838dff419803 100644 (file)
@@ -1353,8 +1353,8 @@ PLyMapping_ToComposite(PLyObToDatum *arg, TupleDesc desc, PyObject *mapping)
    Assert(PyMapping_Check(mapping));
 
    /* Build tuple */
-   values = palloc(sizeof(Datum) * desc->natts);
-   nulls = palloc(sizeof(bool) * desc->natts);
+   values = palloc_array(Datum, desc->natts);
+   nulls = palloc_array(bool, desc->natts);
    for (i = 0; i < desc->natts; ++i)
    {
        char       *key;
@@ -1435,8 +1435,8 @@ PLySequence_ToComposite(PLyObToDatum *arg, TupleDesc desc, PyObject *sequence)
                 errmsg("length of returned sequence did not match number of columns in row")));
 
    /* Build tuple */
-   values = palloc(sizeof(Datum) * desc->natts);
-   nulls = palloc(sizeof(bool) * desc->natts);
+   values = palloc_array(Datum, desc->natts);
+   nulls = palloc_array(bool, desc->natts);
    idx = 0;
    for (i = 0; i < desc->natts; ++i)
    {
@@ -1493,8 +1493,8 @@ PLyGenericObject_ToComposite(PLyObToDatum *arg, TupleDesc desc, PyObject *object
    volatile int i;
 
    /* Build tuple */
-   values = palloc(sizeof(Datum) * desc->natts);
-   nulls = palloc(sizeof(bool) * desc->natts);
+   values = palloc_array(Datum, desc->natts);
+   nulls = palloc_array(bool, desc->natts);
    for (i = 0; i < desc->natts; ++i)
    {
        char       *key;
index 73d660e88a693700e062b9aacc2b8559ac13f67e..187698ccdd25c814690693eceb32b8b4502f75ff 100644 (file)
@@ -1586,7 +1586,7 @@ compile_pltcl_function(Oid fn_oid, Oid tgreloid,
         * struct prodesc and subsidiary data must all live in proc_cxt.
         ************************************************************/
        oldcontext = MemoryContextSwitchTo(proc_cxt);
-       prodesc = (pltcl_proc_desc *) palloc0(sizeof(pltcl_proc_desc));
+       prodesc = palloc0_object(pltcl_proc_desc);
        prodesc->user_proname = pstrdup(user_proname);
        MemoryContextSetIdentifier(proc_cxt, prodesc->user_proname);
        prodesc->internal_proname = pstrdup(internal_proname);
@@ -2668,7 +2668,7 @@ pltcl_SPI_prepare(ClientData cdata, Tcl_Interp *interp,
                                     "PL/Tcl spi_prepare query",
                                     ALLOCSET_SMALL_SIZES);
    MemoryContextSwitchTo(plan_cxt);
-   qdesc = (pltcl_query_desc *) palloc0(sizeof(pltcl_query_desc));
+   qdesc = palloc0_object(pltcl_query_desc);
    snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
    qdesc->nargs = nargs;
    qdesc->argtypes = (Oid *) palloc(nargs * sizeof(Oid));
index 94ef639b6fcdb6573d3ae373545496b5d4a3863f..a34382a5f7909011a642406ce493a8ffb60be598 100644 (file)
@@ -138,7 +138,7 @@ dibuild(Relation heap, Relation index, IndexInfo *indexInfo)
 {
    IndexBuildResult *result;
 
-   result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
+   result = palloc_object(IndexBuildResult);
 
    /* let's pretend that no tuples were scanned */
    result->heap_tuples = 0;
index 38e54e0e0a443d8c1393b167f8d65bbf14e564f8..f32dfb317fabc5091174280c9905a081a405d4f3 100644 (file)
@@ -171,7 +171,7 @@ spgist_name_choose(PG_FUNCTION_ARGS)
            }
            out->result.splitTuple.prefixNNodes = 1;
            out->result.splitTuple.prefixNodeLabels =
-               (Datum *) palloc(sizeof(Datum));
+               palloc_object(Datum);
            out->result.splitTuple.prefixNodeLabels[0] =
                Int16GetDatum(*(unsigned char *) (prefixStr + commonLen));
 
@@ -243,7 +243,7 @@ spgist_name_choose(PG_FUNCTION_ARGS)
        out->result.splitTuple.prefixHasPrefix = in->hasPrefix;
        out->result.splitTuple.prefixPrefixDatum = in->prefixDatum;
        out->result.splitTuple.prefixNNodes = 1;
-       out->result.splitTuple.prefixNodeLabels = (Datum *) palloc(sizeof(Datum));
+       out->result.splitTuple.prefixNodeLabels = palloc_object(Datum);
        out->result.splitTuple.prefixNodeLabels[0] = Int16GetDatum(-2);
        out->result.splitTuple.childNodeN = 0;
        out->result.splitTuple.postfixHasPrefix = false;
@@ -318,9 +318,9 @@ spgist_name_inner_consistent(PG_FUNCTION_ARGS)
     * and see if it's consistent with the query.  If so, emit an entry into
     * the output arrays.
     */
-   out->nodeNumbers = (int *) palloc(sizeof(int) * in->nNodes);
-   out->levelAdds = (int *) palloc(sizeof(int) * in->nNodes);
-   out->reconstructedValues = (Datum *) palloc(sizeof(Datum) * in->nNodes);
+   out->nodeNumbers = palloc_array(int, in->nNodes);
+   out->levelAdds = palloc_array(int, in->nNodes);
+   out->reconstructedValues = palloc_array(Datum, in->nNodes);
    out->nNodes = 0;
 
    for (i = 0; i < in->nNodes; i++)
index 8162285fcb3979c102917c464470c8022ad45286..69383a98e37c4cf43a8962053fad221d4c5ac4d2 100644 (file)
@@ -622,7 +622,7 @@ test_random_operations(PG_FUNCTION_ARGS)
     * still possible if all the operations hit the "0" case during phase 4
     * where multiple operation types are mixed together.
     */
-   members = palloc(sizeof(int) * num_ops);
+   members = palloc_array(int, num_ops);
 
    /* Phase 1: Random insertions in first set */
    for (int i = 0; i < num_ops / 2; i++)
index cfdc6762785de999398d242c41729d376a1c44e8..7ac1fe6cb23b89087d14b99c7c0c2f44ff0df84b 100644 (file)
@@ -385,7 +385,7 @@ test_single_value_and_filler(uint64 value, uint64 filler_min, uint64 filler_max)
 
    intset = intset_create();
 
-   iter_expected = palloc(sizeof(uint64) * (filler_max - filler_min + 1));
+   iter_expected = palloc_array(uint64, filler_max - filler_min + 1);
    if (value < filler_min)
    {
        intset_add_member(intset, value);
index 8c78061ee4692d9dd2df5f8384fac59c67ed0dde..a95ac7984818ccb9d05b59290bb71a65a8db7348 100644 (file)
@@ -124,7 +124,7 @@ main(int argc, char **argv)
                break;
            case 's':           /* do semantic processing */
                testsem = &sem;
-               sem.semstate = palloc(sizeof(struct DoState));
+               sem.semstate = palloc_object(struct DoState);
                ((struct DoState *) sem.semstate)->lex = lex;
                ((struct DoState *) sem.semstate)->buf = makeStringInfo();
                need_strings = true;
index 15ed3617cb50ee77d286039620bf14ee514e3c15..353f90728192d0fed9c6aaca6afcb888c2fc942c 100644 (file)
@@ -46,7 +46,7 @@ PG_FUNCTION_INFO_V1(testprs_lextype);
 Datum
 testprs_start(PG_FUNCTION_ARGS)
 {
-   ParserState *pst = (ParserState *) palloc0(sizeof(ParserState));
+   ParserState *pst = palloc0_object(ParserState);
 
    pst->buffer = (char *) PG_GETARG_POINTER(0);
    pst->len = PG_GETARG_INT32(1);
@@ -112,7 +112,7 @@ testprs_lextype(PG_FUNCTION_ARGS)
     * the same lexids like Teodor in the default word parser; in this way we
     * can reuse the headline function of the default word parser.
     */
-   LexDescr   *descr = (LexDescr *) palloc(sizeof(LexDescr) * (2 + 1));
+   LexDescr   *descr = palloc_array(LexDescr, 2 + 1);
 
    /* there are only two types in this parser */
    descr[0].lexid = 3;
index 606d8d3cd2da95407fea8d1f90192540425e9ff7..031e8737d453b6828851afea939865ff456c33c6 100644 (file)
@@ -183,7 +183,7 @@ test_basic(rt_node_class_test_elem *test_info, int shift, bool asc)
    elog(NOTICE, "testing node %s with shift %d and %s keys",
         test_info->class_name, shift, asc ? "ascending" : "descending");
 
-   keys = palloc(sizeof(uint64) * children);
+   keys = palloc_array(uint64, children);
    for (int i = 0; i < children; i++)
    {
        if (asc)
index 2548a0ef7b159b83b86f6bb05964265fdad580ee..32ff9b13d8f95184bda10a7f0f06728be6e236ec 100644 (file)
@@ -107,10 +107,8 @@ test_regex(PG_FUNCTION_ARGS)
                                      true);
 
        /* Pre-create workspace that build_test_match_result needs */
-       matchctx->elems = (Datum *) palloc(sizeof(Datum) *
-                                          (matchctx->npatterns + 1));
-       matchctx->nulls = (bool *) palloc(sizeof(bool) *
-                                         (matchctx->npatterns + 1));
+       matchctx->elems = palloc_array(Datum, matchctx->npatterns + 1);
+       matchctx->nulls = palloc_array(bool, matchctx->npatterns + 1);
 
        MemoryContextSwitchTo(oldcontext);
        funcctx->user_fctx = matchctx;
@@ -436,7 +434,7 @@ setup_test_matches(text *orig_str,
                   Oid collation,
                   bool use_subpatterns)
 {
-   test_regex_ctx *matchctx = palloc0(sizeof(test_regex_ctx));
+   test_regex_ctx *matchctx = palloc0_object(test_regex_ctx);
    int         eml = pg_database_encoding_max_length();
    int         orig_len;
    pg_wchar   *wide_str;
@@ -457,7 +455,7 @@ setup_test_matches(text *orig_str,
 
    /* convert string to pg_wchar form for matching */
    orig_len = VARSIZE_ANY_EXHDR(orig_str);
-   wide_str = (pg_wchar *) palloc(sizeof(pg_wchar) * (orig_len + 1));
+   wide_str = palloc_array(pg_wchar, orig_len + 1);
    wide_len = pg_mb2wchar_with_len(VARDATA_ANY(orig_str), wide_str, orig_len);
 
    /* do we want to remember subpatterns? */
@@ -474,7 +472,7 @@ setup_test_matches(text *orig_str,
    }
 
    /* temporary output space for RE package */
-   pmatch = palloc(sizeof(regmatch_t) * pmatch_len);
+   pmatch = palloc_array(regmatch_t, pmatch_len);
 
    /*
     * the real output space (grown dynamically if needed)
@@ -483,7 +481,7 @@ setup_test_matches(text *orig_str,
     * than at 2^27
     */
    array_len = re_flags->glob ? 255 : 31;
-   matchctx->match_locs = (int *) palloc(sizeof(int) * array_len);
+   matchctx->match_locs = palloc_array(int, array_len);
    array_idx = 0;
 
    /* search for the pattern, perhaps repeatedly */
index 1f64939404f48ae2a14dece9567a0b7045037f84..e43f911244a9b9abfe2c3c09b190d23c972cd480 100644 (file)
@@ -121,7 +121,7 @@ RememberManyTestResources(ResourceOwner owner,
 
    for (int i = 0; i < nresources; i++)
    {
-       ManyTestResource *mres = palloc(sizeof(ManyTestResource));
+       ManyTestResource *mres = palloc_object(ManyTestResource);
 
        mres->kind = &kinds[kind_idx];
        dlist_node_init(&mres->node);
index b1f161cf7bbf7be161bb2d877913541a9413766f..86453f9614756d0c287412f2b0b95b656feaff8f 100644 (file)
@@ -44,7 +44,7 @@ List *
 test_rls_hooks_permissive(CmdType cmdtype, Relation relation)
 {
    List       *policies = NIL;
-   RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
+   RowSecurityPolicy *policy = palloc0_object(RowSecurityPolicy);
    Datum       role;
    FuncCall   *n;
    Node       *e;
@@ -112,7 +112,7 @@ List *
 test_rls_hooks_restrictive(CmdType cmdtype, Relation relation)
 {
    List       *policies = NIL;
-   RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
+   RowSecurityPolicy *policy = palloc0_object(RowSecurityPolicy);
    Datum       role;
    FuncCall   *n;
    Node       *e;
index bea8339f46469077fbd72486e6693953475d9257..1a21b8c88760b19db1605d922b9a0888247a1453 100644 (file)
@@ -142,7 +142,7 @@ worker_spi_main(Datum main_arg)
    char       *p;
    bits32      flags = 0;
 
-   table = palloc(sizeof(worktable));
+   table = palloc_object(worktable);
    sprintf(name, "schema%d", index);
    table->schema = pstrdup(name);
    table->name = pstrdup("counted");
index 56cc0567b1c6595180c7c146532b63d6740b14c8..c27305cf10bef5bc7974988bb566ba2b8139d12d 100644 (file)
@@ -198,7 +198,7 @@ widget_in(PG_FUNCTION_ARGS)
                 errmsg("invalid input syntax for type %s: \"%s\"",
                        "widget", str)));
 
-   result = (WIDGET *) palloc(sizeof(WIDGET));
+   result = palloc_object(WIDGET);
    result->center.x = atof(coord[0]);
    result->center.y = atof(coord[1]);
    result->radius = atof(coord[2]);
index 504c0235ffbcbcd43383b7a68154c6c7270f6ae7..505a2d33839400d07e3c0bc2e46801774ba14f22 100644 (file)
@@ -396,7 +396,7 @@ struct pg_tzenum
 pg_tzenum *
 pg_tzenumerate_start(void)
 {
-   pg_tzenum  *ret = (pg_tzenum *) palloc0(sizeof(pg_tzenum));
+   pg_tzenum  *ret = palloc0_object(pg_tzenum);
    char       *startdir = pstrdup(pg_TZDIR());
 
    ret->baselen = strlen(startdir) + 1;
index 6798a9e6ba6c086afc37ebd09b9a6ddf377f5b68..46dc54e62d03631f64fb05754c60ad3566866b9c 100644 (file)
@@ -41,7 +41,7 @@ complex_in(PG_FUNCTION_ARGS)
                 errmsg("invalid input syntax for type %s: \"%s\"",
                        "complex", str)));
 
-   result = (Complex *) palloc(sizeof(Complex));
+   result = palloc_object(Complex);
    result->x = x;
    result->y = y;
    PG_RETURN_POINTER(result);
@@ -73,7 +73,7 @@ complex_recv(PG_FUNCTION_ARGS)
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
    Complex    *result;
 
-   result = (Complex *) palloc(sizeof(Complex));
+   result = palloc_object(Complex);
    result->x = pq_getmsgfloat8(buf);
    result->y = pq_getmsgfloat8(buf);
    PG_RETURN_POINTER(result);
@@ -108,7 +108,7 @@ complex_add(PG_FUNCTION_ARGS)
    Complex    *b = (Complex *) PG_GETARG_POINTER(1);
    Complex    *result;
 
-   result = (Complex *) palloc(sizeof(Complex));
+   result = palloc_object(Complex);
    result->x = a->x + b->x;
    result->y = a->y + b->y;
    PG_RETURN_POINTER(result);
index 3cc945341876509e0ab46f335f06c4def54d1c0d..80882cf1414d38193742cb6493bbaaadd545f1ce 100644 (file)
@@ -50,7 +50,7 @@ makepoint(PG_FUNCTION_ARGS)
 {
    Point      *pointx = PG_GETARG_POINT_P(0);
    Point      *pointy = PG_GETARG_POINT_P(1);
-   Point      *new_point = (Point *) palloc(sizeof(Point));
+   Point      *new_point = palloc_object(Point);
 
    new_point->x = pointx->x;
    new_point->y = pointy->y;