linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [Q]staging/comedi: Considation of *_find_boardinfo possible?
@ 2013-01-29 23:41 Peter Hüwe
  2013-01-29 23:56 ` H Hartley Sweeten
  2013-01-29 23:58 ` Joe Perches
  0 siblings, 2 replies; 7+ messages in thread
From: Peter Hüwe @ 2013-01-29 23:41 UTC (permalink / raw)
  To: Ian Abbott, linux-kernel
  Cc: ian Abbott, Dan Carpenter, Greg Kroah-Hartman, devel

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



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-01-31 16:43 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-29 23:41 [Q]staging/comedi: Considation of *_find_boardinfo possible? Peter Hüwe
2013-01-29 23:56 ` 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

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).