tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: [PATCH] style(5): No struct typedefs



On 2023-07-11 12:17, Taylor R Campbell wrote:
I propose the attached change to KNF style(5) to advise against
typedefs for structs or unions, or pointers to them.

Passing around pointers to structs and unions doesn't require the
definition, only a forward declaration:

struct vnode;
int frobnitz(struct vnode *);

Agreed. But this is unrelated to typedefs.

This can dramatically cut down on header file dependencies so that
touching one header file doesn't cause the entire kernel to rebuild.
This also makes it clearer whether you are passing around a copy of a
struct or a pointer to a struct, which is often semantically important
not to conceal.

Typedefs are not necessary for opaque cookies to have labels, like
`typedef void *bus_dma_tag_t;'.  In fact, using void * here is bad
because it prevents the C compiler from checking types; bus_dma_tag_t
is indistinguishable from audio_dai_tag_t, and from pckbc_tag_t, and
from acpipmtimer_t, and from sdmmc_chipset_handle_t.

I don't get it. Why the "void *" stuff? That is where I think the real badness lies, and I agree we should not have that.

But defining something like

typedef struct bus_dma_tag *bus_dma_tag_t;

would mean we could easily change what bus_dma_tag_t actually is, keeping it opaque, while at the same time keeping the type checking.

Basically, getting all the benefits you mention from having it as a proper type, but still also keeping the ability to change what it actually is without any problems.

So, yes to proper forward declarations. But I don't think typedefs as such is a problem.

  Johnny

--
Johnny Billquist                  || "I'm on a bus
                                  ||  on a psychedelic trip
email: bqt%softjar.se@localhost             ||  Reading murder books
pdp is alive!                     ||  tryin' to stay hip" - B. Idol


Home | Main Index | Thread Index | Old Index