comp.lang.ada
 help / color / mirror / Atom feed
From: mgk25@cl.cam.ac.uk (Markus Kuhn)
Subject: Re: Direct memory access
Date: 1999/03/25
Date: 1999-03-25T00:00:00+00:00	[thread overview]
Message-ID: <7de3hl$7tv$1@pegasus.csx.cam.ac.uk> (raw)
In-Reply-To: 7ddj0q$gh6$1@nnrp1.dejanews.com

"Joachim Schroeer" <schroeer@amst.co.at> wrote:
>I'm using GNAT v3.11 for IBM PC platforms.
>
>I'm trying to write some code to allow direct access to memory (for
>testing memory on an embedded system). I simply want something like 'C'
>pointers, so I can write/read values to a given memory address.

Pointer arithmetic in Ada95 is defined in the package System.Storage_Elements.
You can transform an integer into an address using

  function To_Address(Value : Integer_Address) return Address;

You can do pointer arithmetic on type Address. You can convert
an address into a pointer for some object type using the generic
package System.Address_To_Access_Conversions which contains the function

  function To_Pointer(Value : Address) return Object_Pointer;

This is all you need to do all the messy thing that you can do with
C pointer arithmetic. It is just quite a bit more bureaucratic in Ada,
because you have to add one more line than in the corresponding C code
to instantiate Address->pointer conversion function, and because Ada
(unlike C, BCPL, and Assembler) does distinguish between integers and
addresses and therefore requires an additional type conversion.

Note that all these conversion functions are intrinsic functions,
therefore the compiler should not generate a single instruction for them
and the resulting code should be identical to what you get in C with
just *(sometype *)x = y; .

Ada clearly works hard to discourage direct memory access and
pointer arithmetic, therefore developpers are more likely to
encapsulate these clumsy accesses in separate packages, and
as a result the dangerous unchecked code is concentrated in one
place and not spread throught the software.

http://www.adahome.com/rm95/rm9x-13-07.html

Markus

-- 
Markus G. Kuhn, Computer Laboratory, University of Cambridge, UK
Email: mkuhn at acm.org,  WWW: <http://www.cl.cam.ac.uk/~mgk25/>




  parent reply	other threads:[~1999-03-25  0:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-03-25  0:00 Direct memory access Gordy
1999-03-25  0:00 ` Jerry van Dijk
1999-03-25  0:00   ` Tom Moran
1999-03-25  0:00 ` Joachim Schroeer
1999-03-25  0:00   ` dennison
1999-03-25  0:00     ` Jerry van Dijk
1999-03-26  0:00       ` dennison
1999-03-27  0:00         ` Jerry van Dijk
1999-03-25  0:00     ` Markus Kuhn [this message]
1999-03-26  0:00   ` robert_dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox