I see that it works, but I'm not exactly following how. Maybe I just don't have enough C experience. But I can't seem to duplicate that functionality. <div><br></div><div>I understand the way I've normally seen inheritance, where:</div>
<div><br></div><div>struct Child {</div><div> struct Parent p;</div><div> int somethingOnlyForChildren;</div><div>}<br></div><div><br></div>Or something similar. Then the Child struct can be casted as a Parent, and since there is no padding before the first piece of data in a struct, every parent attribute is accessed as though the struct were a Parent.<div>
<br></div><div>I see how it all lines up in memory, but in the case of the kernel code I can't quite follow it. Wouldn't everything assigned to 'inherits' be so much farther down in memory? I feel like I'm missing something. Is there a special way they all need to be accessed?</div>
<div><br></div><div>- Peter</div><div><div><br><div class="gmail_quote">On Thu, May 26, 2011 at 7:23 PM, Greg KH <span dir="ltr"><<a href="mailto:greg@kroah.com">greg@kroah.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On Thu, May 26, 2011 at 02:46:08PM -0600, Peter Hamilton wrote:<br>
> The code in drivers/ata/ uses an implementation of inheritance that I have not<br>
> seen before. It's only briefly explained in the header file ( include/linux/<br>
> libata.h:885):<br>
><br>
> /* <br>
> * ->inherits must be the last field and all the preceding<br>
> * fields must be pointers.<br>
> */<br>
><br>
> The structs are then initialized with .inherits assigned first:<br>
><br>
> drivers/ata/sata_nv.c:475<br>
><br>
> static struct ata_port_operations nv_nf2_ops = { <br>
> .inherits = &nv_generic_ops,<br>
> .freeze = nv_nf2_freeze, <br>
> .thaw = nv_nf2_thaw, <br>
> }; <br>
><br>
><br>
> Is this actually implementing inheritance? Why do all preceding fields need to<br>
> be pointers?<br>
><br>
> As far as I can tell, this style is only found in the ata drivers. <br>
<br>
</div></div>I think you are right, but more subsystems need to emulate it, it is<br>
very powerful and works very well. I have been wanting to convert the<br>
usb-serial layer to use the same thing one of these days.<br>
<div class="im"><br>
> Could anyone explain how this works?<br>
<br>
</div>The code is all there that shows it, but basically the driver is telling<br>
the core to "use this type of functions, but if I set any others, use<br>
them instead." It's a nicer way of doing inheritance in C than we do in<br>
other places in the kernel where we are a bit more "verbose" in making<br>
it happen.<br>
<br>
hope this helps,<br>
<br>
greg k-h<br>
</blockquote></div><br></div></div>