comp.lang.ada
 help / color / mirror / Atom feed
From: sbelmont700@gmail.com
Subject: Re: Importing C function with variable argument list
Date: Tue, 10 Apr 2012 13:25:03 -0700 (PDT)
Date: 2012-04-10T13:25:03-07:00	[thread overview]
Message-ID: <32075438.738.1334089503590.JavaMail.geo-discussion-forums@ynjw14> (raw)
In-Reply-To: <610ee323-4c7f-413d-8568-aed4955f5152@z38g2000vbu.googlegroups.com>

Hi,

I often have occasion to suffer through using a C API with variable argument in Ada, and there really is no good choice.  I have found basically two options:

1. Import the C function with a fixed-set of arguments, with a defaulted-to-zero parameter at the end.

or

2. Roll-your-own.

The first is cheap and easy, but clearly not extensible, as each version of the function must be overloaded, giving you an infinite number of prototypes to maintain.  The second is better, but hardware and implementation specific.  For instance, for x86, it might look something like this (where D is an array of unsigned longs, or whatever the C function needs):

procedure V_A (D : Array_Type) is

   procedure Variable_C_Function;
   pragma Import (C, Variable_C_Function, "Something");
   Result : Interfaces.C.int := 0;
   
begin

  Machine_Code.Asm (Template => "push %0",
                    Inputs => Interfaces.C.unsigned_long'Asm_Input("g", 0),
                    Volatile => true);
                      
  for i in reverse D'range loop
  
    Machine_Code.Asm (Template => "push %0",
                      Inputs => Interfaces.C.unsigned_long'Asm_Input("g", Data(i)),
                      Volatile => true);

  end loop;

  Variable_C_Function;
  
  Machine_Code.Asm (Template => "sub %%esp, %0",
                    Inputs => Integer'Asm_Input("g", ((D'last + 2) * 4)),
                    Volatile => true);
 
 end V_A;

Of course, this is completly non-portable and would depend greatly on both the hardware, O/S, and how the C function is setup, but it's perhaps the least-worst way of dealing a truly awful kludge.

-sb



  parent reply	other threads:[~2012-04-10 20:38 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-10  9:21 Importing C function with variable argument list Maxim Reznik
2012-04-10 11:22 ` Patrick
2012-04-10 13:09   ` Maxim Reznik
2012-04-10 13:50     ` Georg Bauhaus
2012-04-10 13:18 ` Markus Schöpflin
2012-04-10 16:47   ` Simon Wright
2012-04-12 10:08     ` Markus Schöpflin
2012-04-12 16:58       ` Adam Beneschan
2012-04-12 20:47         ` Randy Brukardt
2012-04-13  8:43           ` Markus Schöpflin
2012-04-13 23:01             ` Randy Brukardt
2012-04-27  8:24           ` Natasha Kerensikova
2012-04-27  9:18             ` Jacob Sparre Andersen
2012-04-27 16:48               ` Natasha Kerensikova
2012-05-03 20:15             ` sbelmont700
2012-05-13  4:18               ` David Thompson
2012-05-13  9:03                 ` Simon Wright
2012-05-13 17:01                   ` Jeffrey Carter
2012-05-13 18:20                     ` Simon Wright
2012-05-13 19:11                       ` Jeffrey Carter
2012-05-13 19:55                         ` Ludovic Brenta
2012-05-14  0:52                           ` Jeffrey Carter
2012-05-13 21:12                         ` Simon Wright
2012-05-13 23:57                           ` Georg Bauhaus
2012-05-14  0:54                           ` Jeffrey Carter
2012-05-14  8:10                             ` Nomen Nescio
2012-05-14 15:21               ` Natasha Kerensikova
2012-05-14 20:53                 ` sbelmont700
2012-04-13  4:08         ` Tero Koskinen
2012-04-13  8:04           ` Markus Schöpflin
2012-04-10 20:02 ` Tero Koskinen
2012-04-13  3:28   ` Tero Koskinen
2012-04-10 20:25 ` sbelmont700 [this message]
2012-04-11 23:24 ` Randy Brukardt
replies disabled

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