public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] pragma pack
@ 2008-01-09 14:19 Alexandre
  2008-01-09 14:40 ` Andrew Lunn
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Alexandre @ 2008-01-09 14:19 UTC (permalink / raw)
  To: ecos-discuss

Hi everybody (and happy new year would i add),

I'd like to use the pragma pack(1) in one of my applications to align
properly a 5 bytes structures array into flash.
I know that packing reduces greatly the performances of the system but
performance is not really my problem here as i'm actually looking for
more memory and would greatly need to pack things up a little bit.

I tried to declare the next structure as shown, with the pragmas, but
it's still 8 bytes large, either read with a sizeof(pelco_pattern) or
after being written in rom:

#pragma pack(1)
typedef struct {
   cyg_uint8 command2_byte;
   cyg_uint8 data3;
   cyg_uint8 data4;
   cyg_uint16 timestamp;
} pelco_pattern;
#pragma pack()


Am I doing something wrong here ? Is it even possible to use "pack"'s
pragmas within ecos ? Am I missing some compiler directives ?

I am using the arm-elf-gcc compiler and the lpc2xxx / arm version of ecos.
The compiler does not say anything about the line with the pragma in it.

Thanks in advance for you answers ^^

Alex Garcia
Hymatom SA

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [ECOS] pragma pack
  2008-01-09 14:19 [ECOS] pragma pack Alexandre
@ 2008-01-09 14:40 ` Andrew Lunn
  2008-01-09 14:51 ` Bernard Fouché
  2008-01-09 15:02 ` [ECOS] " EXTERNAL Gladis Olaf (Praktikant; CR/AEM)
  2 siblings, 0 replies; 12+ messages in thread
From: Andrew Lunn @ 2008-01-09 14:40 UTC (permalink / raw)
  To: Alexandre; +Cc: ecos-discuss

On Wed, Jan 09, 2008 at 03:19:28PM +0100, Alexandre wrote:
> Hi everybody (and happy new year would i add),
> 
> I'd like to use the pragma pack(1) in one of my applications to align
> properly a 5 bytes structures array into flash.
> I know that packing reduces greatly the performances of the system but
> performance is not really my problem here as i'm actually looking for
> more memory and would greatly need to pack things up a little bit.
> 
> I tried to declare the next structure as shown, with the pragmas, but
> it's still 8 bytes large, either read with a sizeof(pelco_pattern) or
> after being written in rom:
> 
> #pragma pack(1)
> typedef struct {
>    cyg_uint8 command2_byte;
>    cyg_uint8 data3;
>    cyg_uint8 data4;
>    cyg_uint16 timestamp;
> } pelco_pattern;
> #pragma pack()

Is this legal for gcc? I always use attributes. Take a look at 

http://gcc.gnu.org/onlinedocs/gcc-4.2.2/gcc/Type-Attributes.html#Type-Attributes

        Andrew

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [ECOS] pragma pack
  2008-01-09 14:19 [ECOS] pragma pack Alexandre
  2008-01-09 14:40 ` Andrew Lunn
@ 2008-01-09 14:51 ` Bernard Fouché
  2008-01-09 14:56   ` Alexandre
  2008-01-09 15:02 ` [ECOS] " EXTERNAL Gladis Olaf (Praktikant; CR/AEM)
  2 siblings, 1 reply; 12+ messages in thread
From: Bernard Fouché @ 2008-01-09 14:51 UTC (permalink / raw)
  To: thekyz; +Cc: ecos-discuss

First try reorganizing your structure to have the biggest item at the 
beginning, that will avoid 'loosing' intermediary bytes:

typedef struct {
   cyg_uint16 timestamp;
   cyg_uint8 command2_byte;
   cyg_uint8 data3;
   cyg_uint8 data4;
} pelco_pattern;

