From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29279 invoked by alias); 4 Dec 2013 12:21:36 -0000 Mailing-List: contact libffi-discuss-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libffi-discuss-owner@sourceware.org Received: (qmail 29267 invoked by uid 89); 4 Dec 2013 12:21:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL,BAYES_50,RDNS_NONE,SPF_HELO_PASS,SPF_PASS autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Received: from Unknown (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 04 Dec 2013 12:21:33 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rB4CLKJ3031353 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 4 Dec 2013 07:21:21 -0500 Received: from zebedee.pink (ovpn-113-92.phx2.redhat.com [10.3.113.92]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id rB4CLIL5012454; Wed, 4 Dec 2013 07:21:19 -0500 Message-ID: <529F1E3E.4010401@redhat.com> Date: Wed, 04 Dec 2013 12:21:00 -0000 From: Andrew Haley User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131028 Thunderbird/17.0.10 MIME-Version: 1.0 To: "Hogan, D. (GE Power & Water)" CC: Alan Modra , Jakub Jelinek , "libffi-discuss@sourceware.org" Subject: Re: RFC: variadic closures in x86/x86_64 References: <52931854.6080007@redhat.com> <20131125093715.GU892@tucnak.redhat.com> <5293221D.4010505@redhat.com> <20131126142723.GD9211@bubble.grove.modra.org> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2013/txt/msg00233.txt.bz2 On 12/04/2013 08:02 AM, Hogan, D. (GE Power & Water) wrote: > On Wed, Nov 27, 2013 at 00:57:23PM +1030, Alan Modra wrote: >> The claim to fame looks to be the ability to call variadic functions >> without describing the arguments via ffi_prep_cif_var at the point of >> call. Instead you do so in the function consuming the args. I'm not >> sure what that gains you.. > > This is specifically for variadic callbacks. If it wasn't a callback, > I could use ffi_prep_cif_var. > > To give a little background, FMI is a standard for (among other things) > model exchange so you can use a dynamic system model in various > modeling or simulation environments. The model is exposed as a C shared > library. The shared library executes callbacks (some variadic) provided > by a FMU driver. Please, I'm finding this extremely difficult to understand. Where is the variadic function? You say that a C shared library "executes" a callback, but what does that mean? At the point of the call, the caller knows for certain what arguments are passed, and the type of these arguments. > JFMI allows you to drive a FMU from Java. The Java FMU driver provides > a Java implementation for the C callbacks. Right, so a C program thinks it's calling C, but in fact it's calling Java, and a libffi closure provides the glue. > The nonvariadic callbacks Ah! OK, so the function that is called as a C function is a libffi closure, and it is called in a variadic form. > were already supported by JFMI through libffi and JNA. In order to > handle variadic callbacks, libffi and JNA need to be modified so you > can access the variadic arguments inside of a callback. This patch > adds the libffi support. > > I cannot construct a ffi_prep_cif_var because the model's C shared > library is the one running a variadic callback in the driver. Again, I don't know what it means to "run a variadic callback." Does this mean that the shared library contains this callback function, or that it calls it? > I don't > know how many arguments or what types it will provide in the calls. I > have to rely on printf style formatting flags in order to know what to > access from inside of the callback. OK, so I'm guessing that the C model's shared library calls the variadic function. Why do you not define a variadic C function which does this: void f1(int n, ...) { va_list ap; jintArray argsArray = (*env_p)->NewIntArray(n); jint *args = (*env_p)->GetIntArrayElements(argsArray, NULL); int argno = 0; va_start(ap, n); while (argno < n) { args[argno++] = va_arg(ap, int); } va_end(ap); (*env_p)->ReleaseIntArrayElements(argsArray, args, 0); (*env_p)->CallStaticVoidMethod(clsH, printargs, argsArray); } to call this variadic Java code: public static void printargs(int... args) { for (int i : args) System.out.print(i + " "); System.out.println(); } There is no reason that JNA cannot use this mechanism, is there? Andrew.