Unfortunately delete is a keyword

Rory Mcguire rjmcguire at gm_no_ail.com
Thu Jul 29 02:56:41 PDT 2010


Inspired by the recent thread "Proposal for dual memory management" I 
wondered how long it would take to change scoped into heaped.
Now I wish I could call the free method delete.
I didn't put the full test source here because I had to copy some stuff from 
phobos svn.

Please let me know what you think, is this already done?

-Rory

code:
@system auto heaped(alias T,Args...)(Args args) {
    struct HeapAlloc {
        private void *buf;
        T cpayload() {
            return cast(T)(cast(byte[]*)buf);
        }
        alias cpayload this;

        void free() {
            destroy(cpayload);// not in 2.046
            // not sure what this does!!!
            if ((cast(void**) cpayload)[1]) {
                _d_monitordelete(cpayload, true);
            }
            .free(buf);
        }
    }
    void* mem = malloc(__traits(classInstanceSize, T));
    if (!mem)
        throw new Exception("no mem");

    emplace!T(cast(void[])mem[0..__traits(classInstanceSize,T)],args);

    HeapAlloc ret;
    ret.buf = mem;
    return ret;
}

Usage:
    auto a = heaped!A;
    auto b = heaped!A(10);
    
    assert(a.i==3);
    assert(a.getI()==3);

    assert(b.i==10);
    assert(b.getI()==10);
    
    a.free();
    b.free();



More information about the Digitalmars-d mailing list