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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!mx05.eternal-september.org!feeder.eternal-september.org!usenet.blueworldhosting.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx02.iad01.newshosting.com!newshosting.com!news-out.readnews.com!s09-01.readnews.com!not-for-mail X-Trace: DXC=GWkPQUj:DQU]Ho\e@hKC8S[3OhcoN[H0PX44`8^\]>7Z25[I[UfBM:U_WOU;SDD77T@4=QbQ8mePX24DKT5NOBkWJG?IXdh5TQPalM@JQSKSIT X-Complaints-To: abuse@ngroups.net Date: Mon, 27 May 2013 06:37:30 +0200 From: Per Sandberg Newsgroups: comp.lang.ada Subject: Re: Unchecked_Conversion vs Address-overlaying Message-ID: <20130527063730.46ed8d6d@lufsen.sandat.dyndns.org> References: <6bbc35cb-18ed-4efc-b1ec-6c41dcc0256c@googlegroups.com> X-Newsreader: Claws Mail 3.9.0 (GTK+ 2.24.8; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 08782041.ngroups.net X-Received-Bytes: 3823 Xref: news.eternal-september.org comp.lang.ada:15652 Date: 2013-05-27T06:37:30+02:00 List-Id: On Sun, 26 May 2013 16:34:54 -0700 (PDT) Shark8 wrote: > I recently had to make a dump-function for a collection of bytes and > was wondering if the method I chose (address-overlaying) was better > than using Unchecked_Conversion or not. > > ------------------------------ > > -- The private type BLOB designates some collection of > memory-data; -- it is actually an access to a storage-array [of > Storage-Elements]. -- It's sized to the Cell_Size that all the other > types are given so -- that they can be wrapped in a record and that > pushed onto a stack. Type BLOB is Not Null Access All > System.Storage_Elements.Storage_Array with > Size => Cell_Size; > > > -- Returns a hex-dump of the given BLOB. > Function Image( Item : BLOB ) Return String is > Use System, System.Storage_Elements; > > -- Our undervalued friend, the Nybble and the Byte, as well > -- as a type that serves as a collection of bytes. > Subtype Nybble is Interfaces.Unsigned_8 Range > 16#0#..16#F#; Subtype Byte is Interfaces.Unsigned_8; > Type Byte_String is Array(Positive Range <>) of Byte; > > -- Return a Hex Character for any given Nybble. > Function Nybble_to_Hex( Input : Nybble ) Return Character is > subtype Decimal is Nybble range 0..9; > Base : Constant Interfaces.Unsigned_8:= > ( if Input in Decimal then > Character'Pos('0') else Character'Pos('A') - 10 ); > begin > Return Character'Val( Base + Input ); > End Nybble_To_Hex; > > -- Return the string that is the hex-values for > -- the high- and low-nybbles of the given byte. > Function Image( Item : Byte ) Return String is > begin > Return ( 1 => Nybble_to_Hex(Item / 16), -- High > 2 => Nybble_to_Hex(Item mod 16) ); -- Low > End Image; > > -- Given a Byte_String return the string containing its > Nybbles. Function Image( Item : Byte_String ) return String is > begin > Case Item'Length is > when 0 => Return ""; > when 1 => Return Image(Item(Item'First)); > when others => > declare > Head : String Renames Image(Item(Item'First)); > subtype Tail_Range is Positive Range > Positive'Succ(Item'First)..Item'Last; > > Tail : String Renames Image(Item(Tail_Range)); > begin > Return Head & Tail; > end; > end case; > End Image; > > -- Calculate the length of the BLOB, in bytes. > Length : Constant Natural:= > (Item'Length*Storage_Array'Component_Size) / Byte'Object_Size; > Pragma Warnings(Off); > -- Create an overlayed, non-initializing variable of the > proper size. Image_String : Byte_String(1..Length) > with Import, Convention => Ada, Address => Item.All'Address; > Pragma Warnings(On); > begin > -- Return the image of that overlay. > Return Image(Image_String); > end Image; And have a look on "GNAT.Memory_Dump". /Per