Your target may also require that all items have to be 16 bits aligned 
(that would explain whyt #prama pack(1) gave nothing). In that case try, 
as you are not looking for speed:

typedef struct {
   cyg_uint16 timestamp;
   cyg_uint16 command2_byte_and_data3;
   cyg_uint8 data4;
} pelco_pattern;

With 16bits alignment you should fall back to a 6 bytes data structure, but you'll need code to manage 'command2_byte' and 'data3', so this will add to your program size, as it will slow processing.

Hope it helps!

  Bernard

Alexandre wrote:
> Hi everybody (and happy new year would i add),
>
> I'd like to use the pragma pack(1) in one of my applications to align
> properly a 5 bytes structures array into flash.
> I know that packing reduces greatly the performances of the system but
> performance is not really my problem here as i'm actually looking for
> more memory and would greatly need to pack things up a little bit.
>
> I tried to declare the next structure as shown, with the pragmas, but
> it's still 8 bytes large, either read with a sizeof(pelco_pattern) or
> after being written in rom:
>
> #pragma pack(1)
> typedef struct {
>    cyg_uint8 command2_byte;
>    cyg_uint8 data3;
>    cyg_uint8 data4;
>    cyg_uint16 timestamp;
> } pelco_pattern;
> #pragma pack()
>
>
> Am I doing something wrong here ? Is it even possible to use "pack"'s
> pragmas within ecos ? Am I missing some compiler directives ?
>
> I am using the arm-elf-gcc compiler and the lpc2xxx / arm version of ecos.
> The compiler does not say anything about the line with the pragma in it.
>
> Thanks in advance for you answers ^^
>
> Alex Garcia
> Hymatom SA
>
>   


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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [ECOS] pragma pack
  2008-01-09 14:51 ` Bernard Fouché
@ 2008-01-09 14:56   ` Alexandre
  2008-01-09 21:55     ` Bronislav Gabrhelik
  0 siblings, 1 reply; 12+ messages in thread
From: Alexandre @ 2008-01-09 14:56 UTC (permalink / raw)
  To: Bernard Fouché; +Cc: ecos-discuss

Thanks a lot to all of you,

I'm gonna try what you wrote to see if it works Olaf, but what Andrew
told me resolved the problem. My structure is now defined like this:

typedef struct __attribute__ ((__packed__)) s_pelco_pattern_array{
       cyg_uint8 command2_byte;
       cyg_uint8       data3;
       cyg_uint8       data4;
       cyg_uint16      timestamp;
} pelco_pattern_array;

and is actually 5 bytes large :)

I was reffering to this page of the gcc doc for the "pack" pragma and
thought it would work, maybe what Bernard pointed should also help
resolve the problem about the pragma:

http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html

Again, thanks a lot, i'm still gonna push a little my research on this
matter and i'll get you all updated.

Alex Garcia
Hymatom SA

PS: Sorry for the double mail Bernard :(

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: [ECOS] pragma pack
  2008-01-09 14:19 [ECOS] pragma pack Alexandre
  2008-01-09 14:40 ` Andrew Lunn
  2008-01-09 14:51 ` Bernard Fouché
@ 2008-01-09 15:02 ` EXTERNAL Gladis Olaf (Praktikant; CR/AEM)
  2 siblings, 0 replies; 12+ messages in thread
From: EXTERNAL Gladis Olaf (Praktikant; CR/AEM) @ 2008-01-09 15:02 UTC (permalink / raw)
  To: thekyz, ecos-discuss

Sorry forget to reply to all:

Hi Alex,

I am not 100% sure but i think you have to write it like this:

#pragma pack(push,1)
typedef struct {
   cyg_uint8 command2_byte;
   cyg_uint8 data3;
   cyg_uint8 data4;
   cyg_uint16 timestamp;
} pelco_pattern;
#pragma pack(pop)

You have to add push and pop in you code.
So try this and write if this is helping you.

Greets

Olaf Gladis 

-----Original Message-----
From: ecos-discuss-owner@ecos.sourceware.org
[mailto:ecos-discuss-owner@ecos.sourceware.org] On Behalf Of Alexandre
Sent: Mittwoch, 9. Januar 2008 15:19
To: ecos-discuss
Subject: [ECOS] pragma pack

Hi everybody (and happy new year would i add),

I'd like to use the pragma pack(1) in one of my applications to align
properly a 5 bytes structures array into flash.
I know that packing reduces greatly the performances of the system but
performance is not really my problem here as i'm actually looking for
more memory and would greatly need to pack things up a little bit.

I tried to declare the next structure as shown, with the pragmas, but
it's still 8 bytes large, either read with a sizeof(pelco_pattern) or
after being written in rom:

#pragma pack(1)
typedef struct {
   cyg_uint8 command2_byte;
   cyg_uint8 data3;
   cyg_uint8 data4;
   cyg_uint16 timestamp;
} pelco_pattern;
#pragma pack()


Am I doing something wrong here ? Is it even possible to use "pack"'s
pragmas within ecos ? Am I missing some compiler directives ?

I am using the arm-elf-gcc compiler and the lpc2xxx / arm version of
ecos.
The compiler does not say anything about the line with the pragma in it.

Thanks in advance for you answers ^^

Alex Garcia
Hymatom SA

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


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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [ECOS] pragma pack
  2008-01-09 14:56   ` Alexandre
@ 2008-01-09 21:55     ` Bronislav Gabrhelik
  2008-01-09 22:22       ` Paul D. DeRocco
  0 siblings, 1 reply; 12+ messages in thread
From: Bronislav Gabrhelik @ 2008-01-09 21:55 UTC (permalink / raw)
  To: thekyz; +Cc: eCos discuss mailing list

Beware of bad alignment of timestamp field. Some processors might
raise an exception/interrupt when you use the int like operation
directly on field (including assignment & evaluation). You should copy
out/in this by memcpy into/from temporary variable of the same type
when you need to do some integer computing on this field. In other
case your code might not be portable.

Best Regards
Bronislav Gabrhelik

2008/1/9, Alexandre <thekyz@gmail.com>:
> Thanks a lot to all of you,
>
> I'm gonna try what you wrote to see if it works Olaf, but what Andrew
> told me resolved the problem. My structure is now defined like this:
>
> typedef struct __attribute__ ((__packed__)) s_pelco_pattern_array{
>       cyg_uint8 command2_byte;
>       cyg_uint8       data3;
>       cyg_uint8       data4;
>       cyg_uint16      timestamp;
> } pelco_pattern_array;
>
> and is actually 5 bytes large :)
>
> I was reffering to this page of the gcc doc for the "pack" pragma and
> thought it would work, maybe what Bernard pointed should also help
> resolve the problem about the pragma:
>
> http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html
>
> Again, thanks a lot, i'm still gonna push a little my research on this
> matter and i'll get you all updated.
>
> Alex Garcia
> Hymatom SA
>
> PS: Sorry for the double mail Bernard :(
>
> --
> Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
> and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss
>
>

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: [ECOS] pragma pack
  2008-01-09 21:55     ` Bronislav Gabrhelik
@ 2008-01-09 22:22       ` Paul D. DeRocco
  2008-01-09 23:50         ` [ECOS] " Grant Edwards
  0 siblings, 1 reply; 12+ messages in thread
From: Paul D. DeRocco @ 2008-01-09 22:22 UTC (permalink / raw)
  To: ecos-discuss

> From: Bronislav Gabrhelik
> 
> Beware of bad alignment of timestamp field. Some processors 
> might raise an exception/interrupt when you use the int like 
> operation directly on field (including assignment & 
> evaluation). You should copy out/in this by memcpy into/from 
> temporary variable of the same type when you need to do some 
> integer computing on this field. In other case your code 
> might not be portable.

I think that's the point of the __packed__ attribute: it forces the compiler
for any machine on which it matters to do the necessary byte
packing/unpacking automagically. I know that's what the Gnu ARM compiler
does.

-- 

Ciao,               Paul D. DeRocco
Paul                mailto:pderocco@ix.netcom.com 


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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ECOS]  Re: pragma pack
  2008-01-09 22:22       ` Paul D. DeRocco
@ 2008-01-09 23:50         ` Grant Edwards
  2008-01-10  0:50           ` Paul D. DeRocco
  0 siblings, 1 reply; 12+ messages in thread
From: Grant Edwards @ 2008-01-09 23:50 UTC (permalink / raw)
  To: ecos-discuss

On 2008-01-09, Paul D. DeRocco <pderocco@ix.netcom.com> wrote:
>> From: Bronislav Gabrhelik
>> 
>> Beware of bad alignment of timestamp field. Some processors 
>> might raise an exception/interrupt when you use the int like 
>> operation directly on field (including assignment & 
>> evaluation). You should copy out/in this by memcpy into/from 
>> temporary variable of the same type when you need to do some 
>> integer computing on this field. In other case your code 
>> might not be portable.
>
> I think that's the point of the __packed__ attribute: it
> forces the compiler for any machine on which it matters to do
> the necessary byte packing/unpacking automagically. I know
> that's what the Gnu ARM compiler does.

The trap snaps shut when you take the address of a field in a
packed struct and pass that to somebody who's expecting a
pointer to a normally aligned value.

-- 
Grant Edwards                   grante             Yow! I am covered with
                                  at               pure vegetable oil and I am
                               visi.com            writing a best seller!


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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: [ECOS]  Re: pragma pack
  2008-01-09 23:50         ` [ECOS] " Grant Edwards
