comp.lang.ada
 help / color / mirror / Atom feed
* Ada to C Interface: Passing Pointers Via System.Address
@ 2005-01-12  2:16 ReversedBias
  2005-01-12  3:09 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: ReversedBias @ 2005-01-12  2:16 UTC (permalink / raw)


I am relatively new to Ada.  I am constructing an API so that my Ada
clients can talk to my C code.  My C functions return pointers to data
structures (very large structures).  I need to then pass the pointer to
the data struct to the Ada code and back into other C functions.  There is
no interaction with the data structure in the Ada code except to pass the
pointer from one C function to another.  I have been trying to use the C
interface pragma and return the pointers from C into an Ada System.Address
and then pass that System.Address into the another pragma back down to the
C functions.  My hope is that the C will be able to type cast the address
back into the correct pointer type.  So far I have not been able to get
this to work properly.  Please advise on another approach or post an
example of how I could do this using the System.Address

Thanks,




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada to C Interface: Passing Pointers Via System.Address
  2005-01-12  2:16 Ada to C Interface: Passing Pointers Via System.Address ReversedBias
@ 2005-01-12  3:09 ` tmoran
  2005-01-12  7:36   ` ReversedBias
  2005-01-13 17:14   ` Martin Krischik
  2005-01-13  1:25 ` Jeffrey Carter
  2005-01-13  3:05 ` Steve
  2 siblings, 2 replies; 8+ messages in thread
From: tmoran @ 2005-01-12  3:09 UTC (permalink / raw)


> My C functions return pointers to data structures (very large structures).
> I need to then pass the pointer to the data struct to the Ada code and
> back into other C functions.  There is no interaction with the data
> structure in the Ada code except to pass the pointer from one C function
> to another.
  If the data being passed through Ada is a black box to Ada, why not
just declare a type of the appropriate size, eg
  type C_Thing is range 0 .. 2**32-1;
  for C_Thing'size use 32;
for the Ada code - Ada doesn't need to know if the thing is a pointer,
address, segment:offset, whatever.

> I have been trying to use the C interface pragma and return the pointers
> from C into an Ada System.Address back into the correct pointer type.
  If you declare something to be an Ada access type, and use a pragma
Convention C, the compiler should do the Right Thing.  Note that a
System.Address might not be the right thing (say if System.Address was a
segment:offset while the C pointer was just an offset in a fixed segment).

> So far I have not been able to get this to work properly.
  What kind of error are you getting?



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada to C Interface: Passing Pointers Via System.Address
  2005-01-12  3:09 ` tmoran
@ 2005-01-12  7:36   ` ReversedBias
  2005-01-12  8:38     ` tmoran
  2005-01-13 17:14   ` Martin Krischik
  1 sibling, 1 reply; 8+ messages in thread
From: ReversedBias @ 2005-01-12  7:36 UTC (permalink / raw)


I have something like this in my ada code (i don't have the exact code in
front of me right now so my syntax might be off)

struct_ptr : system.address;

struct_ptr := ada_cfunct1(input1, input2);

cfunct2(struct_ptr, input3);

Where cfunct1 returns a struct pointer and cfunct2 is a second c function
that needs the pointer as an input.  When I try and run it I get a
segmentation fault and a core dump (it compiles fine).  I suspect that I
am not declaring the system.address correctly.  I know that my C code is
correct, and that it allocates the pointer correctly.  However when I look
at the value of the memory of the struct_ptr after it has gone to ada and
back to c it says that it is null.  I must not be using the system.address
correctly?

Thanks for your help,




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada to C Interface: Passing Pointers Via System.Address
  2005-01-12  7:36   ` ReversedBias
@ 2005-01-12  8:38     ` tmoran
  2005-01-12 15:50       ` Mark Lorenzen
  0 siblings, 1 reply; 8+ messages in thread
From: tmoran @ 2005-01-12  8:38 UTC (permalink / raw)


> struct_ptr : system.address;
>
> struct_ptr := ada_cfunct1(input1, input2);
>
> cfunct2(struct_ptr, input3);
> ...
> at the value of the memory of the struct_ptr after it has gone to ada and
> back to c it says that it is null.  I must not be using the system.address
  Could you please post a small, but complete (ie, compiles, but fails to
run) program?  I'm very confused by the above, not knowing which of the
functions are in Ada, which are imported from C, how is struct_ptr's value
created, what are the declarations of ada_cfunct1 and cfunct2, etc.  I'm
guessing that both ada_cfunct1 and cfunct2 are imported C functions.  How
about putting printout inside ada_cfunct1 just before it returns
struct_ptr, another printout just after the "struct_ptr := ...", and a
third printout at the beginning of cfunct2, showing the value passed in
for struct_ptr.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada to C Interface: Passing Pointers Via System.Address
  2005-01-12  8:38     ` tmoran
@ 2005-01-12 15:50       ` Mark Lorenzen
  0 siblings, 0 replies; 8+ messages in thread
From: Mark Lorenzen @ 2005-01-12 15:50 UTC (permalink / raw)


tmoran@acm.org writes:

> > struct_ptr : system.address;
> >
> > struct_ptr := ada_cfunct1(input1, input2);
> >
> > cfunct2(struct_ptr, input3);
> > ...
> > at the value of the memory of the struct_ptr after it has gone to ada and
> > back to c it says that it is null.  I must not be using the system.address
>   Could you please post a small, but complete (ie, compiles, but fails to
> run) program?  I'm very confused by the above, not knowing which of the
> functions are in Ada, which are imported from C, how is struct_ptr's value
> created, what are the declarations of ada_cfunct1 and cfunct2, etc.  I'm
> guessing that both ada_cfunct1 and cfunct2 are imported C functions.  How
> about putting printout inside ada_cfunct1 just before it returns
> struct_ptr, another printout just after the "struct_ptr := ...", and a
> third printout at the beginning of cfunct2, showing the value passed in
> for struct_ptr.

