comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <jrcarter@acm.org>
Subject: Re: Ada to 'C' parameter passing problem
Date: Tue, 18 Feb 2003 16:47:41 GMT
Date: 2003-02-18T16:47:41+00:00	[thread overview]
Message-ID: <3E5263FB.6060901@acm.org> (raw)
In-Reply-To: ea3b54bb.0302180739.3cb6c159@posting.google.com

Patrick wrote:
> I am using GNAT Ada on Linux.  I am trying to port from a Sun
> (Solaris) implementation.  I didn't write the Ada or 'C' myself, but
> am trying to make it work on Linux.  The Ada code passes five
> parameters to the 'C' code.  It is trying to pass the address in each
> case so that the 'C' can pass back status in the variables
> (parameters).  The below Ada code (in tls.ads) contains the definition
> of the function "to_c_pointer" to convert the Ada address to a
> "c_pointer":
>    type us1 is range 0..SYSTEM.MAX_INT;
>    subtype unsigned_long is us1;
>    subtype c_pointer is unsigned_long;
>    -- convert ada address into a value that can be assigned
>    -- to a c pointer.  The following is an example of a call to
>    -- to_c_pointer: TLS.to_c_pointer(i'ADDRESS)
>    function to_c_pointer is new 
>       unchecked_conversion(SYSTEM.address, c_pointer);
>    pragma inline(to_c_pointer);
> 
> Here is what the Ada call to 'C' looks like:
> DMC_Initialize_Database_Configuration_C(
>                     TLS.to_c_pointer(Configuration_Id_In'ADDRESS),
>                     TLS.to_c_pointer(Configuration_Id_Len'ADDRESS),
>                     TLS.to_c_pointer(Initialization_Status_Ind'ADDRESS),
>                     TLS.to_c_pointer(Init_Failure_Text_In'ADDRESS),
>                     TLS.to_c_pointer(Init_Failure_Text_Len'ADDRESS));
> 
> Here is what the 'C' funtion definition looks like:
> DMC_Initialize_Database_Configuration_C(
>                     char *Configuration_Id,
>                     int *Configuration_Id_Len,
>                     int *Initialization_Status,
>                     char *Init_Failure_Text,
>                     int *Init_Failure_Text_Len)
> {
> 
> As I said, there are five parameters.  This worked fine under Solaris
> (Spark compiler for Ada), but under Linux the address of the 2nd and
> 4th parameters ends up being zero on the 'C' side (we can tell this
> with a debugger).  Using the debugger, it looks like the first
> parameter on the Ada side is being passed into the first parameter on
> the 'C' side.  The second parameter on the Ada side shows up on the
> 'C' side as the third parameter.  The third parameter on the Ada side
> shows up on the 'C' side as the fifth parameter.  So, at least on the
> surface, it looks as if the size of the parameter on the Ada side is
> twice the size of the parameter on the 'C' and it is filling in with
> zero.

Compare the values of System.Max_Int on the Solaris compiler and GNAT. 
GNAT supports 64-bit integers on all platforms, while it's likely the 
compiler used on Solaris only supports 32-bit integers. This might 
explain what you're seeing.

There's no need for all this complication in the interface. You could 
declare the procedure as taking System.Address for its parameters in 
Ada, and simply pass the addresses without the conversions. This would 
be a simple modification to get things working. A C pointer is generally 
an address, and Ada and C compilers for the same platform generally use 
the same representation for an address. If you want to put more effort 
into it, you could check out Annex B and learn how things are passed to 
a C subprogram, and get some type checking into the call as well.

-- 
Jeff Carter
"The time has come to act, and act fast. I'm leaving."
Blazing Saddles




  reply	other threads:[~2003-02-18 16:47 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-18 15:39 Ada to 'C' parameter passing problem Patrick
2003-02-18 16:47 ` Jeffrey Carter [this message]
2003-02-18 19:50 ` Rod Chapman
2003-02-20  2:36   ` Matthew Heaney
2003-02-20  9:18     ` Rod Chapman
2003-02-20  9:43       ` Dmitry A. Kazakov
2003-02-20 22:05       ` Simon Wright
2003-02-21  9:53         ` Stuart Palin
2003-02-21 17:39           ` Jeffrey Carter
2003-02-21 18:12           ` Warren W. Gay VE3WWG
2003-02-21 20:25           ` Randy Brukardt
2003-02-24 23:53       ` Matthew Heaney
2003-02-25 17:21         ` Rod Chapman
  -- strict thread matches above, loose matches on Subject: below --
2003-02-21 16:52 Lionel.DRAGHI
replies disabled

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