comp.lang.ada
 help / color / mirror / Atom feed
* Access to classwide type
@ 2000-07-23  0:00 reason67
  2000-07-24  0:00 ` David Botton
  0 siblings, 1 reply; 12+ messages in thread
From: reason67 @ 2000-07-23  0:00 UTC (permalink / raw)


I have what may be a really simple question.

If I have:

type T is abstract tagged private;
type T_Ptr is access T'Class;

How do I deallocate the memory for values in an array of T_Ptr's?

Is it as simple as creating an instantiation of ada.unchecked_conversion
with T'Class and T_Ptr? That seems wrong to me as the various extentions
to T are not known at compile time, but I may be wrong about this.

Any help would be appreciated.
---
Jeffrey Blatt


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Access to classwide type
  2000-07-23  0:00 Access to classwide type reason67
@ 2000-07-24  0:00 ` David Botton
  2000-07-24  0:00   ` reason67
  0 siblings, 1 reply; 12+ messages in thread
From: David Botton @ 2000-07-24  0:00 UTC (permalink / raw)


package Pk1 is
   type T is abstract tagged private;
   type T_Ptr is access T'Class;

private
   type T is abstract tagged null record;
end Pk1;

with Ada.Unchecked_Deallocation;
with Pk1; use Pk1;

procedure Alloc is
   procedure Free_Object is
      new Ada.Unchecked_Deallocation (T'Class, T_Ptr);
begin
   null;
end Alloc;



<reason67@my-deja.com> wrote in message news:8lfuof$ipb$1@nnrp1.deja.com...
> I have what may be a really simple question.
>
> If I have:
>
> type T is abstract tagged private;
> type T_Ptr is access T'Class;
>
> How do I deallocate the memory for values in an array of T_Ptr's?
>
> Is it as simple as creating an instantiation of ada.unchecked_conversion
> with T'Class and T_Ptr? That seems wrong to me as the various extentions
> to T are not known at compile time, but I may be wrong about this.
>
> Any help would be appreciated.
> ---
> Jeffrey Blatt
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>








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

* Re: Access to classwide type
  2000-07-24  0:00 ` David Botton
@ 2000-07-24  0:00   ` reason67
  2000-07-24  0:00     ` Pat Rogers
                       ` (4 more replies)
  0 siblings, 5 replies; 12+ messages in thread
From: reason67 @ 2000-07-24  0:00 UTC (permalink / raw)


How in the heck does it do that? Does Ada.Unchecked_Deallocation read
the tag and do a `size on the data structure to determine the size of
the memory in heap to release? I am suprised by this. It is more
implicit than I am used to in Ada.
---
Jeffrey

In article <xtPe5.19463$T5.28006@east2.usenetserver.com>,
  "David Botton" <David@Botton.com> wrote:

>    procedure Free_Object is
>       new Ada.Unchecked_Deallocation (T'Class, T_Ptr);


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Access to classwide type
  2000-07-24  0:00   ` reason67
                       ` (2 preceding siblings ...)
  2000-07-24  0:00     ` David Starner
@ 2000-07-24  0:00     ` Larry Kilgallen
  2000-07-24  0:00       ` reason67
  2000-07-25  0:00     ` Matthew J Heaney
  4 siblings, 1 reply; 12+ messages in thread
From: Larry Kilgallen @ 2000-07-24  0:00 UTC (permalink / raw)


In article <8li0m5$1i4$1@nnrp1.deja.com>, reason67@my-deja.com writes:
> How in the heck does it do that? Does Ada.Unchecked_Deallocation read
> the tag and do a `size on the data structure to determine the size of
> the memory in heap to release? I am suprised by this. It is more
> implicit than I am used to in Ada.
> ---
> Jeffrey
> 
> In article <xtPe5.19463$T5.28006@east2.usenetserver.com>,
>   "David Botton" <David@Botton.com> wrote:
> 
>>    procedure Free_Object is
>>       new Ada.Unchecked_Deallocation (T'Class, T_Ptr);

If it works for access to a variant type, why shouldn't it work here ?




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

* Re: Access to classwide type
  2000-07-24  0:00   ` reason67
@ 2000-07-24  0:00     ` Pat Rogers
  2000-07-24  0:00     ` Pascal Obry
                       ` (3 subsequent siblings)
  4 siblings, 0 replies; 12+ messages in thread
From: Pat Rogers @ 2000-07-24  0:00 UTC (permalink / raw)


<reason67@my-deja.com> wrote in message
news:8li0m5$1i4$1@nnrp1.deja.com...
> How in the heck does it do that? Does Ada.Unchecked_Deallocation
read
> the tag and do a `size on the data structure to determine the size
of
> the memory in heap to release? I am suprised by this. It is more
> implicit than I am used to in Ada.
> ---
> Jeffrey
>
> In article <xtPe5.19463$T5.28006@east2.usenetserver.com>,
>   "David Botton" <David@Botton.com> wrote:
>
> >    procedure Free_Object is
> >       new Ada.Unchecked_Deallocation (T'Class, T_Ptr);

Have a look at package System.Storage_Pools.  You'll note that the
primitives are abstract -- i.e., dispatching.


--
Pat Rogers                            Consulting and Training in:
http://www.classwide.com      Deadline Schedulability Analysis
progers@classwide.com        Software Fault Tolerance
(281)648-3165                       Real-Time/OO Languages






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

* Re: Access to classwide type
  2000-07-24  0:00     ` Larry Kilgallen
@ 2000-07-24  0:00       ` reason67
  2000-07-24  0:00         ` David Botton
  2000-07-24  0:00         ` Larry Kilgallen
  0 siblings, 2 replies; 12+ messages in thread
From: reason67 @ 2000-07-24  0:00 UTC (permalink / raw)


In article <8GRaL$AJ9PHa@eisner.decus.org>,

> In article <8li0m5$1i4$1@nnrp1.deja.com>, reason67@my-deja.com writes:
> > How in the heck does it do that? Does Ada.Unchecked_Deallocation
read
> > the tag and do a `size on the data structure to determine the size
of
> > the memory in heap to release? I am suprised by this. It is more
> > implicit than I am used to in Ada.

> If it works for access to a variant type, why shouldn't it work here ?
>

Big difference there. A variant record is defined at compile time. The
number of children on a tagged type can not be known until link time.
The machine code for the unchecked deallocation has to be static at
compile time. Like I said, I am betting it reads the 'size inside the
unchecked deallocation at run-time and deleting the size it determines.
---
Jeffrey


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Access to classwide type
  2000-07-24  0:00       ` reason67
  2000-07-24  0:00         ` David Botton
@ 2000-07-24  0:00         ` Larry Kilgallen
  1 sibling, 0 replies; 12+ messages in thread
From: Larry Kilgallen @ 2000-07-24  0:00 UTC (permalink / raw)


In article <8lij0o$fqa$1@nnrp1.deja.com>, reason67@my-deja.com writes:
> In article <8GRaL$AJ9PHa@eisner.decus.org>,
> 
>> In article <8li0m5$1i4$1@nnrp1.deja.com>, reason67@my-deja.com writes:
>> > How in the heck does it do that? Does Ada.Unchecked_Deallocation
> read
>> > the tag and do a `size on the data structure to determine the size
> of
>> > the memory in heap to release? I am suprised by this. It is more
>> > implicit than I am used to in Ada.
> 
>> If it works for access to a variant type, why shouldn't it work here ?
>>
> 
> Big difference there. A variant record is defined at compile time. The

But which variant is addressed by a particular access variable
cannot be known at compile time.

> number of children on a tagged type can not be known until link time.
> The machine code for the unchecked deallocation has to be static at
> compile time. Like I said, I am betting it reads the 'size inside the
> unchecked deallocation at run-time and deleting the size it determines.

That sounds reasonable, but not particularly different from the
variant case.




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

* Re: Access to classwide type
  2000-07-24  0:00       ` reason67
@ 2000-07-24  0:00         ` David Botton
  2000-07-24  0:00         ` Larry Kilgallen
  1 sibling, 0 replies; 12+ messages in thread
From: David Botton @ 2000-07-24  0:00 UTC (permalink / raw)


You may have missed Pat Rogers answer:

>Have a look at package System.Storage_Pools.  You'll note that the
>primitives are abstract -- i.e., dispatching.

Perhaps this will help: Similar to C++ the memory allocation of an object is
a "method" of the object. So just like any "virtual" member of an object
that dispatches, so too the "delete" method.

It is as if you had:

type X is abstract tagged null record;

function Delete (O : X) is abstract;


Then a dispatching function like this is possible:

function Dispatch_Delete (O : X'Class) is
begin
    Delete (O);
end Dispatch_Delete;

delete in C++ and Ada.Unchecked_Deallocation are not like free in C

David Botton


<reason67@my-deja.com> wrote
>Like I said, I am betting it reads the 'size inside the
> unchecked deallocation at run-time and deleting the size it determines.









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

* Re: Access to classwide type
  2000-07-24  0:00   ` reason67
  2000-07-24  0:00     ` Pat Rogers
  2000-07-24  0:00     ` Pascal Obry
@ 2000-07-24  0:00     ` David Starner
  2000-07-25  0:00       ` Laurent Guerby
  2000-07-24  0:00     ` Larry Kilgallen
  2000-07-25  0:00     ` Matthew J Heaney
  4 siblings, 1 reply; 12+ messages in thread
From: David Starner @ 2000-07-24  0:00 UTC (permalink / raw)


On Mon, 24 Jul 2000 18:08:09 GMT, reason67@my-deja.com wrote:
>How in the heck does it do that? Does Ada.Unchecked_Deallocation read
>the tag and do a `size on the data structure to determine the size of
>the memory in heap to release? I am suprised by this. It is more
>implicit than I am used to in Ada.

You're assuming that the 'size is the amount of memory allocated. From
what I've read, most memory allocaters round that up to the nearest 
power of 2, and some add their own chunk of memory for tracking 
purposes. I believe GNAT just implements it as a small wrapper around 
C's free function.

-- 
David Starner - dstarner98@aasaa.ofe.org
http/ftp: x8b4e53cd.dhcp.okstate.edu
It was starting to rain on the night that they cried forever,
It was blinding with snow on the night that they screamed goodbye.
	- Dio, "Rock and Roll Children"




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

* Re: Access to classwide type
  2000-07-24  0:00   ` reason67
  2000-07-24  0:00     ` Pat Rogers
@ 2000-07-24  0:00     ` Pascal Obry
  2000-07-24  0:00     ` David Starner
                       ` (2 subsequent siblings)
  4 siblings, 0 replies; 12+ messages in thread
From: Pascal Obry @ 2000-07-24  0:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 909 bytes --]


reason67@my-deja.com a �crit dans le message
<8li0m5$1i4$1@nnrp1.deja.com>...
>How in the heck does it do that? Does Ada.Unchecked_Deallocation read
>the tag and do a `size on the data structure to determine the size of
>the memory in heap to release? I am suprised by this. It is more
>implicit than I am used to in Ada.

Well "malloc" takes the size of the object but not "free". I think that the
OS is
able to know the size from the start address of the chunk of memory. Nothing
so special here  to me... At least it seems not to be an Ada issue.

Pascal.

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|         http://perso.wanadoo.fr/pascal.obry
--|
--| "The best way to travel is by means of imagination"







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

* Re: Access to classwide type
  2000-07-24  0:00   ` reason67
                       ` (3 preceding siblings ...)
  2000-07-24  0:00     ` Larry Kilgallen
@ 2000-07-25  0:00     ` Matthew J Heaney
  4 siblings, 0 replies; 12+ messages in thread
From: Matthew J Heaney @ 2000-07-25  0:00 UTC (permalink / raw)


reason67@my-deja.com writes:

> How in the heck does it do that? Does Ada.Unchecked_Deallocation read
> the tag and do a `size on the data structure to determine the size of
> the memory in heap to release? I am suprised by this. It is more
> implicit than I am used to in Ada.

More or less, yes.  The language mandates that the Ada run-time has to
keep track of the specific type being designated by the
(pointer-to-class-wide) access object.

This means Unchecked_Deallocation is guaranteed to work.  It even calls
the Finalize operation on controlled objects.





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

* Re: Access to classwide type
  2000-07-24  0:00     ` David Starner
@ 2000-07-25  0:00       ` Laurent Guerby
  0 siblings, 0 replies; 12+ messages in thread
From: Laurent Guerby @ 2000-07-25  0:00 UTC (permalink / raw)


dvdeug@x8b4e53cd.dhcp.okstate.edu (David Starner) writes:
> [...] I believe GNAT just implements it as a small wrapper around 
> C's free function.

For the default allocation pool in GNAT yes (gnat_free). 

In general at the language level, if you write your own pools, the
deallocation function (see System.Storage_Pools) has size and
alignment parameters, and the compiler has always a way to fill them
with the corresponding allocated parameters as required by 13.11.2
(9). So you know a bit more than C free.

I believe that for all tagged types, GNAT generates a few hidden
primitive subprograms to handle this in the class-wide case (see -gnatdg
expansion of 'size attributes on such beasts to make your mind ;-). I
don't know for other compiler technology.

For arrays and discriminated types, the compiler can obviously
generate small pieces of code to compute the size of things, since the
information must be around in some ways to do all the Ada required
checking.

-- 
Laurent Guerby <guerby@acm.org>




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

end of thread, other threads:[~2000-07-25  0:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-07-23  0:00 Access to classwide type reason67
2000-07-24  0:00 ` David Botton
2000-07-24  0:00   ` reason67
2000-07-24  0:00     ` Pat Rogers
2000-07-24  0:00     ` Pascal Obry
2000-07-24  0:00     ` David Starner
2000-07-25  0:00       ` Laurent Guerby
2000-07-24  0:00     ` Larry Kilgallen
2000-07-24  0:00       ` reason67
2000-07-24  0:00         ` David Botton
2000-07-24  0:00         ` Larry Kilgallen
2000-07-25  0:00     ` Matthew J Heaney

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