C

Discussion in 'NZ Computing' started by Shane, Oct 16, 2006.

  1. Shane

    Shane Guest

    Probably not the best place to start, but with an opening like that how can
    I fail?

    Im looking at example code for linux modules (heh, its open source, the
    whole OS is example code!) and I see a lot of the following

    static int __init gotemp_init(void)

    My question is:
    What is the significance of the leading double underscore in the name?


    --
    Fry: Hey, what are these rings in Nibbler's fang?
    Vet: Hmm. I'm still a little woozy from a gazelle kick this morning, but if
    he's anything like the common tree, the rings might indicate his age.
    Fry: Heheh, yeah, well. Good luck. It'd take some kind of genius to count
    all those rings.
    Vet: He's 5.

    blog: http://shanes.dyndns.org
    Shane, Oct 16, 2006
    #1
    1. Advertising

  2. In message <egvd3v$6qj$>, Shane wrote:

    > Im looking at example code for linux modules ... and I see a lot of the
    > following
    >
    > static int __init gotemp_init(void)
    >
    > What is the significance of the leading double underscore in the name?


    It's purely a convention that leading underscores indicate a name with
    special use, internal use, something like that.

    In this case, __init is a macro that expands to special GCC directives that
    indicate that the code for the routine is to be put into
    an "initialization" section that can be discarded from memory once the
    kernel has been properly initialized. It's defined in source file
    include/linux/init.h <http://lxr.linux.no/source/include/linux/init.h>.
    Lawrence D'Oliveiro, Oct 16, 2006
    #2
    1. Advertising

  3. Shane

    Shane Guest

    Lawrence D'Oliveiro wrote:

    > In message <egvd3v$6qj$>, Shane wrote:
    >
    >> Im looking at example code for linux modules ... and I see a lot of the
    >> following
    >>
    >> static int __init gotemp_init(void)
    >>
    >> What is the significance of the leading double underscore in the name?

    >
    > It's purely a convention that leading underscores indicate a name with
    > special use, internal use, something like that.
    >
    > In this case, __init is a macro that expands to special GCC directives
    > that indicate that the code for the routine is to be put into
    > an "initialization" section that can be discarded from memory once the
    > kernel has been properly initialized. It's defined in source file
    > include/linux/init.h <http://lxr.linux.no/source/include/linux/init.h>.



    I have since found that it refers to internal functions, (internal to the
    Linux kernel), pretty much exactly as you say.

    I *think* I am going to look for a list of said functions




    --
    Leela: This society is a bunch of idiots.

    blog: http://shanes.dyndns.org
    Shane, Oct 16, 2006
    #3
  4. In message <eh0h7r$8ea$>, Shane wrote:

    > Lawrence D'Oliveiro wrote:
    >
    >> __init is a macro that expands to special GCC directives
    >> that indicate that the code for the routine is to be put into
    >> an "initialization" section that can be discarded from memory once the
    >> kernel has been properly initialized. It's defined in source file
    >> include/linux/init.h <http://lxr.linux.no/source/include/linux/init.h>.

    >
    > I *think* I am going to look for a list of said functions


    Any particular reason you're interested in functions marked __init?
    Lawrence D'Oliveiro, Oct 16, 2006
    #4
  5. Shane

    Shane Guest

    Lawrence D'Oliveiro wrote:

    > In message <eh0h7r$8ea$>, Shane wrote:
    >
    >> Lawrence D'Oliveiro wrote:
    >>
    >>> __init is a macro that expands to special GCC directives
    >>> that indicate that the code for the routine is to be put into
    >>> an "initialization" section that can be discarded from memory once the
    >>> kernel has been properly initialized. It's defined in source file
    >>> include/linux/init.h <http://lxr.linux.no/source/include/linux/init.h>.

    >>
    >> I *think* I am going to look for a list of said functions

    >
    > Any particular reason you're interested in functions marked __init?


    Nothing particular about __init, was just rnd(sample code)
    Ive been looking at drivers for linux in general, and noted this __ syntax,
    and wondered what it was


    --
    Leela: Kids don't turn rotten just from watching TV.
    Fry: Yeah. Give a little credit to our public schools.

    blog: http://shanes.dyndns.org
    Shane, Oct 17, 2006
    #5
  6. In message <eh1hqe$6eh$>, Shane wrote:

    > Lawrence D'Oliveiro wrote:
    >
    >> In message <eh0h7r$8ea$>, Shane wrote:
    >>
    >>> Lawrence D'Oliveiro wrote:
    >>>
    >>>> __init is a macro that expands to special GCC directives
    >>>> that indicate that the code for the routine is to be put into
    >>>> an "initialization" section that can be discarded from memory once the
    >>>> kernel has been properly initialized. It's defined in source file
    >>>> include/linux/init.h <http://lxr.linux.no/source/include/linux/init.h>.
    >>>
    >>> I *think* I am going to look for a list of said functions

    >>
    >> Any particular reason you're interested in functions marked __init?

    >
    > Nothing particular about __init, was just rnd(sample code)
    > Ive been looking at drivers for linux in general, and noted this __
    > syntax, and wondered what it was


    As far as C syntax is concerned, there's nothing special about the "_"
    character, it's considered equivalent to a letter.
    Lawrence D'Oliveiro, Oct 17, 2006
    #6
  7. Shane

    David Empson Guest

    Lawrence D'Oliveiro <_zealand> wrote:

    > In message <eh1hqe$6eh$>, Shane wrote:
    >
    > > Lawrence D'Oliveiro wrote:
    > >
    > >> Any particular reason you're interested in functions marked __init?

    > >
    > > Nothing particular about __init, was just rnd(sample code)
    > > Ive been looking at drivers for linux in general, and noted this __
    > > syntax, and wondered what it was

    >
    > As far as C syntax is concerned, there's nothing special about the "_"
    > character, it's considered equivalent to a letter.


    The ANSI C (89) standard says that identifiers prefixed with an
    underline (and some complex rules about the second character) are
    reserved for use by the C library (or platform-specific symbols defined
    by the compiler or library header files). If an application defines a
    symbol starting with an underline, it risks portability problems.

    I haven't read the C99 standard in detail so I don't know if it has
    changed the rules.
    --
    David Empson
    David Empson, Oct 17, 2006
    #7
  8. Shane

    jasen Guest

    On 2006-10-16, Shane <-a-geek.net> wrote:
    > Probably not the best place to start, but with an opening like that how can
    > I fail?
    >
    > Im looking at example code for linux modules (heh, its open source, the
    > whole OS is example code!) and I see a lot of the following
    >
    > static int __init gotemp_init(void)
    >
    > My question is:
    > What is the significance of the leading double underscore in the name?


    There is only one underscore in the name of the function.

    __init is something other than an identifier.


    things with double underscores often have a special meaning to the compiler
    but I don't see __init in the gcc info page, so it must be a macro defined
    somewhere before gotemp_init




    --

    Bye.
    Jasen
    jasen, Oct 17, 2006
    #8
  9. Shane

    Shane Guest

    jasen wrote:

    > On 2006-10-16, Shane <-a-geek.net> wrote:
    >> Probably not the best place to start, but with an opening like that how
    >> can I fail?
    >>
    >> Im looking at example code for linux modules (heh, its open source, the
    >> whole OS is example code!) and I see a lot of the following
    >>
    >> static int __init gotemp_init(void)
    >>
    >> My question is:
    >> What is the significance of the leading double underscore in the name?

    >
    > There is only one underscore in the name of the function.
    >
    > __init is something other than an identifier.
    >
    >
    > things with double underscores often have a special meaning to the
    > compiler but I don't see __init in the gcc info page, so it must be a
    > macro defined somewhere before gotemp_init
    >


    Bearing in mind its the double underscore I am trying to grok, and not
    init...
    I have the linux kernel source and am in one of the drivers directorys

    linux-2.6.18.1/drivers/media/video$ grep __init *
    adv7170.c:static int __init
    adv7175.c:static int __init
    arv.c:static int __init ar_init(void)
    arv.c:static int __init ar_init_module(void)
    bt819.c:static int __init
    bt856.c:static int __init
    bw-qcam.c:static int __init init_bw_qcams(void)
    cpia.c:static int __init cpia_init(void)
    cpia_pp.c:static int parport_nr[PARPORT_MAX] __initdata =
    cpia_pp.c:static int __init cpia_pp_setup(char *str)
    cpia_usb.c:static int __init usb_cpia_init(void)
    c-qcam.c:static int __init cqcam_init (void)
    cs53l32a.c:static int __init cs53l32a_init_module(void)
    dabusb.c:static int __init dabusb_init (void)
    dpc7146.c:static int __init dpc_init_module(void)
    hexium_gemini.c:static int __init hexium_init_module(void)
    hexium_orion.c:static int __init hexium_init_module(void)
    indycam.c:static int __init indycam_init(void)
    ir-kbd-i2c.c:static int __init ir_init(void)
    meye.c:static int __init meye_init(void)
    msp3400-driver.c:static int __init msp3400_init_module(void)
    mxb.c:static int __init mxb_init_module(void)
    ov511.c:static int __init
    planb.c:static int __init init_planbs(void)
    pms.c:static int __init init_pms_cards(void)
    saa5246a.c:static int __init init_saa_5246a (void)
    saa5249.c:static int __init init_saa_5249 (void)
    saa6588.c:static int __init saa6588_init_module(void)
    saa7110.c:static int __init
    saa7111.c:static int __init
    saa7114.c:static int __init
    saa7115.c:static int __init saa7115_init_module(void)
    saa711x.c:static int __init
    saa7127.c:static int __init saa7127_init_module(void)
    saa7185.c:static int __init
    se401.c:static int __init usb_se401_init(void)
    stradis.c:static int __init stradis_init(void)
    stv680.c:static int __init usb_stv680_init (void)
    tda7432.c:static int __init tda7432_init(void)
    tda9840.c:static int __init this_module_init(void)
    tda9875.c:static int __init tda9875_init(void)
    tea6415c.c:static int __init this_module_init(void)
    tea6420.c:static int __init this_module_init(void)
    tlv320aic23b.c:static int __init tlv320aic23b_init_module(void)
    tuner-3036.c:static int __init
    tuner-core.c:static int __init tuner_init_module(void)
    tvaudio.c:static int __init audiochip_init_module(void)
    tveeprom.c:static int __init tveeprom_init(void)
    tvmixer.c:static int __init tvmixer_init_module(void)
    tvp5150.c:static int __init tvp5150_init(void)
    upd64031a.c:static int __init upd64031a_init_module(void)
    upd64083.c:static int __init upd64083_init_module(void)
    videocodec.c:static int __init
    videodev.c:static int __init videodev_init(void)
    vino.c:// __initdata
    vino.c:static int __init vino_module_init(void)
    vivi.c:static int __init vivi_init(void)
    vpx3220.c:static int __init
    w9966.c:static int __init w9966_mod_init(void)
    w9968cf.c:static int __init w9968cf_module_init(void)
    wm8739.c:static int __init wm8739_init_module(void)
    wm8775.c:static int __init wm8775_init_module(void)
    zoran_card.c:static int __init
    zr36016.c:static int __init
    zr36050.c:static int __init
    zr36060.c:static int __init
    zr36120.c:void __init handle_chipset(void)
    zr36120.c:int __init find_zoran(void)
    zr36120.c:int __init init_zoran(int card)
    zr36120.c:int __init zr36120_init(void)


    I think google has the last say here
    http://www.gatago.com/linux/kernel/15476882.html
    C99:
    "All identifiers that begin with an underscore and either an uppercase
    letter or another underscore are always reserved for any use."

    That applies to normal userspace programs. It allows compiler, libc
    and possibly the kernel to introduce new identifiers without breaking
    existing programs. If programs break, it is their fault, they
    shouldn't have used such identifiers.

    The kernel is quite different. Basically, all identifiers belong to
    the kernel, none to libc/gcc (with very few exceptions). So
    developers are free to use that at their pleasure and most stick to
    your description in the first paragraph.

    Jörn



    So putting this all together, am I right to assume __foo refers to an
    internal (to the kernel) function?

    --
    Amy: Lucky I'm a member of the Austro-Afro-Antarctico-Amer-Asian Auto
    Association. Hello, Septuple-A?

    blog: http://shanes.dyndns.org
    Shane, Oct 17, 2006
    #9
    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.

Share This Page