[systemd-devel] [PATCH] Add avoid_cleanup macro to cancel _cleanup_ of a pointer

Josh Triplett josh at joshtriplett.org
Mon Mar 10 08:58:35 PDT 2014


On Mon, Mar 10, 2014 at 04:44:02PM +0100, Lennart Poettering wrote:
> On Sat, 08.03.14 20:33, Josh Triplett (josh at joshtriplett.org) wrote:
> 
> > avoid_cleanup also returns a copy of the pointer, making it convenient
> > to use at the point where initialization completes, to hand the constructed
> > object off somewhere without freeing it.
> > 
> > Change all NULL assignments tagged with /* avoid cleanup */ to use this
> > instead.
> > ---
> > 
> > Seems like a common pattern, and this makes it more self-documenting.
> > In particular, the use in systemctl.c's list_timers function now feels
> > like a single logical operation of "hand ownership of this object off to
> > something else and don't clean it up".
> 
> Hmmm, I am all for synctactic sugar, but I don't see the benefit of this
> one really... Especially given that that disabling cleanup is done
> different for different types... For example, disabling cleanup for an
> fd is by assigning -1...

As far as I can tell, that's the *only* differing case.  Everything else
is a pointer and uses NULL.

> I would see benefit in this if we could maybe make this
> type-sensitive... not sure though if C would allow that? I at least
> cannot think of a way to do that?

C11 allows it using _Generic, if you're willing to rely on that.
__builtin_types_compatible_p and some casts could work too.  But again,
I think the only two cases are "int" and "pointer".

- Josh Triplett


More information about the systemd-devel mailing list