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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4947e94bd021c540 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-23 14:27:57 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!small1.nntp.aus1.giganews.com!border3.nntp.aus1.giganews.com!intern1.nntp.aus1.giganews.com!nntp.giganews.com!nntp.clear.net.nz!news.clear.net.nz.POSTED!not-for-mail NNTP-Posting-Date: Thu, 23 Oct 2003 16:27:56 -0500 From: Craig Carey Newsgroups: comp.lang.ada Subject: Re: C array to Ada pointer to unconstrained array without copying memory Date: Fri, 24 Oct 2003 10:27:54 +1300 Message-ID: References: <0TUgb.639$2_2.29@newsfep1-gui.server.ntli.net> X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Organization: Customer of Mercury Telecommunications Ltd Cache-Post-Path: drone5.qsi.net.nz!unknown@tnt1-140.quicksilver.net.nz X-Cache: nntpcache 2.4.0b5 (see http://www.nntpcache.org/) X-Original-NNTP-Posting-Host: drone5-svc-skyt.qsi.net.nz X-Original-Trace: 24 Oct 2003 10:28:06 +1300, drone5-svc-skyt.qsi.net.nz NNTP-Posting-Host: 203.97.37.6 X-Trace: sv3-jkInol3SOy3edoVd4n6zBqQkEBeymyWvvxJAlVlusRKRMfEHomEp7SO83bfPlOBK+S46jRo0Cjnfd5b!N0q+xKGsZ3+DRy+MTveML9dBiDz+Xgwou4hr7Wzwll6GSftDIX5iWGrNeE/qiEPqzAw6WVr1W7hV!d1g2Uxs= X-Complaints-To: Complaints to abuse@clear.net.nz X-DMCA-Complaints-To: Complaints to abuse@clear.net.nz X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.1 Xref: archiver1.google.com comp.lang.ada:1545 Date: 2003-10-24T10:27:54+13:00 List-Id: On Wed, 8 Oct 2003 15:07:48 +0100, "Steve Adams" wrote: >Duncan, > I have had to do this for my project. What I do is not portable really but >in general is OK if no other solution is available. > >Gnat does indeed use Fat pointers. *By Default*. >You can make it use thin pointers though: > >type var_arr is array (positive range <>) of T; >type var_arr_ptr is access var_arr; >for var_arr_ptr'size use DWORD; -- from memory, probably want a 'size of int > or something ... >You need to do some pointer manipulations in C when first allocating them to >set the bounds, but Gnat just treats the arrays as normal. >Gnat is good like this since it means that C pointer is handled the same as >Ada pointer. ... >ObjectAda however has the access value pointing to the bounds, this gives me >quite a few headaches in my work, but gnat has a problem with the calling ... >For information the Fat pointer is a struct with two pointers, one to the >bounds and one to the data. ... Taking the bounds information off is needed for interfacing with C. (It is done by declaring what the exact size of the array is. Then the double pointer becomes the single pointer C code would expect.) ---- package IC renames Interfaces.C; type C_Array is array (Natural range <>) of XYZ; subtype Big_C_Array is C_Array (0 .. Natural'Last); -- Thin pointer -- 0 .. 2,147,483,647 type Big_C_Array_Ptr is access all Big_C_Array; pragma Convention (Convention => C, Entity => C_Array); procedure C_Ada_Proc (W : Big_C_Array_Ptr; Len : IC.int); pragma Export (C, C_Proc); ... procedure C_Ada_Proc (W : Big_C_Array_Ptr; Len : IC.int) is type A_W is array (1 .. Len) of IC.int; type A_W_Ptr is access all A_W; function UC_W is new Ada.Unchecked_Conversion ( Source => Big_C_Array_Ptr, Target => A_W_Ptr); ZW1 : A_W_Ptr := UC_W (W); -- points to 1st element CA : A_W renames ZW1.all; -- The same array C sees Mr Sands question might have been answered better if the code causing the problem was shown. C code won't lead to a need to know about the internal design of an Ada compiler. Actually producing the C code might indicate that somehow. Also perhaps Mr Sands could create the bounds info instead of GNAT. >"Duncan Sands" wrote in message >news:mailman.37.1065537708.25614.comp.lang.ada@ada-france.org... >> Greetings. Given a C pointer Ptr to a type T (which points to a block of >>memory containing a C array of T's) and the length of the array Length, >> there are various tricks for getting an Ada array of the right size >> (defined on the stack - important!) >> that uses the block of memory to hold the array data, i.e. no copying >required. >> >> For example, >> >> type Ada_Array is array (Positive range <>) of T; ... >> The problem is the bounds of course. For example, GNAT usually uses >> fat pointers, consisting of two normal pointers where one points to the ...