comp.lang.ada
 help / color / mirror / Atom feed
From: "Pat Rogers" <progers@classwide.com>
Subject: Re: Simple Question - Direct Access to memory with ADA
Date: Tue, 02 Jul 2002 19:46:21 GMT
Date: 2002-07-02T19:46:21+00:00	[thread overview]
Message-ID: <hqnU8.1561$pT3.713741408@newssvr12.news.prodigy.com> (raw)
In-Reply-To: 3D21E96E.7030109@thedomain.net

>   Sorry for the simple question - I was wondering the most efficient way
> (moral arguments aside) to get direct access to memory in ADA 95
> (specifically using the GNAT compiler) - assuming the OS allows access
> to it. Its quite
> easy to do in C (a couple of lines to write to a memory location)
>
> *my_int_ptr = (int *) 0xF00BA;
> etc..
> This can easily be macro/funcionalized.
>
>   I could use an Address Clause for each location in memory I wish to
> access,
> this could become tiresome for large chunks of memory. There doesnt seem
> to be any provided service from gnat system packages to perform this
> function (probably for very good reasons).

This need not be vendor-specific.

If the large chunk is contiguous, use an array type and map the entire array via
a single address clause (specifying the starting address of the array).  Use the
predefined array type System.Storage_Elements.Storage_Array because it is
guaranteed to be represented contiguously.  Then access memory by indexing into
the array.  However, at any index location the type of value is the component
type of Storage_Array, ie Storage_Element.  Storage_Element is a modular numeric
type (ie unsigned).  If accessing memory in terms of an unsigned numeric type
isn't what you want, you'll need to do something else.  For example, you could
define a record type, including its representation, to contain all the kinds of
different elements at different memory locations in terms of offsets from the
beginning of the record, and then map an object of that type with a single
address clause (like the array type above).

Alternatively, you can use the predefined facility intended for this purpose:

        System.Address_To_Access_Conversions

This is a generic package that gives you type-safe "peek and poke" access to
memory, much like the C code you showed.  It does the necessary work to convert
addresses to access values (which may or may not be identical to what you would
get with unchecked conversion).  Given the access values (ie, pointers) you then
dereference them to access the memory in terms of the type of value supposedly
at those memory locations (hence the "peek/poke" terminology of old).  This is a
much more dynamic approach, with corresponding flexibility, but will be a little
slower than mapping things once using the above approaches.

>   will I need to use the  pragma Atomic as well?

Are tasks concurrently accessing and updating the storage?

> Am I going to have to resort to Unchecked_Conversion?

Probably not, but if you did that would not be an "evil" thing unless you make
the clients do it all over the code.  If you have to do it, hide it behind a
functional interface so that it only occurs within your implementation.

> I hope I have made the question clear.

Hope that helps,

---
Patrick Rogers                       Consulting and Training in:
http://www.classwide.com          Real-Time/OO Languages
progers@classwide.com               Hard Deadline Schedulability Analysis
(281)648-3165                                 Software Fault Tolerance





  reply	other threads:[~2002-07-02 19:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-02 17:57 Simple Question - Direct Access to memory with ADA Fredrick Smith
2002-07-02 19:46 ` Pat Rogers [this message]
2002-07-02 20:23 ` Robert A Duff
2002-07-04  4:01 ` 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