comp.lang.ada
 help / color / mirror / Atom feed
* Re: Ada Address = C pointer ?
  2001-10-26 18:53 Ada Address = C pointer ? M. A. Alves
@ 2001-10-26 18:09 ` Lutz Donnerhacke
  2001-10-26 21:06   ` Florian Weimer
  2001-10-26 19:34 ` Mark Johnson
  1 sibling, 1 reply; 25+ messages in thread
From: Lutz Donnerhacke @ 2001-10-26 18:09 UTC (permalink / raw)


* M. A. Alves wrote:
>Are these equivalences compiler(GNAT)-specific or of the language?
>
>  Ada                   C
>  -----------------------
>  Address          void *
>  Address          char *
>  Null_Address       NULL

GNAT. But pragma (Convention, C, ...); does what you expect.

>If they are of the language, where on the ARM it says so?

The char_p equivalent can be found as Interfaces.C.Strings.chars_p;
The void_p equivalent can be found as
  type void_p is access all Storage_Array;
  pragma (Convention, C, void_p); -- for void_p'Size use chars_p'Size;



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

* Ada Address = C pointer ?
@ 2001-10-26 18:53 M. A. Alves
  2001-10-26 18:09 ` Lutz Donnerhacke
  2001-10-26 19:34 ` Mark Johnson
  0 siblings, 2 replies; 25+ messages in thread
From: M. A. Alves @ 2001-10-26 18:53 UTC (permalink / raw)
  To: comp.lang.ada

Are these equivalences compiler(GNAT)-specific or of the language?

  Ada                   C
  -----------------------
  Address          void *
  Address          char *
  Null_Address       NULL

If they are of the language, where on the ARM it says so?

(Not a pressing issue for me.)

Thanks a lot,

-- 
   ,
 M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
 A M A D O   Rua Campo Alegre, 823         fax 351+226003654
 A L V E S   P-4150 PORTO, Portugal        mob 351+939354002





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

* Re: Ada Address = C pointer ?
  2001-10-26 18:53 Ada Address = C pointer ? M. A. Alves
  2001-10-26 18:09 ` Lutz Donnerhacke
@ 2001-10-26 19:34 ` Mark Johnson
  2001-10-26 23:39   ` tmoran
  1 sibling, 1 reply; 25+ messages in thread
From: Mark Johnson @ 2001-10-26 19:34 UTC (permalink / raw)


"M. A. Alves" wrote:

> Are these equivalences compiler(GNAT)-specific or of the language?
>
>   Ada                   C
>   -----------------------
>   Address          void *
>   Address          char *
>   Null_Address       NULL
>

In general - compiler specific. Having said that, I'm not aware of any
implementations where that kind of relationship is not true.

>
> If they are of the language, where on the ARM it says so?

Actually, I suggest a review of Annex B, section 3 (or B.3). For example,
at the end of B.3, the Implementation Advice describes the interface
correspondence that SHOULD (not must) be implemented. I believe GNAT
follows the implementation advice in this area. I would expect most other
vendors to do the same.
  --Mark




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

* Re: Ada Address = C pointer ?
  2001-10-26 18:09 ` Lutz Donnerhacke
@ 2001-10-26 21:06   ` Florian Weimer
  0 siblings, 0 replies; 25+ messages in thread
From: Florian Weimer @ 2001-10-26 21:06 UTC (permalink / raw)


lutz@iks-jena.de (Lutz Donnerhacke) writes:

> The char_p equivalent can be found as Interfaces.C.Strings.chars_p;
> The void_p equivalent can be found as
>   type void_p is access all Storage_Array;
>   pragma (Convention, C, void_p); -- for void_p'Size use chars_p'Size;

The second part (on void_p) is wrong.  chars_ptr and void * are
guaranteed to have the same representation, but there's no such
guarantee for your suggested equivalent.



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

