| From: | Thomas Munro <thomas(dot)munro(at)gmail(dot)com> |
|---|---|
| To: | Andres Freund <andres(at)anarazel(dot)de> |
| Cc: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: A stack allocation API |
| Date: | 2026-03-11 00:05:22 |
| Message-ID: | CA+hUKG+-=A-xGCKecQF8kG=agMJLoK31gutaS+nVLK=iL-oWMQ@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Sat, Feb 28, 2026 at 7:02 AM Andres Freund <andres(at)anarazel(dot)de> 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/
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Sami Imseih | 2026-03-11 00:11:27 | Re: another autovacuum scheduling thread |
| Previous Message | Tomas Vondra | 2026-03-11 00:00:34 | Re: Remove header lock BufferGetLSNAtomic() on architectures with 64 bit atomic operations |