comp.lang.ada
 help / color / mirror / Atom feed
From: Mats Weber <Mats.Weber@elca-matrix.ch>
Subject: Re: Task deallocation
Date: 1997/02/28
Date: 1997-02-28T00:00:00+00:00	[thread overview]
Message-ID: <3316EB5F.54B7@elca-matrix.ch> (raw)
In-Reply-To: 01bc2193$50d867c0$829d6482@joy.ericsson.se


Jonas Nygren wrote:
> 
> I sent out a question about how to deallocate dynamically allocated tasks.
> There were some answers from which I learnt that deallocating new'd tasks
> was perhaps not so straightforward as I believed.
> 
> Apparently most compilers represents a task by a pointer to some internal
> structure. If this internal storage is not reclaimed when a task is
> deallocated one will have a storage leak.
> 
> So my question is how to deallocate a dynamically allocated task without
> creating a memory leak.
> 
> If I call Free for a dynamically allocated task will this ensure that the
> internal storage structures for tasks are reclaimed? E.g.:
> 
>         type My_Task_Ref is access My_Task;
>         procedure Free is new Unchecked_Deallocation(My_Task, My_Task_Ref);
>         MTR : My_Task_Ref := new My_Task;
>         ...
>         Free(MTR); -- assume MTR'Terminated is true
>         -- will the internal task structures have been reclaimed??

On a reasonable implementation, all memory will be reclaimed when the
task terminates.

Ada 95 (in 13.11.2) says it is an error to deallocate an unterminated
discriminated task object, but doesn't say anything on deallocating a
non-discriminated task object. Could anyone clarify this ?

But note that it is not so easy to make sure that the task is terminated
when you call Free, for instance:

task type my_task is
   entry E;
   entry Done;
end;

task body my_task is
begin
   loop
      accept E;
      exit when ...;
   end loop;
   accept Done;
end;

...

MTR.Done;
Free(MTR);

It is not certain that MTR.all'Terminted is true when Free is called
(the task can still be between "accept Done" and "end"). To be
absolutely sure, you must write

MTR.Done;
begin
   MTR.Done;
exception
   when Tasking_Error =>
      Free(MTR);
end;

which is quite ugly.

In Ada 83, freeing a non-terminted task object did not affect the task
itself and I have never had a problem doing so in programs (particularly
in the above situation). Has this been changed in Ada 95 ?

> Is there really no portable way to deallocate dynamically allocated tasks 
> without storage leaks? 

Strictly speaking, there is no portable way of deallocating _any_ memory
in Ada when you use standard storage pools. The fact that
Unchecked_Deallocation effectively deallocates is just an implementation
advice. But all reasonable implementations will effectively deallocate,
and this should also hold for task memory.

Making memory management non-mandatory is quite reasonable if you
consider that Ada should also be able to run on embedded CPUs with very
little memory where dynamic allocation would not be used and would only
increase the size of the run time system.

On the other hand, a compiler for UNIX will be very hard to sell if
Unchecked_Deallocation does not work (except if it has a garbage
collector).




  reply	other threads:[~1997-02-28  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-02-23  0:00 Task deallocation Jonas Nygren
1997-02-28  0:00 ` Mats Weber [this message]
1997-03-01  0:00   ` Robert Dewar
1997-03-03  0:00     ` Mats Weber
1997-03-04  0:00       ` Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
1997-02-11  0:00 Jonas Nygren
1997-02-12  0:00 ` Mats Weber
replies disabled

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