MmapPool: (p[] is null) != (p[].ptr is null)

This commit is contained in:
Eugen Wissner 2017-01-12 19:31:49 +01:00
parent 7bed7f039f
commit 4c4e65b373
2 changed files with 20 additions and 9 deletions

View File

@ -156,7 +156,7 @@ final class MmapPool : Allocator
*/ */
bool deallocate(void[] p) shared nothrow @nogc bool deallocate(void[] p) shared nothrow @nogc
{ {
if (p is null) if (p.ptr is null)
{ {
return true; return true;
} }

View File

@ -220,15 +220,26 @@ void dispose(T)(shared Allocator allocator, auto ref T p)
} }
/// Ditto. /// Ditto.
void dispose(T)(shared Allocator allocator, auto ref T[] array) void dispose(T)(shared Allocator allocator, auto ref T[] p)
{ {
static if (hasElaborateDestructor!(typeof(array[0]))) static if (hasElaborateDestructor!(typeof(p[0])))
{ {
foreach (ref e; array) import std.algorithm.iteration;
{ p.each!(e => destroy(e));
destroy(e);
} }
} () @trusted { allocator.deallocate(p); }();
() @trusted { allocator.deallocate(array); }(); p = null;
array = null; }
unittest
{
struct S
{
~this()
{
}
}
auto p = cast(S[]) defaultAllocator.allocate(S.sizeof);
defaultAllocator.dispose(p);
} }