www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Unfortunately delete is a keyword

reply Rory Mcguire <rjmcguire gm_no_ail.com> writes:
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();
Jul 29 2010
parent reply Kagamin <spam here.lot> writes:
Rory Mcguire Wrote:

 Inspired by the recent thread "Proposal for dual memory management" I 
 wondered how long it would take to change scoped into heaped.
You can also try to implement switch, for and while statements in a similar way. That's a whole bunch of keywords.
Jul 29 2010
next sibling parent reply =?UTF-8?B?IkrDqXLDtG1lIE0uIEJlcmdlciI=?= <jeberger free.fr> writes:
Kagamin wrote:
 Rory Mcguire Wrote:
=20
 Inspired by the recent thread "Proposal for dual memory management" I =
 wondered how long it would take to change scoped into heaped.
=20 You can also try to implement switch, for and while statements in a sim=
ilar way. That's a whole bunch of keywords. Then you get scheme (or felix) where almost all the language constructs are actually part of the std library... Jerome --=20 mailto:jeberger free.fr http://jeberger.free.fr Jabber: jeberger jabber.fr
Jul 29 2010
parent Kagamin <spam here.lot> writes:
Jérôme M. Berger Wrote:

 Then you get scheme (or felix) where almost all the language
 constructs are actually part of the std library...
every language designer's dream :3
Jul 29 2010
prev sibling parent Rory Mcguire <rjmcguire gm_no_ail.com> writes:
Kagamin wrote:

 Rory Mcguire Wrote:
 
 Inspired by the recent thread "Proposal for dual memory management" I
 wondered how long it would take to change scoped into heaped.
You can also try to implement switch, for and while statements in a similar way. That's a whole bunch of keywords.
hmm, there was a benchmark here somewhere where somone (bearofile?) compared if and if else performance to a switch statements performance and if did get better performance for some things. :D
Jul 29 2010