enum data types in kernel


Results 1 to 5 of 5

Thread: enum data types in kernel

  1. #1
    Join Date
    Nov 2000
    Location
    Ireland
    Posts
    51

    enum data types in kernel

    The following file,
    /usr/src/linux-2.4.27/include/linux/pkt_cls.h makes use of enumerated data types. But it does not give the data types a name. eg. It is a header file that contains constants ofr creating a filter (part of linux traffic control)

    enum
    {
    TCA_FW_UNSPEC,
    TCA_FW_CLASSID,
    TCA_FW_POLICE,
    __TCA_FW_MAX
    };



    enum
    {
    TCA_TCINDEX_UNSPEC,
    TCA_TCINDEX_HASH,
    TCA_TCINDEX_MASK,
    TCA_TCINDEX_SHIFT,
    TCA_TCINDEX_FALL_THROUGH,
    TCA_TCINDEX_CLASSID,
    TCA_TCINDEX_POLICE,
    __TCA_TCINDEX_MAX
    };



    Why doesnt it give the enumerated data type a name? and how can you access the enumerated constants?

  2. #2
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,950
    Because in C, enums don't need a name.

    IIRC, in C, an enum is exactly equivalent to an integer. Any int variable can take on an enum's value.

    The only thing that giving it a name (actually a typedef) does for you is allows you to declare variables to be that type. Then, the compiler will at least warn you if you assign an integer to the variable (some compilers may make it an error (all C++ compilers do), but if you add a cast, it will always work without even a warning).

    In short, the only thing that giving it a name buys you is some compiler warnings if you're not smart enough to keep track of, or check, the valid values yourself.

    As for how to refer to the constants, you do it like this:

    Code:
    int tca_val = TCA_FW_POLICE;
    int tca_val2 = TCA_TCINDEX_HASH;
    And I have to ask: Are you asking this because you want to use those constants, or just because you're curious? If you want to use those constants, you should NOT be including that header file to get them...

  3. #3
    Join Date
    Nov 2000
    Location
    Ireland
    Posts
    51
    ok i get it now...

    So these constants are like #defines.
    I though you had to create a varaible of type enumerated (as in the e.g. below) and then you could set the variable to hold the value of one of the constants of that enumerated type.


    the following is the same, right ?


    enum prs {PAPER,ROCK,SCISSORS};

    enum prs game;

    game = PAPER;




    enum {PAPER,ROCK,SCISSORS};

    int game;

    game = PAPER;




    btw Im just going through the source code of Linux traffic control and am just curious.
    Last edited by ejlynch; 11-24-2004 at 05:39 AM.

  4. #4
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,950
    Yes, they are the same. You can even do this:

    Code:
    enum prs {PAPER, ROCK, SCISSORS};
    
    int game = PAPER;
    i.e. name the enum but don't use the typename when declaring "game", if you want, but it doesn't buy you anything.

    The only difference between your two examples is that if you have another enum:

    Code:
    enum abc {A, B, C};
    and you try to do this:

    Code:
    enum prs game;
    
    game = A;
    then the compiler (in C mode) will give a warning. In C++ mode, it will give an error. Both can be avoided like so:

    Code:
    enum prs game;
    
    game = (enum prs)A;
    In either case -- with or without the cast -- game will compare equal to PAPER (or A, if you add a cast to the comparison), because PAPER and A both have the same underlying value: 0.

    If you declare game as an int, then you can assign it a constant from either the prs enum or the abc enum.

  5. #5
    Join Date
    Sep 2013
    Posts
    1
    I'm looking at a file in the include/linux directory called 'power_supply.h'. It has many enums. For example:

    enum power_supply_property {
    POWER_SUPPLY_PROP_STATUS = 0,
    POWER_SUPPLY_PROP_CHARGE_TYPE,
    POWER_SUPPLY_PROP_HEALTH,
    POWER_SUPPLY_PROP_PRESENT,
    POWER_SUPPLY_PROP_ONLINE,
    ....

    and so on..

    If I add some elements to these enums, will that cause problems? Will it cause incorrect functioning of drivers?

    Andy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •