Linux kernel: why do 'subclass' structs put base class info at end?

后端 未结 4 1981
走了就别回头了
走了就别回头了 2021-02-04 02:37

I was reading the chapter in Beautiful Code on the Linux kernel and the author discusses how Linux kernel implements inheritance in the C language (amongst other topics). In a

相关标签:
4条回答
  • 2021-02-04 03:04

    I don't have fresh experience from the Linux kernel, but from other kernels. I'd say that this doesn't matter at all.

    You are not supposed to cast from one to the other. Allowing casts like that should only be done in very specific situations. In most cases it reduces the robustness and flexibility of the code and is considered quite sloppy. So the deepest "architectural reason" you're looking for might just be "because that's the order someone happened to write it in". Or alternatively, that's what the benchmarks showed would be the best for performance of some important code path in that code. Or alternatively, the person who wrote it thinks it looks pretty (I always build upside-down pyramids in my variable declarations and structs if I have no other constraints). Or someone happened to write it this way 20 years ago and since then everyone else has been copying it.

    There might be some deeper design behind this, but I doubt it. There's just no reason to design those things at all. If you want to find out from an authoritative source why it's done this way, just submit a patch to linux that changes it and see who yells at you.

    0 讨论(0)
  • 2021-02-04 03:10

    It's for multiple inheritance. struct dev isn't the only interface you can apply to a struct in the linux kernel, and if you have more than one, just casting the sub class to a base class wouldn't work. For example:

    struct device {
         int a;
         int b;
         // etc...
    };
    
    struct asdf {
       int asdf_a;
    };
    
    struct usb_device {
        int usb_a;
        int usb_b;
        struct device dev;
        struct asdf asdf;
    };
    
    0 讨论(0)
  • 2021-02-04 03:12

    I'm new to the Linux kernel code, so take my ramblings here with a grain of salt. As far as I can tell, there is no requirement as to where to put the "subclass" struct. That is exactly what the macros provide: You can cast to the "subclass" structure, regardless of its layout. This provides robustness to your code (the layout of a structure can be changed, without having to change your code. Perhaps there is a convention of placing the "base class" struct at the end, but I'm not aware of it. I've seen lots of code in drivers, where different "base class" structs are used to cast back to the same "subclass" structure (from different fields in the "subclass" of course).

    0 讨论(0)
  • 2021-02-04 03:21

    The Amiga OS uses this "common header" trick in a lot of places and it looked like a good idea at the time: Subclassing by simply casting the pointer type. But there are drawbacks.

    Pro:

    • You can extend existing data structures
    • You can use the same pointer in all places where the base type is expected, no pointer arithmetic needed, saving precious cycles
    • It feels natural

    Con:

    • Different compilers tend to align data structures differently. If the base structure ended with char a;, then you could have 0, 1 or 3 pad bytes afterwards before the next field of the subclass starts. This led to quite nasty bugs, especially when you had to maintain backwards compatibility (i.e. for some reason, you have to have a certain padding because an ancient compiler version had a bug and now, there is lots of code which expects the buggy padding).
    • You don't notice quickly when you pass the wrong structure around. With the code in your question, fields get trashed very quickly if the pointer arithmetic is wrong. That is a good thing since it raises chances that a bug is discovered more early.
    • It leads to an attitude "my compiler will fix it for me" (which it sometimes won't) and all the casts lead to a "I know better than the compiler" attitude. The latter one would make you automatically insert casts before understanding the error message, which would lead to all kinds of odd problems.

    The Linux kernel is putting the common structure elsewhere; it can be but doesn't have to be at the end.

    Pro:

    • Bugs will show early
    • You will have to do some pointer arithmetic for every structure, so you're used to it
    • You don't need casts

    Con:

    • Not obvious
    • Code is more complex
    0 讨论(0)
提交回复
热议问题