From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8ecbc35ea893182f X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.germany.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Alex R. Mosteo" Newsgroups: comp.lang.ada Subject: Re: Interfacing to C: big structures Date: Tue, 26 Feb 2008 17:21:42 +0100 Message-ID: <62iskrF23lre0U1@mid.individual.net> References: <62idb0F23ddfnU1@mid.individual.net> <62ip0mF23cobbU1@mid.individual.net> Reply-To: alejandro@mosteo.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: individual.net OPsOSVnTw1GllrprSBv16w/6fdGn4vD+P/kv0KxoClg6VAuzM= Cancel-Lock: sha1:67O6EM9hVBnv59YYV1oHnC2WV5Y= User-Agent: Thunderbird 2.0.0.6 (X11/20071022) In-Reply-To: Xref: g2news1.google.com comp.lang.ada:20095 Date: 2008-02-26T17:21:42+01:00 List-Id: Robert A Duff wrote: > "Alex R. Mosteo" writes: > >> Robert A Duff wrote: >>> "Alex R. Mosteo" 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?)