And please post the C prototype for the function you want to call from Ada.

- Mark Lorenzen



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada to C Interface: Passing Pointers Via System.Address
  2005-01-12  2:16 Ada to C Interface: Passing Pointers Via System.Address ReversedBias
  2005-01-12  3:09 ` tmoran
@ 2005-01-13  1:25 ` Jeffrey Carter
  2005-01-13  3:05 ` Steve
  2 siblings, 0 replies; 8+ messages in thread
From: Jeffrey Carter @ 2005-01-13  1:25 UTC (permalink / raw)


ReversedBias wrote:

> I am relatively new to Ada.  I am constructing an API so that my Ada 
> clients can talk to my C code.  My C functions return pointers to
> data structures (very large structures).  I need to then pass the
> pointer to the data struct to the Ada code and back into other C
> functions.  There is no interaction with the data structure in the
> Ada code except to pass the pointer from one C function to another.

This should be very easy. Define a convention-C access type and use it 
for all your C pointers:

type C_Ptr is access all Integer;
pragma Convention (C, C_Ptr);

function Get_Ptr return C_Ptr;
pragma Import (C, Get_Ptr, "c_func_name");

function Use_Ptr (Ptr : C_Ptr) return Interfaces.C.Int;
pragma Import (C, Use_Ptr, "another_c_name");

Result : Interfaces.C.Int;

Result := Use_Ptr (Get_Ptr);

if Result /= 0 then
    ...
end if;

> I have been trying to use the C interface pragma and return the
> pointers from C into an Ada System.Address and then pass that
> System.Address into the another pragma back down to the C functions.
> My hope is that the C will be able to type cast the address back into
> the correct pointer type.  So far I have not been able to get this to
> work properly.  Please advise on another approach or post an example
> of how I could do this using the System.Address

There's no guarantee that an Ada System.Address has anything in common 
with a C pointer. On some platform/compiler combinations they're the 
same, but on others they're not. A convention-C access type should work 
everywhere. (Caveat: convention-C, in Ada, means a specific C compiler. 
For example, for GNAT, it means gcc, and may not correspond to the 
conventions used by MS C. If you're using another compiler, it's up to 
you to make sure things correspond.)

-- 
Jeff Carter
"That was the most fun I've ever had without laughing."
Annie Hall
43



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada to C Interface: Passing Pointers Via System.Address
  2005-01-12  2:16 Ada to C Interface: Passing Pointers Via System.Address ReversedBias
  2005-01-12  3:09 ` tmoran
  2005-01-13  1:25 ` Jeffrey Carter
@ 2005-01-13  3:05 ` Steve
  2 siblings, 0 replies; 8+ messages in thread
From: Steve @ 2005-01-13  3:05 UTC (permalink / raw)


I have had pretty good luck interfacing C/C++ with Ada.
My specific experience has been with Microsoft C and ObjectAda.

Things often don't work on my first try, but that's usually because I wasn't 
careful enough in setting up the C interface pragmas or didn't declare 
functions in C++ using "external C".

Steve
(The Duck)

"ReversedBias" <thehumblizer@hotmail.com> wrote in message 
news:8e19522cfe30bb2ccbfe1393fdd6b235@localhost.talkaboutprogramming.com...
>I am relatively new to Ada.  I am constructing an API so that my Ada
> clients can talk to my C code.  My C functions return pointers to data
> structures (very large structures).  I need to then pass the pointer to
> the data struct to the Ada code and back into other C functions.  There is
> no interaction with the data structure in the Ada code except to pass the
> pointer from one C function to another.  I have been trying to use the C
> interface pragma and return the pointers from C into an Ada System.Address
> and then pass that System.Address into the another pragma back down to the
> C functions.  My hope is that the C will be able to type cast the address
> back into the correct pointer type.  So far I have not been able to get
> this to work properly.  Please advise on another approach or post an
> example of how I could do this using the System.Address
>
> Thanks,
> 





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Ada to C Interface: Passing Pointers Via System.Address
  2005-01-12  3:09 ` tmoran
  2005-01-12  7:36   ` ReversedBias
@ 2005-01-13 17:14   ` Martin Krischik
  1 sibling, 0 replies; 8+ messages in thread
From: Martin Krischik @ 2005-01-13 17:14 UTC (permalink / raw)


tmoran@acm.org wrote:

>> My C functions return pointers to data structures (very large
>> structures). I need to then pass the pointer to the data struct to the
>> Ada code and
>> back into other C functions.  There is no interaction with the data
>> structure in the Ada code except to pass the pointer from one C function
>> to another.
>   If the data being passed through Ada is a black box to Ada, why not
> just declare a type of the appropriate size, eg
>   type C_Thing is range 0 .. 2**32-1;
>   for C_Thing'size use 32;

The World is on the dawn of 64bit CPUs. Better be prepared:

type C_Thing is mod System.Memory_Size;
for C_Thing'Size use System.Word_Size;

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-01-13 17:14 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-12  2:16 Ada to C Interface: Passing Pointers Via System.Address ReversedBias
2005-01-12  3:09 ` tmoran
2005-01-12  7:36   ` ReversedBias
2005-01-12  8:38     ` tmoran
2005-01-12 15:50       ` Mark Lorenzen
2005-01-13 17:14   ` Martin Krischik
2005-01-13  1:25 ` Jeffrey Carter
2005-01-13  3:05 ` Steve

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