@ 2008-01-10  0:50           ` Paul D. DeRocco
  2008-01-10  9:43             ` Alexandre
  0 siblings, 1 reply; 12+ messages in thread
From: Paul D. DeRocco @ 2008-01-10  0:50 UTC (permalink / raw)
  To: ecos-discuss

> From: Grant Edwards
> 
> The trap snaps shut when you take the address of a field in a 
> packed struct and pass that to somebody who's expecting a 
> pointer to a normally aligned value.

True. I could imagine ways in which the type system could be designed to
prevent that, using a pointer modifier like "int __packed*", but it's not.

Anyway, for normal accesses, it works fine, so you can do things like
operate on the fields in a DOS boot sector without problems. That's the only
situation I've ever needed the feature.

-- 

Ciao,               Paul D. DeRocco
Paul                mailto:pderocco@ix.netcom.com 


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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [ECOS] Re: pragma pack
  2008-01-10  0:50           ` Paul D. DeRocco
@ 2008-01-10  9:43             ` Alexandre
  2008-01-10 10:07               ` EXTERNAL Gladis Olaf (Praktikant; CR/AEM)
  0 siblings, 1 reply; 12+ messages in thread
From: Alexandre @ 2008-01-10  9:43 UTC (permalink / raw)
  To: Paul D. DeRocco; +Cc: ecos-discuss

Actually i had some problems accessing my packed structures without
flows, I think that may help resolve things a bit.
Eg with things like that:

cyg_uint8 i = 0;
struct s_pelco_pattern_array a;

a.command2_byte = i++;
a.data3 = i++;
a.data4 = i++;
a.timestamp = i;

I finish having a structure which looks like this in memory:

cm2: 0x00
data3: 0x01
data4: 0x02
ts: 0x0300

Which is really not what i want to have eventually.

On Jan 10, 2008 1:50 AM, Paul D. DeRocco <pderocco@ix.netcom.com> wrote:
> > From: Grant Edwards
> >
> > The trap snaps shut when you take the address of a field in a
> > packed struct and pass that to somebody who's expecting a
> > pointer to a normally aligned value.
>
> True. I could imagine ways in which the type system could be designed to
> prevent that, using a pointer modifier like "int __packed*", but it's not.
>
> Anyway, for normal accesses, it works fine, so you can do things like
> operate on the fields in a DOS boot sector without problems. That's the only
> situation I've ever needed the feature.
>
> --
>
> Ciao,               Paul D. DeRocco
> Paul                mailto:pderocco@ix.netcom.com
>
>
> --
>
> Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
> and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss
>
>

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: [ECOS] Re: pragma pack
  2008-01-10  9:43             ` Alexandre
