comp.lang.ada
 help / color / mirror / Atom feed
From: "Dr. Adrian Wrigley" <amtw@linuxchip.demon.co.uk.uk.uk>
Subject: Re: C bindings, Interfaces.C.Pointers etc.
Date: Thu, 13 May 2004 16:56:17 +0100
Date: 2004-05-13T16:56:17+01:00	[thread overview]
Message-ID: <pan.2004.05.13.15.56.14.678895@linuxchip.demon.co.uk.uk.uk> (raw)
In-Reply-To: 4934218.sMSg6xXRUe@linux1.krischik.com

Thanks Martin for this suggestion.

I have given (an approximation of) my code below.

It doesn't use the I.C.Pointers package, and allows
Ada programs to access elements of the arrays using
standard array notation.  The main problem is that
the bound Ada uses are over-sized, and simple use
of 'range and 'last will fall off the ends.

I have used a generic package to create the appropriate
types needed, and wrap the "malloc" and "free".
This way, the C library can intermix allocations and
deallocations with those of the Ada code.

I put in the "aliased" - something I often forget
until the compiler complains at me!

Overall this solution meets my needs very well, and
if I accidentally use the invalid array (upper) bound,
the program crashes very rapidly (like it would have
done in C with a similar bug!).

I'm a little surprised that I.C.Pointers doesn't seem
to provide a convenient and efficient solution to this
problem - using C pointers directly in array access.

The next problem I am thinking about with this project
is how to abort a call into the C/C++ library safely.
Sometimes the library takes *much* too long to compute
a result. (If the parameters are unsuitable, it can
take hours vs seconds with good parameters).
If the thread executing the library call is aborted,
memory will hemmorage from the application.  This problem
will probably need a hacked version of the (Open Source)
library.  I'm not sure how to communicate with the
call as it executes telling it to clean up and exit
immediately.  Shared memory flags? Signals?

Thanks for the input on these problems!
--
Adrian


generic

   type Element_T is private;
   IndexBase : Integer := 0;

package CArrayUtils is

   type ElementArray_T is array (0 .. Interfaces.C.Size_T'Last) of aliased Element_T;
   pragma Convention (C, ElementArray_T);
   type ElementArray_A is access ElementArray_T;
   pragma Convention (C, ElementArray_A);

   function CAlloc (N : Integer) return ElementArray_A;
   procedure CFree (X : in out ElementArray_A);

end CArrayUtils;

package body CArrayUtils is

   function CAlloc (N : Integer) return ElementArray_A is
      function Malloc (A : Interfaces.C.Size_T) return ElementArray_A;
      pragma Import (C, Malloc, "malloc");
   begin
-- Is the size in bytes calculated correctly???  Probably not!
      return Malloc (Interfaces.C.Size_T (N * Element_T'Size / 8 + 1));
   end CAlloc;

   procedure CFree (X : in out ElementArray_A) is
      procedure Free (X : ElementArray_A);
      pragma Import (C, Free, "free");
   begin
      Free (X);
      X := null;
   end CFree;

end CArrayUtils;

with Interfaces.C;

package Blob is

  type Node_T is record
      Index : Interfaces.C.Int;
      Value : Interfaces.C.Double;
   end record;
   pragma Convention (C, Node_T);

   package NodeArray_P is new CArrayUtils (Element_T => Node_T);
   subtype NodeArray_A is NodeArray_P.ElementArray_A;

   package NodeArrayArray_P is new CArrayUtils (Element_T => NodeArray_A);
   subtype NodeArrayArray_A is NodeArrayArray_P.ElementArray_A;

   package DoubleArray_P is new CArrayUtils (Element_T => Interfaces.C.Double);
   subtype DoubleArray_A is DoubleArray_P.ElementArray_A;


   type SVMProblem_T is record
      L  : Interfaces.C.Int;
      Y  : DoubleArray_A;       -- Pointer to array of double
      X  : NodeArrayArray_A; -- (pointer to array of (pointer to array of SVMNode))
   end record;
   pragma Convention (C, Problem_T);

end Blob;




  reply	other threads:[~2004-05-13 15:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-11 13:14 C bindings, Interfaces.C.Pointers etc Dr. Adrian Wrigley
2004-05-11 18:17 ` tmoran
2004-05-11 18:48 ` Jeffrey Carter
2004-05-12  4:50   ` Simon Wright
2004-05-13 15:26   ` Dr. Adrian Wrigley
2004-05-12  6:30 ` Martin Krischik
2004-05-13 15:56   ` Dr. Adrian Wrigley [this message]
2004-05-13 17:37     ` Martin Krischik
2004-05-13 22:42     ` Jeffrey Carter
replies disabled

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