comp.lang.ada
 help / color / mirror / Atom feed
From: Damien Carbonne <damien.carbonne@free.fr>
Subject: Re: Interfacing to C without dynamic memory
Date: Fri, 14 Nov 2008 21:35:33 +0100
Date: 2008-11-14T21:35:33+01:00	[thread overview]
Message-ID: <491de115$0$29376$426a74cc@news.free.fr> (raw)
In-Reply-To: <8a0e9a41-6670-486b-bbb7-7ef706643930@a17g2000prm.googlegroups.com>

Hi,

I have never tried what you propose, but I think it should work.
Of course, all fields of T should be initialized in C code (in createT, 
I guess).

In Ada, you could also wrap the array in a record.
That way, you could initialize everything to 0:

type T is record
    Bytes :  System.Storage_Array (...);
end record;
pragma Convention (C, T);
+ other pragma if necessary
...

Make T private, or even limited private: Ada users should not have 
direct access to T fields.
Also, you shouldn't need to use 'Address.
By default, when you use C convention/import, structures and arrays are 
passed by address.

Of course, as you don't have direct access to T fields, you need to 
provide all necessary functions to do that.

So you would have:

package XXX is
    type T is [limited] private;

    procedure Create (O : in out T);
    procedure Destroy (O : in out T);
    ...

private
    type T is ... -- as your proposal
    ...

    pragma Import (C, Create, "CreateT");
    pragma Import (C, Destroy, "DestroyT");
    ...
end XXX;


Maciej Sobczak a �crit :
> Hi,
> 
> Consider a C (or C++) library that defines some type T with some
> functions operating on it.
> The type T can be a complex type, encapsulated in a struct or class.
> Assuming extern "C" interface, the library functions usually accept a
> pointer to T:
> 
> struct T
> {
>     // lots of interesting stuff
> };
> 
> void createT(T * object);
> void destroyT(T * object);
> void foo(T * object, int something);
> void bar(T * object, int something_else);
> 
> In C and C++ it is possible to use objects of type T without
> allocating them dynamically, which would be otherwise mandated by an
> alternative interface:
> 
> T * newT();
> void deleteT(T * object);
> 
> How would you approach wrapping such a library for Ada while retaining
> the requirement that dynamic memory is not obligatory (ie. with the
> original interface above)?
> 
> My first ideas are:
> 1. Extract sizeof(T) at C level.
> 2. In Ada, create appropriately aligned:
> 
> type T is System.Storage_Array (1 .. Size_Of_T);
> for T'Alignment use Appropriate_Alignment_Value;
> 
> 3. Wrap imported C functions by passing to them the 'Address of T's
> instances.
> 
> This way, users will be able to use T as any other value type, without
> resorting to dynamic memory.
> 
> Does it make sense? Is there any better way?
> 
> --
> Maciej Sobczak * www.msobczak.com * www.inspirel.com
> 
> Database Access Library for Ada: www.inspirel.com/soci-ada



  reply	other threads:[~2008-11-14 20:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-14 13:58 Interfacing to C without dynamic memory Maciej Sobczak
2008-11-14 20:35 ` Damien Carbonne [this message]
2008-11-15  1:12   ` Randy Brukardt
2008-11-14 23:13 ` Robert A Duff
2008-11-15 11:52   ` Samuel Tardieu
2008-11-16 21:31     ` 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