comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Interfacing between Ada and C: records and structs
Date: Tue, 31 Jul 2012 20:16:10 +0300
Date: 2012-07-31T20:16:10+03:00	[thread overview]
Message-ID: <a7qi6qFoc3U1@mid.individual.net> (raw)
In-Reply-To: <296e271a-967b-4dfb-8dca-f278ecfae03d@googlegroups.com>

On 12-07-31 19:38 , awdorrin wrote:
> I have some legacy code that I am working with, they share common data structures.
> 
> Simplifying my code, In Ada, I have a record:
> 
> type FP_SHARED_BUF_TYPE is
> record
>   LOCK_SHM_1 : aliased Mutex;
>   LOCK_SHM_2 : aliased Mutex;
>   LOCK_SHM_3 : aliased Mutex;
> end record;
> pragma Convention(C,FP_SHARED_BUF_TYPE);
> 
> In C:
> 
> struct FP_SHARED_TYPE {
>   pthread_mutex_t LOCK_SHM_1;
>   pthread_mutex_t LOCK_SHM_2;
>   pthread_mutex_t LOCK_SHM_3;
> };
> 
> Now, the Mutex/pthread_mutex_t is 192bits, not 255bits, but when I compile the program.

How is pthread_mutex_t declared in Ada form? And in C form?

Have you checked that in Ada, pthread_mutex_t'Size is 192?

What is the storage unit of your target processor? 8 bits?

> The Ada code defines the record as:
> 
> for FP_SHARED_BUF_TYPE'Alignment use 9;

That is a strange alignment. Do you see any need for it? If not, try
removing it.

> for FP_SHARED_BUF_TYPE use record
>   LOCK_SHM_1 at - range  0 .. 255;
>   LOCK_SHM_1 at - range 32 .. 255;
>   LOCK_SHM_1 at - range 64 .. 255;
> end record;

Do you see any need for this representation clause? If not, remove it.
Or change it to use 192 bits per component.

(What are the "-" characters after the "at" keywords? Shouldn't it read
"at 0 range"?)

> While the C code defines the structure elements as 192bits, and packs them right next to each other
> 
> Addresses of:
> LOCK_SHM_1	0xF5650008
> LOCK_SHM_2	0xF5650020
> LOCK_SHM_3	0xF5650038
> 
> The only way I can get this to work, is to add padding to my C struct definition:
> 
> struct FP_SHARED_TYPE {
>   pthread_mutex_t LOCK_SHM_1;
>   int pad_for_m1[2];
>   pthread_mutex_t LOCK_SHM_2;
>   int pad_for_m2[2];
>   pthread_mutex_t LOCK_SHM_3;
>   int pad_for_m3[2];
> };
> 
> Which, makes the C code really ugly.
> 
> Is there a way to make the Ada code pack the record?

If your Ada code includes the record representation clause quoted above,
just remove it, or change it to specify 192 bits per component.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
      .      @       .



  reply	other threads:[~2012-08-02 12:19 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-31 16:38 Interfacing between Ada and C: records and structs awdorrin
2012-07-31 17:16 ` Niklas Holsti [this message]
2012-07-31 18:44   ` awdorrin
2012-07-31 19:08     ` awdorrin
2012-07-31 19:27       ` Simon Wright
2012-07-31 19:44         ` awdorrin
2012-07-31 20:46           ` Niklas Holsti
2012-08-01 12:16             ` awdorrin
2012-07-31 19:23     ` Simon Wright
2012-08-02  4:39     ` Shark8
2012-08-02 12:32       ` awdorrin
     [not found] <7beb59ba-ca6f-476e-8b39-604196b0b79f@googlegroups.com>
2012-08-01  2:51 ` Jeffrey Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox