comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: memory management in Ada: tedious without GC?
Date: Sat, 24 May 2008 21:04:42 +0200
Date: 2008-05-24T21:04:43+02:00	[thread overview]
Message-ID: <ckjg7hcfgx0s.19ac65x5lo8oq.dlg@40tude.net> (raw)
In-Reply-To: wccskw7isqn.fsf@shell01.TheWorld.com

On Sat, 24 May 2008 12:14:56 -0400, Robert A Duff wrote:

> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
> 
>> On Fri, 23 May 2008 19:15:45 -0400, Robert A Duff wrote:
>>
>>> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
>>> 
>>>> 1. You don't know exactly the order in which the Finalize of the controlled
>>>> component gets called.  So if you access the container object over an
>>>> access discriminant, that might be already invalid.
>>> 
>>> You know something about the order.  Initialize is bottom-up,
>>> Finalize is top-down.  And when there are access discrims, the
>>> order among subling-components is specified by the RM.
>>> 
>>> I would prefer a fully defined order, based on the declaration order.
>>
>> Hmm, that would be even more broken, I think. The language should not
>> require more than necessary to implement the programmer's intent.
> 
> How does the compiler know the programmer's intent?  Maybe order is
> important, maybe not.

Yes, this why it is safe to assume that the program may not rely on it.

>>...The order
>> of record components or discriminants has no semantic meaning (except when
>> representation clauses involved).
> 
> It sometimes has meaning -- that's why we have positional-notation
> record aggregates.

Just a lexical convention to me.

>>... as well as
>> initialization of.
> 
> There, I disagree.  I see zero advantage to allowing arbitrary order for
> the calls to Initialize and Finalize.  And one huge disadvantage -- if
> you depend on the order, either on purpose or by accident, you might get
> a bug introduced years later, when you switch compilers (or even
> compiler versions).  It will be very hard to detect and to fix that
> bug!

Yes, this is the same dilemma as with re-ordering operands in expressions.
I understand your argument, but I think that the solution is wrong. I'd
prefer a better control over the side effects in order to make such
(erroneous) programs illegal. It is especially important for modern
pipelined, multi-core architectures. Why not to perform initialization of
components concurrently?

> Consider two finalization actions -- one flushes a buffer to disk,
> and the other closes the file handle.

It is a wrong design to handle this from components and from different
actions of finalization. Buffer cannot be flushed without knowing the file
handle. Thus with right design the buffer component will have no access to
the file handle. Hence flushing could only be put into the finalization of
the composite object, which would automatically enforce proper order.

>>... Consider a statically constrained discriminant as an
>> example. If the compiler could remove it, we would have the problem of
>> measurement units solved!
> 
> Yes.  I've always wanted to do that optimization.
> 
> The strange thing is that most compilers do exactly that for array
> bounds, but not for discriminants.  Array bounds are really
> discriminants, deep down!  ;-)

Yes, and don't forget type tags. They are discriminants too!

>>>> 4. The pattern is exposed to multiple inheritance diamond diagram problem,
>>>> when such components are added independently.
>>> 
>>> I don't understand your point here.  Could you give an example of the
>>> problem?
>>
>> type A is limited tagged record ...;
>> type B is new A with record
>>    -- a controlled component here to handle finalization of A
>>
>> generic
>>    type Some_A is new A with private;
>> package P is
>>    type C is new Some_A with record
>>       -- a controlled component here to handle finalization of A
>>
>> Now, if P is instantiated with B, finalization of A will be handled twice.
>>
>> The problem is that finalization in C++ terms is a "virtual" member, while
>> record components are "non-virtual". Implementation of one through another
>> is IMO fundamentally broken.
> 
> OK, I understand the above example (although I don't see a diamond).

  A
 /  \
B   C (generic)
 \  /
  C (instance)

> My answer is, "So don't do that".  ;-)

I thought Ada is a safe language (:-)) Seriously, I think that the Rosen
trick should be made illegal for controlled components. It is unsafe.

> Why would you put the finalization for A in B?  Put it in A where it
> belongs.

Exactly, but I cannot if A is not controlled!

> The main reason to add finalization to B is if B adds
> new components that need to be cleaned up.  If so, then wrap
> those components in a controlled type.

This is not the only case. There is a different case when B adds new
functionality rather than components. This might require some book keeping
actions upon initialization and finalization. These actions cannot be
expressed in the terms of individual components, but in primitive
operations of A/B. This is the notorious dispatching from
constructor/destructor. I call it "class-wide" initialization/finalization.

