linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Peter Hüwe" <PeterHuewe@gmx.de>
To: Ian Abbott <abbotti@mev.co.uk>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Cc: ian Abbott <ian.abbott@mev.co.uk>,
	Dan Carpenter <dan.carpenter@oracle.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"devel@linuxdriverproject.org" <devel@linuxdriverproject.org>
Subject: [Q]staging/comedi: Considation of *_find_boardinfo possible?
Date: Wed, 30 Jan 2013 00:41:35 +0100	[thread overview]
Message-ID: <201301300041.36064.PeterHuewe@gmx.de> (raw)

Hi,

while analyzing the comedi drivers, I noticed that quite a lot of them use a 
more or less similar find_boardinfo function.
e.g.: 
cb_pcidas64.c 

static const struct pcidas64_board
*cb_pcidas64_find_pci_board(struct pci_dev *pcidev)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(pcidas64_boards); i++)
		if (pcidev->device == pcidas64_boards[i].device_id)
			return &pcidas64_boards[i];
	return NULL;
}

and ni_6527.c:
static const struct ni6527_board *
ni6527_find_boardinfo(struct pci_dev *pcidev)
{
	unsigned int dev_id = pcidev->device;
	unsigned int n;

	for (n = 0; n < ARRAY_SIZE(ni6527_boards); n++) {
		const struct ni6527_board *board = &ni6527_boards[n];
		if (board->dev_id == dev_id)
			return board;
	}
	return NULL;
}




The names and the exact implementation differ slightly, but in most cases it 
boils down to:
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(__BOARD_ARRAY__); i++)
		if (pcidev->device == __BOARD_ARRAY__[i].device_id)
			return &__BOARD_ARRAY__[i];
	return NULL;

unfortunately the __BOARD_ARRAY__ is always of a different type (but all 
contain the device_id field) and size.


---> is there a way to consolidate these functions into one function (which 
can operate on the different types) ?  It's almost a bit like 'templates'.
Maybe with some gcc extensions or kernel magic functions ?

I already thought about passing a void pointer to the __BOARD_ARRAY__ and the 
size of one element of the __BOARD_ARRAY__ and doing pointer calculations - 
but I guess there must be a better way.

Or is the only option to write a macro ?


Looking forward to your replies.

Thanks,
Peter



             reply	other threads:[~2013-01-29 23:38 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-29 23:41 Peter Hüwe [this message]
2013-01-29 23:56 ` [Q]staging/comedi: Considation of *_find_boardinfo possible? H Hartley Sweeten
2013-01-30 11:04   ` Ian Abbott
2013-01-30 11:06     ` Ian Abbott
2013-01-30 17:54     ` H Hartley Sweeten
2013-01-31 16:43       ` Ian Abbott
2013-01-29 23:58 ` Joe Perches

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=201301300041.36064.PeterHuewe@gmx.de \
    --to=peterhuewe@gmx.de \
    --cc=abbotti@mev.co.uk \
    --cc=dan.carpenter@oracle.com \
    --cc=devel@linuxdriverproject.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=ian.abbott@mev.co.uk \
    --cc=linux-kernel@vger.kernel.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).