Structs with functions?

Discussion in 'C Programming' started by robert, Oct 29, 2007.

  1. robert

    robert Guest

    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
     
    robert, Oct 29, 2007
    #1
    1. Advertising

  2. robert <> 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.
     
    Ben Bacarisse, Oct 29, 2007
    #2
    1. Advertising

  3. robert

    Guest

    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.
     
    , Oct 29, 2007
    #3
  4. robert

    Richard Guest

    Ben Bacarisse <> 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>
     
    Richard, Oct 29, 2007
    #4
  5. robert

    Tor Rustad Guest

    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 < | tr i-za-h a-z>
     
    Tor Rustad, Oct 29, 2007
    #5
  6. robert

    Guest

    On Oct 29, 9:58 am, 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:
    >
    > /
    > ***************************************************************************­**
    > *
    > * 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
     
    , Oct 29, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    646
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    561
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    647
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,411
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    479
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page