The Rosen trick on a controlled component emulates such class-wide
initialization/finalization. But defining an order of component
initialization does not save it. Because if you derived from that type, new
components would be initialized after the inherited controlled one. So
dispatching from its initialization to an overridden operation would become
a disaster.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2008-05-24 19:04 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-16 17:44 memory management in Ada: tedious without GC? jhc0033
2008-05-16 18:56 ` Ludovic Brenta
2008-05-16 20:42 ` Maciej Sobczak
2008-05-16 21:45   ` Ivan Levashew
2008-05-16 22:59   ` Peter C. Chapin
2008-05-17  5:24     ` jhc0033
2008-05-17  7:50       ` Ivan Levashew
2008-05-16 23:05   ` Randy Brukardt
2008-05-19  3:50   ` Matthew Heaney
2008-05-19  7:55     ` Dmitry A. Kazakov
2008-05-19 13:18       ` Georg Bauhaus
2008-05-19 14:16         ` Dmitry A. Kazakov
2008-05-23 23:15       ` Robert A Duff
2008-05-24  0:45         ` Randy Brukardt
2008-05-24  8:25         ` Dmitry A. Kazakov
2008-05-24 16:14           ` Robert A Duff
2008-05-24 19:04             ` Dmitry A. Kazakov [this message]
2008-05-24 20:52               ` Robert A Duff
2008-05-25  8:12                 ` Dmitry A. Kazakov
2008-05-25 11:28                   ` Maciej Sobczak
2008-05-25 12:35                   ` Robert A Duff
2008-05-26  8:16                     ` Dmitry A. Kazakov
2008-05-24 19:39             ` Georg Bauhaus
2008-05-24 20:45               ` Robert A Duff
2008-05-19  8:35     ` Maciej Sobczak
2008-05-19 15:11       ` Matthew Heaney
2008-05-19 21:13         ` Maciej Sobczak
2008-05-23 23:03         ` Robert A Duff
2008-05-24  0:12           ` Adam Beneschan
2008-05-16 22:45 ` anon
2008-05-17  7:34 ` Pascal Obry
2008-05-17 15:11   ` Bob Klungle
2008-05-17 15:27     ` Pascal Obry
2008-05-17 16:18       ` Georg Bauhaus
2008-05-20  8:04         ` Ole-Hjalmar Kristensen
2008-05-20  8:01       ` Ole-Hjalmar Kristensen
2008-05-20 10:03         ` Martin Krischik
2008-05-17 17:23     ` Martin Krischik
2008-05-17 16:51   ` Maciej Sobczak
2008-05-17 17:45     ` Pascal Obry
2008-05-17 22:28       ` Samuel Tardieu
2008-05-18  7:03         ` Martin Krischik
2008-05-18  8:50           ` jhc0033
2008-05-18  9:31             ` Dmitry A. Kazakov
2008-05-18 14:10               ` Maciej Sobczak
2008-05-18 14:59                 ` Dmitry A. Kazakov
2008-05-18 20:51                   ` Maciej Sobczak
2008-05-19  8:36                     ` Dmitry A. Kazakov
2008-05-18 15:03             ` Martin Krischik
2008-05-18 18:27               ` jhc0033
2008-05-19  4:12                 ` Matthew Heaney
2008-05-19  8:39                   ` Maciej Sobczak
2008-05-19 15:37                     ` Matthew Heaney
2008-05-19 21:21                       ` Maciej Sobczak
2008-05-19 23:02                         ` Matthew Heaney
2008-05-19 10:27                 ` Martin Krischik
2008-05-17 22:42       ` Peter C. Chapin
2008-05-18  6:58         ` Martin Krischik
2008-05-18  6:52     ` Martin Krischik
2008-05-18 14:16       ` Maciej Sobczak
2008-05-17 14:30 ` Brian Drummond
2008-05-17 16:47   ` Maciej Sobczak
2008-05-19 14:45     ` Brian Drummond
2008-05-20  7:42       ` Maciej Sobczak
2008-05-20 18:01         ` jayessay
2008-05-18  8:06   ` Simon Wright
2008-05-18 14:21     ` Maciej Sobczak
2008-05-18 20:48       ` Simon Wright
2008-05-19 14:40     ` Brian Drummond
2008-05-19  3:44 ` Matthew Heaney
replies disabled

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