comp.lang.ada
 help / color / mirror / Atom feed
* Interfacing between Ada and C: records and structs
@ 2012-07-31 16:38 awdorrin
  2012-07-31 17:16 ` Niklas Holsti
  0 siblings, 1 reply; 12+ messages in thread
From: awdorrin @ 2012-07-31 16:38 UTC (permalink / raw)


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.

The Ada code defines the record as:

for FP_SHARED_BUF_TYPE'Alignment use 9;
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;

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?

I have been looking through the docs and haven't seen it yet.



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

end of thread, other threads:[~2012-08-07  7:36 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <7beb59ba-ca6f-476e-8b39-604196b0b79f@googlegroups.com>
2012-08-01  2:51 ` Interfacing between Ada and C: records and structs Jeffrey Carter
2012-07-31 16:38 awdorrin
2012-07-31 17:16 ` Niklas Holsti
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

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