Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Structs with functions?

Reply
Thread Tools

Structs with functions?

 
 
robert
Guest
Posts: n/a
 
      10-29-2007
Hi all,

I'm trying to understand some code - u-boot 1.3rc3 - that reads data
from an ethernet phy. A struct is defined as:

struct mii_dev {
struct list_head link;
char *name;
int (* read)(char *devname, unsigned char addr,
unsigned char reg, unsigned short *value);
int (* write)(char *devname, unsigned char addr,
unsigned char reg, unsigned short value);
};

I've never seen syntax like this for its read and write members, how
does that work - what is doing the underlying read() and write() ?
Where is that defined? Here's an example of its use:

/
************************************************** ***************************
*
* Read to variable <value> from the PHY attached to device <devname>,
* use PHY address <addr> and register <reg>.
*
* Returns:
* 0 on success
*/
int miiphy_read(char *devname, unsigned char addr, unsigned char reg,
unsigned short *value)
{
struct list_head *entry;
struct mii_dev *dev;
int found_dev = 0;
int read_ret = 0;

if (!devname) {
printf("NULL device name!\n");
return 1;
}

list_for_each(entry, &mii_devs) {
dev = list_entry(entry, struct mii_dev, link);

if (strcmp(devname, dev->name) == 0) {
found_dev = 1;
read_ret = dev->read(devname, addr, reg,
value);
break;
}
}

if (found_dev == 0)
printf("No such device: %s\n", devname);

return ((found_dev) ? read_ret : 1);
}

Just trying to understand, please help,
Robert

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-29-2007
robert <(E-Mail Removed)> writes:

> I'm trying to understand some code - u-boot 1.3rc3 - that reads data
> from an ethernet phy. A struct is defined as:
>
> struct mii_dev {
> struct list_head link;
> char *name;
> int (* read)(char *devname, unsigned char addr,
> unsigned char reg, unsigned short *value);
> int (* write)(char *devname, unsigned char addr,
> unsigned char reg, unsigned short value);
> };
>
> I've never seen syntax like this for its read and write members, how
> does that work - what is doing the underlying read() and write() ?


read and write are pointers to functions. That is what the extra
brackets do:

char *f(int) /* f is function taking an int and returning a
char pointer */
char (*f)(int) /* f is pointer to function taking an int and
returning a char */

> Where is that defined? Here's an example of its use:

<snip>
> int miiphy_read(char *devname, unsigned char addr, unsigned char reg,
> unsigned short *value)
> {

<snip>
> }


Somewhere there will be a struct mii_dev object whose read field is
set to point to miiphy_read:

struct mii_dev actual_mii_device = {
.read = miiphy_read,
.write = miiphy_write,
};

