Thread overview
hiding a pointer
Oct 24, 2006
Thomas Kuehne
Oct 24, 2006
Stewart Gordon
Oct 24, 2006
Thomas Kuehne
October 24, 2006
Sometimes you might have to hide a pointer, so that the GC doesn't "see" it and might reclaim the pointer's destination if there are no other pointers into this area.

XORing and other mathematical transformations aren't save(the result might look like a pointer into some other GC-controlled area).

Using memory allocated by std.c.stdlib.malloc to store the pointer works, but will require you to call std.c.stdlib.free at some later point.

As far as I am aware Linux and Windows never allocate user memory
at 0x0000_00XX(32bit) or 0x0000_0000_0000_00XX(64bit):

# struct HiddenPointer(T : T*){
#     size_t[size_t.sizeof] data;
#
#     void set(T* ptr){
#         ubyte[] raw = (cast(ubyte*)&ptr)[0 .. size_t.sizeof];
#         foreach(size_t i, ubyte element; raw){
#             data[i] = element;
#         }
#     }
#
#     T* get(){
#         T* result;
#         ubyte[] raw = (cast(ubyte*)&result)[0 .. size_t.sizeof];
#         foreach(size_t i, size_t element; data){
#             raw[i] = cast(ubyte)element;
#         }
#         return result;
#     }
# }

The above code should work regardless of endianess or pointer size.

trivial usage sample:

# int main(){
#     HiddenPointer!(int*) hidden_pointer;
#
#     int x;
#     hidden_pointer.set(&x);
#     int* y = hidden_pointer.get();
#
#     return 0;
# }

Thomas


October 24, 2006
Thomas Kuehne wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Sometimes you might have to hide a pointer, so that the GC doesn't
> "see" it and might reclaim the pointer's destination if there are
> no other pointers into this area.
<snip>

And won't update it if it's moved by a copying GC.

The only infallible solution I can see is if D finally gets weak references.

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.
October 24, 2006
Stewart Gordon schrieb am 2006-10-24:
> Thomas Kuehne wrote:
>> Sometimes you might have to hide a pointer, so that the GC doesn't "see" it and might reclaim the pointer's destination if there are no other pointers into this area.
><snip>
>
> And won't update it if it's moved by a copying GC.

Right, this aint no weak pointer but a hidden one.

Thomas