Add repalloc0 and repalloc0_array
authorPeter Eisentraut <peter@eisentraut.org>
Sat, 12 Nov 2022 19:31:27 +0000 (20:31 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Sat, 12 Nov 2022 19:34:44 +0000 (20:34 +0100)
These zero out the space added by repalloc.  This is a common pattern
that is quite hairy to code by hand.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/b66dfc89-9365-cb57-4e1f-b7d31813eeec@enterprisedb.com

src/backend/executor/nodeHash.c
src/backend/libpq/be-fsstubs.c
src/backend/optimizer/util/placeholder.c
src/backend/optimizer/util/relnode.c
src/backend/parser/parse_param.c
src/backend/storage/lmgr/lwlock.c
src/backend/utils/adt/ruleutils.c
src/backend/utils/cache/typcache.c
src/backend/utils/mmgr/mcxt.c
src/include/utils/palloc.h

index 6622b202c229f656a1044ed826f2f6d6f9abfb31..2e6cce4802e5ddfba19bf08f5b89ff5dc84d0a6e 100644 (file)
@@ -940,12 +940,8 @@ ExecHashIncreaseNumBatches(HashJoinTable hashtable)
    else
    {
        /* enlarge arrays and zero out added entries */
-       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,
-              (nbatch - oldnbatch) * sizeof(BufFile *));
+       hashtable->innerBatchFile = repalloc0_array(hashtable->innerBatchFile, BufFile *, oldnbatch, nbatch);
+       hashtable->outerBatchFile = repalloc0_array(hashtable->outerBatchFile, BufFile *, oldnbatch, nbatch);
    }
 
    MemoryContextSwitchTo(oldcxt);
index 3e5cada7eb53ca81c9c1d30bcc02020232fc6e4b..106fdcdf817bd93fe0cc583c9fa0c480f5a5c126 100644 (file)
@@ -696,19 +696,16 @@ newLOfd(void)
        newsize = 64;
        cookies = (LargeObjectDesc **)
            MemoryContextAllocZero(fscxt, newsize * sizeof(LargeObjectDesc *));
-       cookies_size = newsize;
    }
    else
    {
        /* Double size of array */
        i = cookies_size;
        newsize = cookies_size * 2;
-       cookies = (LargeObjectDesc **)
-           repalloc(cookies, newsize * sizeof(LargeObjectDesc *));
-       MemSet(cookies + cookies_size, 0,
-              (newsize - cookies_size) * sizeof(LargeObjectDesc *));
-       cookies_size = newsize;
+       cookies =
+           repalloc0_array(cookies, LargeObjectDesc *, cookies_size, newsize);
    }
+   cookies_size = newsize;
 
    return i;
 }
index c7bfa293c9ff26df74f69fde312b870fc1fe55df..c55027377fe7f84ecb4ed295fc87999eb17c6292 100644 (file)
@@ -133,16 +133,11 @@ find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv)
        while (phinfo->phid >= new_size)
            new_size *= 2;
        if (root->placeholder_array)
-       {
-           root->placeholder_array = (PlaceHolderInfo **)
-               repalloc(root->placeholder_array,
-                        sizeof(PlaceHolderInfo *) * new_size);
-           MemSet(root->placeholder_array + root->placeholder_array_size, 0,
-                  sizeof(PlaceHolderInfo *) * (new_size - root->placeholder_array_size));
-       }
+           root->placeholder_array =
+               repalloc0_array(root->placeholder_array, PlaceHolderInfo *, root->placeholder_array_size, new_size);
        else
-           root->placeholder_array = (PlaceHolderInfo **)
-               palloc0(new_size * sizeof(PlaceHolderInfo *));
+           root->placeholder_array =
+               palloc0_array(PlaceHolderInfo *, new_size);
        root->placeholder_array_size = new_size;
    }
    root->placeholder_array[phinfo->phid] = phinfo;
index 1786a3daddc81e92d11e9085035da4ca383a1826..d7b4434e7f4a3acaf40a6e99b52fbc83491f90ec 100644 (file)
@@ -157,31 +157,18 @@ expand_planner_arrays(PlannerInfo *root, int add_size)
 
    new_size = root->simple_rel_array_size + add_size;
 
-   root->simple_rel_array = (RelOptInfo **)
-       repalloc(root->simple_rel_array,
-                sizeof(RelOptInfo *) * new_size);
-   MemSet(root->simple_rel_array + root->simple_rel_array_size,
-          0, sizeof(RelOptInfo *) * add_size);
+   root->simple_rel_array =
+       repalloc0_array(root->simple_rel_array, RelOptInfo *, root->simple_rel_array_size, new_size);
 
-   root->simple_rte_array = (RangeTblEntry **)
-       repalloc(root->simple_rte_array,
-                sizeof(RangeTblEntry *) * new_size);
-   MemSet(root->simple_rte_array + root->simple_rel_array_size,
-          0, sizeof(RangeTblEntry *) * add_size);
+   root->simple_rte_array =
+       repalloc0_array(root->simple_rte_array, RangeTblEntry *, root->simple_rel_array_size, new_size);
 
    if (root->append_rel_array)
-   {
-       root->append_rel_array = (AppendRelInfo **)
-           repalloc(root->append_rel_array,
-                    sizeof(AppendRelInfo *) * new_size);
-       MemSet(root->append_rel_array + root->simple_rel_array_size,
-              0, sizeof(AppendRelInfo *) * add_size);
-   }
+       root->append_rel_array =
+           repalloc0_array(root->append_rel_array, AppendRelInfo *, root->simple_rel_array_size, new_size);
    else
-   {
-       root->append_rel_array = (AppendRelInfo **)
-           palloc0(sizeof(AppendRelInfo *) * new_size);
-   }
+       root->append_rel_array =
+           palloc0_array(AppendRelInfo *, new_size);
 
    root->simple_rel_array_size = new_size;
 }
index f668abfcb336c04b6789c9626cfb78954d29fd18..e80876aa25e0f5724011ce86a6c0d5407a3a07d4 100644 (file)
@@ -145,14 +145,10 @@ variable_paramref_hook(ParseState *pstate, ParamRef *pref)
    {
        /* Need to enlarge param array */
        if (*parstate->paramTypes)
-           *parstate->paramTypes = (Oid *) repalloc(*parstate->paramTypes,
-                                                    paramno * sizeof(Oid));
+           *parstate->paramTypes = repalloc0_array(*parstate->paramTypes, Oid,
+                                                   *parstate->numParams, paramno);
        else
-           *parstate->paramTypes = (Oid *) palloc(paramno * sizeof(Oid));
-       /* Zero out the previously-unreferenced slots */
-       MemSet(*parstate->paramTypes + *parstate->numParams,
-              0,
-              (paramno - *parstate->numParams) * sizeof(Oid));
+           *parstate->paramTypes = palloc0_array(Oid, paramno);
        *parstate->numParams = paramno;
    }
 
index 0fc0cf6ebbd9d896ec606c0ac05c032aa6bd0fff..532cd67f4e3cc91bded5768c25a1d9f68f09a624 100644 (file)
@@ -668,13 +668,8 @@ LWLockRegisterTranche(int tranche_id, const char *tranche_name)
                MemoryContextAllocZero(TopMemoryContext,
                                       newalloc * sizeof(char *));
        else
-       {
-           LWLockTrancheNames = (const char **)
-               repalloc(LWLockTrancheNames, newalloc * sizeof(char *));
-           memset(LWLockTrancheNames + LWLockTrancheNamesAllocated,
-                  0,
-                  (newalloc - LWLockTrancheNamesAllocated) * sizeof(char *));
-       }
+           LWLockTrancheNames =
+               repalloc0_array(LWLockTrancheNames, const char *, LWLockTrancheNamesAllocated, newalloc);
        LWLockTrancheNamesAllocated = newalloc;
    }
 
index 70d723e80cac3c04e5b460288d18f8af47b8cc80..c5a49d0be34258fac09213941b762cd1b5bbd816 100644 (file)
@@ -4855,14 +4855,9 @@ expand_colnames_array_to(deparse_columns *colinfo, int n)
    if (n > colinfo->num_cols)
    {
        if (colinfo->colnames == NULL)
-           colinfo->colnames = (char **) palloc0(n * sizeof(char *));
+           colinfo->colnames = palloc0_array(char *, n);
        else
-       {
-           colinfo->colnames = (char **) repalloc(colinfo->colnames,
-                                                  n * sizeof(char *));
-           memset(colinfo->colnames + colinfo->num_cols, 0,
-                  (n - colinfo->num_cols) * sizeof(char *));
-       }
+           colinfo->colnames = repalloc0_array(colinfo->colnames, char *, colinfo->num_cols, n);
        colinfo->num_cols = n;
    }
 }
index 808f9ebd0d2f2fb02c12b3ec684af3d4b6cb6ed5..b69366fa29deb0bde5600cc9dcff2e58086a5be8 100644 (file)
@@ -1714,14 +1714,8 @@ ensure_record_cache_typmod_slot_exists(int32 typmod)
    {
        int32       newlen = pg_nextpower2_32(typmod + 1);
 
-       RecordCacheArray = (TupleDesc *) repalloc(RecordCacheArray,
-                                                 newlen * sizeof(TupleDesc));
-       memset(RecordCacheArray + RecordCacheArrayLen, 0,
-              (newlen - RecordCacheArrayLen) * sizeof(TupleDesc));
-       RecordIdentifierArray = (uint64 *) repalloc(RecordIdentifierArray,
-                                                   newlen * sizeof(uint64));
-       memset(RecordIdentifierArray + RecordCacheArrayLen, 0,
-              (newlen - RecordCacheArrayLen) * sizeof(uint64));
+       RecordCacheArray = repalloc0_array(RecordCacheArray, TupleDesc, RecordCacheArrayLen, newlen);
+       RecordIdentifierArray = repalloc0_array(RecordIdentifierArray, uint64, RecordCacheArrayLen, newlen);
        RecordCacheArrayLen = newlen;
    }
 }
index f526ca82c15da5e20c5cdfc7f8fe9308afce82dd..57bd6690ca0e929a91200ec3087577a20408fe8c 100644 (file)
@@ -1395,6 +1395,26 @@ repalloc_extended(void *pointer, Size size, int flags)
    return ret;
 }
 
+/*
+ * repalloc0
+ *     Adjust the size of a previously allocated chunk and zero out the added
+ *     space.
+ */
+void *
+repalloc0(void *pointer, Size oldsize, Size size)
+{
+   void       *ret;
+
+   /* catch wrong argument order */
+   if (unlikely(oldsize > size))
+       elog(ERROR, "invalid repalloc0 call: oldsize %zu, new size %zu",
+            oldsize, size);
+
+   ret = repalloc(pointer, size);
+   memset((char *) ret + oldsize, 0, (size - oldsize));
+   return ret;
+}
+
 /*
  * MemoryContextAllocHuge
  *     Allocate (possibly-expansive) space within the specified context.
index 8eee0e293857ca839a40487dbd8220c74509293d..72d4e70dc64921db6379f739d1b850f3c5aa1bf3 100644 (file)
@@ -80,6 +80,7 @@ extern void *palloc_extended(Size size, int flags);
 extern pg_nodiscard void *repalloc(void *pointer, Size size);
 extern pg_nodiscard void *repalloc_extended(void *pointer,
                                            Size size, int flags);
+extern pg_nodiscard void *repalloc0(void *pointer, Size oldsize, Size size);
 extern void pfree(void *pointer);
 
 /*
@@ -103,6 +104,7 @@ extern void pfree(void *pointer);
  * objects of type "type"
  */
 #define repalloc_array(pointer, type, count) ((type *) repalloc(pointer, sizeof(type) * (count)))
+#define repalloc0_array(pointer, type, oldcount, count) ((type *) repalloc0(pointer, sizeof(type) * (oldcount), sizeof(type) * (count)))
 
 /*
  * The result of palloc() is always word-aligned, so we can skip testing