Re: Expand palloc/pg_malloc API
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Cc: Peter Eisentraut <peter.eisentraut@enterprisedb.com>,
pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2022-05-17T18:43:51Z
Lists: pgsql-hackers
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com> writes:
> On Tue, May 17, 2022 at 5:11 PM Peter Eisentraut
> <peter.eisentraut@enterprisedb.com> wrote:
>> This adds additional variants of palloc, pg_malloc, etc. that
>> encapsulate common usage patterns and provide more type safety.
> I see lots of instances where there's no explicit type-casting to the
> target variable type -
> retval = palloc(sizeof(GISTENTRY));
> Interval *p = palloc(sizeof(Interval));
> macaddr *v = palloc0(sizeof(macaddr)); and so on.
Yeah. IMO the first of those is very poor style, because there's
basically nothing enforcing that you wrote the right thing in sizeof().
The others are a bit safer, in that at least a human can note that
the two types mentioned on the same line match --- but I doubt any
compiler would detect it if they don't. Our current preferred style
Interval *p = (Interval *) palloc(sizeof(Interval));
is really barely an improvement, because only two of the three types
mentioned are going to be checked against each other.
So I think Peter's got a good idea here (I might quibble with the details
of some of these macros). But it's not really going to move the
safety goalposts very far unless we make a concerted effort to make
these be the style everywhere. Are we willing to do that? What
will it do to back-patching difficulty? Dare I suggest back-patching
these changes?
regards, tom lane
Commits
-
Add repalloc0 and repalloc0_array
- b4b7ce8061d3 16.0 landed
-
Expand palloc/pg_malloc API for more type safety
- 2864f7755611 10.23 landed
- e962235fe1f6 11.18 landed
- 7dd9b469bc56 12.13 landed
- 172882292451 13.9 landed
- b7f37af7c195 14.6 landed
- 7fe55d5e12b6 15.0 landed
- 2016055a92f2 16.0 landed
-
Assorted examples of expanded type-safer palloc/pg_malloc API
- 5015e1e1b58f 16.0 landed