(this uses the new "designator" syntax from C99 to initialise named
fields in the structure.

--
Ben.
 
Reply With Quote
 
 
 
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      10-29-2007
robert wrote:
> Hi all,
>
> I'm trying to understand some code - u-boot 1.3rc3 - that reads data
> from an ethernet phy. A struct is defined as:
>
> struct mii_dev {
> struct list_head link;
> char *name;
> int (* read)(char *devname, unsigned char addr,
> unsigned char reg, unsigned short *value);
> int (* write)(char *devname, unsigned char addr,
> unsigned char reg, unsigned short value);
> };
>
> I've never seen syntax like this for its read and write members, how
> does that work - what is doing the underlying read() and write() ?
> Where is that defined? Here's an example of its use:


The read and write members are not actual functions; they are pointers
to functions.
....
> struct mii_dev *dev;

....
> dev = list_entry(entry, struct mii_dev, link);


It is probably the case that list_entry() is what actually causes
those pointers to be initialized to point at some particular
functions. Since 'struct mii_dev' is a type name, list_entry is
presumably a function-like macro. Find out what the definition of
that macro is, and you'll have a better idea of what's going on.
However, it's quite possible that the setting of the 'read' and
'write' members occurs inside code that you have no access to, so it
won't necessarily be possible to track this down in full detail.

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      10-29-2007
Ben Bacarisse <(E-Mail Removed)> writes:

> char (*f)(int) /* f is pointer to function taking an int and
> returning a char */


Yeah, but returning a char pointer divided by what?
..
..
..
..
..
..
..
..
..
..<joke>
 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      10-29-2007
robert wrote:

> Hi all,
>
> I'm trying to understand some code - u-boot 1.3rc3 - that reads data
> from an ethernet phy. A struct is defined as:
>
> struct mii_dev {
> struct list_head link;
> char *name;
> int (* read)(char *devname, unsigned char addr,
> unsigned char reg, unsigned short *value);
> int (* write)(char *devname, unsigned char addr,
> unsigned char reg, unsigned short value);
> };
>
> I've never seen syntax like this for its read and write members, how
> does that work - what is doing the underlying read() and write() ?


Those two struct members are pointer to functions. Let say you have a
library and want to support two versions of mii_dev I/O:

int mii_dev_read_v1(char *devname, unsigned char addr, unsigned char reg,
unsigned short *value)
{
[...]
}

int mii_dev_read_v2(char *devname, unsigned char addr, unsigned char reg,
unsigned short *value)
{
[...]
}
etc.

then during initialization of struct mii_dev, you can dynamically assign
which version to use, like this:

struct mii_dev dev;

if (version == v1) {
dev->read = mii_dev_read_v1;
dev->write = mii_dev_write_v1;
} else {
dev->read = mii_dev_read_v2;
dev->write = mii_dev_write_v2;
}

Hence, the caller don't see the nasty details.

--
Tor <(E-Mail Removed) | tr i-za-h a-z>


 
Reply With Quote
 
fred.l.kleinschmidt@boeing.com
Guest
Posts: n/a
 
      10-29-2007
On Oct 29, 9:58 am, robert <(E-Mail Removed)> wrote:
> Hi all,
>
> I'm trying to understand some code - u-boot 1.3rc3 - that reads data
> from an ethernet phy. A struct is defined as:
>
> struct mii_dev {
> struct list_head link;
> char *name;
> int (* read)(char *devname, unsigned char addr,
> unsigned char reg, unsigned short *value);
> int (* write)(char *devname, unsigned char addr,
> unsigned char reg, unsigned short value);
>
> };
>
> I've never seen syntax like this for its read and write members, how
> does that work - what is doing the underlying read() and write() ?
> Where is that defined? Here's an example of its use:
>
> /
> ************************************************** ****************************
> *
> * Read to variable <value> from the PHY attached to device <devname>,
> * use PHY address <addr> and register <reg>.
> *
> * Returns:
> * 0 on success
> */
> int miiphy_read(char *devname, unsigned char addr, unsigned char reg,
> unsigned short *value)
> {
> struct list_head *entry;
> struct mii_dev *dev;
> int found_dev = 0;
> int read_ret = 0;
>
> if (!devname) {
> printf("NULL device name!\n");
> return 1;
> }
>
> list_for_each(entry, &mii_devs) {
> dev = list_entry(entry, struct mii_dev, link);
>
> if (strcmp(devname, dev->name) == 0) {
> found_dev = 1;
> read_ret = dev->read(devname, addr, reg,
> value);
> break;
> }
> }
>
> if (found_dev == 0)
> printf("No such device: %s\n", devname);
>
> return ((found_dev) ? read_ret : 1);
>
> }
>
> Just trying to understand, please help,
> Robert


They are function pointers.
Your program is expected to assign a function to do the task.
For example,

struct mii_dev myStruct;

myStruct.read = MyReadFunction;
myStruct.write = myWriteFunction;

where you have defined MyreadFunction and MyWriteFunction using
the prototypes specified in the struct definition; i.e., the read
function is a function that returns an int and has
arguments char *, unsigned char, unsigned char, and unsigned short *

--
Fred L. Kleinschmidt


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Packed structs vs. unpacked structs: what's the difference? Daniel Rudy C Programming 15 04-10-2006 08:10 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments