comp.lang.ada
 help / color / mirror / Atom feed
From: "Alex R. Mosteo" <amosteo@unizar.es>
Subject: Re: Interfacing to C: big structures
Date: Tue, 26 Feb 2008 17:21:42 +0100
Date: 2008-02-26T17:21:42+01:00	[thread overview]
Message-ID: <62iskrF23lre0U1@mid.individual.net> (raw)
In-Reply-To: <wccoda368ck.fsf@shell01.TheWorld.com>

Robert A Duff wrote:
> "Alex R. Mosteo" <amosteo@unizar.es> writes:
> 
>> Robert A Duff wrote:
>>> "Alex R. Mosteo" <amosteo@unizar.es> writes:
>>>
>>>> 2. Create an Ada type as
>>>>
>>>> type S is new Ada.Streams.Stream_Element_Array
>>>>                 (1 .. Stream_Offset (Size_Of_S));
>>>> pragma Pack (S); -- Not sure if this is even needed.
>>>>
>>>> Note that this type gets its size at elaboration time, and thus this is
>>>> what fixes this type if the C corresponding struct changes.
>>> Nice trick.
>>>
>>> But I think you need to make sure the alignment is correct.
>> I'm out of my expertise here, but why it's that necessary? In the end
>> you're passing S address to the C subprograms and I don't see how
>> alignment may have influence (but, again, I don't know about this).
> 
> Suppose the C struct contains a double, and suppose the C compiler
> therefore insists that all objects be allocated on an 8-byte boundary.
> On some machines, failure to do so will cause the program to
> crash; on others, it will cause the program to be slow.
> 
> But the Ada compiler thinks it's an array of bytes,
> so S'Alignment will probably be 1, so the compiler
> can allocate objects at an improperly-aligned address.
> 
>     type T is
>         record
>             Flag : Boolean;
>             X : S;
>         end record;
> 
> - Bob

I see. So, for example, let's say there's this C function:

void init (struct S *s) {...};

and you pass the Ada array address to it.

It may crash/ill-behave, if the C code takes for granted that the s
pointer is, say, 8-aligned, and some C generated code within "init"
depends on this assumption.

It may be slow, otherwise, if no such assumption exists.

I'm missing some other possibilities?

And, finally, is adding an alignment clause enough to solve this issue?
If so, how do you query the alignment of a C datatype?

(Could a conservative approach be to force the Ada side type to be
max-aligned for the architecture?)



  reply	other threads:[~2008-02-26 16:21 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-25 21:17 Interfacing to C: big structures Maciej Sobczak
2008-02-25 23:26 ` Randy Brukardt
2008-02-27 13:23   ` Maciej Sobczak
2008-02-25 23:26 ` Randy Brukardt
2008-02-25 23:26 ` Randy Brukardt
2008-02-25 23:43 ` Robert A Duff
2008-02-26 13:53   ` Stephen Leake
2008-02-26 21:12     ` Randy Brukardt
2008-02-26  2:36 ` Steve
2008-02-26 12:00 ` Alex R. Mosteo
2008-02-26 14:05   ` Robert A Duff
2008-02-26 15:19     ` Alex R. Mosteo
2008-02-26 15:33       ` Robert A Duff
2008-02-26 16:21         ` Alex R. Mosteo [this message]
2008-02-27  1:28           ` Robert A Duff
2008-03-10  1:38             ` David Thompson
2008-02-26 17:35   ` Adam Beneschan
2008-02-26 19:47     ` Simon Wright
2008-02-26 21:14       ` Randy Brukardt
2008-02-27  1:40         ` Robert A Duff
2008-02-27 17:12       ` Adam Beneschan
2008-02-27 20:37         ` Simon Wright
2008-02-28 11:30         ` Alex R. Mosteo
2008-02-28 15:53           ` Robert A Duff
2008-02-29 10:45             ` Alex R. Mosteo
2008-02-27  1:37     ` Robert A Duff
2008-02-27 13:49       ` Maciej Sobczak
replies disabled

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