| From: | Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com> |
|---|---|
| To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
| Cc: | Andres Freund <andres(at)anarazel(dot)de>, pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Subject: | Re: Fix and improve allocation formulas |
| Date: | 2025-12-12 08:46:52 |
| Message-ID: | aTvWfM73VP08sG1t@ip-10-97-1-34.eu-west-3.compute.internal |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
On Thu, Dec 11, 2025 at 11:43:27AM -0500, Tom Lane wrote:
> Andres Freund <andres(at)anarazel(dot)de> writes:
> > I tend to agree that what you propose is the better style, but I seriously
> > doubt that
>
> > a) changing over everything at once is worth the backpatch hazard and review
> > pain
> > b) that to judge whether we should do this a 277kB patch is useful
> > c) that changing the existing code should be the first thing, if we want to
> > make this the new style, we should first document the sizeof(*var) approach to
> > be preferred.
>
> 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.
Right, thanks to the cast in palloc_object()/palloc_array() that produces
-Wincompatible-pointer-types or -Wpointer-sign warnings for most cases.
Still that does not protect against the ones that are semantically wrong, say:
TransactionId *xids = palloc_array(CommandId, 100);
That's not a major concern though.
> However, suppose you write
>
> foo = palloc(sizeof(*bar))
We could imagine a macro like:
#define palloc_set_var(var, count) \
((var) = palloc((count) * sizeof(*(var))))
to prevent those typos, but that's useless if we remove all those palloc
calls and adopt palloc_object() and palloc_array() usage instead.
Regards,
--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Chao Li | 2025-12-12 08:50:09 | Fix memory leak in gist_page_items() of pageinspect |
| Previous Message | Xuneng Zhou | 2025-12-12 08:45:56 | Re: Add WALRCV_CONNECTING state to walreceiver |