kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: greg@kroah.com (Greg KH)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Struct Inheritance in drivers/ata/
Date: Fri, 27 May 2011 09:23:42 +0800	[thread overview]
Message-ID: <20110527012342.GH24630@kroah.com> (raw)
In-Reply-To: <BANLkTimjsJEZKqzX=btryBoXD0FNAsW8Ag@mail.gmail.com>

On Thu, May 26, 2011 at 02:46:08PM -0600, Peter Hamilton wrote:
> The code in drivers/ata/ uses an implementation of inheritance that I have not
> seen before. ?It's only briefly explained in the header file ( include/linux/
> libata.h:885):
> 
> /* ?
> ?* ->inherits must be the last field and all the preceding
> ?* fields must be pointers.
> ?*/
> 
> The structs are then initialized with .inherits assigned first:
> 
> drivers/ata/sata_nv.c:475
> 
> static struct ata_port_operations nv_nf2_ops = { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
> ? ? ? ? .inherits ? ? ? ? ? ? ? = &nv_generic_ops,
> ? ? ? ? .freeze ? ? ? ? ? ? ? ? = nv_nf2_freeze, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
> ? ? ? ? .thaw ? ? ? ? ? ? ? ? ? = nv_nf2_thaw, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
> }; ? ? ?
> 
> 
> Is this actually implementing inheritance? ?Why do all preceding fields need to
> be pointers?
> 
> As far as I can tell, this style is only found in the ata drivers. ?

I think you are right, but more subsystems need to emulate it, it is
very powerful and works very well.  I have been wanting to convert the
usb-serial layer to use the same thing one of these days.

> Could anyone explain how this works?

The code is all there that shows it, but basically the driver is telling
the core to "use this type of functions, but if I set any others, use
them instead."  It's a nicer way of doing inheritance in C than we do in
other places in the kernel where we are a bit more "verbose" in making
it happen.

hope this helps,

greg k-h

  reply	other threads:[~2011-05-27  1:23 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-26 20:46 Struct Inheritance in drivers/ata/ Peter Hamilton
2011-05-27  1:23 ` Greg KH [this message]
2011-05-27  2:20   ` Peter Hamilton
2011-05-27  4:49     ` Ankit Jain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110527012342.GH24630@kroah.com \
    --to=greg@kroah.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).