Skip to content

Commit

Permalink
fix issue 16507 - std.experimental.allocator: FreeTree clears too eag…
Browse files Browse the repository at this point in the history
…erly

Try parent allocator without clearing first. Documentation says so.
  • Loading branch information
aG0aep6G committed Sep 18, 2016
1 parent 2a2e04b commit 46599a6
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions std/experimental/allocator/building_blocks/free_tree.d
Expand Up @@ -291,13 +291,20 @@ struct FreeTree(ParentAllocator)
if (n == 0) return null;

immutable s = goodAllocSize(n);

// Consult the free tree.
auto result = findAndRemove(root, s);
if (result.ptr) return result.ptr[0 .. n];

// No block found, try the parent allocator.
result = parent.allocate(s);
if (result.ptr) return result.ptr[0 .. n];

// Parent ran out of juice, desperation mode on
static if (hasMember!(ParentAllocator, "deallocate"))
{
clear;
// Try parent allocator again.
result = parent.allocate(s);
if (result.ptr) return result.ptr[0 .. n];
return null;
Expand Down Expand Up @@ -425,3 +432,21 @@ unittest // issue 16506
f!Mallocator(43);
f!GCAllocator(1);
}

unittest // issue 16507
{
static struct MyAllocator
{
byte dummy;
static bool alive = true;
void[] allocate(size_t s) { return new byte[](s); }
bool deallocate(void[] ) { if (alive) assert(false); return true; }
enum alignment = size_t.sizeof;
}

FreeTree!MyAllocator ft;
void[] x = ft.allocate(1);
ft.deallocate(x);
ft.allocate(1000);
MyAllocator.alive = false;
}

0 comments on commit 46599a6

Please sign in to comment.