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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,76be2011d1dba323 X-Google-Attributes: gid103376,public From: "David C. Hoos, Sr." Subject: Re: Convert an address range into an array Date: 1999/06/06 Message-ID: #1/1 X-Deja-AN: 486287271 Content-Transfer-Encoding: 7bit References: <7jdjov$21$1@pegasus.csx.cam.ac.uk> X-Priority: 3 Content-Type: text/plain; charset="iso-8859-1" X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-MSMail-Priority: Normal MIME-Version: 1.0 Newsgroups: comp.lang.ada Date: 1999-06-06T00:00:00+00:00 List-Id: Markus Kuhn wrote in message news:7jdjov$21$1@pegasus.csx.cam.ac.uk... > Problem: I have memory-mapped a file into my address space using > mmap(). I have parsed data structures in this file and I have located > a continuous string of characters in the address range A to B. > I now want to convert this address range into an Ada String type, > which I can then pass on to a procedure that expects a String > parameter. I do NOT want to copy the (potentially very large) > sequence of bytes for performance reasons. > > Basically, I want to get somehow a value > > S : String; > > such that > > S(S'First)'Access = A > S(S'Last)'Access = B > S'First = 1 > First, this would be a violation of Ada's strong type model, because you are asking access values to be equal to addresses. > which I can then pass on to a prodecure without copying the bytes. > All I want to allocate is the (First,Last) meta-data of the array, > not the array data itself. In other words, I want to combine the > efficiency of C's pointer+length representation of memory chunks > with the comfort and safety of Ada's arrays, but none of the > textbooks explain how to do this. > > What is the proper Ada way of turning a variable address range > into an array located there? Representation clauses? Or should the > efficient zero-copying handling of memory mapped files, packet > headers, etc. better be done via good old pointer arithmetic > like in C? > > The compiler is GNAT on Linux, but the most portable solution > would be preferred. > My solution would be the code which is at the end of this message. Although the code would not be guaranteed (by the RM) to have pass-by-reference semantics, examination of the assembly code generated by GNAT does, and I would suspect that other compilers would, too, given the nature of strings. -- begin source code -- with Ada.Text_IO; with System.Storage_Elements; procedure Kuhn is type String_Access is access all String; Start_Address : System.Address; End_Address : System.Address; procedure Show_String (The_String : String) is begin Ada.Text_IO.Put_Line (The_String); end Show_String; The_String : String := "junk The quick brown fox jumps over the lazy dog's back. junk"; begin Start_Address := The_String (5)'Address; End_Address := The_String(The_String'Last - 5)'Address; declare use type System.Storage_Elements.Storage_Count; String_Length : Natural := Natural (End_Address - Start_Address + 1); The_String : String (1 .. String_Length); for The_String'Address use Start_Address; begin Show_String (The_String); end; end Kuhn; -- end source code --