Add returns_nonnull to infallible allocators

Started by Tristan Partin19 days ago2 messageshackers
Jump to latest
#1Tristan Partin
tristan@partin.io

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.

--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)

#2Tristan Partin
tristan@partin.io
In reply to: Tristan Partin (#1)
Re: Add returns_nonnull to infallible allocators

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.

...

--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)

Attachments:

v1-0001-Add-returns_nonnull-attribute-to-infallible-alloc.patchtext/x-patch; charset=utf-8; name=v1-0001-Add-returns_nonnull-attribute-to-infallible-alloc.patchDownload+43-34