* Re: Ada Address = C pointer ?
  2001-10-26 19:34 ` Mark Johnson
@ 2001-10-26 23:39   ` tmoran
  2001-10-29 13:07     ` M. A. Alves
  0 siblings, 1 reply; 25+ messages in thread
From: tmoran @ 2001-10-26 23:39 UTC (permalink / raw)


> >   Ada                   C
> >   -----------------------
> >   Address          void *
> >   Address          char *
> >   Null_Address       NULL
> >
>
> In general - compiler specific. Having said that, I'm not aware of any
> implementations where that kind of relationship is not true.
  ARM 13.7(37) gives the implementation advice that "Address should be
of a private type."  So it most assuredly is not guaranteed to be anything
at all.  In Ada compilers allowing segment+offset addressing modes
on Intel CPUs, for instance, System.Address will be 48 bits wide, whereas
a C pointer is probably 32 bits with segment implied.  Use access types,
not System.Address.

> > If they are of the language, where on the ARM it says so?
>
> Actually, I suggest a review of Annex B, section 3 (or B.3). For example,
> at the end of B.3, the Implementation Advice describes the interface
> correspondence that SHOULD (not must) be implemented. I believe GNAT
> follows the implementation advice in this area. I would expect most other
> vendors to do the same.
  B.3(67 .. 71) refer to Ada access types, not to System.Address, and
to parameter passing, where of course the compiler may well do some
implicit conversions.



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

* Re: Ada Address = C pointer ?
  2001-10-26 23:39   ` tmoran
@ 2001-10-29 13:07     ` M. A. Alves
  2001-10-29 20:19       ` Matthew Heaney
  2001-10-29 23:15       ` tmoran
  0 siblings, 2 replies; 25+ messages in thread
From: M. A. Alves @ 2001-10-29 13:07 UTC (permalink / raw)
  To: comp.lang.ada

>   ARM 13.7(37) gives the implementation advice that "Address should be
> of a private type."  So it most assuredly is not guaranteed to be anything
> at all.

Right. Hence my question.

> . . . Use access types, not System.Address.

You mean use Address_To_Access conversion?  Because what I have is C
pointers I need to interface with.  I am using Chars_Ptr because it has a
Null_Ptr value and I need to test for, and pass, NULL.  (And in C char *
is assuredly able to point to anything.)  But I am indeed considering
using Address et al., and Address to Access conversion.

Yes, the ARM has a lot of nice implementaion _advices_ on this.

-- 
   ,
 M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
 A M A D O   Rua Campo Alegre, 823         fax 351+226003654
 A L V E S   P-4150 PORTO, Portugal        mob 351+939354002





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

* Re: Ada Address = C pointer ?
  2001-10-29 13:07     ` M. A. Alves
@ 2001-10-29 20:19       ` Matthew Heaney
  2001-10-29 23:15         ` tmoran
  2001-10-30  9:58         ` M. A. Alves
  2001-10-29 23:15       ` tmoran
  1 sibling, 2 replies; 25+ messages in thread
From: Matthew Heaney @ 2001-10-29 20:19 UTC (permalink / raw)



"M. A. Alves" <maa@liacc.up.pt> wrote in message
news:mailman.1004357020.21840.comp.lang.ada@ada.eu.org...
> You mean use Address_To_Access conversion?  Because what I have is C
> pointers I need to interface with.  I am using Chars_Ptr because it has a
> Null_Ptr value and I need to test for, and pass, NULL.  (And in C char *
> is assuredly able to point to anything.)  But I am indeed considering
> using Address et al., and Address to Access conversion.

The Ada type that corresponds to the C type char* is
Interfaces.C.Strings.chars_ptr.  This is enunciated explicitly in RM95 B.3.1
(1) and (22).

Unfortunately, there is no binding to the C type void* -- this was omitted
from the standard (I assume by accident).  I assume that there will be a
binding to void* in Ada0Y.

I think it's safe to assume that void* is the same as char*, so you should
be OK using C.Strings.chars_ptr where a void* is expected.

I do NOT think you need to use System.Address and Addr_To_Acc_Conv.  The
types declared in C.Strings should be adequate.  That being said, I would be
very surprised if System.Address had a different representation from char*
or void*.






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

* Re: Ada Address = C pointer ?
  2001-10-29 13:07     ` M. A. Alves
  2001-10-29 20:19       ` Matthew Heaney
