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=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: border1.nntp.ams3.giganews.com!border2.nntp.ams3.giganews.com!border2.nntp.ams2.giganews.com!border4.nntp.ams.giganews.com!border2.nntp.ams.giganews.com!nntp.giganews.com!weretis.net!feeder4.news.weretis.net!newsfeed.datemas.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Tue, 02 Jul 2013 10:55:39 +0200 From: Georg Bauhaus User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Thick bindings to a C library and gnattest: suggestions? References: <1fxsf70zl2ckq.aysy7d9c8jkl$.dlg@40tude.net> <40bf5a31-b09a-4106-a57a-7ac3dd5f951e@googlegroups.com> In-Reply-To: <40bf5a31-b09a-4106-a57a-7ac3dd5f951e@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <51d29586$0$6567$9b4e6d93@newsspool4.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 02 Jul 2013 10:55:34 CEST NNTP-Posting-Host: f659dee9.newsspool4.arcor-online.net X-Trace: DXC=6DGmc>j7a4RAX0F2i>ZLh>_cHTX3j]V; e[_?imLYT X-Complaints-To: usenet-abuse@arcor.de X-Original-Bytes: 3528 Xref: number.nntp.dca.giganews.com comp.lang.ada:182205 Date: 2013-07-02T10:55:34+02:00 List-Id: On 01.07.13 13:11, Maurizio Tomasi wrote: > Hi Dmitry, >> Why don't you simply pass the array down to the C subprogram? You can do >> something like: > > But what if A'Length is so large that the array does not fit into the stack? Avoid pointers on the Ada side and you need not worry about the stack. This is true insofar as Ada language definition says that Ada arrays will be passed as pointers to the C world, automatically. (See Interfacing to C) Indeed, just do what is natural on both sides: You can pass C-array variables (pointers) on the C side and expect Ada to handle plain Ada-array variables. No pointers needed. In fact, they complicate things due to doubled indirections. The following seems to work on my system. The Ada side "imports" data and exports its subprograms. #include #include double call_ada (size_t n) { extern void ada_side_takes_vector (double*, size_t); double *thing = malloc(n * sizeof(double)); for (int k = 0; k < (int)n; ++k) { thing[k] = k; } ada_side_takes_vector(thing, n); return thing[n/2]; } int main() { extern void adainit(void); extern void adafinal(void); double result; #define M ((2<<20)/sizeof(double)) adainit(); result = call_ada(500 * M); adafinal(); printf("result is %f\n", result); return 0; } with Interfaces.C; use Interfaces; package Bigimport is pragma Pure (Bigimport); subtype Dbl is C.Double; subtype Zint is C.ptrdiff_t range 0 .. C.ptrdiff_t'Last; type Lots_Of_Numbers is array (Zint) of Dbl; pragma Convention (C, Lots_Of_Numbers); procedure Takes_Vector (V : in out Lots_Of_Numbers; N : C.size_t); pragma Export (C, Takes_Vector, "ada_side_takes_vector"); end Bigimport; package body Bigimport is procedure Takes_Vector (V : in out Lots_Of_Numbers; N : C.size_t) is use type Dbl, C.size_t; begin for K in Zint range 0 .. Zint(N-1) loop V(K) := V(K) / 2.0; end loop; end Takes_Vector; end Bigimport;