comp.lang.ada
 help / color / mirror / Atom feed
* Converting access values
@ 2005-01-05 22:31 Mark Lorenzen
  2005-01-05 23:32 ` Stephen Leake
                   ` (3 more replies)
  0 siblings, 4 replies; 20+ messages in thread
From: Mark Lorenzen @ 2005-01-05 22:31 UTC (permalink / raw)


Hello,

Imagine that we have a program that reads large amount of data from
(for example) a network connection into buffers. The data is just to
be seen as a sequence of octets.

We now want to copy parts ("slices") of the data to other tasks that
may do something interesting with these slices. The rate of data is
too high to simply copy the wanted slices, so instead we make "cheap
copies".

The idea is that these cheap copies simply point to slices within the
large buffer and this way all copies refer to a common buffer and no
real copying is going on.

We do not care about concurrent access to the buffers, allocation and
deallocation of the buffers. All this is taken care of by some safe
mechanism which would just clutter up the following little example.

I am able to actually create such a slice. It contains the correct
values, has the correct constraints and so on. The problem is simply
that my buffer has type access-to-unconstrained-array type and my copy
has an access-to-constrained-array type.

Question: How do i convert an access-to-constraint-array type to
access-to-unconstrained-array type in a (more or less) portable way?

As the example involves access types, adresses and conversions, I have
taken on my asbestos long-johns - so let the flaming begin!

Regards,
- Mark Lorenzen

type Buffer_Ptr is access Ada.Streams.Stream_Element_Array;

procedure Copy (Source : in Buffer_Ptr;
                Offset : in Ada.Streams.Stream_Element_Count;
                Length : in Ada.Streams.Stream_Element_Count;
                Target : out Buffer_Ptr) is
   use type Ada.Streams.Stream_Element_Offset;

   Slice_Begin : constant Ada.Streams.Stream_Element_Offset :=
     Source'First + Offset;
   Slice_End   : constant Ada.Streams.Stream_Element_Offset :=
     Slice_Begin + Length - 1;

   -- Subtype defining the desired constraints (ie. the slice) in
   --  the source buffer.
   subtype Constrained_Buffer is
     Ada.Streams.Stream_Element_Array (Slice_Begin .. Slice_End);

   package Conversions is
      new System.Address_To_Access_Conversions (Constrained_Buffer);

   Slice : Conversions.Object_Pointer;

   function To_Buffer_Ptr is
      new Ada.Unchecked_Conversion (Conversions.Object_Pointer, Buffer_Ptr);
begin
   -- Use the address of element Source(Offset) and convert it into
   --  an access-to-constrained-array type.

   Slice := Conversions.To_Pointer (Source(Slice_Begin)'Address);

   -- Now Slice actually points to a constrained array with the correct
   --  'First, 'Last and 'Length attributes. It's location in the storage
   --  also coincides with Source (Slice_Begin .. Slice_End). Fine!

   -- Problem: Slice is of type Conversions.Object_Pointer, but we
   --  really want a value of type Buffer_Ptr. How do we convert an
   --  access-to-constrained-array value to an
   --  access-to-unconstrained-array value in a (more or less)
   --  portable way?

   Target := ????
end Copy;



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

end of thread, other threads:[~2005-01-12 10:48 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-05 22:31 Converting access values Mark Lorenzen
2005-01-05 23:32 ` Stephen Leake
2005-01-05 23:51   ` Mark Lorenzen
2005-01-06  0:18 ` Jeffrey Carter
2005-01-06  0:28   ` Mark Lorenzen
2005-01-07 16:55     ` Nick Roberts
2005-01-07 19:49       ` Mark Lorenzen
2005-01-07 20:23         ` Nick Roberts
2005-01-07 21:23           ` Robert A Duff
2005-01-11 17:02             ` Upkeep
2005-01-11 21:37               ` Robert A Duff
2005-01-12  4:56                 ` Alexander E. Kopilovich
2005-01-12 10:48                 ` Dmitry A. Kazakov
2005-01-07 21:17         ` Randy Brukardt
2005-01-07 22:15           ` Robert A Duff
2005-01-06 10:52 ` Dmitry A. Kazakov
2005-01-06 11:02 ` Duncan Sands
2005-01-06 12:17   ` Martin Dowie
2005-01-06 19:30   ` Mark Lorenzen
2005-01-06 20:40     ` Randy Brukardt

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