comp.lang.ada
 help / color / mirror / Atom feed
* RE: Variable length raw-byte data
@ 2000-12-12  3:30 Beard, Frank
  2000-12-12  5:54 ` tmoran
  0 siblings, 1 reply; 19+ messages in thread
From: Beard, Frank @ 2000-12-12  3:30 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

Try looking at Interfaces.C and Interfaces.C.Pointers.
If it's string data, look at Interfaces.C.Strings.
I've used this successfully to interface to C.

Or

In your interface routine, receive the pointer as an
address, and then unchecked convert it.

  type Byte is mod 2**8;

  type Byte_Array is array (positive range <>) of Byte;

  type Byte_Array_Pointer is access Byte_Array;

  buffer_Address : System.Address;
  buffer_Pointer : Byte_Array_Pointer;

begin
...
  status := Get_C_Stuff(length         => length,
                        buffer_Address => buffer_Address);

  if (length > 0) then
    declare
      subtype Constrained_Byte_Array is Byte_Array(1..length);
      function To_Buffer_Pointer is
        new Ada.Unchecked_Conversion(System.Address,Constrained_Byte_Array);
    begin
      buffer_Pointer := To_Buffer_Pointer(buffer_Address);
    end;
    ...
  end if;
...

Something like that.  (Constructive corrections only please!)
I haven't done it this way in a while.

Frank

-----Original Message-----
From: Julian Morrison [mailto:julian.morrison@virgin.net]
Sent: Monday, December 11, 2000 2:38 PM
To: comp.lang.ada@ada.eu.org
Subject: Variable length raw-byte data


I'm trying to shim to a C library that returns some data as a length in
bytes and a pointer to the first byte. The length is not known at compile
time. Is there any way I can morph this into an array of mod 2**8? I'm
using the latest Linux GNAT.
_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada




^ permalink raw reply	[flat|nested] 19+ messages in thread
* RE: Variable length raw-byte data
@ 2000-12-13 20:39 Beard, Frank
  2000-12-14 13:30 ` Robert Dewar
  0 siblings, 1 reply; 19+ messages in thread
From: Beard, Frank @ 2000-12-13 20:39 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

-----Original Message-----
From: Robert Dewar [mailto:robert_dewar@my-deja.com]

> Of *course* you never allocate an instance of a
> big array. in fact it is not a bad idea to add

Allocate was a bad choice of words.  But you missed
my real point.  My biggest concern is that by doing
this:

   type byte is mod 2 ** 8;

   type memory is array (natural) of byte;
   type memptr is access memory;

   myMemory : memptr;

You are telling the compiler that you're pointing
at a 2GB array and depending on the programmer to be
"kind" and use the length returned (smacks way too
much of C/C++), instead of letting the language tell
you when you've over-reached your bounds.  What if
the programmer inadvertently uses "myMemory.all'last"?  
If you use the constrained sub-type approach, you
can still use 'first and 'last, and it properly sets
the state data of the access type.

Of course the counter-danger is that the programmer
will inadvertently uncheck convert something using
the unconstrained type instead of the constrained
subtype.  Which will likely lead to peculiar behavior.

Another approach is to do the following:

  type Byte is mod 2**8;

  buffer_Address : System.Address;

begin
...
  status := Get_C_Stuff(length         => length,
                        buffer_Address => buffer_Address);

  if (length > 0) then
    declare
      type Byte_Array is Byte_Array(1..length) of Byte;
      type Byte_Array_Pointer is access Byte_Array;
      buffer_Pointer : Byte_Array_Pointer;
      function To_Buffer_Pointer is
        new Ada.Unchecked_Conversion(System.Address,Byte_Array);
    begin
      buffer_Pointer := To_Buffer_Pointer(buffer_Address);
    end;
    ...
  end if;
...

This is fine so long as everything you need to do is
within the declare block.

Every system I've worked on, both the unconstrained
subtype and the declare type have worked fine.

The DEC VAX Ada compiler would let you do unchecked
conversion with unconstrained types, other compilers
require constrained types.  What's the rule in Ada 95?
Does the language require both types to be constrained?

Frank




^ permalink raw reply	[flat|nested] 19+ messages in thread
* RE: Variable length raw-byte data
@ 2000-12-13  2:56 Beard, Frank
  2000-12-13 15:52 ` Robert Dewar
  0 siblings, 1 reply; 19+ messages in thread
From: Beard, Frank @ 2000-12-13  2:56 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

>The important thing is to NEVER use pointers to unconstrained
>arrays in such a case. this is asking for non-portable, peculiar
>behavior.

That's exactly why my example contained a pointer to the constrained
subtype.

> A good type to use in this case is what we in GNAT-land call
> big arrays:
>
>   type byte is mod 2 ** 8;
>
>   type memory is array (natural) of byte;
>   type memptr is access memory;

So you're going to allocate 2GB to handle what might
be only 10 bytes (or even 1K or 10K).  That should
work well on a dedicated processor with 64 K of memory.

And again if you use a pointer to the constrained subtype,
you only allocate what you need. 

Or are you saying, you will be kind and make sure you
don't go beyond the length value returned, instead of
letting Ada constraint checking make sure you don't do it.

>Now it is almost certainly safe to use address_to_access
>conversions to go between address and memptr.
>
>Of course you have to be careful not to access the array
>out of bounds, but that's going to be up to the calling
>program anyway if the bounds don't come built in :-)

And you're going to depend on C to do this?




^ permalink raw reply	[flat|nested] 19+ messages in thread
* RE: Variable length raw-byte data
@ 2000-12-12 21:11 Beard, Frank
  0 siblings, 0 replies; 19+ messages in thread
From: Beard, Frank @ 2000-12-12 21:11 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

Your right.  When I wrote the code that used Unchecked_Conversion,
it was on an HP-UX box with the Alsys Ada83 compiler that didn't
have System.Address_To_Access_Conversions.  The addresses there
did translate nicely to access types.

With Ada95, the only interfaces I've written to C have used the
Interfaces.C... packages.

Frank

-----Original Message-----
From: tmoran@acm.org [mailto:tmoran@acm.org]

  Instead of trying to use Unchecked_Conversion to change an address
into an access type (I presume that was meant here), use
System.Address_To_Access_Conversions.  That should work.  But a
System.Address need not be the same size as an access type (consider
various Intel memory models), and even an access to an unconstrained
array (which will involve an actual 'range being stored somewhere) and
an access to a constrained array (where the compiler might keep the
range somewhere else) may be different, and different from a
System.Address.





^ permalink raw reply	[flat|nested] 19+ messages in thread
* RE: Variable length raw-byte data
@ 2000-12-12 21:00 Beard, Frank
  2000-12-13 15:48 ` David Botton
  0 siblings, 1 reply; 19+ messages in thread
From: Beard, Frank @ 2000-12-12 21:00 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

Yes, I forgot about this.  I've used this several times
in the past with great success.

Frank

-----Original Message-----
From: Jeff Carter [mailto:jrcarter@acm.org]

Often, you can receive the pointer as an address and use it for an
object.

type Byte_List is array (Positive range <>) of Unsigned_8;

C_Thing (Length, Pointer);

Use_Thing : declare
   Thing : Byte_List (1 .. Length);
   for Thing'Address use Pointer;
begin -- Use_Thing
   ...
end Use_Thing;

-- 
Jeff Carter
"I blow my nose on you."
Monty Python & the Holy Grail
_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada




^ permalink raw reply	[flat|nested] 19+ messages in thread
* Variable length raw-byte data
@ 2000-12-11 19:38 Julian Morrison
  2000-12-12  5:19 ` Jeff Carter
  2000-12-13  0:50 ` Robert Dewar
  0 siblings, 2 replies; 19+ messages in thread
From: Julian Morrison @ 2000-12-11 19:38 UTC (permalink / raw)


I'm trying to shim to a C library that returns some data as a length in
bytes and a pointer to the first byte. The length is not known at compile
time. Is there any way I can morph this into an array of mod 2**8? I'm
using the latest Linux GNAT.



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

end of thread, other threads:[~2000-12-14 13:30 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-12-12  3:30 Variable length raw-byte data Beard, Frank
2000-12-12  5:54 ` tmoran
  -- strict thread matches above, loose matches on Subject: below --
2000-12-13 20:39 Beard, Frank
2000-12-14 13:30 ` Robert Dewar
2000-12-13  2:56 Beard, Frank
2000-12-13 15:52 ` Robert Dewar
2000-12-13 18:23   ` Larry Kilgallen
2000-12-13 19:26     ` Robert Dewar
2000-12-12 21:11 Beard, Frank
2000-12-12 21:00 Beard, Frank
2000-12-13 15:48 ` David Botton
2000-12-13 15:51   ` Lutz Donnerhacke
2000-12-13 19:34     ` Robert Dewar
2000-12-14  8:54       ` Lutz Donnerhacke
2000-12-13 23:10   ` Jeff Carter
2000-12-11 19:38 Julian Morrison
2000-12-12  5:19 ` Jeff Carter
2000-12-13  0:50 ` Robert Dewar
2000-12-13  8:56   ` Tristan Gingold

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