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,826cd690cb6a7585 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!t30g2000prm.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Address and bit mask Date: Tue, 30 Aug 2011 08:14:16 -0700 (PDT) Organization: http://groups.google.com Message-ID: <7d70b603-a03d-456a-b775-1325eac83aa0@t30g2000prm.googlegroups.com> References: <71159ccc-bf20-4fcf-a7f1-3b90629c1ecb@l4g2000vbv.googlegroups.com> <87fwkk0zv7.fsf@ludovic-brenta.org> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1314717373 27372 127.0.0.1 (30 Aug 2011 15:16:13 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 30 Aug 2011 15:16:13 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: t30g2000prm.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: ARLUEHNKC X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:20748 Date: 2011-08-30T08:14:16-07:00 List-Id: On Aug 30, 2:14=A0am, milouz 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 : > > =A0 p =3D (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 :=3D 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