comp.lang.ada
 help / color / mirror / Atom feed
* Accessing C arrays
@ 1998-08-11  0:00 Richard Beare
  1998-08-11  0:00 ` jsanchor
  0 siblings, 1 reply; 2+ messages in thread
From: Richard Beare @ 1998-08-11  0:00 UTC (permalink / raw)


I'm trying to build some bindings to some of our in house C code, and
one thing I would like to be able to do in a nice way is to put the c
allocated array memory into an ada form, complete with ranges.

I run into problems when trying to write a function that returns an
access type referencing the memory block allocated in the C code.

Here's what I had in mind
---------------------------------------
with System;

generic
   type Pixel is private;
package Image_Types is

   type Image_2d is array (Natural range <>, Natural range <>) of
aliased Pixel;

   type Image_1d is array (Natural range <>) of aliased Pixel;

   type H_Image_2d is access all Image_2d;
   type H_Image_1d is access all Image_1d;

   function Import(P : System.Address;
		   X, Y : Natural) return Image_2d;

   function Import(P : System.Address;
		   Len : Natural) return Image_1d;

   function Import(P : System.Address;
		   X, Y : Natural) return H_Image_2d;

end Image_Types;
---------------------------------------
package body Image_Types is

   function Import(P : System.Address;
		   X, Y : Natural) return Image_2d is

      Ada_Version : Image_2d(1 .. X, 1 .. Y);
      for Ada_Version'Address use P;
   begin
      return Ada_Version;
   end;

   ------------------------------------------------------------------
    function Import(P : System.Address;
		   Len : Natural) return Image_1d is

      Ada_Version : aliased Image_1d(1 .. Len);
      for Ada_Version'Address use P;
   begin
      return Ada_Version;
   end;
   -------------------------------------------------------------------
   function Import(P : System.Address;
		   X, Y : Natural) return H_Image_2d is

      L : H_Image_2d;
      Ada_Version : aliased Image_2d(1 .. X, 1 .. Y);
      for Ada_Version'Address use P;

   begin
      L := Ada_Version'Access;
      return L;
   end;

end Image_Types;
----------------------------------------------------

I'm not entirely sure what is wrong with the last function, but it
doesn't compile with gnat. The message is 

 L := Ada_Version'Access;
                   |
        >>> object subtype must statically match designated subtype

I'm not sure that this is the appropriate approach anyway. Is there a
way to create the additional information required by ada without
overwriting the work done by the C code. Surely something like

l : h_image_2d := new image_2d(1 .. X, 1 .. Y);
for l.all'address use p;

is performing the allocation anyway, and overwriting the original data.
(This doesn't compile either - I'm not sure about the correct use of
representation clauses.)

Any advice welcome.

-- 
Richard Beare
Richard.Beare@cmis.csiro.au




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

* Re: Accessing C arrays
  1998-08-11  0:00 Accessing C arrays Richard Beare
@ 1998-08-11  0:00 ` jsanchor
  0 siblings, 0 replies; 2+ messages in thread
From: jsanchor @ 1998-08-11  0:00 UTC (permalink / raw)


In article <35CFEF57.DEC91044@cmis.csiro.au>,
  Richard Beare <Richard.Beare@cmis.csiro.au> wrote:
>
> ----------------------------------------------------
>
> I'm not entirely sure what is wrong with the last function, but it
> doesn't compile with gnat. The message is
>
>  L := Ada_Version'Access;
>                    |
>         >>> object subtype must statically match designated subtype
>
> you could say:
   package Convert is new System.Address_to_access_conversions(Image_2d);

   ptr_to_image : Convert.Object_Pointer;
   ptr_to_image := CONVERT_TO_NVM_TYPE.To_Pointer(Ada_Version.all'address);
         L := H_Image_2d(ptr_to_image);
> Hopefully this works for you.

Jay S.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum




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

end of thread, other threads:[~1998-08-11  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-08-11  0:00 Accessing C arrays Richard Beare
1998-08-11  0:00 ` jsanchor

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