comp.lang.ada
 help / color / mirror / Atom feed
* Interfacing to C without dynamic memory
@ 2008-11-14 13:58 Maciej Sobczak
  2008-11-14 20:35 ` Damien Carbonne
  2008-11-14 23:13 ` Robert A Duff
  0 siblings, 2 replies; 6+ messages in thread
From: Maciej Sobczak @ 2008-11-14 13:58 UTC (permalink / raw)


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



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

end of thread, other threads:[~2008-11-16 21:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-14 13:58 Interfacing to C without dynamic memory Maciej Sobczak
2008-11-14 20:35 ` Damien Carbonne
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

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