comp.lang.ada
 help / color / mirror / Atom feed
* Simple Question - Direct Access to memory with ADA
@ 2002-07-02 17:57 Fredrick Smith
  2002-07-02 19:46 ` Pat Rogers
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Fredrick Smith @ 2002-07-02 17:57 UTC (permalink / raw)


*** post for FREE via your newsreader at post.newsfeed.com ***

Hi,

  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).

  Anyone have an efficient method to do this? will I need to use the 
pragma Atomic as well? Am I going to have to resort to Unchecked_Conversion?

I hope I have made the question clear.

Regards
Fred.



 -----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----
                  



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Simple Question - Direct Access to memory with ADA
  2002-07-02 17:57 Simple Question - Direct Access to memory with ADA Fredrick Smith
@ 2002-07-02 19:46 ` Pat Rogers
  2002-07-02 20:23 ` Robert A Duff
  2002-07-04  4:01 ` Robert Dewar
  2 siblings, 0 replies; 4+ messages in thread
From: Pat Rogers @ 2002-07-02 19:46 UTC (permalink / raw)


>   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





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Simple Question - Direct Access to memory with ADA
  2002-07-02 17:57 Simple Question - Direct Access to memory with ADA Fredrick Smith
  2002-07-02 19:46 ` Pat Rogers
@ 2002-07-02 20:23 ` Robert A Duff
  2002-07-04  4:01 ` Robert Dewar
  2 siblings, 0 replies; 4+ messages in thread
From: Robert A Duff @ 2002-07-02 20:23 UTC (permalink / raw)


Fredrick Smith <ascii@thedomain.net> writes:

>   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;

Don't you mean something like this:

    my_int_ptr = (int *) 0xF00BA;
    *my_int_ptr = 123;

?

> 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.

If you have a large chunk of memory, you can use an address for an array
that covers that chunk.  Or you can use Address_To_Access_Conversions.
Or you can use Unchecked_Conversions from integers to pointers.
There is also address arithmetic in System.Storage_Elements.
And Integer_To_Address (or something like that).

>... There doesnt seem
> to be any provided service from gnat system packages to perform this 
> function (probably for very good reasons).
> 
>   Anyone have an efficient method to do this? will I need to use the 
> pragma Atomic as well?

It depends what you're doing.  If some piece of hardware is messing
around with location 0xF00FA at the same time as your program, you
need some form of synchronization.  Pragma Atomic might well be the
right form.

>... Am I going to have to resort to Unchecked_Conversion?

I'm not sure why you call it "resort to Unchecked_Conversion".
The "(int *)" in the above C code is an unchecked conversion.
If you program at this level, you have to "resort" to *some*
low-level (unchecked) features.  Unchecked_Conversion is no less
"checked" than address clauses, for example.

- Bob



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Simple Question - Direct Access to memory with ADA
  2002-07-02 17:57 Simple Question - Direct Access to memory with ADA Fredrick Smith
  2002-07-02 19:46 ` Pat Rogers
  2002-07-02 20:23 ` Robert A Duff
@ 2002-07-04  4:01 ` Robert Dewar
  2 siblings, 0 replies; 4+ messages in thread
From: Robert Dewar @ 2002-07-04  4:01 UTC (permalink / raw)


Fredrick Smith <ascii@thedomain.net> wrote in message news:<3D21E96E.7030109@thedomain.net>...
> *** post for FREE via your newsreader at post.newsfeed.com ***
>   Anyone have an efficient method to do this? will I need to use the 
> pragma Atomic as well? Am I going to have to resort to
> Unchecked_Conversion?
> 
> I hope I have made the question clear.
> 
> Regards
> Fred.


What you are probably missing is that it is perfectly fine
to use a dynamic expression in an address clause, so it is
perfectly easy to write peek or poke functions that contain
an address clause to address the memory. That gives you
a diction essentially equivalent to C,

As others have pointed out, your C code contains unchecked
conversions, so why you should be so concerned about using
this in Ada is peculiar. Probably you don't even think of
the C cast as an unchecked and dangerous conversion (which
makes the point for the Ada design I think :-)



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-07-04  4:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-02 17:57 Simple Question - Direct Access to memory with ADA Fredrick Smith
2002-07-02 19:46 ` Pat Rogers
2002-07-02 20:23 ` Robert A Duff
2002-07-04  4:01 ` Robert Dewar

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