dexed-d, remove unused stuff added after copy of iz.memory

This commit is contained in:
Basile Burg 2022-05-12 09:50:04 +02:00
parent 5b59cf30a4
commit e9bcb9b6ac
1 changed files with 0 additions and 464 deletions

View File

@ -274,92 +274,6 @@ package template hasManagedDimension(T)
enum hasManagedDimension = false;
}
unittest
{
alias A0 = int[];
static assert(hasManagedDimension!A0);
alias A1 = int[2][];
static assert(hasManagedDimension!A1);
alias A2 = int[][2];
static assert(hasManagedDimension!A2);
alias A3 = int[3][2];
static assert(!hasManagedDimension!A3);
alias A4 = int[][3][2];
static assert(hasManagedDimension!A4);
}
unittest
{
class Foo{int[][2][4] a;}
static assert(MustAddGcRange!Foo);
class Bar{@NoGc int[][2][4] a;}
static assert(!MustAddGcRange!Bar);
}
/**
* When mixed in a aggregate this template has for effect to disable the usage
* the $(D new) operator.
*/
mixin template disableNew()
{
@disable new();
}
///
unittest
{
// class requiring users to use allocators.
class NotUsableWithNew
{
mixin disableNew;
}
// statically verify that `new` cannot be used.
static assert(!__traits(compiles, new NotUsableWithNew));
// Ok with a custom allocator
auto a = construct!NotUsableWithNew();
destruct(a);
}
/**
* When mixed in class this template has for effect to automatically call the
* nearest inherited destructor if no destructor is present, otherwise a call
* to $(D callInheritedDtor()) should be made in the last LOC of the destructor.
*/
mixin template inheritedDtor()
{
static assert(is(typeof(this) == class));
private
{
import std.traits: BaseClassesTuple;
alias __iz_B = BaseClassesTuple!(typeof(this));
enum hasDtor = __traits(hasMember, typeof(this), "__dtor");
static if (hasDtor && !__traits(isSame, __traits(parent, typeof(this).__dtor), typeof(this)))
enum inDtor = true;
else
enum inDtor = false;
}
public void callInheritedDtor(classT = typeof(this))()
{
import std.meta: aliasSeqOf;
import std.range: iota;
foreach(i; aliasSeqOf!(iota(0, __iz_B.length)))
static if (__traits(hasMember, __iz_B[i], "__xdtor"))
{
mixin("this." ~ __iz_B[i].stringof ~ ".__xdtor;");
break;
}
}
static if (!hasDtor || inDtor)
public ~this() {callInheritedDtor();}
}
/**
* Returns a new, GC-free, class instance.
*
@ -384,33 +298,6 @@ if (is(CT == class) && !isAbstractClass!CT)
return cast(CT) memory;
}
/**
* Returns a new, GC-free, class instance.
*
* This overload is designed to create factories and like the default
* Object.factory method, it only calls, if possible, the default constructor.
* The factory() function implemented in this iz.memory is based on this
* construct() overload.
*
* Params:
* tic = The TypeInfo_Class of the Object to create.
*/
Object construct(TypeInfo_Class tic) @trusted
{
if (tic.m_flags & 64)
return null;
auto size = tic.initializer.length;
auto memory = getMem(size);
memory[0 .. size] = tic.initializer[];
Object result = cast(Object) memory;
if (tic.defaultConstructor)
tic.defaultConstructor(result);
import core.memory: GC;
GC.addRange(memory, size, tic);
return result;
}
/**
* Returns a new, GC-free, pointer to a struct or to an union.
*
@ -610,357 +497,6 @@ if (isBasicType!T)
instance = null;
}
/**
* Returns a pointer to a new, GC-free, basic variable.
* Any variable allocated using this function must be manually freed with freeMem.
*
* Params:
* T = The type of the pointer to return.
* preFill = Optional, boolean indicating if the result has to be initialized.
* a = Optional, the value.
*/
T* newPtr(T, bool preFill = false, A...)(A a)
if (isBasicType!T && A.length <= 1)
{
T* result = cast(T*) getMem(T.sizeof);
static if (A.length == 1)
*result = a[0];
else static if (preFill)
*result = T.init;
return result;
}
/**
* Frees and invalidates a list of classes instances or struct pointers.
* $(D destruct()) is called for each item.
*
* Params:
* objs = Variadic list of Object instances.
*/
void destructEach(Objs...)(auto ref Objs objs)
if (Objs.length > 1)
{
foreach(ref obj; objs)
destruct(obj);
}
/**
* Register a class type that can be created dynamically, using its name.
*
* Class registration should only be done in module constructors. This allow
* the registration to be thread safe since module constructors are executed
* in the main thread.
*
* Params:
* T = A class.
* name = The name used to register the class.
* By default the $(D T.stringof) is used.
* f = The class repository, a hashmap of TypeInfo_Class by string.
*/
void registerFactoryClass(T, F)(ref F f, string name = "")
if (is(T == class) && !isAbstractClass!T)
{
if (!name.length)
name = T.stringof;
f[name] = typeid(T);
}
/**
* Calls registerClass() for each template argument.
*
* Params:
* A = A list of classes, from one up to nine.
* f = The class repository, a hashmap of TypeInfo_Class by string.
*/
void registerFactoryClasses(A1, A2 = void, A3 = void, A4 = void, A5 = void,
A6 = void, A7 = void, A8 = void, A9 = void, F)(ref F f)
{
void tryRegister(A)()
{
static if (is(A == class))
registerFactoryClass!(A, F)(f);
else static if (!is(A == void))
static assert(0, A.stringof ~ " is not a class");
}
tryRegister!A1;tryRegister!A2;tryRegister!A3;
tryRegister!A4;tryRegister!A5;tryRegister!A6;
tryRegister!A7;tryRegister!A8;tryRegister!A9;
}
/**
* Constructs a class instance using a gc-free factory.
* The usage is similar to Object.factory() except that by default
* registered classes don't take the module in account.
*
* Params:
* className = the class name, as registered in registerFactoryClass().
* factory = The class repository, a hashmap of TypeInfo_Class by string.
* Throws:
* An Exception if the class is not registered.
*/
Object factory(F)(ref F f, string className)
{
TypeInfo_Class* tic = className in f;
if (!tic)
throw construct!Exception("factory exception, unregistered class");
return
construct(*tic);
}
unittest
{
import core.memory: GC;
interface I{}
class AI: I{}
auto a = construct!Object;
a.destruct;
assert(!a);
a.destruct;
assert(!a);
a.destruct;
auto b = construct!Object;
auto c = construct!Object;
destructEach(a,b,c);
assert(!a);
assert(!b);
assert(!c);
destructEach(a,b,c);
assert(!a);
assert(!b);
assert(!c);
Object foo = construct!Object;
Object bar = new Object;
assert( GC.addrOf(cast(void*)foo) == null );
assert( GC.addrOf(cast(void*)bar) != null );
foo.destruct;
bar.destroy;
struct Foo
{
this(size_t a,size_t b,size_t c)
{
this.a = a;
this.b = b;
this.c = c;
}
size_t a,b,c;
}
Foo * foos = construct!Foo(1,2,3);
Foo * bars = new Foo(4,5,6);
assert(foos.a == 1);
assert(foos.b == 2);
assert(foos.c == 3);
assert( GC.addrOf(cast(void*)foos) == null );
assert( GC.addrOf(cast(void*)bars) != null );
foos.destruct;
bars.destroy;
assert(!foos);
foos.destruct;
assert(!foos);
union Uni{bool b; ulong ul;}
Uni* uni0 = construct!Uni();
Uni* uni1 = new Uni();
assert( GC.addrOf(cast(void*)uni0) == null );
assert( GC.addrOf(cast(void*)uni1) != null );
uni0.destruct;
assert(!uni0);
uni0.destruct;
assert(!uni0);
auto ai = construct!AI;
auto i = cast(I) ai;
destruct(i);
assert(i is null);
abstract class Abst {}
Object ab = construct(typeid(Abst));
assert(ab is null);
}
@nogc unittest
{
class Foo {@nogc this(int a){} @nogc~this(){}}
Foo foo = construct!Foo(0);
destruct(foo);
assert(foo is null);
static struct Bar {}
Bar* bar = construct!Bar;
destruct(bar);
assert(bar is null);
static struct Baz {int i; this(int,int) @nogc {}}
Baz* baz = construct!Baz(0,0);
destruct(baz);
assert(baz is null);
}
unittest
{
struct Foo {int a; ulong b;}
Foo* f = construct!Foo(1,2);
assert(f.a == 1);
assert(f.b == 2);
destruct(f);
}
unittest
{
import core.memory: GC;
import std.math: isNaN;
auto f = newPtr!(float,true);
assert(isNaN(*f));
auto ui = newPtr!int;
auto i = newPtr!uint;
auto l = new ulong;
auto u = newPtr!uint(8);
assert(*u == 8u);
assert(ui);
assert(i);
assert(f);
assert(GC.addrOf(f) == null);
assert(GC.addrOf(i) == null);
assert(GC.addrOf(ui) == null);
assert(GC.addrOf(l) != null);
assert(GC.addrOf(u) == null);
*i = 8u;
assert(*i == 8u);
freeMem(ui);
freeMem(i);
freeMem(f);
freeMem(u);
assert(ui == null);
assert(i == null);
assert(f == null);
auto ptr = getMem(16);
assert(ptr);
assert(GC.addrOf(ptr) == null);
ptr.freeMem;
assert(!ptr);
}
unittest
{
class A{string text; this(){text = "A";}}
class B{string text; this(){text = "B";}}
class C{int[] array; this(){array = [1,2,3];}}
enum TypeInfo_Class[3] tics = [typeid(A),typeid(B),typeid(C)];
A a = cast(A) construct(tics[0]);
assert(a.text == "A");
B b = cast(B) construct(tics[1]);
assert(b.text == "B");
C c = cast(C) construct(tics[2]);
assert(c.array == [1,2,3]);
destructEach(a, b, c);
}
unittest
{
alias Factory = TypeInfo_Class[string];
__gshared Factory classRegistry;
class A{string text; this(){text = "A";}}
class B{string text; this(){text = "B";}}
class C{int[] array; this(){array = [1,2,3];}}
registerFactoryClass!A(classRegistry);
classRegistry.registerFactoryClass!B;
classRegistry.registerFactoryClass!C;
registerFactoryClasses!(A,B)(classRegistry);
A a = cast(A) classRegistry.factory("A");
assert(a.text == "A");
B b = cast(B) classRegistry.factory("B");
assert(b.text == "B");
C c = cast(C) classRegistry.factory("C");
assert(c.array == [1,2,3]);
version(checkleaks) {} else
{
import std.exception: assertThrown;
assertThrown(classRegistry.factory("D"));
}
destructEach(a,b,c);
}
@nogc unittest
{
void* src = getMem(32);
void* dst = getMem(32);
(cast (ubyte*)src)[0..32] = 8;
copyMem(dst, src, 32);
foreach(immutable i; 0 .. 32)
assert((cast (ubyte*)src)[i] == (cast (ubyte*)dst)[i]);
(cast (ubyte*)src)[0..32] = 7;
src = reallocMem(src, 32 + 16);
ubyte* ovl = (cast (ubyte*)src) + 16;
moveMem(cast(void*)ovl, cast(void*)src, 32);
assert((cast (ubyte*)ovl)[31] == 7);
freeMem(src);
freeMem(dst);
}
@nogc unittest
{
@NoInit static struct Foo {uint a = 1; ~this() @nogc{}}
Foo* foo = construct!Foo;
assert(foo.a != 1);
@NoInit static class Bar {uint a = 1; ~this() @nogc{}}
Bar bar = construct!Bar;
assert(bar.a != 1);
destructEach(foo, bar);
}
unittest
{
static int i;
template impl()
{
~this(){i += 1;}
}
static class Base
{
mixin impl;
mixin inheritedDtor;
~this(){i += 2;}
}
static class Derived: Base
{
mixin inheritedDtor;
~this(){i += 3; callInheritedDtor;}
}
Base b = construct!Derived;
// without static type
destruct(cast(Object) b);
assert(i == 6);
i = 0;
Derived d = construct!Derived;
// with static type
destruct(d);
assert(i == 6);
}
/**
* Generic, manually managed, array.
*