Re: Fix and improve allocation formulas

From: Peter Eisentraut <peter(at)eisentraut(dot)org>
To: Michael Paquier <michael(at)paquier(dot)xyz>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Andres Freund <andres(at)anarazel(dot)de>, Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Fix and improve allocation formulas
Date: 2025-12-16 15:19:08
Message-ID: fc0b1449-e95b-4211-8a12-748fad7de9b8@eisentraut.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 12.12.25 10:53, Michael Paquier wrote:
> On Thu, Dec 11, 2025 at 11:43:27AM -0500, Tom Lane wrote:
>> And before that, you'd have to get consensus that sizeof(*var) *is*
>> the preferred style. I for one don't like it a bit. IMO what it
>> mostly accomplishes is to remove a cue as to what we are allocating.
>> I don't agree that it removes a chance for error, either. Sure,
>> if you write
>>
>> foo = palloc(sizeof(typeA))
>>
>> when foo is of type typeB*, you made a mistake --- but we know how
>> to get the compiler to warn about such mistakes, and indeed the
>> main point of the palloc_object() changes was to catch those.
>> However, suppose you write
>>
>> foo = palloc(sizeof(*bar))
>>
>> I claim that's about an equally credible typo, and there is
>> nothing that will detect it.
>
> Yeah, I'd prefer something where we keep track of the type, with the
> extra layer that enforces a cast to the type of the variable like
> palloc_object/array macros. The latter style of specifying a variable
> pointer within the sizeof is more error-prone long-term, so it's not
> something I think we should encourage.

The original proposal that led to palloc_object() etc.[0] contained a
function palloc_ptrtype() that you would use like

foo = palloc_ptrtype(foo)

but people didn't like that for all these reasons.

[0]:
https://www.postgresql.org/message-id/flat/bb755632-2a43-d523-36f8-a1e7a389a907(at)enterprisedb(dot)com

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Paul A Jungwirth 2025-12-16 15:24:33 Re: amcheck: support for GiST
Previous Message Tomas Vondra 2025-12-16 15:17:51 Re: failed NUMA pages inquiry status: Operation not permitted