comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Question on interface Ada to C
Date: 25 May 2004 22:38:07 +0100
Date: 2004-05-25T22:38:07+01:00	[thread overview]
Message-ID: <x7vr7t85gsg.fsf@smaug.pushface.org> (raw)
In-Reply-To: rac7b0tnlsoauhcharlguhr7v3g30npk6b@4ax.com

James Alan Farrell <anonymous@anonymous.com> writes:

>  type stuff is integer; -- just for example
> 
>  type List_Type is array(Integer range <>) of stuff;
>  
>  package MyPointers is 
>               new System.Address_To_Access_Conversions(List_Type);
>  subtype List_Pointer is MyPointers.Object_Pointer;
> 
>  procedure MyProc
>      (Items     : in out List_Pointer;
>       Nitems    : in out Integer) is
> 
>       List : List_Type := function_that_returns_a_list;

List is on the stack

> 
>    begin
>       Nitems := List'Length;
>       Items  := MyPointers.To_Pointer(List'Address);
>    end;

List has gone away!

I think you're going to need to allocate the particular array and get
the C code to free it (well, that's dangerous, perhaps you should
supply an Ada subprogram to actually do the freeing so as to be sure
the right storage pool is used; or perhaps you could import malloc?)

And I don't think C will understand a List_Pointer, because List_Type
is unconstrained so any concrete List may have bounds with it. There's
no reason to suppose that List'Address is the same as List
(List'First)'Address. The C is expecting effectively a System.Address,
so make your Items parameter a System.Address (why is it in out, BTW?)

One "trick" is to declare a local subtype which is constrained by the
actual bounds.

  List : List_Type := function_that_returns_a_list;
  type actual_list_type is list_type (list'range); -- or suchlike
  -- instantiate System.Address_To_Access_Conversions for actual_list_type
  function malloc (n : natural) return system.address;
  pragma import (c, malloc, "malloc");
begin
  Items := malloc (actual_list_type'max_size_in_storage_elements);
  -- copy the contents of List using the System.Address_To_Access_Conversions
  -- instantiation above

Good grief, that looks complicated, I dare say I've missed something
simple ..

-- 
Simon Wright                               100% Ada, no bugs.



  parent reply	other threads:[~2004-05-25 21:38 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-25 21:00 Question on interface Ada to C James Alan Farrell
2004-05-25 21:30 ` Dale Stanbrough
2004-05-25 21:38 ` Simon Wright [this message]
2004-05-26 10:23   ` Dale Stanbrough
2004-05-26 13:05   ` James Alan Farrell
2004-05-26 16:01     ` Martin Krischik
2004-05-26  7:34 ` Martin Krischik
replies disabled

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