comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Address of an object
Date: Sat, 16 Sep 2006 10:13:46 +0200
Date: 2006-09-16T10:13:29+02:00	[thread overview]
Message-ID: <1i3ne95psolai.abl9eureg5t9$.dlg@40tude.net> (raw)
In-Reply-To: 1158363114.393740.59040@h48g2000cwc.googlegroups.com

On 15 Sep 2006 16:31:54 -0700, Adam Beneschan wrote:

> Dmitry A. Kazakov wrote:
>> Both X'Address and Unchecked_Conversion of a pointer to X would not give
>> the true address of X (i.e. the address returned by Allocate of the
>> corresponding storage pool). For an array type, X'Address is the address of
>> the first element, the dope is out.
>>
>> Is there any better way than this extremely ugly and slow:
>>
>>    type Fake_Pool is new Root_Storage_Pool with record
>>       Location : Address;
>>    end record;
>>
>>    procedure Allocate (...) is -- Never called
>>    begin
>>       raise Program_Error;
>>    end Allocate;
>>
>>    procedure Deallocate
>>              (  Pool            : in out Fake_Pool;
>>                 Storage_Address : in Address;
>>                 Size            : Storage_Count;
>>                 Alignment       : Storage_Count
>>              )  is
>>    begin
>>       Pool.Location := Storage_Address;
>>    end Deallocate;
>>
>>    function Storage_Size (Pool : Fake_Pool) -- Never called
>>       return Storage_Count is
>>    begin
>>       return 0;
>>    end Storage_Size;
>>
>>    function Address_Of (Pointer : Object_Ptr) return Address is
>>       Pool : Fake_Pool; -- A controlled object, that must be slow!
>>       type Fake_Ptr is access Object_Type;
>>       for Fake_Ptr'Storage_Pool use Pool;
>>       function To_Fake is
>>          new Ada.Unchecked_Conversion (Object_Ptr, Fake_Ptr);
>>       procedure Free is
>>          new Ada.Unchecked_Deallocation (Object_Type, Fake_Ptr);
>>       Ptr : Fake_Ptr := To_Fake (Pointer);
>>    begin
>>       Free (Ptr);
>>       return Pool.Location;
>>    end Address_Of;
>>
>> ?
> 
> This sounds highly implementation-dependent.

At best!

> I don't think there's an implementation-independent way to get what
> you're looking for (and, frankly, I'm not even sure there's an
> implementation-independent way to *define* what you're looking for).
> Assuming that you're interested in only one implementation, perhaps you
> can ask the implementors to provide an implementation-dependent
> attribute or something to give you what you need.

Hmm, actually it is quite easy to define formally:

Let P be a pointer to X. Then what I need is the address A, which the pool
P'Storage_Pool returned when X was allocated there. This is the same
address the pool will receive upon freeing X.

----------
The problem behind. What I need is to be able to add some dopes to the
objects allocated in *my* pool. new T calls to the pool's Allocate, but the
address it returns gets mangled by the compiler, when converted to the
pointer P [for example, when T is String]. So, having P I cannot get at my
dope. Its address should be

A = P.all'Address - Offs

where Offs is known, alas, to the compiler only. The compiler must know
that, otherwise it couldn't pass that to Deallocate.

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



  reply	other threads:[~2006-09-16  8:13 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-15 20:24 Address of an object Dmitry A. Kazakov
2006-09-15 23:31 ` Adam Beneschan
2006-09-16  8:13   ` Dmitry A. Kazakov [this message]
2006-09-18 10:29     ` Stephen Leake
2006-09-18 12:09       ` Dmitry A. Kazakov
2006-09-18 13:54         ` Maciej Sobczak
2006-09-18 14:53           ` Dmitry A. Kazakov
2006-09-19  9:15         ` Stephen Leake
2006-09-19 13:29           ` Dmitry A. Kazakov
2006-09-16 13:21 ` Stephen Leake
2006-09-16 17:06   ` Dmitry A. Kazakov
2006-09-20  0:43     ` Randy Brukardt
replies disabled

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