On Wed, Aug 21, 2024 at 8:43 AM Martin Uecker <uec...@tugraz.at> wrote:
>
> Am Mittwoch, dem 21.08.2024 um 15:24 +0000 schrieb Qing Zhao:
> > >
> > > But if we changed it to return a void pointer,  we could make this
> > > a compile-time check:
> > >
> > > auto ret = __builtin_get_counted_by(__p->FAM);
> > >
> > > _Generic(ret, void*: (void)0, default: *ret = COUNT);
> >
> > Is there any benefit to return a void pointer than a SIZE_T pointer for
> > the NULL pointer?
>
> Yes! You can test with _Generic (or __builtin_types_compatible_p)
> at compile-time based on the type whether you can set *ret to COUNT
> or not as in the example above.
>
> So it is not a weird run-time test which needs to be optimized
> away.
>
Using a '_Generic' moves so much of the work onto the programmer that
it would be far easier, and cleaner, for them simply to specify the
'counter' field in the macro and be done with it. Something like:

  #define alloc(PTR, COUNT, FAM, COUNTER)

If the FAM doesn't have a 'counted_by' field:

  #define alloc(PTR, COUNT, FAM)

(It would use VAR_ARGS of course). Why not simply have the compiler
automatically adjust the return type? It's perfectly capable of Doing
the Right Thing(tm). Otherwise, this builtin becomes even less
desirable to use than it currently is.

> > > > Yes, I do feel that the approach __builtin_get_counted_by is not very 
> > > > good.
> > > > Maybe it’s better to provide
> > > > A. __builtin_set_counted_by
> > > > or
> > > > B. The unary operator __counted_by(PTR) to return a Lvalue, in this 
> > > > case,
> > > > we need a __builtin_has_attribute first to check whether PTR has the
> > > > counted_by attribute first.
> > >
> > > You could potentially do the same __counted_by and test for type void.
> > >
> > > _Generic(typeof(__counted_by(PTR)), void: (void)0, __counted_by(PTR) = 
> > > COUNT);
> >
> > Oh, so, is there any benefit for the unary operator __counted_by(PTR) than
> > the current __builtin_get_counted_by?
>
> I don't know. You suggested it ;-)
>
> It probably makes it harder to test the type because you need the
> typeof / C2Y Generic combination, but maybe there are other ways
> to test.
>
>
> Martin

Reply via email to