comp.lang.ada
 help / color / mirror / Atom feed
From: Maciej Sobczak <see.my.homepage@gmail.com>
Subject: Interfacing to C without dynamic memory
Date: Fri, 14 Nov 2008 05:58:03 -0800 (PST)
Date: 2008-11-14T05:58:03-08:00	[thread overview]
Message-ID: <8a0e9a41-6670-486b-bbb7-7ef706643930@a17g2000prm.googlegroups.com> (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



             reply	other threads:[~2008-11-14 13:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-14 13:58 Maciej Sobczak [this message]
2008-11-14 20:35 ` Interfacing to C without dynamic memory 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
replies disabled

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