comp.lang.ada
 help / color / mirror / Atom feed
From: Craig Carey <research@ijs.co.nz>
Subject: Re: C array to Ada pointer to unconstrained array without copying memory
Date: Fri, 24 Oct 2003 10:27:54 +1300
Date: 2003-10-24T10:27:54+13:00	[thread overview]
Message-ID: <uvfgpvkrd7o1s3tklvldppcoi63vj262pe@4ax.com> (raw)
In-Reply-To: 0TUgb.639$2_2.29@newsfep1-gui.server.ntli.net

On Wed, 8 Oct 2003 15:07:48 +0100, "Steve Adams"
<s.adams_s_p_a_m_217@_s_p_a_m_ntlworld.csopam> wrote:

>Duncan,
>  I have had to do this for my project. What I do is not portable really but
>in general is OK if no other solution is available.
>
>Gnat does indeed use Fat pointers. *By Default*.
>You can make it use thin pointers though:
>
>type var_arr is array (positive range <>) of T;
>type var_arr_ptr is access var_arr;
>for var_arr_ptr'size use DWORD; -- from memory, probably want a 'size of int
>  or something
...

>You need to do some pointer manipulations in C when first allocating them to
>set the bounds, but Gnat just treats the arrays as normal.
>Gnat is good like this since it means that C pointer is handled the same as
>Ada pointer.
...
>ObjectAda however has the access value pointing to the bounds, this gives me
>quite a few headaches in my work, but gnat has a problem with the calling
...

>For information the Fat pointer is a struct with two pointers, one to the
>bounds and one to the data.
...

Taking the bounds information off is needed for interfacing with C.

(It is done by declaring what the exact size of the array is. Then the
double pointer becomes the single pointer C code would expect.)

----
   package IC renames Interfaces.C;

   type C_Array is array (Natural range <>) of XYZ;
   subtype Big_C_Array is C_Array (0 .. Natural'Last);  -- Thin pointer
                             --    0 .. 2,147,483,647
   type Big_C_Array_Ptr is access all Big_C_Array;

   pragma Convention (Convention => C, Entity => C_Array);

   procedure C_Ada_Proc (W : Big_C_Array_Ptr; Len : IC.int);
   pragma Export (C, C_Proc); 
...

   procedure C_Ada_Proc (W : Big_C_Array_Ptr; Len : IC.int)
   is
      type A_W is array (1 .. Len) of IC.int;
      type A_W_Ptr is access all A_W;

      function UC_W is new Ada.Unchecked_Conversion (
                        Source => Big_C_Array_Ptr, Target => A_W_Ptr);

      ZW1         : A_W_Ptr := UC_W (W);   --  points to 1st element

      CA          : A_W renames ZW1.all;   --  The same array C sees


Mr Sands question might have been answered better if the code causing
the problem was shown. C code won't lead to a need to know about the
internal design of an Ada compiler. Actually producing the C code might
indicate that somehow. Also perhaps Mr Sands could create the bounds
info instead of GNAT.


>"Duncan Sands"  wrote in message
>news:mailman.37.1065537708.25614.comp.lang.ada@ada-france.org...
>> Greetings.  Given a C pointer Ptr to a type T (which points to a block of
>>memory containing a C array of T's) and the length of the array Length,
>> there are various tricks for getting an Ada array of the right size
>> (defined on the stack - important!)
>> that uses the block of memory to hold the array data, i.e. no copying
>required.
>>
>> For example,
>>
>> type Ada_Array is array (Positive range <>) of T;
...
>> The problem is the bounds of course.  For example, GNAT usually uses
>> fat pointers, consisting of two normal pointers where one points to the
...





      parent reply	other threads:[~2003-10-23 21:27 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-07 14:43 C array to Ada pointer to unconstrained array without copying memory Duncan Sands
2003-10-07 18:30 ` tmoran
2003-10-07 19:24   ` Duncan Sands
2003-10-08  0:02   ` sk
     [not found]   ` <3F83540E.5000302@myob.com>
2003-10-08  9:18     ` Duncan Sands
2003-10-07 20:00 ` Jeffrey Carter
2003-10-07 20:39   ` Duncan Sands
2003-10-08  1:27     ` Jeffrey Carter
2003-10-07 20:53 ` Chad R. Meiners
2003-10-07 21:24   ` Duncan Sands
2003-10-07 22:44     ` C array to Ada pointer to unconstrained array without copyingmemory Chad R. Meiners
2003-10-07 22:52       ` Chad R. Meiners
2003-10-08  9:20         ` Duncan Sands
2003-10-08 16:16           ` C array to Ada pointer to unconstrained array withoutcopyingmemory Chad R. Meiners
2003-10-08 16:49             ` Duncan Sands
2003-10-08  2:14     ` C array to Ada pointer to unconstrained array without copying memory Robert I. Eachus
2003-10-08  9:27       ` Duncan Sands
2003-10-08 22:43         ` Robert I. Eachus
2003-10-09  9:31           ` Duncan Sands
2003-10-08 14:07 ` Steve Adams
2003-10-08 14:33   ` Preben Randhol
2003-10-09 23:04     ` Steve Adams
2003-10-09 23:11       ` Steve Adams
2003-10-08 16:30   ` Duncan Sands
2003-10-09 22:59     ` C array to Ada pointer to unconstrained array without copyingmemory Steve Adams
2003-10-10  7:02       ` Duncan Sands
2003-10-10 16:44         ` C array to Ada pointer to unconstrained array withoutcopyingmemory Steve Adams
2003-10-23 21:27   ` Craig Carey [this message]
replies disabled

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