Fix always calling GC.addRange in ChoppedVector if necessary.

This commit is contained in:
Sönke Ludwig 2017-08-26 22:33:00 +02:00
parent 74352a9ac5
commit eb595ef858

View file

@ -38,9 +38,7 @@ struct ChoppedVector(T, size_t CHUNK_SIZE = 16*64*1024/nextPOT(T.sizeof)) {
@safe: nothrow: @safe: nothrow:
import core.stdc.stdlib : calloc, free, malloc, realloc; import core.stdc.stdlib : calloc, free, malloc, realloc;
import std.traits : hasElaborateDestructor; import std.traits : hasIndirections;
static assert(!hasElaborateDestructor!T, "Cannot store element with elaborate destructor in ChoppedVector.");
alias chunkSize = CHUNK_SIZE; alias chunkSize = CHUNK_SIZE;
@ -65,6 +63,8 @@ struct ChoppedVector(T, size_t CHUNK_SIZE = 16*64*1024/nextPOT(T.sizeof)) {
@nogc { @nogc {
() @trusted { () @trusted {
foreach (i; 0 .. m_chunkCount) { foreach (i; 0 .. m_chunkCount) {
destroy(m_chunks[i]);
static if (hasIndirections!T)
GC.removeRange(m_chunks[i]); GC.removeRange(m_chunks[i]);
free(m_chunks[i]); free(m_chunks[i]);
} }
@ -127,6 +127,8 @@ struct ChoppedVector(T, size_t CHUNK_SIZE = 16*64*1024/nextPOT(T.sizeof)) {
() @trusted { () @trusted {
auto ptr = cast(ChunkPtr)calloc(chunkSize, T.sizeof); auto ptr = cast(ChunkPtr)calloc(chunkSize, T.sizeof);
assert(ptr !is null, "Failed to allocate chunk!"); assert(ptr !is null, "Failed to allocate chunk!");
// FIXME: initialize with T.init instead of 0
static if (hasIndirections!T)
GC.addRange(ptr, chunkSize * T.sizeof); GC.addRange(ptr, chunkSize * T.sizeof);
m_chunks[m_chunkCount++] = ptr; m_chunks[m_chunkCount++] = ptr;
} (); } ();