From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,4ea86b01a068c433 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.204.143.143 with SMTP id v15mr307752bku.8.1336054696394; Thu, 03 May 2012 07:18:16 -0700 (PDT) Path: h15ni194291bkw.0!nntp.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!bloom-beacon.mit.edu!newsswitch.lcs.mit.edu!nntp.TheWorld.com!.POSTED!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Container for access values. Date: Thu, 03 May 2012 10:18:15 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <3000608.845.1336028922599.JavaMail.geo-discussion-forums@vbvx4> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 X-Trace: pcls6.std.com 1336054695 16907 192.74.137.71 (3 May 2012 14:18:15 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 3 May 2012 14:18:15 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:QNqN1alqiTzp0nLIHalXdn0p0n4= Content-Type: text/plain; charset=us-ascii Date: 2012-05-03T10:18:15-04:00 List-Id: David Pettersson 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