@ 2001-10-29 23:15       ` tmoran
  1 sibling, 0 replies; 25+ messages in thread
From: tmoran @ 2001-10-29 23:15 UTC (permalink / raw)


> > . . . Use access types, not System.Address.
>
> You mean use Address_To_Access conversion?  Because what I have is C
> pointers I need to interface with.
  No.  Forget about System.Address.  Use access types and pragma
Convention (or Import & Export).



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

* Re: Ada Address = C pointer ?
  2001-10-29 20:19       ` Matthew Heaney
@ 2001-10-29 23:15         ` tmoran
  2001-10-30 10:30           ` M. A. Alves
  2001-10-30  9:58         ` M. A. Alves
  1 sibling, 1 reply; 25+ messages in thread
From: tmoran @ 2001-10-29 23:15 UTC (permalink / raw)


>very surprised if System.Address had a different representation from char*
>or void*.
  Hold onto your hat!  The program
with system;
with ada.text_io;
procedure junk is
  x : integer;
  a : constant system.address := x'address;
  package sio is new ada.text_io.integer_io(system.word);
  package oio is new ada.text_io.integer_io(system.offset_type);
begin
  sio.put(a.segment, base=>16);
  oio.put(a.offset, base=>16);
  ada.text_io.new_line;
end junk;
compiles (Janus compiler) and runs, producing
16#157# 16#60FCD4#
on the Windows 95 system I'm posting from.



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

* Re: Ada Address = C pointer ?
  2001-10-29 20:19       ` Matthew Heaney
  2001-10-29 23:15         ` tmoran
@ 2001-10-30  9:58         ` M. A. Alves
  2001-10-30 12:02           ` Lutz Donnerhacke
                             ` (2 more replies)
  1 sibling, 3 replies; 25+ messages in thread
From: M. A. Alves @ 2001-10-30  9:58 UTC (permalink / raw)
  To: comp.lang.ada

On Mon, 29 Oct 2001, Matthew Heaney wrote:
> "M. A. Alves" <maa@liacc.up.pt> wrote in message
> news:mailman.1004357020.21840.comp.lang.ada@ada.eu.org...
> > You mean use Address_To_Access conversion?  Because what I have is C
> > pointers I need to interface with.  I am using Chars_Ptr because it has a
> > Null_Ptr value and I need to test for, and pass, NULL.  (And in C char *
> > is assuredly able to point to anything.)  But I am indeed considering
> > using Address et al., and Address to Access conversion.
>
> . . .
>
> I do NOT think you need to use System.Address and Addr_To_Acc_Conv.  The
> types declared in C.Strings should be adequate.

I am in fact using Address and Access_To_Address_Conversions(*) because I
simply did not find out how to do the trick otherwise.  For example, the C
function is returning an array of unsigned long.  I want to access that
array without copying it.  How can you do that with Interfaces.C.Strings
or even Interfaces.C.Pointers ?

Also, I did not found the equivalence Null_Ptr = NULL stated in the RM.

> That being said, I would be very surprised if System.Address had a
> different representation from char* or void*.

Yes, and for now my implementation is bound to that assumption.
_____
(*) I am using a technique similar to the one referenced as 2c in
"Accessing Memory as a String" by Robert Dewar, available at
adapower.com/lang/accessmem.html .

-- 
   ,
 M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
 A M A D O   Rua Campo Alegre, 823         fax 351+226003654
 A L V E S   P-4150 PORTO, Portugal        mob 351+939354002






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

