Structs with functions?

R

robert

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
 
B

Ben Bacarisse

robert said:
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:
int miiphy_read(char *devname, unsigned char addr, unsigned char reg,
unsigned short *value)
{

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

jameskuyper

robert said:
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.
 
R

Richard

Ben Bacarisse said:
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>
 
T

Tor Rustad

robert said:
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.
 
F

fred.l.kleinschmidt

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 *
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top