summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2025-12-09 14:53:17 +0900
committerMichael Paquier <michael@paquier.xyz>2025-12-09 14:53:17 +0900
commit0c3c5c3b06a37c13a811ea93044202e06523b705 (patch)
treeabcde572bfd5b10dc11476706d6616ff0cad2828 /src/common
parentaa749bde323364039f369784071f315ad53c1325 (diff)
Use palloc_object() and palloc_array() in more areas of the tree
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
Diffstat (limited to 'src/common')
-rw-r--r--src/common/blkreftable.c21
-rw-r--r--src/common/parse_manifest.c4
-rw-r--r--src/common/pgfnames.c5
-rw-r--r--src/common/rmtree.c2
-rw-r--r--src/common/stringinfo.c2
5 files changed, 16 insertions, 18 deletions
diff --git a/src/common/blkreftable.c b/src/common/blkreftable.c
index b935baf9ad4..0e08f4e40ed 100644
--- a/src/common/blkreftable.c
+++ b/src/common/blkreftable.c
@@ -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;
diff --git a/src/common/parse_manifest.c b/src/common/parse_manifest.c
index 58e0948100f..cc5fa0e5e07 100644
--- a/src/common/parse_manifest.c
+++ b/src/common/parse_manifest.c
@@ -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;
diff --git a/src/common/pgfnames.c b/src/common/pgfnames.c
index 8fb79105714..591535a24b8 100644
--- a/src/common/pgfnames.c
+++ b/src/common/pgfnames.c
@@ -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);
}
diff --git a/src/common/rmtree.c b/src/common/rmtree.c
index 2f364f84ae5..47cd0a4d8a1 100644
--- a/src/common/rmtree.c
+++ b/src/common/rmtree.c
@@ -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)))
{
diff --git a/src/common/stringinfo.c b/src/common/stringinfo.c
index 22d03807697..a3e77088f8e 100644
--- a/src/common/stringinfo.c
+++ b/src/common/stringinfo.c
@@ -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;