comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Access Type
Date: Tue, 28 Dec 2010 23:52:20 +0000
Date: 2010-12-28T23:52:20+00:00	[thread overview]
Message-ID: <m2pqsl78wb.fsf@pushface.org> (raw)
In-Reply-To: 76b6b4aa-201c-412d-93de-d22b532fde3f@w17g2000yqh.googlegroups.com

kug1977 <kug1977@web.de> writes:

> For my programm I need a type like System.Address but with a
> definitive size of 32-bits. So I've create one
>
>    type t_OFW_vAddress is mod t_Cell_Size;
> 	for t_OFW_vAddress'Size use 32 ;
> 	pragma Assert (t_OFW_vAddress'Size = 32);
> 	pragma Assert (t_OFW_vAddress'Alignment = 4) ;
>
>    OFW_Null_vAddress : constant t_OFW_vAddress := 0;

You shouldn't need the first Assert, because if the compiler can't
honour the "for t_OFW_vAddress'Size use 32;" it should refuse the
compilation.

Similarly, replace the second Assert by "for t_OFW_vAddress'Alignment
use 4;".

But .. are you using GNAT? because, being built on GCC, its natural
tendency is to use the same representations as C would. In any case, I
think you're making things more complicated than they need to be.

Assuming that both your Ada and C compilers are 32-bit (you have big
problems ahead if they are different!), type System.Address is going to
be 32-bits and 4-byte aligned anyway.

So why not just use System.Address? (I guess in that case it would make
sense to assert that System.Address'Size is 32, etc. Though the main
thing is to be sure that your compilers and system libraries agree!)

> I've now a record I will pass to a C-like function looks like this:
>
> 	type Chain_CI_Args is record
>          ...
> 		Another_Clients_Entry     : t_OFW_vAddress := OFW_Null_vAddress;
> 		Another_Clients_Arguments : t_OFW_vAddress := OFW_Null_vAddress;
>          ...
> 	end record;
> 	pragma Convention (C, Chain_CI_Args);
>
> 'Another_Clients_Entry' will hold the address of a procedure I defined
> by this:
>
> 	type Client_Entry is access procedure;
> 	pragma Convention (C, Client_Entry);
>
> and
> 'Another_Clients_Arguments' will be an array
>
>         type t_OFW_Buffer     is array (Positive range <>) of
> t_OFW_Byte;

If "type Client_Entry is" just "access procedure", how come it has
arguments?

> In my procedure I fill this record by
>
>    procedure Chain
>      (Virtual_Address           : in t_OFW_vAddress;
>       Free_Number_Of_Bytes      : in Cell;
>       Another_Clients_Entry     : in Client_Entry;
>       Another_Clients_Arguments : in t_OFW_Buffer)
> 	is
>       Arg : Chain_CI_Args :=
>                (...
> 		Another_Clients_Entry            =>
> t_OFW_vAddress'(Another_Clients_Entry), -- ???
> 		Another_Clients_Arguments        => Another_Clients_Arguments
> (Another_Clients_Arguments'First)'Address, -- ??
> 		...
>                );

I would define the Entry item by just

   type Chain_CI_Args is record
      ...
      Another_Clients_Entry : Client_Entry;

The Arguments part is a bit more tricky. It's a bad idea to try passing
indefinite items like a t_OFW_Buffer via a single thin pointer; GNAT
does it using a two-component (fat) pointer. You'll need either to pass
another element saying how many bytes there are in the Arguments, or to
have it terminated somehow.

I'd then try

   type Chain_CI_Args is record
      ...
      Another_Clients_Entry : Client_Entry;
      Another_Clients_Arguments : System.Address;
      Another_Clients_Args_Len : Integer;
      ...

so you'd write

      ...
      Another_Clients_Entry => Another_Clients_Entry,
      Another_Clients_Arguments => 
         Another_Clients_Arguments
      (Another_Clients_Arguments'First)'Address,
      Another_Clients_Args_Len => Another_Clients_Arguments'Length,
      ...

which does assume that the elements of t_OFW_Buffer are contiguous and
that Integer is OK to be passed in a C struct (both pretty good guesses
for any normal compiler, I'd say).

> PS: I know, that this isn't something a beginner in Ada should start
> with, but I like to finnish porting this from C to Ada.

You start from wherever you happen to be!


Hope that helps; my laptop's battery is running out & it's bedtime!



  reply	other threads:[~2010-12-28 23:52 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-24 16:36 Access Type kug1977
2010-12-24 17:30 ` Robert A Duff
2010-12-24 20:59   ` kug1977
2010-12-24 22:06     ` Simon Wright
2010-12-27  1:50     ` Ludovic Brenta
2010-12-27 12:29       ` Simon Wright
2010-12-27 17:53         ` Ludovic Brenta
2010-12-27 18:15           ` Simon Wright
2010-12-27 20:03             ` Pascal Obry
2010-12-28 20:08               ` kug1977
2010-12-28 23:52                 ` Simon Wright [this message]
2010-12-29  8:46                   ` kug1977
2010-12-29 15:18                     ` Simon Wright
2010-12-29 15:27                       ` Ludovic Brenta
replies disabled

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