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-Thread: 103376,565ddc0e6b80e338 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!feeder.news-service.com!border-1.ams.xsnews.nl!feed.xsnews.nl!border-1.ams.xsnews.nl!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!nntp.giganews.com!feeder2.news.saunalahti.fi!feeder1.news.saunalahti.fi!newsfeed.bahnhof.se!fi.sn.net!newsfeed1.fi.sn.net!news.song.fi!not-for-mail Date: Mon, 03 Sep 2007 14:09:34 +0300 From: Niklas Holsti User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20060628 Debian/1.7.8-1sarge7.1 X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: copying data between memory locations References: <1188809968.217323.145640@r29g2000hsg.googlegroups.com> In-Reply-To: <1188809968.217323.145640@r29g2000hsg.googlegroups.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <46dbe975$0$27817$39db0f71@news.song.fi> Organization: TDC Song Internet Services NNTP-Posting-Host: laku61.adsl.netsonic.fi X-Trace: 1188817269 news.song.fi 27817 81.17.205.61:32924 X-Complaints-To: abuse@song.fi Xref: g2news2.google.com comp.lang.ada:1682 Date: 2007-09-03T14:09:34+03:00 List-Id: jef.mangelschots@gmail.com wrote: > In our current design, we have a package that maintains a list of > descriptors of various buffers in the application. We don't want to > know their datatypes or how to write data into them. We only want to > maintain a physical address of them and use that address to directly > write data into these buffers (this data comes out incoming packets > over the network). > > In C, this is very straightforward and use the address as a pointer > and simply copy the packet into the memory location. Easy to code, hard to debug, ... as you probably know :-) > I can't figure out a way to do this in Ada (at least not Ada83). > > suppose the following: > > type BUFFER_DETAILS_RECORD is > record > num_bytes : integer; > start_address : system.address; > end record; > type BUFFER_DETAILS_ARRAY_TYPE is array(1..2) of > BUFFER_DETAILS_RECORD; > BUFFER_DETAILS : constant BUFFER_DETAILS_ARRAY_TYPE := > BUFFER_DETAILS_ARRAY_TYPE'(1 => (num_bytes => 100, start_address => > 16#123# ), 1 => (num_bytes => 200, start_address => 16#456# )); > > procedure insert_block(idx : in integer; num_bytes : in integer; > offset : in integer; data : BYTE_ARRAY_TYPE) is > begin > ??? how do I copy the content of data into the memory pointed to by > BUFFER_DETAILS(idx).start_address + offset > end; You have two problems, in fact: (1) adding an address and an (integer) offset to give a new address, and (2) accessing the data at that address. For the addition problem (1) Ada 95/2007 provide the standard package System.Storage_Elements. For Ada 83 I don't know of any standard method; I would look for a compiler-specific method, perhaps compiler-specific functions in package System or in some other compiler-supplied package. You could also try to use Unchecked_Conversion to convert System.Address to some integer type, add the integers, and convert back, but that works only if the System.Address is really implemented as an integral type (and not, for example, as a segment + offset). But you can also delegate the offset to the second problem (2), see below. For the accessing problem (2) Ada 95/2007 provide the standard package System.Address_To_Access_Conversions that can be used to convert the physical address into an access to the buffer, of any type desired, for example a Byte_Array_Type. In Ada 83 there are two methods for accessing data at a given System.Address: - Declare an "overlay" for the buffer using an address clause: Vec : Byte_Array_Type; for Vec use at the_known_address; The LRM says that this feature "should not" be used to achieve overlays of objects. In practice it is (or was) used in this way, at least when the compiler supported it. - Use Unchecked_Conversion to convert the physical address to an access value (access Byte_Array_Type). This works only if access values are really implemented as addresses, which is not always the case. If you always access the buffer as a vector of bytes, you could delegate problem (1) to problem (2) by setting up the overlay or the access value using Buffer.start_address without including the offset, then adding the offset part when you index the Byte_Array, using Vec(Buffer.offset), Vec(Buffer.offset+1) .. instead of Vec(0), Vec(1), ... HTH -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .