comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Container for access values.
Date: Thu, 03 May 2012 10:18:15 -0400
Date: 2012-05-03T10:18:15-04:00	[thread overview]
Message-ID: <wccobq5nyag.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 3000608.845.1336028922599.JavaMail.geo-discussion-forums@vbvx4

David Pettersson <jan.karv@gmail.com> writes:

> Hi!
>
> If I want to store values of an access type in a Set, how do I do
> it. I want to implement a simple and inefficient garbage collector and
> need to store pointers (yes, coming from C) to objects that are roots
> for the scan for reachable objects.

The Boehm garbage collector works with Ada.

You should also look into storage pools, including Ada 2012
"subpools".  Maybe you don't need a garbage collector.

> If I try Hashed_Sets I get in trouble since I don't know how to
> compute a reasonable hash value for the access. I can do an unchecked
> cast to integer but the two methods for this that I know is not good
> for the purpose (?).

If you're writing a garbage collector, you're going to need some
low-level hacks, and it's not going to be portable to every
possible Ada implementation.  But you can make it portable
to most machines.

On the other hand, garbage collection is hard when you don't
have control over the compiler.  Are you sure you want to do it?

Look up Integer_Address, Address_To_Access_Conversions, and
Unchecked_Conversion.

Convert to a modular integer, and do your hashing on that.
For a good hash function, you want to ignore the low-order
bits, because addresses are usually aligned.  And ignore the
high-order bits, because objects aren't allocated at high
addresses.  A bit of experimentation should tell you which
bits to use.

> If I try Ordered_Sets there is no "<" defined for access and I have to
> define it myself in the same manner as hash value.

Yes, same issue there.  Hashing is probably faster,
but you'd have to measure to be sure.

> One problem with casting is that I might not get all the information
> if the destination is smaller than the size of the access (two words
> on gnat?). Or is it enought to use a long integer?

All access-to-object values in GNAT are represented as a single machine
address (a "thin pointer"), with one exception:
access-to-unconstrained-array is represented as a "fat pointer", which
contains the address of the dope and the address of the data.  You can
force GNAT to use thin pointers even in that case.

> Is there a standard way to store accesses in Sets.

No.  You have to build your own.

Beware hidden pointers.  For example:

    X : Access_String := new ...;

    procedure P (Y : String) is
    begin
        X := null;
        ...
        -- The object that was X.all is still live here,
        -- accessible via Y.
        Put(Y);
    end P;
    
    P(X); -- GNAT will pass X by reference (as a fat pointer)

Another:

    X : Access_String := new ...;
    Y : String renames X.all;
    
    X := null;
    ...
    Put(Y);

- Bob



      parent reply	other threads:[~2012-05-03 14:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-03  7:08 Container for access values David Pettersson
2012-05-03  7:56 ` Dmitry A. Kazakov
2012-05-03  8:26 ` David Pettersson
2012-05-03  8:40   ` Dmitry A. Kazakov
2012-05-03 14:18 ` Robert A Duff [this message]
replies disabled

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