From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,f0256820d7b60c30,start X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!o21g2000vbl.googlegroups.com!not-for-mail From: dhenry Newsgroups: comp.lang.ada Subject: Ada to C interfacing with access on unconstrained array Followup-To: comp.lang.ada Date: Tue, 20 Oct 2009 06:07:18 -0700 (PDT) Organization: http://groups.google.com Message-ID: <2ec52d54-31f6-4289-9a9a-d947be65758c@o21g2000vbl.googlegroups.com> NNTP-Posting-Host: 80.156.46.53 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1256044039 25667 127.0.0.1 (20 Oct 2009 13:07:19 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 20 Oct 2009 13:07:19 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: o21g2000vbl.googlegroups.com; posting-host=80.156.46.53; posting-account=EZVF2goAAABmd4nprEqIYBBoiiKbAL2e User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.0.14) Gecko/2009090216 Ubuntu/9.04 (jaunty) Firefox/3.0.14,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:8745 Date: 2009-10-20T06:07:18-07:00 List-Id: Hello, I am trying to interface C with Ada: I want to call an Ada procedure from C. The Ada procedure takes as parameter a record, containing some integers and fat access types ("access all"). Ideally, I'd like to have no copy during the interfacing between Ada and C. Here is the Ada code: -------------------------------------------------- package Foo is type Nut_Type is record Diameter : Integer; Weight : Integer; Age : Integer; end record; type Nut_Array_Type is array (Positive range <>) of Nut_Type; type Nut_Array_Access is access Nut_Array_Type; type Coco_Type is record X : Integer; Y : Integer; Nuts : Nut_Array_Access; end record; -- This is the procedure I want to be executed from C procedure Climb (Coco : in Coco_Type); end Foo; -------------------------------------------------- package body Foo is procedure Climb (Coco : in Coco_Type) is begin -- Climb on the coco... end Climb; end Foo; -------------------------------------------------- I want to use the Climb procedure. For interfacing, I can create an intermediate procedure C_Climb that fits well with C, but the Climb procedure can't be changed. The C_Climb procedure will take some data from C, and rebuild a Coco_Type object to give to the Climb procedure: "C program" => C_Climb => Climb. My main problem is the Coco_Type.Nuts component, which is an access to an unconstrained array. I can't find how to map it without allocation/ copy data. Here is an attempt to implement C_Climb: -------------------------------------------------- with Interfaces.C; with Interfaces.C.Pointers; package Foo.C_Interface is type C_Nut_Type is record Diameter : Interfaces.C.int; Weight : Interfaces.C.int; Age : Interfaces.C.int; end record; type C_Nut_Array is array (Interfaces.C.int range <>) of aliased C_Nut_Type; type C_Nut_Array_Access is access all C_Nut_Array; package C_Nut_Pointers is new Interfaces.C.Pointers (Interfaces.C.int, C_Nut_Type, C_Nut_Array, (0, 0, 0)); procedure Climb_C (C_X : in Interfaces.C.int; C_Y : in Interfaces.C.int; C_Nuts : in C_Nut_Pointers.Pointer; C_Len : in Interfaces.C.int); pragma Export (C, Climb_C, "climb"); end Foo.C_Interface; -------------------------------------------------- package body Foo.C_Interface is procedure Climb_C (C_X : in Interfaces.C.int; C_Y : in Interfaces.C.int; C_Nuts : in C_Nut_Pointers.Pointer; C_Len : in Interfaces.C.int) is subtype My_Nuts is C_Nut_Array (1 .. C_Len); type My_Nuts_Access is access all My_Nuts; Nuts : aliased My_Nuts; for Nuts'Address use C_Nuts.all'Address; Nuts_Ptr : My_Nuts_Access := Nuts'Unchecked_Access; Coco : Coco_Type; begin Coco.X := Integer (C_X); Coco.Y := Integer (C_Y); -- How do I connect this access with my C pointer C_Nuts? Coco.Nuts := Nuts_Ptr; -- FAIL because My_Nuts_Access is not an Nut_Array_Access Climb (Coco); end Climb_C; end Foo.C_Interface; -------------------------------------------------- Currently, the only way I found is to declare a Nut_Array_Access and allocate memory with new: Nuts_Ptr : Nut_Array_Access := new Nut_Array_Type (1 .. Integer (C_Len)); So I can write "Coco.Nuts := Nuts_Access;". But I must fill Nuts_Access.all with C_Nuts' data, and that's what I'd like to avoid. Any idea on the interfacing is welcome. Yours, David.