@ 2008-01-10 10:07               ` EXTERNAL Gladis Olaf (Praktikant; CR/AEM)
  2008-01-10 10:29                 ` Alexandre
  0 siblings, 1 reply; 12+ messages in thread
From: EXTERNAL Gladis Olaf (Praktikant; CR/AEM) @ 2008-01-10 10:07 UTC (permalink / raw)
  To: thekyz, Paul D. DeRocco; +Cc: ecos-discuss

 

-----Original Message-----
From: ecos-discuss-owner@ecos.sourceware.org
[mailto:ecos-discuss-owner@ecos.sourceware.org] On Behalf Of Alexandre
Sent: Donnerstag, 10. Januar 2008 10:43
To: Paul D. DeRocco
Cc: ecos-discuss@ecos.sourceware.org
Subject: Re: [ECOS] Re: pragma pack

> Actually i had some problems accessing my packed structures without
> flows, I think that may help resolve things a bit.
> Eg with things like that:
> 
> cyg_uint8 i = 0;
> struct s_pelco_pattern_array a;
> 
> a.command2_byte = i++;
> a.data3 = i++;
> a.data4 = i++;
> a.timestamp = i;
> 
> I finish having a structure which looks like this in memory:
> 
> cm2: 0x00
> data3: 0x01
> data4: 0x02
> ts: 0x0300
> 
> Which is really not what i want to have eventually.
> 

