Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1037 from rainers/aa_typeinfo
Browse files Browse the repository at this point in the history
Provide more TypeInfo for AA allocations
  • Loading branch information
MartinNowak committed Jan 23, 2015
2 parents 2bccc29 + 407bda0 commit f0c1e13
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/object.di
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ extern (C)
// size_t _aaLen(in void* p) pure nothrow @nogc;
private void* _aaGetX(void** paa, const TypeInfo keyti, in size_t valuesize, in void* pkey) pure nothrow;
// inout(void)* _aaGetRvalueX(inout void* p, in TypeInfo keyti, in size_t valuesize, in void* pkey);
inout(void)[] _aaValues(inout void* p, in size_t keysize, in size_t valuesize) pure nothrow;
inout(void)[] _aaKeys(inout void* p, in size_t keysize) pure nothrow;
inout(void)[] _aaValues(inout void* p, in size_t keysize, in size_t valuesize, const TypeInfo tiValArray) pure nothrow;
inout(void)[] _aaKeys(inout void* p, in size_t keysize, const TypeInfo tiKeyArray) pure nothrow;
void* _aaRehash(void** pp, in TypeInfo keyti) pure nothrow;

// alias _dg_t = extern(D) int delegate(void*);
Expand Down Expand Up @@ -507,7 +507,7 @@ auto byValue(T : Value[Key], Value, Key)(T *aa) pure nothrow @nogc

Key[] keys(T : Value[Key], Value, Key)(T aa) @property
{
auto a = cast(void[])_aaKeys(cast(inout(void)*)aa, Key.sizeof);
auto a = cast(void[])_aaKeys(cast(inout(void)*)aa, Key.sizeof, typeid(Key[]));
return *cast(Key[]*)&a;
}

Expand All @@ -518,7 +518,7 @@ Key[] keys(T : Value[Key], Value, Key)(T *aa) @property

Value[] values(T : Value[Key], Value, Key)(T aa) @property
{
auto a = cast(void[])_aaValues(cast(inout(void)*)aa, Key.sizeof, Value.sizeof);
auto a = cast(void[])_aaValues(cast(inout(void)*)aa, Key.sizeof, Value.sizeof, typeid(Value[]));
return *cast(Value[]*)&a;
}

Expand Down
8 changes: 4 additions & 4 deletions src/object_.d
Original file line number Diff line number Diff line change
Expand Up @@ -1962,8 +1962,8 @@ extern (C)
// size_t _aaLen(in void* p) pure nothrow @nogc;
private void* _aaGetX(void** paa, const TypeInfo keyti, in size_t valuesize, in void* pkey) pure nothrow;
// inout(void)* _aaGetRvalueX(inout void* p, in TypeInfo keyti, in size_t valuesize, in void* pkey);
inout(void)[] _aaValues(inout void* p, in size_t keysize, in size_t valuesize) pure nothrow;
inout(void)[] _aaKeys(inout void* p, in size_t keysize) pure nothrow;
inout(void)[] _aaValues(inout void* p, in size_t keysize, in size_t valuesize, const TypeInfo tiValArray) pure nothrow;
inout(void)[] _aaKeys(inout void* p, in size_t keysize, const TypeInfo tiKeyArray) pure nothrow;
void* _aaRehash(void** pp, in TypeInfo keyti) pure nothrow;

// alias _dg_t = extern(D) int delegate(void*);
Expand Down Expand Up @@ -2107,7 +2107,7 @@ auto byValue(T : Value[Key], Value, Key)(T *aa) pure nothrow @nogc

Key[] keys(T : Value[Key], Value, Key)(T aa) @property
{
auto a = cast(void[])_aaKeys(cast(inout(void)*)aa, Key.sizeof);
auto a = cast(void[])_aaKeys(cast(inout(void)*)aa, Key.sizeof, typeid(Key[]));
return *cast(Key[]*)&a;
}

Expand All @@ -2118,7 +2118,7 @@ Key[] keys(T : Value[Key], Value, Key)(T *aa) @property

Value[] values(T : Value[Key], Value, Key)(T aa) @property
{
auto a = cast(void[])_aaValues(cast(inout(void)*)aa, Key.sizeof, Value.sizeof);
auto a = cast(void[])_aaValues(cast(inout(void)*)aa, Key.sizeof, Value.sizeof, typeid(Value[]));
return *cast(Value[]*)&a;
}

Expand Down
43 changes: 35 additions & 8 deletions src/rt/aaA.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private
import core.stdc.string;
import core.stdc.stdio;
import core.memory;
import rt.lifetime : _d_newarrayU;

