Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The purpose of wrapping a pointer in struct in C

In Linux kernel code (up to 3.1.*) I saw such structure definition:

struct skb_frag_struct {
    struct page *page;
    /* ... */

In newer kernel versions this has evolved into:

struct skb_frag_struct {
    struct {
        struct page *p;
    } page;
    /* ... */

For what purpose could this wrapping be done in this particular case? And why it may be needed in general case?

like image 346
z0lupka Avatar asked Nov 09 '18 16:11

z0lupka


1 Answers

It can be helpful in understanding a code change to use the Git Blame View:

https://github.com/torvalds/linux/blame/a978a5b8d83f795e107a2ff759b28643739be70e/include/linux/skbuff.h

You can see what it says in the description of the commit where the change was made:

net: add opaque struct around skb frag page

I've split this bit out of the skb frag destructor patch since it helps enforce the use of the fragment API.

So it seems this was done just to catch dangling references in the code which were making raw access to a pointer, and force usage of a higher level API. It shouldn't add any runtime cost.

As an aside: the last time I did this myself I had a kind of silly idea which is to split the name of the struct out to something like struct { struct page *ge; } pa;...this way the privileged code can read as fragstruct.pa.ge instead of fragstruct.page.p. But outside of that being silly, an advantage of using the same name for the outer struct is helping guide old usages with clear errors. Though as your confusion points out, a comment // don't access directly, use fragment API on the p member would probably have been appropriate in this case.

like image 113
HostileFork says dont trust SE Avatar answered Nov 12 '22 00:11

HostileFork says dont trust SE