From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!mx05.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Thick bindings to a C library and gnattest: suggestions? Date: Mon, 1 Jul 2013 11:45:04 +0200 Organization: cbb software GmbH Message-ID: <1fxsf70zl2ckq.aysy7d9c8jkl$.dlg@40tude.net> References: Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: IenaDxMXK2hi7fvYcb+MlQ.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: 40tude_Dialog/2.0.15.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:16003 Date: 2013-07-01T11:45:04+02:00 List-Id: 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