Struct beeing moved around
Benjamin Thaut
code at benjamin-thaut.de
Fri May 20 11:46:03 PDT 2011
Am 20.05.2011 19:51, schrieb Michel Fortin:
> On 2011-05-20 13:36:58 -0400, Benjamin Thaut <code at benjamin-thaut.de> said:
>
>> What if I need a value type that may be copied, but may not be moved?
>
> I don't think that's possible. Why would you want that?
>
Lets say you want to implement a GC that is not a Mark & Sweep, and thus
you can not do the pointer guessing game the current D GC does. So you
have to create a reference value type, that registers itself with the GC
when it is constructed and deregisteres itself on destruction. This
reference type is only to be used inside functions for references that
are constructed on the stack.
class foo { ... }
void bar(){
foo f = new foo();
}
would become
void bar(){
Ref!foo f = Ref!foo(new foo());
}
where Ref is:
struct Ref(T){
T m_Ref;
alias m_Ref this;
this(){
GC.addRoot(&m_Ref);
}
~this(){
GC.removeRoot(&m_Ref);
}
}
If there is no garantuee that a struct stays on the same memory location
within 1 Stack frame, you pretty much can not implement a other GC
unless you hack into the compiler.
--
Kind Regards
Benjamin Thaut
More information about the Digitalmars-d
mailing list