This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: structure size


>>>>> "Grant" == Grant Edwards <grante@visi.com> writes:

    William> Although your answer is a 'better' solution in that
    William> it gives a developer more power over individual
    William> structures, I thought it would be interesting to turn it
    William> on once for an entire module.
    >> 
    >> That is usually a bad idea. When you #include system headers
    >> you would be defining data structures laid out differently
    >> from how the rest of the system understands those structures.
    >> Those headers may also define inline functions which
    >> manipulate those structures, and things quickly get very
    >> messy.
    >> 
    >> Also, when the compiler inserts padding it usually does so for
    >> a good reason: manipulating packed structures can involve a
    >> significant performance penalty.

    Grant> Not only that, on many systems (ARM, SPARC, H8, ...)
    Grant> manipulating packed structures can cause bus faults (if
    Grant> you're lucky) or silently produce incorrect results.

    Grant> Be very, very careful when packing structs. You can get
    Grant> bit. Or more likely, somebody else will get bit in the
    Grant> future when they try to figure out why your code stopped
    Grant> working...

Not necessarily true. When you use attribute(packed) you are telling
the compiler that some of the fields within the structure will not
be aligned to their natural boundaries, so the compiler has enough
information to do the right thing. i.e. generate additional code to
access fields a byte at a time, shifting as required. This is very
different from code that misaligns data without telling the compiler
about it, which will indeed lead to bus faults or incorrect results.

It is true that attribute(packed) is not mainstream, so there is an
increased risk that the compiler will fail to do the right thing.
There are also some things you really won't want to do, e.g. taking
the address of one field within a packed structure and then treating
that as an ordinary pointer. If there are problems it is worthwhile
checking exactly what code the compiler is generating.

Bart

-- 
Bart Veer                       eCos Configuration Architect
http://www.ecoscentric.com/     The eCos and RedBoot experts

-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]