www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - hiding a pointer

reply Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----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.

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


-----BEGIN PGP SIGNATURE-----

iD8DBQFFPcHMLK5blCcjpWoRAtk9AJ9obolpi2cPg0C55yjnHdAousvVaACfaZhf
LTOod2fWIxnfSsT5AnOaLcQ=
=b8dw
-----END PGP SIGNATURE-----
Oct 23 2006
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
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.

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.
Oct 24 2006
parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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.

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

Right, this aint no weak pointer but a hidden one. Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFFPqFKLK5blCcjpWoRAtpLAKCjfjn+Gmrmy3hyDfpt3yThSnj2AwCfVOPC aegnJxYeIc+Jhz2AiGxJ2JY= =05o9 -----END PGP SIGNATURE-----
Oct 24 2006