* Re: Ada Address = C pointer ?
  2001-10-29 23:15         ` tmoran
@ 2001-10-30 10:30           ` M. A. Alves
  2001-10-30 19:53             ` tmoran
  0 siblings, 1 reply; 25+ messages in thread
From: M. A. Alves @ 2001-10-30 10:30 UTC (permalink / raw)
  To: comp.lang.ada

On Mon, 29 Oct 2001 tmoran@acm.org wrote:
> >very surprised if System.Address had a different representation from char*
> >or void*.
>   Hold onto your hat!  The program
> with system;
> with ada.text_io;
> procedure junk is
>   x : integer;
>   a : constant system.address := x'address;
>   package sio is new ada.text_io.integer_io(system.word);
>   package oio is new ada.text_io.integer_io(system.offset_type);
> begin
>   sio.put(a.segment, base=>16);
>   oio.put(a.offset, base=>16);
>   ada.text_io.new_line;
> end junk;
> compiles (Janus compiler) and runs, producing
> 16#157# 16#60FCD4#
> on the Windows 95 system I'm posting from.

Interesting. The code is incompatible with GNAT.

The original issue in this thread is the set of equivalences:

Ada                C
--------------------
Address       * void
Address       * char
Null_Address    NULL

GNAT seems to honor these equivalences.  Does Janus?

-- 
   ,
 M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
 A M A D O   Rua Campo Alegre, 823         fax 351+226003654
 A L V E S   P-4150 PORTO, Portugal        mob 351+939354002





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

* Re: Ada Address = C pointer ?
  2001-10-30  9:58         ` M. A. Alves
@ 2001-10-30 12:02           ` Lutz Donnerhacke
  2001-10-30 12:53             ` M. A. Alves
  2001-10-30 17:10           ` Mark Johnson
  2001-10-30 19:53           ` tmoran
  2 siblings, 1 reply; 25+ messages in thread
From: Lutz Donnerhacke @ 2001-10-30 12:02 UTC (permalink / raw)


* M. A. Alves wrote:
>I am in fact using Address and Access_To_Address_Conversions(*) because I
>simply did not find out how to do the trick otherwise.  For example, the C
>function is returning an array of unsigned long.  I want to access that
>array without copying it.  How can you do that with Interfaces.C.Strings
>or even Interfaces.C.Pointers?

How about:

with Interfaces.C.Pointers;

procedure t is
   type long_array is array (Integer range <>) of aliased Interfaces.C.long;
   
   package long_pointers is new Interfaces.C.Pointers (
     Index              => Integer,
     Element            => Interfaces.C.long,
     Element_Array      => long_array,
     Default_Terminator => 0
   );
   
   function get (a : long_pointers.Pointer; i : Interfaces.C.ptrdiff_t)
     return Interfaces.C.long is
     
      use type long_pointers.Pointer;
      
      v : long_array (1 .. 1) :=
        long_pointers.Value (a + i, Interfaces.C.ptrdiff_t (1));
   begin
      return v (1);
   end get;
   pragma Inline (get);
   
   --  real work starts here
   
   function get_long_array return long_pointers.Pointer;
   pragma Import (C, get_long_array);
   
   my_array : constant long_pointers.Pointer := get_long_array;
begin
   ... := get (my_array, 4);
end t;




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

* Re: Ada Address = C pointer ?
  2001-10-30 12:02           ` Lutz Donnerhacke
@ 2001-10-30 12:53             ` M. A. Alves
  2001-10-30 12:56               ` Lutz Donnerhacke
  2001-10-30 14:19               ` Matthew Heaney
  0 siblings, 2 replies; 25+ messages in thread
From: M. A. Alves @ 2001-10-30 12:53 UTC (permalink / raw)
  To: comp.lang.ada

On Tue, 30 Oct 2001, Lutz Donnerhacke wrote:
> How about:
>
> with Interfaces.C.Pointers;
>
> procedure t is
>    type long_array is array (Integer range <>) of aliased Interfaces.C.long;
>
>    package long_pointers is new Interfaces.C.Pointers (
>      Index              => Integer,
>      Element            => Interfaces.C.long,
>      Element_Array      => long_array,
>      Default_Terminator => 0
>    );
>
>    function get (a : long_pointers.Pointer; i : Interfaces.C.ptrdiff_t)
>      return Interfaces.C.long is
>
>       use type long_pointers.Pointer;
>
>       v : long_array (1 .. 1) :=
>         long_pointers.Value (a + i, Interfaces.C.ptrdiff_t (1));
>    begin
>       return v (1);
>    end get;
>    pragma Inline (get);
>
>    --  real work starts here
>
>    function get_long_array return long_pointers.Pointer;
>    pragma Import (C, get_long_array);
>
>    my_array : constant long_pointers.Pointer := get_long_array;
> begin
>    ... := get (my_array, 4);
> end t;

Seems right.  The C.Pointers tutorial I was missing, thanks a lot.