// Convenience function to make sure the NO_INTERIOR gets set on the
// bucket array.
Expand Down Expand Up @@ -164,6 +165,31 @@ in
{
assert(aa);
}
body
{
if (aa.impl is null)
{
aa.impl = new Impl();
aa.impl.buckets = aa.impl.binit[];
aa.impl.firstUsedBucket = aa.impl.buckets.length;
aa.impl._keyti = cast() keyti;
}
return _aaGetImpl(aa, keyti, valuesize, pkey);
}

void* _aaGetY(AA* aa, const TypeInfo_AssociativeArray ti, in size_t valuesize, in void* pkey)
{
if (aa.impl is null)
{
aa.impl = new Impl();
aa.impl.buckets = aa.impl.binit[];
aa.impl.firstUsedBucket = aa.impl.buckets.length;
aa.impl._keyti = cast() ti.key;
}
return _aaGetImpl(aa, ti.key, valuesize, pkey);
}

void* _aaGetImpl(AA* aa, const TypeInfo keyti, in size_t valuesize, in void* pkey)
out (result)
{
assert(result);
Expand Down Expand Up @@ -208,7 +234,7 @@ body
// Not found, create new elem
//printf("create new one\n");
size_t size = Entry.sizeof + aligntsize(keytitsize) + valuesize;
e = cast(Entry *) GC.malloc(size);
e = cast(Entry *) GC.malloc(size, 0); // TODO: needs typeid(Entry+)
e.next = null;
e.hash = key_hash;
ubyte* ptail = cast(ubyte*)(e + 1);
Expand Down Expand Up @@ -348,7 +374,7 @@ bool _aaDelX(AA aa, in TypeInfo keyti, in void* pkey)
/********************************************
* Produce array of values from aa.
*/
inout(ArrayRet_t) _aaValues(inout AA aa, in size_t keysize, in size_t valuesize) pure nothrow
inout(ArrayRet_t) _aaValues(inout AA aa, in size_t keysize, in size_t valuesize, const TypeInfo tiValueArray) pure nothrow
{
size_t resi;
Array a;
Expand All @@ -358,8 +384,7 @@ inout(ArrayRet_t) _aaValues(inout AA aa, in size_t keysize, in size_t valuesize)
if (aa.impl !is null)
{
a.length = _aaLen(aa);
a.ptr = cast(byte*) GC.malloc(a.length * valuesize,
valuesize < (void*).sizeof ? GC.BlkAttr.NO_SCAN : 0);
a.ptr = cast(byte*) _d_newarrayU(tiValueArray, a.length).ptr;
resi = 0;
foreach (inout(Entry)* e; aa.impl.buckets[aa.impl.firstUsedBucket..$])
{
Expand All @@ -368,6 +393,7 @@ inout(ArrayRet_t) _aaValues(inout AA aa, in size_t keysize, in size_t valuesize)
memcpy(a.ptr + resi * valuesize,
cast(byte*)e + Entry.sizeof + alignsize,
valuesize);
// TODO: no postblit here?
resi++;
e = e.next;
}
Expand Down Expand Up @@ -448,14 +474,13 @@ body
/********************************************
* Produce array of N byte keys from aa.
*/
inout(ArrayRet_t) _aaKeys(inout AA aa, in size_t keysize) pure nothrow
inout(ArrayRet_t) _aaKeys(inout AA aa, in size_t keysize, const TypeInfo tiKeyArray) pure nothrow
{
auto len = _aaLen(aa);
if (!len)
return null;

immutable blkAttr = !(aa.impl.keyti.flags & 1) ? GC.BlkAttr.NO_SCAN : 0;
auto res = (cast(byte*) GC.malloc(len * keysize, blkAttr))[0 .. len * keysize];
void[] res = _d_newarrayU(tiKeyArray, len);

size_t resi = 0;
// note, can't use firstUsedBucketCache here, aa is inout
Expand All @@ -464,6 +489,7 @@ inout(ArrayRet_t) _aaKeys(inout AA aa, in size_t keysize) pure nothrow
while (e)
{
memcpy(&res[resi * keysize], cast(byte*)(e + 1), keysize);
// TODO: no postblit here?
resi++;
e = e.next;
}
Expand Down Expand Up @@ -643,8 +669,9 @@ Impl* _d_assocarrayliteralTX(const TypeInfo_AssociativeArray ti, void[] keys, vo
{
// Not found, create new elem
//printf("create new one\n");
e = cast(Entry *) cast(void*) new void[Entry.sizeof + keytsize + valuesize];
e = cast(Entry *) GC.malloc(Entry.sizeof + keytsize + valuesize); // TODO: needs typeid(Entry+)
memcpy(e + 1, pkey, keysize);
e.next = null;
e.hash = key_hash;
*pe = e;
result.nodes++;
Expand Down

0 comments on commit f0c1e13

Please sign in to comment.