comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Interfacing to C: big structures
Date: Tue, 26 Feb 2008 09:35:53 -0800 (PST)
Date: 2008-02-26T09:35:53-08:00	[thread overview]
Message-ID: <56a177e4-506f-4bc8-a531-7f2dd15d50c8@i7g2000prf.googlegroups.com> (raw)
In-Reply-To: 62idb0F23ddfnU1@mid.individual.net

On Feb 26, 4:00 am, "Alex R. Mosteo" <amos...@unizar.es> wrote:
> Maciej Sobczak wrote:
> > Hi,
>
> > Consider the C API that contains some structure S and a couple of
> > functions that operate on the state that is stored in the Struct. A
> > typical usage in C might be:
>
> > struct S s;
>
> > init(&s);
> > foo(&s);
> > bar(&s);
> > baz(&s);
>
> > Above, s is created with automatic storage duration.
>
> > How to interface to this from Ada? I see two options:
>
> > 1. Write a simple wrapper that will create S on the heap and use the
> > pointer everywhere. This is easy, but imposes the use of dynamic
> > memory, which for some reasons I might want to avoid.
> > 2. Create an Ada array of basic storage units of "appropriate size"
> > and pass its address to the C functions. The advantage of this
> > solution is that it does not impose any particular memory management
> > scheme, but the disadvantage is that it is potentially non-portable
> > (how to get the "appropriate size"?).
>
> > Would you suggest some other solution?
>
> I have used this trick for types in C that I don't want to replicate in
> Ada with pragma convention C, and that adds the bonus of being always in
> sync with the C part, even if it changes, by means of a single
> recompiling. You can see examples of use in my ada-player binding [1]:
>
> The following is not compiled, there may be mistakes. Hope you get the idea.
>
> 1. Create a C helper file with
>
> size_t size_of_S(void) { return sizeof (struct S); };
>
> and a corresponding helper binding in Ada:
>
> function Size_Of_S return Interfaces.C.Size_T;
> pragma Import (Size_Of_S, "size_of_S");
>
> 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.
>
> 3. Use the S Ada type through binding functions to the C functions. In
> practice, you have a byte receptacle in the Ada side, and that's all.
> You retain the use in the stack and through the C API. I find this very
> practical for well-designed C libraries where all usage is through
> functions with client-opaque types, since it requires zero maintenance
> if the C type changes.

I thought of something like this, but Maciej said something about
wanting to avoid dynamic memory, and it seems that you're defining a
type whose size isn't going to be known until runtime, and therefore
if you declare an Ada variable of that type, it will have to be
allocated dynamically in some way.  Maybe the particular way it would
be done is acceptable to Maciej, though (a local variable could be
allocated simply by moving the stack pointer, depending on the
implementation).  Anyway, this seems like the best way to do things if
it isn't unacceptable for that reason.

                                 -- Adam






  parent reply	other threads:[~2008-02-26 17:35 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-25 23:26 ` Randy Brukardt
2008-02-27 13:23   ` Maciej Sobczak
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
2008-02-27  1:28           ` Robert A Duff
2008-03-10  1:38             ` David Thompson
2008-02-26 17:35   ` Adam Beneschan [this message]
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