comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Thick bindings to a C library and gnattest: suggestions?
Date: Mon, 1 Jul 2013 11:45:04 +0200
Date: 2013-07-01T11:45:04+02:00	[thread overview]
Message-ID: <1fxsf70zl2ckq.aysy7d9c8jkl$.dlg@40tude.net> (raw)
In-Reply-To: bbb47fc9-3b53-47a8-b996-3a495e48445e@googlegroups.com

On Mon, 1 Jul 2013 02:02:34 -0700 (PDT), ziotom78@gmail.com wrote:

> First question: the vectors used by the CFITSIO library are sometimes
> huge (millions of elements), sometimes very small (~ 10 elements).
> I decided to always allocate them on the heap, using declarations like
> these:

Why should bindings care about that?

>    subtype Double is Interfaces.C.double;
>    type Double_Array is array (Positive range <>)
>      of Interfaces.C.double;
>    pragma Convention (C, Double_Array);
>    type Double_Array_Ptr is access Double_Array;

Using unconstrained arrays could be troublesome because they contain
bounds. You may consider the package B.3.2

   Interfaces.C.Pointers

which does this.

But normally you do not need array pointers in bindings except for the
cases when the C library stores the pointer to keep it after returning from
the subprogram.

> (similar declarations exist for arrays of integers/long...), and then
> using "new Double_Array" whenever I need to allocate room for an
> array. Every binding function only uses Double_Array_Ptr. Is this the best
> way to do this in Ada? Every text I read about Ada says to stay away
> from objects allocated on the heap, but I do not see any other
> solution here.

Why don't you simply pass the array down to the C subprogram? You can do
something like:

   type Double_Array is array (Positive range <>)
      of aliased Interfaces.C.double;
   pragma Convention (C, Double_Array);
   procedure Foo (A : Double_Array);

Implementation:

   type Double_Ptr is access all Interfaces.C.double;
   pragma Convention (C, Double_Ptr);

   procedure Foo (A : Double_Array) is
   --
   -- Assuming foo's signature in C:
   --
   --    foo (double * a, unsigned n);
   --
      procedure Internal (A : Double_Ptr; N : Interfaces.C.unsigned);
      pragma Import (C, Internal, "foo");
   begin
      Internal (A (A'First)'Access, A'Length);
   end Foo;

> I am sure there is some clever way to solve these two minor points,
> but so far I have not been able to find it. I tried e.g. to put
> "-lcfitsio" in the project file of the AdaFITS library, but with no
> success.

Make a library project file for cfitsio instead. "with" it from your
project. GNAT knows how to handle it and will add appropriate linker
switches to any project using it directly or indirectly. A library project
file could look like:

project cfitsio is
   for Externally_Built use "true"; -- Do not bother to compile me
   for Source_Files use (); -- No sources
   for Library_Dir use ".";   -- Where .llb, .a, .dll, .so are
   for Library_Name use "cfitsio"; -- Without "lib" prefix!
   for Library_Kind use "dynamic"; -- A DLL
end cfitsio;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  reply	other threads:[~2013-07-01  9:45 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-01  9:02 Thick bindings to a C library and gnattest: suggestions? ziotom78
2013-07-01  9:45 ` Dmitry A. Kazakov [this message]
2013-07-01 11:11   ` Maurizio Tomasi
2013-07-01 11:41     ` Simon Wright
2013-07-01 12:00       ` Maurizio Tomasi
2013-07-01 12:42         ` Dmitry A. Kazakov
2013-07-01 19:07           ` Simon Wright
2013-07-01 12:32     ` Dmitry A. Kazakov
2013-07-01 12:41       ` Maurizio Tomasi
2013-07-01 12:47       ` Simon Wright
2013-07-02  8:55     ` Georg Bauhaus
2013-07-02  8:33   ` Maurizio Tomasi
2013-07-02  8:58     ` Dmitry A. Kazakov
2013-07-02 16:58     ` Robert A Duff
2013-07-02 17:00     ` Jeffrey Carter
2013-07-01 17:16 ` Jeffrey Carter
2013-07-02  4:24   ` Randy Brukardt
2013-07-02  4:37     ` Shark8
2013-07-02  5:04     ` tmoran
2013-07-02 22:27       ` Randy Brukardt
2013-07-03 12:02   ` Jacob Sparre Andersen
2013-07-02  3:16 ` Jerry
2013-07-02  4:02   ` Shark8
replies disabled

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