Re: Add returns_nonnull to infallible allocators - Mailing list pgsql-hackers

From Peter Eisentraut
Subject Re: Add returns_nonnull to infallible allocators
Date
Msg-id 72c8b75b-65bc-46b1-a2ef-2d3375c3cc82@eisentraut.org
Whole thread
In response to Re: Add returns_nonnull to infallible allocators  ("Tristan Partin" <tristan@partin.io>)
List pgsql-hackers
On 06.07.26 20:11, Tristan Partin wrote:
> On Mon Jul 6, 2026 at 6:10 PM UTC, Tristan Partin wrote:
>> Postgres memory allocators, by default, ERROR out on memory allocation
>> failures. An ERROR leads to a longjmp, which means that the caller of
>> the allocator will never see a NULL return value. We can explicitly let
>> the compiler know about this behavior by adding the returns_nonnull
>> attribute to the allocators that follow this behavior. Postgres does
>> support _extended versions of some of the allocators that take a flaks
>> argument. The caller can provide the MCXT_ALLOC_NO_OOM flag to these
>> allocators to request that they return NULL on allocation failure
>> instead of ERROR-ing out. The _extended allocators cannot be marked as
>> returns_nonnull because of that.
>>
>> By using returns_nonnull, we can help the compiler to optimize call
>> sites.

Your patch marks palloc_mul_extended() as pg_attribute_returns_nonnull, 
which seems incorrect per the above description.

Also, per the discussion in the counted_by thread, lets put these new 
attributes in their more correct position in front of the declaration. 
(Note that several of these already use pg_nodiscard, which is also an 
attribute.)  That way we can also use the MSVC annotation _Ret_notnull_.




pgsql-hackers by date:

Previous
From: shihao zhong
Date:
Subject: Re: Parallel vacuum: I/O timings in the log leave out the parallel workers
Next
From: Nitin Motiani
Date:
Subject: Re: [PATCH v1] Fix for Bug#19724 - ALTER TYPE ... ALTER ATTRIBUTE triggers internal error for base type of domain with check