D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3517 - Allocators proposal
Summary: Allocators proposal
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: Other Linux
: P2 enhancement
Assignee: Andrei Alexandrescu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-11-16 15:14 UTC by Kyle Foley
Modified: 2015-11-03 14:02 UTC (History)
1 user (show)

See Also:


Attachments
proposed implementation and test/example (2.37 KB, application/x-gzip)
2009-11-16 15:14 UTC, Kyle Foley
Details

Note You need to log in before you can comment on or make changes to this issue.
Description Kyle Foley 2009-11-16 15:14:34 UTC
Created attachment 498 [details]
proposed implementation and test/example

I'm proposing a standard way to define and pass around allocators.  My primary
motivation for proposing this is because I intend to later submit a containers
proposal that would then use this allocators structure, where an allocator
would be one of the parameters of a container type.

But allocators could also be directly usable through some create!(MyType)
syntax, as demonstrated in the example.  All criticism is welcome :)

If there's a better way to submit proposals like this, I want to know about it.
 The following is test.d from the attachment demonstrating how allocators could
be directly usable:

---

import std.stdio;

import memory.allocators.gc;
import memory.allocators.cmallocator;

// overload set: choose the gc
alias memory.allocators.gc.create create;
alias memory.allocators.gc.destroy destroy;

struct TestStruct
{
    int a = 42;
    int b = 23;

    ~this() { writeln("Struct Destructed"); }
}

interface TestInterface
{
    bool test();
}

class TestClass1 : TestInterface
{
    int a = 99;
    int b = 1337;
    string id = "";

    this(string rhs) { id = rhs; writeln(id, ".__ctor(string)"); }
    ~this() { writeln(id, ".__dtor()"); }

    void print() { writeln("a: ", a, " and b: ", b); }
    override bool test() { return a < 100; }
}

class TestClass2 : TestInterface
{
    int a = 23;

    override bool test() { return a < 20; }
}

int main()
{
    TestInterface aa = create!(TestClass1)("rawr");

    writeln("1: ", aa.test);

    with ( CMallocator_ClassCreator!TestClass1 )
    {
        auto a = create("CM First"); scope (exit) destroy(a);
        write("2: "); a.print;
    }

    with ( CMallocator_ClassCreator!TestClass2 )
    {
        aa = create(); scope(exit) destroy( cast(TestClass2)aa );
        writeln("3: ", (cast(TestClass2)aa).a, " < 20 is ", aa.test);
    }

    auto b = create!(TestStruct);
    auto bb = b;

    writeln(b.a);
    bb.a = 92;
    writeln(b.a);

    auto c = create!(TestClass2);
    c.a = 19;
    writeln("c.a = ", c.a);

    aa = c;
    writeln("5: c.a < 20 is ", aa.test);

    return 0;
}
Comment 1 Andrei Alexandrescu 2013-11-15 21:05:11 UTC
Taking this over.
Comment 2 Andrei Alexandrescu 2015-11-03 14:02:03 UTC
Finally can mark this as fixed.