Re: cleanup: Make no_free_ptr() __must_check

From: Rasmus Villemoes
Date: Wed Aug 16 2023 - 03:24:26 EST


On 15/08/2023 15.53, Peter Zijlstra wrote:
> On Tue, Aug 15, 2023 at 01:28:37PM +0200, Rasmus Villemoes wrote:
>
>> Also, isn't it more complicated than necessary? Can we get rid of the
>> inner stmt expr and tmp var by just making it
>>
>> ((void) (p), ((typeof(p))__no_free_ptr((void **)&(p)))
>>
>> which is more or less the whole reason comma expressions is a thing.
>
> Ah, so the point of the statement expression before the comma is to
> validate that (p) is in fact a pointer, and to that effect we assign it
> to a 'void *' temporary.
>
> If that case is invalid, we'll get a compile fail with a dodgy message.
>
> I did this, because (void **)&(p) looses type integrity due to the cast.
>
> But yeah, I suppose it all needs a wee comment.

Ah, ok, I thought the purpose was to ensure the p expression gets
evaluated. Well, we can still do without the temp var and weird comma or
statement expressions:

static inline __must_check void * __no_free_ptr(void **pp, const void
*must_be_pointer_dummy)
{ void *p = *pp; *pp = NULL; return p; }

#define no_free_ptr(p) (__no_free_ptr((void **)&(p), p) )

Rasmus