From d8b01ee0973d213b171297287fa68a08ac5ac70e Mon Sep 17 00:00:00 2001 From: Mergul Date: Fri, 11 Oct 2019 19:15:01 +0200 Subject: [PATCH] -working betterC --- source/ecs/std.d | 62 ++++++++++++++---------------------------------- tests/tests.d | 2 +- 2 files changed, 19 insertions(+), 45 deletions(-) diff --git a/source/ecs/std.d b/source/ecs/std.d index e4dc5cd..249ec41 100644 --- a/source/ecs/std.d +++ b/source/ecs/std.d @@ -16,17 +16,6 @@ version (Windows) /*extern(Windows) void* __alloca(size_t size) @nogc nothrow @system; alias alloca = __alloca;*/ - private const uint max_alloca = 10000; - private char[max_alloca] alloca_array; - private uint alloca_pos = 0; - void* alloca(size_t length) @nogc nothrow - { - if(alloca_pos + length > max_alloca)alloca_pos = 0; - void* ret = &alloca_array[alloca_pos]; - alloca_pos += length; - return ret; - } - extern(Windows) void ___chkstk_ms() @nogc nothrow @system; extern(Windows) void __chkstk() @@ -34,34 +23,35 @@ version (Windows) ___chkstk_ms(); } } - else - { - private const uint max_alloca = 10000; - private char[max_alloca] alloca_array; - private uint alloca_pos = 0; - void* alloca(size_t length) @nogc nothrow - { - if(alloca_pos + length > max_alloca)alloca_pos = 0; - void* ret = &alloca_array[alloca_pos]; - alloca_pos += length; - return ret; - } - } } else version (Posix) { import core.sys.posix.pthread; import core.sys.posix.stdlib; - public import core.stdc.stdlib : alloca; } +version(D_betterC) +{ + private const uint max_alloca = 10000; + private char[max_alloca] alloca_array; + private uint alloca_pos = 0; + void* alloca(size_t length) @nogc nothrow + { + if(alloca_pos + length > max_alloca)alloca_pos = 0; + void* ret = &alloca_array[alloca_pos]; + alloca_pos += length; + return ret; + } +} +else public import core.stdc.stdlib : alloca; + static struct Mallocator { static T[] makeArray(T)(size_t length) nothrow @nogc { T[] ret = (cast(T*)malloc(T.sizeof * length))[0 .. length]; - /*static if(__traits(isPOD, T)) + static if(__traits(isPOD, T)) { static immutable T init = T.init; @@ -71,21 +61,13 @@ static struct Mallocator } } else - {*/ + { static import std.conv; foreach(i;0..ret.length) { std.conv.emplace(&ret[i]); } - // } - - //static if(is(T == struct))std.conv.emplace(ret); - //static import std.conv; - //emplace - /*foreach(i;0..ret.length) - { - memcpy(ret); - }*/ + } return ret; } @@ -94,7 +76,6 @@ static struct Mallocator T[] ret = (cast(T*)malloc(T.sizeof * length))[0 .. length]; foreach(ref v; ret)v = initializer; return ret; - //return (cast(T*)ret)[0 .. length]; } static T[] expandArray(T)(T[] array, size_t length) nothrow @nogc @@ -107,17 +88,12 @@ static struct Mallocator { T[] ret = (cast(T*)malloc(T.sizeof * array.length))[0 .. array.length];//Mallocator.makeArray!(T)(array.length); foreach(i, ref v;ret)v = array[i]; - //ret[0 .. $] = array; return ret; } static T* make(T, Args...)(Args args) { T* ret = cast(T*)malloc(T.sizeof); - //*ret = T.init; - //static immutable T init = T(); - //memcpy(ret, &init, T.sizeof); - //static if(__traits(hasMember, T, "__ctor"))ret.__ctor(args); static import std.conv; static if(__traits(isPOD, T)) { @@ -125,7 +101,6 @@ static struct Mallocator memcpy(ret, &init, T.sizeof); } else static if(is(T == struct))std.conv.emplace(ret, args); - //else std.conv.emplace(ret, args); return ret; } @@ -135,7 +110,6 @@ static struct Mallocator version(Posix)posix_memalign(&ret, alignment, length);//ret = aligned_alloc(alignment, length); else version(Windows)ret = _aligned_malloc(length, alignment); else static assert(0, "Unimplemented platform!"); - //posix_memalign(&ret, alignment, length); return ret; } diff --git a/tests/tests.d b/tests/tests.d index 6784ea9..f966005 100644 --- a/tests/tests.d +++ b/tests/tests.d @@ -512,7 +512,7 @@ extern(C) int main() printf("TestComp(%u, %u)",test_comp.a,test_comp.b);//write(*test_comp); TestComp2* test_comp2 = entity.getComponent!TestComp2; if (test_comp2) - printf("TestComp2(%u, %u)",test_comp2.a,test_comp2.b);//write(*test_comp2); + printf("TestComp2(%u, %u)",test_comp2.b,test_comp2.a);//write(*test_comp2); TestComp3* test_comp3 = entity.getComponent!TestComp3; if (test_comp3) printf("TestComp3(%u, %u)",test_comp3.gg,test_comp3.bg);//write(*test_comp3);