I would say this looks correct.
Maybe you thought there must be 0x0003 in the last one?

But if you have a little endian CPU it swaps the bytes.
If you have more questions about Endianness take a look at

http://en.wikipedia.org/wiki/Endianness

Greets olaf

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [ECOS] Re: pragma pack
  2008-01-10 10:07               ` EXTERNAL Gladis Olaf (Praktikant; CR/AEM)
@ 2008-01-10 10:29                 ` Alexandre
  0 siblings, 0 replies; 12+ messages in thread
From: Alexandre @ 2008-01-10 10:29 UTC (permalink / raw)
  To: EXTERNAL Gladis Olaf (Praktikant, CR/AEM); +Cc: Paul D. DeRocco, ecos-discuss

Thanks for the link ^^ I'm a bit dumb when dealing with that kind of things.
The problem is when I put the data in flash and read it thereafter, I
obtain the value 768 for timestamp, which is 300 in hex.
I must have made something wrong there ...

On Jan 10, 2008 11:06 AM, EXTERNAL Gladis Olaf (Praktikant; CR/AEM)
<external.Olaf.Gladis@de.bosch.com> wrote:
>
>
> -----Original Message-----
> From: ecos-discuss-owner@ecos.sourceware.org
> [mailto:ecos-discuss-owner@ecos.sourceware.org] On Behalf Of Alexandre
> Sent: Donnerstag, 10. Januar 2008 10:43
> To: Paul D. DeRocco
> Cc: ecos-discuss@ecos.sourceware.org
> Subject: Re: [ECOS] Re: pragma pack
>
> > Actually i had some problems accessing my packed structures without
> > flows, I think that may help resolve things a bit.
> > Eg with things like that:
> >
> > cyg_uint8 i = 0;
> > struct s_pelco_pattern_array a;
> >
> > a.command2_byte = i++;
> > a.data3 = i++;
> > a.data4 = i++;
> > a.timestamp = i;
> >
> > I finish having a structure which looks like this in memory:
> >
> > cm2: 0x00
> > data3: 0x01
> > data4: 0x02
> > ts: 0x0300
> >
> > Which is really not what i want to have eventually.
> >
>
> I would say this looks correct.
> Maybe you thought there must be 0x0003 in the last one?
>
> But if you have a little endian CPU it swaps the bytes.
> If you have more questions about Endianness take a look at
>
> http://en.wikipedia.org/wiki/Endianness
>
> Greets olaf
>

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2008-01-10 10:29 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-09 14:19 [ECOS] pragma pack Alexandre
2008-01-09 14:40 ` Andrew Lunn
2008-01-09 14:51 ` Bernard Fouché
2008-01-09 14:56   ` Alexandre
2008-01-09 21:55     ` Bronislav Gabrhelik
2008-01-09 22:22       ` Paul D. DeRocco
2008-01-09 23:50         ` [ECOS] " Grant Edwards
2008-01-10  0:50           ` Paul D. DeRocco
2008-01-10  9:43             ` Alexandre
2008-01-10 10:07               ` EXTERNAL Gladis Olaf (Praktikant; CR/AEM)
2008-01-10 10:29                 ` Alexandre
2008-01-09 15:02 ` [ECOS] " EXTERNAL Gladis Olaf (Praktikant; CR/AEM)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).