comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Address and bit mask
Date: Tue, 30 Aug 2011 08:14:16 -0700 (PDT)
Date: 2011-08-30T08:14:16-07:00	[thread overview]
Message-ID: <7d70b603-a03d-456a-b775-1325eac83aa0@t30g2000prm.googlegroups.com> (raw)
In-Reply-To: add89805-0790-4f31-8d73-00696eec0768@o26g2000vbi.googlegroups.com

On Aug 30, 2:14 am, milouz <a.micheli...@gmail.com> wrote:
> Ok, thank you very much for all your answers, but I'm not really more
> advanced. Let me summarize :
>
> - my question was about address arithmetic (howto ?)
> - Some fellows gave me cues about the use of the
> System.Storage_Elements, but without gaving any concrete example.
> - Some others just wandered about the need of doing address
> arithmetic.
>
> Hum... well... I still do not know how to do some simple arithmetic
> with address. So, let me give you an example in C :
>
>   p = (char*) (((unsigned long)p + ALIGN_MASK) & ~ALIGN_MASK);
>
> How do you write that in Ada ?

It looks like you have a char* and you want to find the address equal
to or greater than p that is a multiple of some alignment.  I can
think of only one application where you would want to do something
like that, so I'll give you some ideas about how to accomplish it.

But first I want to echo what everyone else is saying: asking "How do
I write this C code in Ada" is the WRONG approach.  I can't emphasize
this strongly enough.  The right approach is to ask "What do I want to
accomplish?" or "What is this C code accomplishing?", and then "How do
I accomplish that in Ada?"  Trying to translate C code just does not
work.  I have seen a lot of absolutely horrendous code in Ada written
by programmers who figured out how they would solve their problem in
C, and then tried to do a more-or-less literal translation.  Please,
don't go that way.

Anyway, the case I can think of where you'd want to align a pointer
upward has to do with memory allocation; you have a pool of memory and
want to allocate a chunk of it, but sometimes the chunks have to be
aligned on a certain boundary.  In other words, writing your own
version of malloc().  This is a useful thing to do in Ada, because
once you write your own routines for maintaining the memory pool, you
can use the "storage pools" feature to hook up Ada's access types and
allocator operation to them.  But you still have to write the pool
maintenance routines.

The way I've seen this done is to set up an array of bytes (storage
elements):

   type Pool_Type is array (Natural range <>) of
System.Storage_Elements.Storage_Element;
   for Pool_Type'Alignment use 2048;  -- or whatever the maximum
possible alignment you
                                      -- might need is

   Pool_Size : constant := 100_000;
   Pool : Pool_Type (0 .. Pool_Size - 1);
   subtype Pool_Index is Natural range 0 .. Pool_Size - 1;

Now, instead of doing arithmetic on addresses, you keep variables of
type Pool_Index to refer to the next available location, keep free
lists, etc., and do your alignment arithmetic on those indexes.  When
you need the address of the pool element at a given index, then use
Pool(Index)'Address.  But don't do the arithmetic on the addresses.

This is a very rough outline.  There should be some publicly available
code out there that already does storage pool handling, and I think
some of the other posters on this thread can point you to that.  So if
that's the job you're trying to accomplish, I'd suggest you take a
look at the existing code for this.

If the reason you want to do address arithmetic is something else,
though, you're welcome to ask about it, but rather than giving us C
code with no context and asking us how to translate it, please give us
a bigger-picture description of what you're trying to accomplish and
let us give you ideas about that (like I tried to do with the storage
pool example).

                                 -- Adam







  parent reply	other threads:[~2011-08-30 15:14 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-29 15:46 Address and bit mask milouz
2011-08-29 16:06 ` Martin
2011-08-29 16:33   ` milouz
2011-08-29 17:47     ` Dmitry A. Kazakov
2011-08-29 17:54     ` Martin
2011-08-29 18:46     ` tmoran
2011-08-29 19:41 ` Ludovic Brenta
2011-08-29 19:54   ` Adam Beneschan
2011-08-30  9:14     ` milouz
2011-08-30 10:34       ` Ludovic Brenta
2011-08-30 10:58         ` Ludovic Brenta
2011-08-30 12:44           ` Georg Bauhaus
2011-08-30 14:04             ` Dmitry A. Kazakov
2011-08-30 16:12               ` Georg Bauhaus
2011-08-30 16:59                 ` Dmitry A. Kazakov
2011-08-30 14:52         ` Adam Beneschan
2011-08-30 10:40       ` Simon Wright
2011-08-30 10:44         ` Simon Wright
2011-08-30 15:20         ` tmoran
2011-08-30 16:08           ` milouz
2011-08-30 16:45             ` Georg Bauhaus
2011-08-30 19:31               ` Adam Beneschan
2011-08-30 19:56                 ` Dmitry A. Kazakov
2011-08-31  6:16                   ` The simple Image issue (was: Address and bit mask) Georg Bauhaus
2011-08-31 14:44                     ` The simple Image issue Dmitry A. Kazakov
2011-08-31 15:36                       ` Georg Bauhaus
2011-08-31 15:53                         ` Dmitry A. Kazakov
2011-08-31 16:23                           ` Georg Bauhaus
2011-08-31 16:27                             ` Dmitry A. Kazakov
2011-08-31 16:30                               ` Georg Bauhaus
2011-08-31 16:50                                 ` Dmitry A. Kazakov
2011-08-31 20:41                                   ` Georg Bauhaus
2011-08-31 21:17                                     ` Robert A Duff
2011-09-01  7:36                                       ` Dmitry A. Kazakov
2011-09-01  7:46                                     ` Dmitry A. Kazakov
2011-09-01  9:50                                       ` Overloading parentheses and type expectations (was: The simple Image issue) Georg Bauhaus
2011-09-02  7:54                                         ` Overloading parentheses and type expectations Dmitry A. Kazakov
2011-09-02 10:37                                           ` Georg Bauhaus
2011-09-02 12:40                                             ` Dmitry A. Kazakov
2011-09-02 16:08                                               ` Georg Bauhaus
2011-09-02 17:29                                                 ` Dmitry A. Kazakov
2011-08-31 15:53                     ` The simple Image issue Hyman Rosen
2011-08-31 16:07                       ` Dmitry A. Kazakov
2011-08-31 16:08                       ` Simon Wright
2011-08-31 16:26                         ` Dmitry A. Kazakov
2011-08-31 16:25                       ` Georg Bauhaus
2011-08-31 16:30                         ` Hyman Rosen
2011-08-31 16:34                           ` Georg Bauhaus
2011-08-31 16:43                             ` Adam Beneschan
2011-08-31 21:58                               ` Georg Bauhaus
2011-09-01  7:59                                 ` Dmitry A. Kazakov
2011-08-31 16:08                     ` The simple Image issue (was: Address and bit mask) Adam Beneschan
2011-08-31 16:53                       ` The simple Image issue Simon Wright
2011-08-31 17:02                         ` Hyman Rosen
2011-08-31 20:33                       ` Georg Bauhaus
2011-08-30 19:37             ` Address and bit mask Martin
2011-08-30 16:32           ` Simon Wright
2011-08-31  7:55             ` Ludovic Brenta
2011-08-30 12:35       ` Georg Bauhaus
2011-08-30 13:03       ` Georg Bauhaus
2011-08-30 15:14       ` Adam Beneschan [this message]
2011-08-30 15:59         ` Adam Beneschan
2011-08-31  7:45         ` milouz
2011-08-31  8:35           ` Ludovic Brenta
replies disabled

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