comp.lang.ada
 help / color / mirror / Atom feed
* stream_element array contiguity
@ 2003-06-27 17:07 Ian Leroux
  2003-06-27 18:20 ` Robert I. Eachus
  2003-06-27 18:29 ` Randy Brukardt
  0 siblings, 2 replies; 3+ messages in thread
From: Ian Leroux @ 2003-06-27 17:07 UTC (permalink / raw)


From reading previous posts, I gather that a common approach to
dealing with bitwise input is to use Unchecked_Conversion to transform
to packed arrays of Boolean from arrays of Stream_Element, obtained
from an appropriate Stream package. Given that Stream_Element'Size is
not guaranteed to be a multiple or factor of Storage_Element'Size, is
there any way of being sure that the elements of a Stream_Element
array will be contiguous? If not, is there any standard way of
avoiding the problem whereby padding bits in the Stream_Element array
corrupt the resulting boolean array?

I realize that on most compilers and platforms this is unlikely to be
a real issue, but I'm wondering if there is any way to be sure (since
being sure about this sort of thing is one of the features I find
appealing about Ada).

I am a newcomer to the language, and would be glad to be called on any
factual errors in this post.

Ian Leroux



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

* Re: stream_element array contiguity
  2003-06-27 17:07 stream_element array contiguity Ian Leroux
@ 2003-06-27 18:20 ` Robert I. Eachus
  2003-06-27 18:29 ` Randy Brukardt
  1 sibling, 0 replies; 3+ messages in thread
From: Robert I. Eachus @ 2003-06-27 18:20 UTC (permalink / raw)


Ian Leroux wrote:
> From reading previous posts, I gather that a common approach to
> dealing with bitwise input is to use Unchecked_Conversion to transform
> to packed arrays of Boolean from arrays of Stream_Element, obtained
> from an appropriate Stream package. Given that Stream_Element'Size is
> not guaranteed to be a multiple or factor of Storage_Element'Size, is
> there any way of being sure that the elements of a Stream_Element
> array will be contiguous? If not, is there any standard way of
> avoiding the problem whereby padding bits in the Stream_Element array
> corrupt the resulting boolean array?
> 
> I realize that on most compilers and platforms this is unlikely to be
> a real issue, but I'm wondering if there is any way to be sure (since
> being sure about this sort of thing is one of the features I find
> appealing about Ada).
> 
> I am a newcomer to the language, and would be glad to be called on any
> factual errors in this post.

It would be possible to write Ada code to correctly deal with this case, 
and not all that difficult.  But the code would invovle assigning bit 
slices, and the efficiency of that code would be compiler dependent.

In most areas I would tell you to trust the optimizer, but in this case 
I would probably write:

pragma Assert(Storage_Element'Size mod Stream_Element'Size = 0);

or whatever the actual assumption in your code is, even

paragma Assert(Storage_Element'Size = Stream_Element'Size);

I really don't think I'd like to try to get the code right for the more 
general case unless I had an implementation that I could correctly test 
the code on.




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

* Re: stream_element array contiguity
  2003-06-27 17:07 stream_element array contiguity Ian Leroux
  2003-06-27 18:20 ` Robert I. Eachus
@ 2003-06-27 18:29 ` Randy Brukardt
  1 sibling, 0 replies; 3+ messages in thread
From: Randy Brukardt @ 2003-06-27 18:29 UTC (permalink / raw)


Ian Leroux wrote in message ...
>From reading previous posts, I gather that a common approach to
>dealing with bitwise input is to use Unchecked_Conversion to transform
>to packed arrays of Boolean from arrays of Stream_Element, obtained
>from an appropriate Stream package. Given that Stream_Element'Size is
>not guaranteed to be a multiple or factor of Storage_Element'Size, is
>there any way of being sure that the elements of a Stream_Element
>array will be contiguous? If not, is there any standard way of
>avoiding the problem whereby padding bits in the Stream_Element array
>corrupt the resulting boolean array?


I generally just put in a check at elaboration time that the assumptions
of the code are not violated. For instance, if you assumed that
Stream_Element'Size = Storage_Element'Size, then I'd put:

   if Stream_Element'Size /= Storage_Element'Size then
      raise Program_Error;
   end if;

into the "begin" part of the package body. If this somehow gets
violated, you hopefully will get a warning, but certainly will get a
Program_Error on the first test. (If no one bothers to run a test, like
the Ariene 5, well, not much can help that project).

What the condition will be depends on the details of your code, of
course. I often have code that looks like:
    Item_Size : constant := 8;
    ...
    if Some_Type'Size /= Item_Size then
     raise Program_Error;
   end if;
because "Some_Type'Size" isn't static, and thus cannot be used directly
in representation clauses. But we want to make sure that the rep.
clauses are accurate. (It won't do to have the size actually be 6 or
12.)

           Randy.





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

end of thread, other threads:[~2003-06-27 18:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-27 17:07 stream_element array contiguity Ian Leroux
2003-06-27 18:20 ` Robert I. Eachus
2003-06-27 18:29 ` Randy Brukardt

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