comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: API design problem - buffer scatter I/O
Date: Sat, 22 Nov 2008 19:01:03 -0500
Date: 2008-11-22T19:01:03-05:00	[thread overview]
Message-ID: <wcczljrl39c.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 36d5b92c-e28d-4aae-8495-8d3f0cc6defc@13g2000yql.googlegroups.com

Maciej Sobczak <see.my.homepage@gmail.com> writes:

> On 22 Lis, 23:16, Robert A Duff <bobd...@shell01.TheWorld.com> wrote:
>
>> There is no such concept as "fat pointer" or "thin pointer" in Ada!
>
> I understand that the distinction is to make the reader aware that
> some access values need to carry over the bounds whereas some other
> don't.

Perhaps, but I still find that wiki page confusing.

When you have an access to unconstrained array, the implementation
has to arrange some way to find the bounds -- e.g. if the program
says X.all'Last.  Fat pointers are one way to do that, but not the
only one.  GNAT can use either fat pointers or thin pointers
(for access to unconstrained array) -- it's a programmer choice.

And (I claim) this has no place in a description of the semantics
of the language.  It's an implementation detail.

>> It does point to a real problem, though. �Given your above declarations,
>> you might want to say:
>>
>> � � This_Buf : aliased Stream_Element_Array (1..10);
>> � � That_Buf : aliased Stream_Element_Array (1..10_000);
>>
>> � � Scatter_Input_Data ((This_Buf'Access, That_Buf'Access)); -- wrong!
>>
>> but that's illegal. �You can do this instead:
>>
>> � � This_Buf : aliased Stream_Element_Array := (1..10 => <>);
>> � � That_Buf : aliased Stream_Element_Array := (1..10_000 => <>);
>>
>> � � Scatter_Input_Data ((This_Buf'Access, That_Buf'Access)); -- OK
>
> Yes, I have noticed that. Not a big issue for the user (all bounds can
> be still variable at run-time), but highlights some part of the
> language that is maybe unnecessarily rigid.

Perhaps.  If the above "wrong!" thing were allowed, there would be a
slight efficiency hit.  That (efficiency) is the _only_ reason for
this rule.  Maybe it was a mistake.

>> Another workaround is to wrap the Stream_Element_Array in a record
>
> Good idea.
>
> Still, in both of these solutions, there is a problem of scope - the
> type of access value is defined by the library API (well, at the
> library level), whereas user-provided buffers are likely to be defined
> locally.

Right.  You have to use Local'Unchecked_Access, and take care to avoid
dangling pointers.

> This brings the old problem of having an access value of global type
> pointing to local object (GNAT says: "non-local pointer cannot point
> to local object").
> Unrestricted_Access hides the issue,...

Don't use Unrestricted_Access here (which is not standard Ada).
Use Unchecked_Access.  Yes, it's dangerous -- you need to ensure that
Scatter_Input_Data doesn't save pointers to locals into globals,
along with comments explaining what clients can do.
There's no better solution.

>... but I know from Rationale that
> anonymous access types were supposed to solve some of that mess.

"Supposed to", maybe.  But they don't.  I suggest you stay away from
them.  They are confusing, and don't help here.

>...The
> problem is that I cannot find any way to benefit from them.

Right.  Neither can I.
[snipped discussion of failed attempts to use anonymous
access types]

By the way, ARG is discussing some ideas for improving anonymous
access types.  My own opinion is that this will not work out,
but others do not agree with me.

- Bob



  reply	other threads:[~2008-11-23  0:01 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-22 16:05 API design problem - buffer scatter I/O Maciej Sobczak
2008-11-22 16:54 ` sjw
2008-11-22 19:43 ` george.priv
2008-11-22 22:16 ` Robert A Duff
2008-11-22 23:34   ` Maciej Sobczak
2008-11-23  0:01     ` Robert A Duff [this message]
2008-11-24  8:10       ` Brad Moore
2008-11-24  7:55   ` christoph.grein
2008-11-24 20:03     ` Robert A Duff
2008-11-25  5:59       ` christoph.grein
2008-11-25  8:34         ` Dmitry A. Kazakov
2008-11-25 14:25         ` Robert A Duff
2008-11-25 22:20       ` Randy Brukardt
2008-11-24 21:23     ` Robert A Duff
2008-11-22 23:01 ` Georg Bauhaus
2008-11-23  5:57 ` anon
2008-11-24 17:16 ` tmoran
2008-11-26  8:34   ` Maciej Sobczak
2008-11-26 20:39     ` sjw
replies disabled

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