However, I am still not sure of the equivalence between null (a value of
Pointer) and NULL (C), i.e. is it garanteed that if the C function returns
NULL get_long_array returns null?

-- 
   ,
 M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
 A M A D O   Rua Campo Alegre, 823         fax 351+226003654
 A L V E S   P-4150 PORTO, Portugal        mob 351+939354002





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

* Re: Ada Address = C pointer ?
  2001-10-30 12:53             ` M. A. Alves
@ 2001-10-30 12:56               ` Lutz Donnerhacke
  2001-10-30 14:26                 ` M. A. Alves
  2001-10-30 14:19               ` Matthew Heaney
  1 sibling, 1 reply; 25+ messages in thread
From: Lutz Donnerhacke @ 2001-10-30 12:56 UTC (permalink / raw)


* M. A. Alves wrote:
>However, I am still not sure of the equivalence between null (a value of
>Pointer) and NULL (C), i.e. is it garanteed that if the C function returns
>NULL get_long_array returns null?

Import conventions are your friend.



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

* Re: Ada Address = C pointer ?
  2001-10-30 12:53             ` M. A. Alves
  2001-10-30 12:56               ` Lutz Donnerhacke
@ 2001-10-30 14:19               ` Matthew Heaney
  2001-10-30 14:41                 ` M. A. Alves
  1 sibling, 1 reply; 25+ messages in thread
From: Matthew Heaney @ 2001-10-30 14:19 UTC (permalink / raw)



"M. A. Alves" <maa@liacc.up.pt> wrote in message
news:mailman.1004446456.21280.comp.lang.ada@ada.eu.org...
> However, I am still not sure of the equivalence between null (a value of
> Pointer) and NULL (C), i.e. is it garanteed that if the C function returns
> NULL get_long_array returns null?

Yes.  See RM95 B3.2 (17).






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

* Re: Ada Address = C pointer ?
  2001-10-30 12:56               ` Lutz Donnerhacke
@ 2001-10-30 14:26                 ` M. A. Alves
  0 siblings, 0 replies; 25+ messages in thread
From: M. A. Alves @ 2001-10-30 14:26 UTC (permalink / raw)
  To: comp.lang.ada

On Tue, 30 Oct 2001, Lutz Donnerhacke wrote:
> * M. A. Alves wrote:
> >However, I am still not sure of the equivalence between null (a value of
> >Pointer) and NULL (C), i.e. is it garanteed that if the C function returns
> >NULL get_long_array returns null?
>
> Import conventions are your friend.

