The purpose of wrapping a pointer in struct in C









up vote
9
down vote

favorite












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?










share|improve this question

























    up vote
    9
    down vote

    favorite












    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?










    share|improve this question























      up vote
      9
      down vote

      favorite









      up vote
      9
      down vote

      favorite











      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?










      share|improve this question













      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?







      c pointers struct linux-kernel






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 16:33









      z0lupka

      30819




      30819






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          7
          down vote



          accepted










          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.






          share|improve this answer






















          • Beat me to it! :-)
            – Groo
            Nov 9 at 16:50










          • @Groo Great minds think alike. :-)
            – HostileFork
            Nov 9 at 16:50










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229726%2fthe-purpose-of-wrapping-a-pointer-in-struct-in-c%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          7
          down vote



          accepted










          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.






          share|improve this answer






















          • Beat me to it! :-)
            – Groo
            Nov 9 at 16:50










          • @Groo Great minds think alike. :-)
            – HostileFork
            Nov 9 at 16:50














          up vote
          7
          down vote



          accepted










          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.






          share|improve this answer






















          • Beat me to it! :-)
            – Groo
            Nov 9 at 16:50










          • @Groo Great minds think alike. :-)
            – HostileFork
            Nov 9 at 16:50












          up vote
          7
          down vote



          accepted







          up vote
          7
          down vote



          accepted






          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 17:10

























          answered Nov 9 at 16:48









          HostileFork

          24.7k775131




          24.7k775131











          • Beat me to it! :-)
            – Groo
            Nov 9 at 16:50










          • @Groo Great minds think alike. :-)
            – HostileFork
            Nov 9 at 16:50
















          • Beat me to it! :-)
            – Groo
            Nov 9 at 16:50










          • @Groo Great minds think alike. :-)
            – HostileFork
            Nov 9 at 16:50















          Beat me to it! :-)
          – Groo
          Nov 9 at 16:50




          Beat me to it! :-)
          – Groo
          Nov 9 at 16:50












          @Groo Great minds think alike. :-)
          – HostileFork
          Nov 9 at 16:50




          @Groo Great minds think alike. :-)
          – HostileFork
          Nov 9 at 16:50

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229726%2fthe-purpose-of-wrapping-a-pointer-in-struct-in-c%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20