Skip to content

Commit 58c2310

Browse files
CyberShadow0xEAB
authored andcommitted
Fix #10334 - std.allocator: Regions are non-copyable, yet are passed around in examples
This doesn't fix that non-copyable regions are still passed around in examples, so we still rely on NRVO to do its thing and elide the copy, but at least this will now catch wrong code that mistakenly copied Regions around.
1 parent a7e90e9 commit 58c2310

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

std/experimental/allocator/building_blocks/free_list.d

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,9 @@ struct ContiguousFreeList(ParentAllocator,
814814
import std.experimental.allocator.building_blocks.region : Region;
815815
import std.experimental.allocator.gc_allocator : GCAllocator;
816816
import std.typecons : Ternary;
817-
alias A = ContiguousFreeList!(Region!GCAllocator, 0, 64);
818-
auto a = A(Region!GCAllocator(1024 * 4), 1024);
817+
alias A = ContiguousFreeList!(Region!GCAllocator*, 0, 64);
818+
auto r = Region!GCAllocator(1024 * 4);
819+
auto a = A(&r, 1024);
819820

820821
assert((() nothrow @safe @nogc => a.empty)() == Ternary.yes);
821822

std/experimental/allocator/building_blocks/region.d

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ struct Region(ParentAllocator,
104104
this(cast(ubyte[]) (parent.allocate(n.roundUpToAlignment(alignment))));
105105
}
106106

107-
/*
108-
TODO: The postblit of `BasicRegion` should be disabled because such objects
109-
should not be copied around naively.
110-
*/
111-
112107
/**
113108
If `ParentAllocator` defines `deallocate`, the region defines a destructor
114109
that uses `ParentAllocator.deallocate` to free the memory chunk.
@@ -119,6 +114,13 @@ struct Region(ParentAllocator,
119114
with (_impl) parent.deallocate(_begin[0 .. _end - _begin]);
120115
}
121116

117+
/**
118+
`Region` deallocates on destruction (see above), therefore is not copyable.
119+
*/
120+
static if (!is(ParentAllocator == NullAllocator)
121+
&& hasMember!(ParentAllocator, "deallocate"))
122+
@disable this(this);
123+
122124
/**
123125
Rounds the given size to a multiple of the `alignment`
124126
*/

0 commit comments

Comments
 (0)