public inbox for [email protected]
help / color / mirror / Atom feedFrom: Thomas Munro <[email protected]>
To: Andres Freund <[email protected]>
Cc: Tom Lane <[email protected]>
Cc: PostgreSQL Hackers <[email protected]>
Subject: Re: A stack allocation API
Date: Wed, 11 Mar 2026 13:05:22 +1300
Message-ID: <CA+hUKG+-=A-xGCKecQF8kG=agMJLoK31gutaS+nVLK=iL-oWMQ@mail.gmail.com> (raw)
In-Reply-To: <rc5bkn7ikwo6cir5w4jilxtqeiczdup7ytooav7lufa77brz5p@54gexczvq25p>
References: <CA+hUKG+ixUUYOGRcuZpkk5pmJZaUQv6VCPAjdTZXFP5vA8jxcA@mail.gmail.com>
<[email protected]>
<rc5bkn7ikwo6cir5w4jilxtqeiczdup7ytooav7lufa77brz5p@54gexczvq25p>
On Sat, Feb 28, 2026 at 7:02 AM Andres Freund <[email protected]> wrote:
> I wish msvc implemented something akin to gcc/clang's
> attribute(cleanup(cleanup_function)), but it doesn't look like it
> does. Obviously it would if we were to compile with C++, but I don't think
> anybody has appetite for the work it'd need to get there.
Well, it does have __try/__finally, but the future of the idea for C2y
looks like Go or Zig:
#include <stddefer.h>
p = malloc();
defer free(p);
It's already available in bleeding edge GCC and Clang 22, based on the
TS 25755 draft[1]. Unfortunately for the real world, it's annoyingly
difficult to come up with *nice* looking macros that can expand to the
dialects we'd need to cover even if we were OK with abandoning the
notion of supporting C < 2y compilers other than the 3 we talk about,
due to syntactic structure and semantic differences, presumably:
1. C2y defer
2. C++ RAII
3. GCC/Clang cleanup attributes
4. MSVC __try/__finally
But with big honking framing macros BEGIN_MAGIC_PIXIE_DUST(), ...
pg_defer_pfree(p) (assuming we'd want a style with a pathway to the
future standard?); ... END_MAGIC_PIXIE_DUST(), I think it's probably
doable... There are lots of people trying to suffer through
portable-enough-for-me and just-enough-functionality-for-my-project,
maybe some inspiration[2]...
[1] https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3734.pdf
[2] https://antonz.org/defer-in-c/
view thread (6+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected]
Subject: Re: A stack allocation API
In-Reply-To: <CA+hUKG+-=A-xGCKecQF8kG=agMJLoK31gutaS+nVLK=iL-oWMQ@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox