producing the memory layout you really want is non-portable anddeprecated

Discussion in 'C Programming' started by aarklon@gmail.com, Apr 10, 2008.

  1. Guest

    Hi,

    I used to argue at this point that using memory-mapped IO made it
    easier to write device drivers in C, since you could map devices to C
    structures. Unfortunately, modern C compilers pad structures for
    performance reasons, and trying to coerce the compiler into producing
    the memory layout you really want is non-portable and deprecated. So
    I'm leaving the argument in place, but it's really for historical
    interest at this point. The comments about trying to create in and out
    instructions do remain valid, however.

    Also, when writing device drivers in C, it's a lot easier to work
    with when devices are in memory space. Let's suppose you have a simple
    device (for concreteness, let's look at the A/D convertor on an HC11.
    It's controlled by five registers (not counting the OPTION register),
    located at addresses $1030-$1034. We can define a struct that looks
    like this:

    see:- http://www.cs.nmsu.edu/~pfeiffer/classes/473/notes/io.html
     
    , Apr 10, 2008
    #1
    1. Advertising

  2. Richard Guest

    Re: producing the memory layout you really want is non-portable and deprecated

    writes:

    > Hi,
    >
    > I used to argue at this point that using memory-mapped IO made it
    > easier to write device drivers in C, since you could map devices to C
    > structures. Unfortunately, modern C compilers pad structures for
    > performance reasons, and trying to coerce the compiler into producing
    > the memory layout you really want is non-portable and deprecated. So
    > I'm leaving the argument in place, but it's really for historical
    > interest at this point. The comments about trying to create in and out
    > instructions do remain valid, however.
    >
    > Also, when writing device drivers in C, it's a lot easier to work
    > with when devices are in memory space. Let's suppose you have a simple
    > device (for concreteness, let's look at the A/D convertor on an HC11.
    > It's controlled by five registers (not counting the OPTION register),
    > located at addresses $1030-$1034. We can define a struct that looks
    > like this:
    >
    > see:- http://www.cs.nmsu.edu/~pfeiffer/classes/473/notes/io.html


    Which begs the question : since people have known for a long time that C
    was going to be used primarily for embedded systems in the future, why
    oh why is the support for non padded structures so bad? Sure some
    compilers have a pragma to remove padding but thats not "standard".

    The FAQ says this:

    http://c-faq.com/struct/padding.html

    I would be interested in seeing a general approach to this

    ,----
    | If you're worried about wasted space, you can minimize the effects of padding by ordering the members of a structure based on their base types, from largest to smallest.
    `----

    that is "standards compliant" and not "hard coded".
     
    Richard, Apr 10, 2008
    #2
    1. Advertising

  3. Ian Collins Guest

    Richard wrote:
    >
    > Which begs the question : since people have known for a long time that C
    > was going to be used primarily for embedded systems in the future, why
    > oh why is the support for non padded structures so bad? Sure some
    > compilers have a pragma to remove padding but thats not "standard".
    >

    Every embedded compiler (ant that's quite a lot!) I've used supported
    packed structures. I certainly would think many times about using one
    that didn't.

    --
    Ian Collins.
     
    Ian Collins, Apr 10, 2008
    #3
  4. Richard Guest

    Re: producing the memory layout you really want is non-portable and deprecated

    Ian Collins <> writes:

    > Richard wrote:
    >>
    >> Which begs the question : since people have known for a long time that C
    >> was going to be used primarily for embedded systems in the future, why
    >> oh why is the support for non padded structures so bad? Sure some
    >> compilers have a pragma to remove padding but thats not "standard".
    >>

    > Every embedded compiler (ant that's quite a lot!) I've used supported
    > packed structures. I certainly would think many times about using one
    > that didn't.


    Is the pragma line itself "standard"?
     
    Richard, Apr 10, 2008
    #4
  5. Ian Collins Guest

    Richard wrote:
    > Ian Collins <> writes:
    >
    >> Richard wrote:
    >>> Which begs the question : since people have known for a long time that C
    >>> was going to be used primarily for embedded systems in the future, why
    >>> oh why is the support for non padded structures so bad? Sure some
    >>> compilers have a pragma to remove padding but thats not "standard".
    >>>

    >> Every embedded compiler (ant that's quite a lot!) I've used supported
    >> packed structures. I certainly would think many times about using one
    >> that didn't.

    >
    > Is the pragma line itself "standard"?


    Don't be silly, how can you standardise something a lot of machines
    can't do?

    --
    Ian Collins.
     
    Ian Collins, Apr 10, 2008
    #5
  6. Richard Guest

    Re: producing the memory layout you really want is non-portable and deprecated

    Ian Collins <> writes:

    > Richard wrote:
    >> Ian Collins <> writes:
    >>
    >>> Richard wrote:
    >>>> Which begs the question : since people have known for a long time that C
    >>>> was going to be used primarily for embedded systems in the future, why
    >>>> oh why is the support for non padded structures so bad? Sure some
    >>>> compilers have a pragma to remove padding but thats not "standard".
    >>>>
    >>> Every embedded compiler (ant that's quite a lot!) I've used supported
    >>> packed structures. I certainly would think many times about using one
    >>> that didn't.

    >>
    >> Is the pragma line itself "standard"?

    >
    > Don't be silly, how can you standardise something a lot of machines
    > can't do?


    It could stop the compilation? Hence standard code base.
     
    Richard, Apr 10, 2008
    #6
  7. Ian Collins wrote:
    > Richard wrote:
    >> Ian Collins <> writes:
    >>
    >>> Richard wrote:
    >>>> Which begs the question : since people have known for a long time that C
    >>>> was going to be used primarily for embedded systems in the future, why
    >>>> oh why is the support for non padded structures so bad? Sure some
    >>>> compilers have a pragma to remove padding but thats not "standard".
    >>>>
    >>> Every embedded compiler (ant that's quite a lot!) I've used supported
    >>> packed structures. I certainly would think many times about using one
    >>> that didn't.

    >> Is the pragma line itself "standard"?

    >
    > Don't be silly, how can you standardise something a lot of machines
    > can't do?


    The same way you standardise IEEE floating point - that is, make it an
    optional part of the standard.

    Philip
     
    Philip Potter, Apr 10, 2008
    #7
    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. Eli Bendersky
    Replies:
    1
    Views:
    1,171
    Mike Treseler
    Mar 1, 2006
  2. Replies:
    1
    Views:
    588
    John Timney \(MVP\)
    Jun 19, 2006
  3. Replies:
    5
    Views:
    1,050
  4. Replies:
    7
    Views:
    921
  5. Replies:
    30
    Views:
    999
    Arne Vajhøj
    Feb 10, 2008
Loading...

Share This Page