comp.lang.ada
 help / color / mirror / Atom feed
From: Richard Beare <Richard.Beare@cmis.csiro.au>
Subject: Accessing C arrays
Date: 1998/08/11
Date: 1998-08-11T00:00:00+00:00	[thread overview]
Message-ID: <35CFEF57.DEC91044@cmis.csiro.au> (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




             reply	other threads:[~1998-08-11  0:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-08-11  0:00 Richard Beare [this message]
1998-08-11  0:00 ` Accessing C arrays jsanchor
replies disabled

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