I guess you mean the equivalence is garanteed for Imported functions.
Sure, I also expect that, *** but could not found it stated in the RM ***.
I dont think the RM ever references NULL--and it should.  Maybe the AARM
says something (didn't look).

-- 
   ,
 M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
 A M A D O   Rua Campo Alegre, 823         fax 351+226003654
 A L V E S   P-4150 PORTO, Portugal        mob 351+939354002






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

* Re: Ada Address = C pointer ?
  2001-10-30 14:19               ` Matthew Heaney
@ 2001-10-30 14:41                 ` M. A. Alves
  0 siblings, 0 replies; 25+ messages in thread
From: M. A. Alves @ 2001-10-30 14:41 UTC (permalink / raw)
  To: comp.lang.ada

On Tue, 30 Oct 2001, Matthew Heaney wrote:
> "M. A. Alves" <maa@liacc.up.pt> wrote in message
> news:mailman.1004446456.21280.comp.lang.ada@ada.eu.org...
> > However, I am still not sure of the equivalence between null (a value of
> > Pointer) and NULL (C), i.e. is it garanteed that if the C function returns
> > NULL get_long_array returns null?
>
> Yes.  See RM95 B3.2 (17).

Yes, I know that null = NULL seems an obvious interpretation of
"C-compatible", but the RM is not known for relying on the obvious.

So, either way (Interfaces.C.Pointers vs.
System.Address_To_Access_Conversions), _assumptions_ are made (by the
programmer).

But I agree a Pointers idiom is a viable, perhaps better, alternative to a
System one. Perhaps I'll change my code some day.

-- 
   ,
 M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
 A M A D O   Rua Campo Alegre, 823         fax 351+226003654
 A L V E S   P-4150 PORTO, Portugal        mob 351+939354002





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

* Re: Ada Address = C pointer ?
  2001-10-30  9:58         ` M. A. Alves
  2001-10-30 12:02           ` Lutz Donnerhacke
@ 2001-10-30 17:10           ` Mark Johnson
  2001-10-30 18:01             ` M. A. Alves
  2001-10-30 19:53           ` tmoran
  2 siblings, 1 reply; 25+ messages in thread
From: Mark Johnson @ 2001-10-30 17:10 UTC (permalink / raw)


"M. A. Alves" wrote:

> [snip]

> I am in fact using Address and Access_To_Address_Conversions(*) because I
> simply did not find out how to do the trick otherwise.  For example, the C
> function is returning an array of unsigned long.  I want to access that
> array without copying it.  How can you do that with Interfaces.C.Strings
> or even Interfaces.C.Pointers ?
>

Reread the implementation advice in B.3....
 An Ada parameter of an array type with component type T, of any mode, is passed
as a t* argument to a C function, where t is the C type corresponding to the Ada
type T.

Of course, your mileage may vary - but here's a simple test case that I ran using
GNAT on Linux....

a.c...
#include <string.h>
void a ( int * x, int * y) {
  memcpy(y, x, 40);
}
b.adb...
with Text_Io;
procedure B is
  type Xt is array (1..10) of Integer;
  Y : Xt := (others => 1);
  Z : Xt := (others => 0);

  procedure A (X : Xt; Y : Xt);
  pragma Import(C, A, "a");

  begin
  A(Y, Z);
  if (Z(1)=1) then
    Text_Io.Put_Line("Ok");
  else
    Text_Io.Put_Line("Failed");
  end if;
end B;

which after being built runs and prints out "Ok".

Good luck.
  --Mark




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

* Re: Ada Address = C pointer ?
  2001-10-30 17:10           ` Mark Johnson
@ 2001-10-30 18:01             ` M. A. Alves
  2001-10-30 20:23               ` tmoran
  0 siblings, 1 reply; 25+ messages in thread
From: M. A. Alves @ 2001-10-30 18:01 UTC (permalink / raw)
  To: comp.lang.ada

On Tue, 30 Oct 2001, Mark Johnson wrote:
> "M. A. Alves" wrote:
> > I am in fact using Address and Access_To_Address_Conversions(*) because I
> > simply did not find out how to do the trick otherwise.  For example, the C
> > function is returning an array of unsigned long.  I want to access that
> > array without copying it.  How can you do that with Interfaces.C.Strings
> > or even Interfaces.C.Pointers ?
>
> Reread the implementation advice in B.3....
>  An Ada parameter of an array type with component type T, of any mode, is passed
> as a t* argument to a C function, where t is the C type corresponding to the Ada
> type T.

Yes, a very sensible _advice_ indeed. Somehow I feel more assured with
Address_To_Access_Conversions. Actually it is more than just a feeling,
according to http://adapower.com/lang/accessmem.html .

But adhering to the advice surely makes for the most elegant coding.

Note that by now, thanks to all, this thread has tutored all possible
idioms of C-interfacing namely:

  * using Address_To_Access_Conversions

  * using Interfaces.C.Pointers

  * commiting to implementation advice in B.3...

Well done guys!

-- 
   ,
 M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
 A M A D O   Rua Campo Alegre, 823         fax 351+226003654
 A L V E S   P-4150 PORTO, Portugal        mob 351+939354002





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

* Re: Ada Address = C pointer ?
  2001-10-30 10:30           ` M. A. Alves
@ 2001-10-30 19:53             ` tmoran
  2001-10-31 13:01               ` M. A. Alves
  0 siblings, 1 reply; 25+ messages in thread
From: tmoran @ 2001-10-30 19:53 UTC (permalink / raw)


> Interesting. The code is incompatible with GNAT.
  Of course it is.  The particular Gnat you are using has a System.Address
that only works within a single segment, so it does not include the
segment part.  The ARM suggests System.Address should be private,
presumably to discourage coding that depends on a particular
implementation.

> Ada                C
> --------------------
> Address       * void
> Address       * char
> Null_Address    NULL
>
> GNAT seems to honor these equivalences.  Does Janus?
  (I'm not sure I'd use the word "honor" to describe a particular
implementation artifact. ;)
  I haven't used any Janus compilers on other platforms, so can't answer
for them.  On x86's Janus appears to convert (I haven't experimented with this
since I use Access types) automagically on parameter passing, but I'm sure

  type Address_List is array(1 .. 10) of System.Address;
  X : Address_List;
  procedure Cfunc(X : out Address_List);
  pragma Import(C, Cfunc);
---
  void Cfunc(char **p)

would give unpleasant results.

Do all versions of Gnat on all platforms implement those particular
equivalences?  Do even all other Ada compilers on your current platform
implement them?

> Yes, I know that null = NULL seems an obvious interpretation of
> "C-compatible", but the RM is not known for relying on the obvious.
>
> So, either way (Interfaces.C.Pointers vs.
> System.Address_To_Access_Conversions), _assumptions_ are made (by the
> programmer).
  Can you suggest any other reasonable interpretation besides null = NULL?
Does the RM even suggest that a System.Address has anything to do with
interfacing to other languages?  The RM may not be obvious, but it doesn't
*try* to mislead.

> But I agree a Pointers idiom is a viable, perhaps better, alternative to a
> System one. Perhaps I'll change my code some day.
  In the meantime, put in a comment for future maintainers that
your code assumes a specific Gnat version on a specific platform.
(Having maintained code dependent on a particular, ancient, version of
a compiler, I offer my condolences to those maintainers.)



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

* Re: Ada Address = C pointer ?
  2001-10-30  9:58         ` M. A. Alves
  2001-10-30 12:02           ` Lutz Donnerhacke
  2001-10-30 17:10           ` Mark Johnson
@ 2001-10-30 19:53           ` tmoran
  2 siblings, 0 replies; 25+ messages in thread
From: tmoran @ 2001-10-30 19:53 UTC (permalink / raw)


> For example, the C function is returning an array of unsigned long.  I
> want to access that array without copying it.

  type UL is array(1 .. 100) of Unsigned_Long;
  type P_UL is access all UL;
  function Cfunc(c : in character) return P_UL;
  pragma (C, Cfunc);
  P : P_UL;
  ...
  Ada.Text_IO.Put_Line(Unsigned_Long'image(Cfunc('a').all(99)));
  P := Cfunc('b');
  Ada.Text_IO.Put_Line(Unsigned_Long'image(P.all(98)));



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

* Re: Ada Address = C pointer ?
  2001-10-30 18:01             ` M. A. Alves
@ 2001-10-30 20:23               ` tmoran
  2001-10-31 13:13                 ` M. A. Alves
  0 siblings, 1 reply; 25+ messages in thread
From: tmoran @ 2001-10-30 20:23 UTC (permalink / raw)


> Somehow I feel more assured with Address_To_Access_Conversions.
> Actually it is more than just a feeling, according to
> http://adapower.com/lang/accessmem.html .
  That is an unfortunate reference since it contains a couple of errors
that have led you astray.  In particular:

>   S : XString;
>   for S'Address use A;
>
> This will likely work on most implementations, assuming that
> they do not use dope vectors or descriptors for constrained
> arrays. This is a reasonable assumption, and certainly true
> of GNAT,
  It certainly seems that it *must* work on compilers that follow the
implementation advice ARM 13.3(14) "For an array X, X'Address should point
at the first component of the array, ..."  It was perhaps more of a
problem in Ada 83.

> Method 1b.  Use Unchecked_Conversion
>   in practice, this works fine, since all
>   implementations will use the same representation
>   for constrained pointers and addresses.
  This is simply incorrect.  There exist counterexamples.

As it says, the System.Address_To_Access method is preferable in
Ada 95, but was not available in Ada 83.  The web page says nothing
about Interfaces.C, which presumably are supposed to let you avoid
this kind of bare metal System.Address stuff entirely.

Hopefully Robert Dewar will update the web page.



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

* Re: Ada Address = C pointer ?
  2001-10-30 19:53             ` tmoran
@ 2001-10-31 13:01               ` M. A. Alves
  2001-10-31 14:44                 ` Marin David Condic
  0 siblings, 1 reply; 25+ messages in thread
From: M. A. Alves @ 2001-10-31 13:01 UTC (permalink / raw)
  To: comp.lang.ada

On Tue, 30 Oct 2001 tmoran@acm.org wrote:
> . . .
>
> > So, either way (Interfaces.C.Pointers vs.
> > System.Address_To_Access_Conversions), _assumptions_ are made (by the
> > programmer).
>
> . . . Does the RM even suggest that a System.Address has anything to
> do with interfacing to other languages?

Hmmm... touch�.

> . . .
>
>   In the meantime, put in a comment for future maintainers that
> your code assumes a specific Gnat version on a specific platform.

Something like that, yes.

> . . .

Thanks,

-- 
   ,
 M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
 A M A D O   Rua Campo Alegre, 823         fax 351+226003654
 A L V E S   P-4150 PORTO, Portugal        mob 351+939354002





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

* Re: Ada Address = C pointer ?
  2001-10-30 20:23               ` tmoran
@ 2001-10-31 13:13                 ` M. A. Alves
  0 siblings, 0 replies; 25+ messages in thread
From: M. A. Alves @ 2001-10-31 13:13 UTC (permalink / raw)
  To: comp.lang.ada

On Tue, 30 Oct 2001 tmoran@acm.org wrote:
> > . . . according to
> > http://adapower.com/lang/accessmem.html .
>   That is an unfortunate reference since it contains a couple of errors
> that have led you astray.

Not really _errors_. But I see your point.

> . . . The web page says nothing about Interfaces.C

It is not about interfacing with another language, just mapping "bare
metal" addresses onto a String.

> . . .

-- 
   ,
 M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
 A M A D O   Rua Campo Alegre, 823         fax 351+226003654
 A L V E S   P-4150 PORTO, Portugal        mob 351+939354002





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

* Re: Ada Address = C pointer ?
  2001-10-31 13:01               ` M. A. Alves
@ 2001-10-31 14:44                 ` Marin David Condic
  0 siblings, 0 replies; 25+ messages in thread
From: Marin David Condic @ 2001-10-31 14:44 UTC (permalink / raw)


"M. A. Alves" <maa@liacc.up.pt> wrote in message
news:mailman.1004534209.16912.comp.lang.ada@ada.eu.org...
> >
> >   In the meantime, put in a comment for future maintainers that
> > your code assumes a specific Gnat version on a specific platform.
>
> Something like that, yes.
>
Better yet - isolate it in a package of system dependencies if at all
possible so that if there is ever a need to port, you know exactly where to
look to make all the changes.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/





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

end of thread, other threads:[~2001-10-31 14:44 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-26 18:53 Ada Address = C pointer ? M. A. Alves
2001-10-26 18:09 ` Lutz Donnerhacke
2001-10-26 21:06   ` Florian Weimer
2001-10-26 19:34 ` Mark Johnson
2001-10-26 23:39   ` tmoran
2001-10-29 13:07     ` M. A. Alves
2001-10-29 20:19       ` Matthew Heaney
2001-10-29 23:15         ` tmoran
2001-10-30 10:30           ` M. A. Alves
2001-10-30 19:53             ` tmoran
2001-10-31 13:01               ` M. A. Alves
2001-10-31 14:44                 ` Marin David Condic
2001-10-30  9:58         ` M. A. Alves
2001-10-30 12:02           ` Lutz Donnerhacke
2001-10-30 12:53             ` M. A. Alves
2001-10-30 12:56               ` Lutz Donnerhacke
2001-10-30 14:26                 ` M. A. Alves
2001-10-30 14:19               ` Matthew Heaney
2001-10-30 14:41                 ` M. A. Alves
2001-10-30 17:10           ` Mark Johnson
2001-10-30 18:01             ` M. A. Alves
2001-10-30 20:23               ` tmoran
2001-10-31 13:13                 ` M. A. Alves
2001-10-30 19:53           ` tmoran
2001-10-29 23:15       ` tmoran

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